December 2022
Beginner to intermediate
512 pages
14h 57m
English
Conditional statements allow your script or program to have “control flow”. We have the option of using the if, elif, and else statements.
Let’s combine these examples into a simplified version of a popular programming interview problem: Fizz Buzz.
If the number we want to check is a multiple of 2, we want to print "fizz". We can use the modulo operator in Python, %, to give us the remainder of a number after division. So, a number is a multiple of 2 if the modulo (i.e., remainder) is 0. If that statement is true it will run the code in that if block (denoted by the indentation).
my_num = 4
if my_num % 2 == 0:
print("fizz")
fizz
If we put multiple if statements after one another it will run through each of them ...
Read now
Unlock full access