Python is Login
Python is an extremely popular programming language. It was created by Guido van Rossum and was first released in 1991. You should know that there are two different versions of the Python language: Python 2.x and Python 3.x. There are a number of differences between versions, so there is no harmony between codes, ie the code you write in Python 2.x will not work in Python 3.x and vice versa. Python 2.0 was first introduced in 2000, while Python 3.0 was introduced in late 2008. In the intervening 8 years, Python 2.x was used and many code and toolboxes were produced in this version. Because of this, most data scientists did not immediately move to Python 3.0. So far, almost all libraries from Python 2.x have been moved to Python 3.0; however, Python 2.7 and the code written there still exists on many platforms.
Where can Python be used?
Ptyhon has a wide range of uses. Some of those; web based application development (server side), software development, mathematical applications, data science applications etc.
What can be done with Python?
Actually, there is no limit to this.
Python can be used on a server to build web applications.
Python can be used alongside software to create workflows.
It can connect to Python database systems. It can also read and update files.
Python can be used to process big data and complex math problems (such as deep learning applications).
Python can be used for rapid prototyping or production-ready software development.
Why is Python more preferred?
Python runs on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.).
Python has a simple syntax similar to the English language.
Python has a syntax that allows developers to write programs with fewer lines than other programming languages.
Python runs on an interpreter system, meaning it can be run as soon as the code is written.
Python can behave in a procedural, object-oriented, or functional way.
The latest version of Python is Python 3. To write code in Python, you need an Integrated Development Environment such as Anaconda, Thonny, Pycharm, Netbeans or Eclipse.
Application Environment Example: Anaconda
To download Anaconda: https://www.anaconda.com/products/individual
To run it on your computer: Start> Anaconda> Anaconda Navigator
By default, environments such as JupyterLab, Jupyter Notebook and Spyder where you can code in Phyton come.
Follow the path Spyder> Launch> Google Chrome, type your code, select the line to run your code> press F9.
For Jupyter Notebook, which allows you to create a unique notebook layout while writing code;
Follow Jupyter Notebook> Launch> Google Chrome, New> Pyton 3>, Type your code, Ctrl + Enter to run, Ctrl + Shift to move to the next line.
Follow JupyterLab> Launch> Google Chrome. This platform offers a more flexible usage; For example, you can create Notebook, Console, Terminal together (it is like a more developed version of Jupyter Notebook.)
My First Code in Python
Let’s write a familiar line for our first code. The name of our file is hello.py (Python files have the extension .py.)
Type print (“Hello Python!”) and run it.
In other programming languages the indentation in the code is for readability only, but for Python the indent space is very important. Python uses an indentation space to specify a code block.
If there was no indent on the second line in the code block above, we would get the following error: IndentationError: expected an indented block. Because Python expects you to add a space indent in the second line. The number of spaces is up to you as a programmer, but there must be at least one.
Variables in Python
In Python, there is no variable declaration command. When variables are assigned a value, it means the variables are defined automatically. Check out the example below.
When coding in Python, # is used to create a note or comment. Comments start with #, then Python displays the rest of the line as a comment.
Comments can be used to explain Python code or to make the code more readable. It can also be used in front of unwanted codes.
For more than one comment line, # can be added to the beginning of each line, as well as three double quotes (“” ”) at the beginning and end of the lines.
Creating Variables in Python
Variables can be defined as containers used to store data values. Unlike other programming languages, Python does not have a variable setting command. The variable was created when the variable was initially assigned a value. On the other hand, in Python, it is not necessary to declare the data type of the variables, after defining a certain data type, the data type can be changed at any time.
Single or double quotes can be used when assigning values to variables of string type.
Naming Variables in Python
The short name of a variable can be a value such as x, y, or preferably more descriptive names. For example; such as age, location, name, surname. When naming variables, as in other programming languages, the following variable setting rules apply:
The variable name usually begins with a letter or may begin with an underscore character.
· Variable name cannot start with a number.
Variable name can contain alphanumeric characters, underscores (A-z, 0–9, and _)
· Variable names are case sensitive. For example, Name, Name and AD are three different variable names.
Also, variables can contain Turkish characters, but this is not preferred.
Assigning Values to Multiple Variables
Python allows you to assign values to multiple variables in one row.
Variables created outside of a function are defined as global variables. Global variables can be used both inside and outside functions.
For example; Let’s define a variable outside of a function (this is a global variable), and use that variable inside the function.
If you create variables with the same name inside and outside a function, it will be a local variable inside the function and can only be used inside that function. The global variable with the same name except the function will remain with its original value when the function is executed. Let’s examine our example.
Defining Variables with the Word Global
Normally, when a variable is defined inside a function, that variable is a local variable and can only be used inside that function.
The word “global” can be used to create a global variable within a function.
The word global can be used to change the value of a global variable within a function.
In the example above, would the result change if the variable x was not globally defined in the function? Let’s look at the example below.
So far, I have touched on the subject of variables in Python in the first part. I hope it was useful.