Booleans

And finally booleans, it is a variable that can have one of two possible values, either 0 (False), or 1 (True).

Booleans are very useful to test various conditions to determine if it is True or False.

# true or false
is_true = True
is_false = False
print(is_true, is_false)

# bool operators
sum = 0.5 + 1.0
print(sum == 1.5) # equal
print(sum != 1.5) # not equal
print(sum < 1.5) # minor
print(sum >= 1.5) # major or equal


Control flow, if statement


Booleans are used in a very useful synthax that you can use to adapt your script to different conditions with a if statement, which is the first step to control the flow of your script. It is a conditional statement that runs or skips code based on whether a condition is true or false. Here’s the synthax: () Note the *indent (the space on the left) that is important to indicate the code that is part of the if statement.

# This is an "if..else" statement
if CONDITION:
····do something
else:
····do something else

# in an example
if x >= y:
····print("The value is bigger or equal to 6")
else:
····print("The value is smaller than 6")


Control flow, elif statement


A elif statement is used to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.

# This is an "if..elif..else" statement
if CONDITION:
····do something
elif CONDITION:
····do something else
else:
····do something else

# in an example
if x >= y:
····print("The value is bigger or equal to 6")
elif x == 5:
····print("The value is equal to 5")
else:
····print("The value is smaller than 6")


Control flow, nested if statement


A nested if statement is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

# This is an "if..elif..else" statement
if CONDITION:
····do something
elif CONDITION:
····do something else
else:
····do something else

# in an example
if x >= y:
····if x == 5:
········print("The value is equal to 5")
····else:
········print("The value is bigger or equal to 6")
else:
····print("The value is smaller than 6")


Logical operators


Logical operators are used to combine conditional statements. Here are the most common ones:

# AND
if CONDITION1 and CONDITION2:
····do something

# OR
if CONDITION1 or CONDITION2:
····do something

# NOT
if not CONDITION:
····do something

# AND example
if x >= y and x == 5:
····print("The value is bigger or equal to 6 and equal to 5")
else:
····print("The value is smaller than 6 or not equal to 5")

# OR example
if x >= y or x == 5:
····print("The value is bigger or equal to 6 or equal to 5")
else:
····print("The value is smaller than 6 and not equal to 5")

# NOT example:
if not x == 5:
····print("The value is not equal to 5")
else:
····print("The value is equal to 5")


Boolean conversion


It is possible to convert a boolean to an integer and vice-versa.

# Convert boolean to integer
print(int(True)) # 1
print(int(False)) # 0

# Convert integer to boolean
print(bool(1)) # any number different than 0 is True
print(bool(0)) # 0 is False


🛠 Exercises


01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 02: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 03: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 04: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

Solutions:

01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 02: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 03: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 04: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍