Step-by-step tutorial on Python programming language in simple language
Part 1: Install Python Interpreter on Windows, Linux, GNU
To install the Python programming language, you must first download the version you want through the Python site. When you go to the Downloads option as shown below, a new section will open in which you can download Python depending on the type of operating system (Windows, Mac, Linux,…).
In the category of Education Python programming language with you.
There are different versions of Python and there are differences between Python2 and Python3, and here we will teach Python 3.8.1, as recommended by Python creator Guido van Rossum, the latest version of Python that has just been released.
Note that the version in question cannot be installed on Windows XP. To install Python on Linux, the terminal instructions inside Linux will be used for installation, which we will say in the installation section on Linux and GNU (if you want to download the file directly from the site, in the Source Code section You can download the file you want)
The version you see on the first page is for the 32-bit version of Python, and if your Windows operating system is 64-bit, go to the windows page and download the desired version.
On Mac OS for Python version 3.8.1 only 64-bit version can be downloaded and for 32-bit version you can download version 3.7.6.
Install Python Interpreter on Windows
We will first install the version compatible with the Windows operating system. After downloading the desired version, run the downloaded file to open the following page for you. We download and install the 64-bit version here. There is no significant difference in the performance of the two versions.
Part I
This section, which is recommended, click on this section to go to the next section, includes all the required packages of Python plus its document, which if you are new to Python programming language, this option is recommended to you.
part 2
Inside this section you can select the items that are installed by section 1 and you do not intend to install, and you can also change the default installation location.
Part 3
In this section you have two options to choose from. The first option is for the case that you have multiple users on your Windows and by checking this section, you notify the program to install Python for all users. The second option is for easier access to Python, especially in the installation of packages and use in Windows CMD. It is recommended that you check this section as well so that you do not have problems in the next sections.
Here we select part 1 and enter the next step.
After the installation is successful, the following image will be displayed for you. If you are presented with an option called disable path length limit, click on it to add Python installation path to the Windows path for better access.
To check if the Python programming language is installed correctly on the operating system, type cmd in the search field at the bottom of the window menu and click on Command Prompt. If you are using Windows 10, you should see a similar image below:
After clicking on cmd, the following image will open for you.
Here we can see the version of Python installed on the operating system using the following command.
1 | <span style=“font-size: 16px;”>python –version</span> |
After running the command in cmd, you should see the Python version similar to the version you installed.
Install Python Interpreter on Linux, GNU to teach Python programming language
Sometimes Python is installed by default on some Linux / GNU versions. To check this, first enter the following commands in the terminal:
1 2 3 4 5 | <span style=“font-size: 16px;”>$ python —version $ python2 —version $ python3 —version</span> |
If some value is not returned, it means that Python is not installed on your operating system.
Installing Python on Linux or GNU is very simple and just run two commands.
The first command is for the apt-get update, which you can do with the following command:
1 | <span style=“font-size: 16px;”>$ sudo apt–get update</span> |
The second command is to install the desired version of Python using apt-get, which is as follows:
1 | <span style=“font-size: 16px;”>$ sudo apt–get install python3</span> |
After running these two commands, using the commands mentioned at the beginning, you can make sure that the Python and the installed version are installed correctly.
Well, in this section, we were able to install our desired version of Python on our operating system. In the next section, we enter the Python programming space.
Part 2 Teaching Python Programming Language: Familiarity with Python environments
You can code in different environments with Python. From the various IDEs (IDEs are programs that you can code) to Windows cmd and the Linux terminal and Python IDLE itself, you can easily code in all of these environments.
Windows cmd coding (Linux terminal)
First we see an example of coding in Windows cmd space. To do this, and if you have added the Python installation path in the Windows path, you can enter the Python coding space using the python command. If you have done the steps correctly, you will see a space similar to the space in the photo below:
To get started and to write the first command line in Python, we will print a phrase using the print command. The phrase to print is:
1 | <span style=“font-size: 16px;”>Print(‘hello python’)</span> |
After executing the command, the output of the program will be similar to the following image:
We are now able to easily execute the first command from the Python programming language . In Linux, similar to Windows, you can enter the Python space and then type the same command and view the same output.
Note that when you enter the Python space, you will first see the phrase >>> for each line of coding.
To view other environments, we will run the same command in other environments.
Coding in Python IDLE space
The next environment to work in is the IDLE space for Python itself (Windows operating system), which will be installed with Python. You can view the IDLE (Python GUI) program by using the Windows search box and typing python.
After executing the program, we will execute the command that we executed in the previous part here again, and you can see the desired output, which is similar to the previous part.
If you have several versions of Python installed on your operating system, you must open the IDLE corresponding to the desired version, which first, like the image above, displays the type of Python version you want.
As you can see, the thing about IDLE and cmd is the more attractive and beautiful IDLE Python environment. In this environment, according to the type of commands and their features, different colors have been used, which has also significantly increased the readability of the code compared to using cmd. Using this environment is not the preferred option for teaching Python programming language in this series of articles, but for those who want to know the meaning of each color, we explain the definition of each color here.
Red
This color is used in two cases:
To write a comment: A comment is a phrase that we add inside ourselves for higher readability and has no effect on the output of our code. Sometimes multiple programmers may work on the same code, or you may have a function that has a specific function, in which case using a comment will make you a better, more professional programmer.
When you mistype a command and you get an error.
Purple
These colors are for displaying functions and commands that are defined by default in Python. For example, print is a command that Python has by default that has a specific function.
Green
This color is for displaying values that are in the form of strings. We’ll talk about strings in more detail later, but the expressions inside “” are called strings.
Orange
This color will be seen when you use a Python keyword, these words are defined by default in the Python language. For example, the use of loops and conditional statements such as if are among the items that are considered Python keywords and are displayed with this color.
Blue
This color is used in two places:
- When output output. For example, the output value of the print command is displayed in blue here.
- To specify the name of functions or classes. We will talk about classes and functions later.
Black: This color is used for the values that Python receives as input or defines as a variable.
The point to note is that these colors are the default mode in Python IDLE, and you can use the following path inside IDLE to select the colors you want for each item.
Options -> configure IDLE -> Highlights
Coding in Visual Studio Code space
The last space we will talk about is the Visual Studio Code space, or VS Code for short. This IDE is one of the most popular IDEs in the field of coding. After installing and running the program, you will see the following page:
Note that Python files are known by the .py extension, and you should save them with this extension. If you are opening this software for the first time, create a new file from the path below and after saving the file, save it in .py format so that the IDE recognizes the Python language for your coding space.
File -> New File
Extension capability
This IDE has a feature called extension that you can use to install plugins to provide a better coding environment. We also want to install these plugins for Python. According to the figure below, click on the extension to see a page similar to the image below. Then in the search field, type python to display the Python plugins.
It is recommended that you install the two plugins specified. Next to the names of the plugins on the right, you can see the number of installations and points that each plugin has received, and also if you click on the plugin, you can see the detailed information of the plugin.
After clicking on install and the installation is done, you have to reload your VS code once to install the plugin for you, similar to the image above. Now, we execute our command. After executing the command, the output is visible at the bottom of the page.
Note that Python is a language that is case sensitive, meaning that uppercase and lowercase letters have different meanings and are sensitive to it. For example, if you type the print () command as Print (), Python does not recognize it. “For Python’s default functions and words, it must be spelled uppercase and lowercase.”
Python programming environments
In this article, you will learn about different programmable environments in Python. Other environments are also good options for Python python, but in the Python programming language tutorial we will run commands on the VS Code environment, and if you are interested you can run commands on other environments. In the next article, we will look at the types of variables that can be defined in Python.
Part 3 Teaching Python Programming Language: Types of Data in Python Language
In the Python programming language, there are a variety of variables, including integers, decimals, strings, arrays, and so on. In this section, we are going to explain each of these in full. And in programming language, types of variables are called Data Types. So if you are asked in an interview or conversation what Data Types are in Python or any other programming language, know what they mean by the variables available in the programming language. The types of data available in the Python programming language are as follows:
- Numeric Type
- Text type
- Boolean Type
- Sequence Type
- Binary Type
- Mapping Type
- Set Type
“Translating some terms and definitions into Persian is sometimes not appropriate in programming. It is recommended that you learn and use English phrases as much as you can. ”
We will first define the variable and then define the types of numerical data available in the Python programming language.
Variables in Python
Variables are parameters that can take a value from any different type of data. For example, in the following command, a is a variable that has a numeric value of 4 and b is another variable that has a text value of “maktabkhooneh”.
1 2 | a=4 b=”maktabkhooneh” |
There is a special rule for naming variables in Python, and the following are unacceptable for naming variables in Python:
- Python keywords cannot be defined as variables. Such as if, for, list,…
- Variables in Python cannot start with a number. 2myvar cannot be a variable name.
- It is not possible to use – or spaces in the names of variables. my var or my-var cannot be a variable name.
If the above three items are met, variable naming is acceptable.
You can separate the three variables in a line using a comma (,) and assign a value to them, similar to the following code:
1 | x, y, z = “Orange”, “Banana”, “Cherry” |
You can assign a value to several variables:
1 | x = y = z = “Orange” |
Variables can be defined in different types of data. Variables have global and local types, which we will discuss the difference between the two in the topic of functions.
Numeric Types in Python Programming Tutorial
In the mathematical literature we have all kinds of numbers. Natural numbers, integers, decimal numbers, real numbers and different types of intervals that have their own numbers and definitions. In the Python programming language, there are several classifications for numbers, including:
- Correct type (int)
- Decimal type (float)
- Complex type
Integer type numbers
We start with integer numbers. The range of these numbers is similar to integers in the mathematical definition, ie from positive infinity to negative infinite non-decimal numbers include the integer type in the Python programming language.
Decimal type numbers
Decimal-type numbers are numbers that are similar to integers from negative to infinite to positive infinite, except that they also include decimal numbers. Depending on the type of command and the definition used, we will discuss it later. By default, this command displays up to 17 decimal places.
complex numbers
Complex numbers are also a type of number. In addition to the real value part, they also contain imaginary values. These numbers are a group of numbers that when we get to the power of 2, unlike real numbers, will be the result of a negative number. Also the square root of the negative numbers will give a complex number. In fact, real numbers are a kind of complex numbers whose imaginary part is zero.
These numbers are displayed as follows:
1 | a+bj |
Where a is the real value and b is the imaginary value.
We will now look at an example of numeric types in the Python programming language tutorial . Consider six different types of variables:
1 2 3 4 5 6 | a = 4 b = –61234124 c = 1.546 d = –1.2 e = 5j f = 4+23j |
These numbers are integer (int), decimal (float), complex (respectively). To view the type of these numbers in the Python programming language, we use a function called type (). All you need to do to figure out the type of data you want is to type the value in parentheses in the type function. As follows:
1 2 3 4 5 6 | type(a) type(b) type(c) type(d) type(e) type(f) |
When you run these commands, you will get the following outputs:
1 2 3 4 5 6 | <class ‘int’> <class ‘int’> <class ‘float’> <class ‘float’> <class ‘complex’> <class ‘complex’> |
You will see the following image by executing the items mentioned in Python:
The concept of class in teaching Python programming language
We will talk more fully about the concept of class before the data type in the output.
Now suppose we want to convert a decimal value to an integer value. As in a code, we have received a variable that has been a decimal, but for us only the integer value is important. One of the methods that can be used for this problem is to convert the decimal type to the correct type. The command used for this is as follows:
1 | h = int(c) |
In this case, when you print the variable h in the output using the print () command, it will print only its correct value, the number 1. Now suppose we want to convert the correct value to decimals. To do this, use the following command:
1 | h = float(a) |
The output will be 4.0, which will be a decimal value or float. To ensure this, you can check this with the type () command. Similarly, the complex statement can be used to convert a number to a complex value.
The conversions mentioned above are used as needed in the code. One of the applications of saying conversion is when converting a number from text to numeric type, which we will see in the next section, which deals with text type. ”
You can apply different mathematical operations to any of the above types.
Text Types
This type of data is called textual expressions that are used as strings. You saw an example of this type of data when you first started working with Python. Strings in Python have different functions, each of which we are going to define and use here.
Special characters in strings
In the table below we will define the characters that have a meaning with a certain value.
https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.bestprog.net%2Fen%2F2019%2F11%2F06%2Fpython-strings-escape-sequences-unformatted-strings-multiline-text-blocks%2F&psig=AOvVaw30kuDo-065ik7g5YlE1ZAh&ust=1608401686456000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCJjdo6uR2O0CFQAAAAAdAAAAABAE
Functions and methods in strings
In the table below, we want to introduce you to the different functions and methods in the disciplines and its effect. The method of using the methods is as follows.
1 2 3 | ()Method “String” |
Parameter or input value in methods and functions means values and variables that are written in parentheses in front of the method. This is one of the features that exists in classes and functions. If you want to give multiple inputs to the function, just put a comma (,) after each parameter. The order in which the input parameters are placed is important. ”
As you can see, some commands are not used in Persian, so if you are coding for Persian strings, you will not need some commands.
To better understand the commands, try each command and see its output. Some commands are used in specific cases and it is rare for you to use some commands.
All the mentioned methods and functions do not change the main string and only return new values. This means that if you run any of the above methods and functions on a string, the original string will not change, and if you want to have a new value, you will have to save it in a new variable, or if you want to make changes to If the main string is created, you must save the method output and functions again inside the main string variable. ”