Welcome to the second episode in our series of introductions to Python programming. In the previous episode, you saw how useful it is to master this language and how to get started with the different variable types. In this episode, you'll discover the different operators on Python, as well as how loops work and various useful functions on Python. Let's get started:
A Python shell can replace your pocket calculator, with basic arithmetic operations implemented natively:
+ for addition
– for subtraction
* for multiplication
/ for division
% for modulo (returns the remainder of the division)
These operations can be performed on numbers, but also on other types of variables
Tests (Conditional instructions)
The if statement is the simplest test structure. Its syntax in Python uses the notion of a block. First, let’s take a look at this notion.
Instruction block - Indentation
To indicate a block of code in Python, you must indent (using tabs) each line of the block by the same amount.
The instruction syntax is as follows:
condition is a Boolean expression, i.e. an expression that takes the value True or False.
Here’s an example:
This table summarizes the comparison operations that can be used in conditions:
In programming, we often need to repeat a sequence of instructions a certain number of times, depending on one or more conditions.
Loops enable us to do this simply and efficiently.
Boucles while :
The principle is simple. A while loop executes a series of instructions until a condition is met. To operate, it generally requires three steps:
initialization of an iteration variable.(i)
Test the iteration variable.(i<3)
Updating the iteration variable. (i=i+1)
Boucles for :
The for loop can also be used to repeat a block of instructions. It is generally used to iterate over a predefined sequence of values. Sequences are often of the range, list or tuple type, but can also be character strings:
The in is essential to the use of the loop, as it is used to traverse the sequence.
When the loop is executed, i contains the first element of L, and the instruction block is executed. Then i contains the second element of L, and so on to the last element of the list.
For both types of loop, you may need to exit them before they end. To do this, we use the break instruction.
In this example, if the password entered is different from ‘0000’ more than three times, the loop displays ‘Try again later’ and the break instruction takes us out of the loop immediately.
In the same register as the break instruction, continue is also an instruction inside the loop, this time allowing you to bypass an iteration of the loop.
In the following example, the for loop running through the list a will pass through the value 0. But as the calculation of 1/0 is not possible, the continue instruction will ignore this value and move on to the next one.
As in mathematics, functions can be used to group together a sequence of reusable instructions, avoiding the need to recopy the same code several times in a program.
What’s more, functions can be parameterized and return one or more results, thus widening the range of possibilities. In some cases, they can also return nothing.
When calling a function, it is necessary to specify the parameter values to be used during execution (in brackets, after the function name). This is not mandatory for a parameter with a “default value”, which in this case will take a default value. To specify a parameter not to take its default value, simply specify the value to be used.
To define and call a function, use the following syntax:
For example, calling the following function will return the square of a number if it is positive and 0 otherwise.
Global variables in a function :
A variable defined outside a function can be reused within that function. However, if a modification is made to this variable within the function, it will only last as long as the function call:
It is then possible to modify the variable in the function if it has been declared as a global variable in the function:
Python also supports recursion. Recursion is the ability of a function to define itself by calling itself.
It is essential to set a “stop condition” so that a call to a function does not generate an infinite number of calls (and therefore a halt to your program).
Well done for following this introduction to Python so far.
Just a little more effort and in 2 weeks you’ll be able to create a Data Science application. See you next week for a look at loading a dataset and cleaning it.