Install Python is very simple and we will see it in detail on all systems. But before installation let’s make a brief introduction to this amazing programming language
Python is a language that adapts to multiple fields, such as websites, computer science, and artificial intelligence. During this tutorial we will therefore explore the various areas of expertise.
In this lesson we will see how to install Python. First let’s make a brief introduction of this fantastic programming language.
It is a language that supports object-oriented programming and is being addressed more and more, even in universities.
There are numerous frameworks developed for Python and often represent optimal solutions for quickly building applications. Among these we will deal with Flask and Django, two important open-source web frameworks, useful for creating simple internet and e-commerce sites.
I will also mention Plone, a CMS (Content Management System) used to manage a website with ease.
This last part will therefore be useful for those who want to work with these tools.
How to install Python?
To install, go to the official website: https://www.python.org/downloads/ and download the version suitable for your operating system.
Windows
First we show the installation of Python in a Windows environment.
As soon as the installation starts, you will be asked for some options, make sure you select the Add Python 3.X to PATH item.
But, if you forget to do so, don’t worry, you can still change this option by going to: control panel -> system-> advanced system settings -> environment variable.
As well as from the pictures shown below.
Then click on the edit button and add the path specifying the appropriate path.
Alternatively, you can launch the executable again, click on modify and add the check mark on Add Python to environment variables.
The Python installation is complete
Working in interactive mode
After installing Python on your PC, you can start working in interactive mode or creating scripts that can then be executed.
The interactive mode allows you to interact directly with the interpreter, this is only convenient at the beginning to learn how to do some small tests.
To work in this mode, you need to start the IDLE. IDLE is software that allows both to work interactively and to create scripts.
Starting it, the interactive interpreter will be presented with the cursor positioned after the symbol: >>> (triple major sign), the Python shell.
You can consult information about the language specifications by entering the help() command.
After entering the help, you can request, for example, information on the individual instructions of the language.
Then try to request the if statement, you will see information on the use of this statement.
To exit the help you need to type quit.
You can also avoid entering the help by typing, after the >>> symbol, the word help followed by the required term inserted in quotes inside the round brackets.
For example help (‘if’).
Well in this guide we have seen the installation of Python, in the next tutorial we will start creating our first Python program.
Assignment operators in Python, and in other language programming, are used to assign values to variables.
For example, if we indicate a = 5 and b = 3 we are assigning the value 5 to the variable a and the value 3 to the variable b.
But let’s see in detail what other assignment operators exist:
Assignment Operators Python = assign
Example:
a = 5 assigns the value 5 to the variable a on the left (ie what is to the right of the equal.)
Another example could be a = b + c where to a this time we assign the sum b + c.
Assignment Operators Python += add and assign
Assigns the sum between it and the right operand to the left operand.
For example:
a += 2 is equivalent to doing a = a + 2, so it assigns to the variable a on the left the value of a (we always suppose a = 5) added to 2. So we will get a = 7.
Assignment Operators Python -= subtract and assign
Assigns the difference between it and the right operand to the left operand.
Ex:
a – = 2 is equivalent to doing a = a-2, so it assigns to the variable a on the left the value of a (we always assume a = 5) minus 2. So we will get a = 3.
Assignment Operators Python *= multiply and assign
Assigns the product between it and the right operand to the left operand.
An example:
a * = 2 is equivalent to doing a = a * 2, so it assigns to the variable a on the left the value of a (let’s always suppose a = 5) multiplied by 2. So we will get a = 10.
Assignment Operators Python /= divide and assign
Assigns the real division between it and the right operand to the left operand.
As for example:
a /= 2 is equivalent to doing a = a / 2, so it assigns to the variable a on the left the value of a (we always assume a = 5) divided by 2. So we will get a = 2.5.
Assignment Operators Python //= divide and assign
Assign the rounded division between it and the right operand to the left operand.
Eg: a //= 2 is equivalent to doing a = a // 2, so it assigns the value of a to the variable a on the left (we always assume a = 5) divided by 2 rounded to the integer. So we will get a = 2.
Assignment Operators Python %= calculate the remainder and assign
Assigns to the left operand the remainder obtained from the division between it and the right operand.
Example:
a%= 2 is equivalent to doing a = a% 2, so it assigns the value of a to the variable a on the left (we always assume a = 5) divided by 2 rounded to the integer. So we will get a = 1.
Assignment Operators Python **= calculate the power and assign
Assigns to the left operand the result of exponentiation of it as the base and exponent of the right operand.
Example:
a **= 2 is equivalent to doing a=a2, so it assigns to the variable a on the left the value of a (we always suppose a = 5) raised to 2. So we will get a = 25.
Try these simple examples interactively to familiarize yourself with the language.
Some exercises
Some exercises on assignment operators in Python
a = 10
b = 15 + a
c = b * 2
c += 1
print(a,b,c)
Other exercises
a = b = c = 10
a *= 10
b **= 2
c //= 4
print(a,b,c)
In this lesson we have studied the assignment operators in Python and have seen many exercises and practical examples.
In this lesson we will look for the maximum value of a list in Python either by using the Python max() function, or by building our own algorithm.
Exercise – Python max()
Populate a list with 20 numbers of your choice. After entering, view all the elements of the list with their index. Then find the maximum value among those entered in the list.
First methodwith Python max()
In this first method, we populate a list of numbers. Next we simply use the max () function to determine the maximum of the list.
Here is the code:
number = [13, 22, 18, 3, 1, 9]
n_max = max(number);
print("The largest number is:", n_max)
Second methodwithout Python max()
In this second method we will create a custom algorithm.
First we initialize the list of name numbers to the empty list. Then we insert as input 20 numbers of your choice in the list and display them using another for loop.
Take care to view the elements with their index using another for loop, as you are asked to view the list after entering the numbers.
N.B. The entered values can be both positive and negative.
So the numbers can also be all negative, for example. This is why the intuition of setting the maximum to zero is wrong.
In this regard, let’s make an example by setting maximum = 0
And we put the numbers in the list:
-5, -12, -1, -8, -15
At the end, the maximum value will be 0, as the comparisons made did not allow to replace this value.
This is clearly a mistake!
Then we need to initialize the maximum value to the first element of the list and then with a for loop we compare the other elements with the maximum value.
If the found element is greater than the maximum value then it is replaced, otherwise the maximum will be the first element in the list.
Here is the code:
n = 5
numbers = []
for i in range(n):
number = int(input('Insert a number: '))
numbers.append(number)
n_max = numbers[0]
for i in range(n):
if numbers[i] > n_max:
n_max = numbers[i]
print('The largest number is: ', n_max)
In this last example we did not use the Python max() function