Python Lessons – 2.1 Flow control

Python Lessons - 2.1 Flow control
Scrivi una didascalia…

Boolean values and flow control

You have seen integer and float data in the first part of the course. Well there are also other types of data very useful in programming languages and one of these are the Boolean values.

The Boolean values are only two:

  • True
  • False

Attention to the initial capital letter. the values true and false are not Boolean.


In programming the availability of these values allows us to manage in a very simple way some operations such as conditions.

In this second part of the course we will see how the conditions are a very important mechanism with which to control the flow of a program.

We saw in the first part how the commands were executed one at a time. Interacting with the Python console gradually and changing the commands according to the results obtained. Well this however is not possible with the programs (let’s say more correctly that it is not correct). Indeed, programs must be able to make decisions on their own, and this can be done through assessments of conditions.

If certain conditions are met the program will execute commands, otherwise it will execute others.

This is the control of the flow of the program.

Much of the second part of the course will be based on this concept.

Comparison operators

First, to build a condition we need a set of operators to make comparisons.

For example, the equality operator == checks that two values, or variables, have the same value. If they have it, it will return True, otherwise False. However it will return a Boolean value. Here we have a condition.

Continuing to work on the Python console. We test the functionality of the comparison operator

Note: for simple assignments we can use multiple commands on the same line separating them with ‘;’. Be careful not to abuse this in order not to lower the legibility of the code.

The comparison operator also works for string values

Another operator is that of inequality,! =. This works exactly the opposite of the previous one, returning True if the values contained in the two variables are different, False otherwise.

But there are not only these types of operators. In fact, the comparison operators>,> =, <and <= are used a lot with the numbers.

This class of comparison operators allow us to set conditions to know if one number is greater or less than another.

⇐ Go to Python Lesson 1.11 – Using an editor 

Go to Python Lesson 2.2 – The conditional construct IF ⇒

Leave a Reply