Conditional statements
if
conditional statement
The if
conditional statement allows us to check if a given condition is fulfilled. We commonly use logical operators in conditional statements.
x = int(input())
if x == 5:
print("The user entered the number 5.")
We use the modulo operator very often in conditional statements. In the example below, the condition checks if the x
variable is divisible by 2 (whether it is even).
x = int(input())
if x % 2 == 0:
print("The user entered an even number.")
We can simplify the if
statement by using boolean conditions. We can just write if x:
, and if x
is True
, the instruction will be executed. True
means any value other than 0.
else
and elif
conditional statements
else
is a statement that allows us to simplify our code by avoiding the need for another if
statement, replacing it with an alternative action when the original condition is False
.
x = int(input())
if x > 0:
print("The user entered a positive number.")
else:
print("The user entered a negative number.")
The code above doesn't handle the case where the user enters 0. To do that, we need elif
(else
+ if
). It works similarly to else
, but allows us to specify an additional condition (like in an if
statement). We can use as many elif
instructions as we want, relating to a single if
.
x = int(input())
if x > 0:
print("The user entered a positive number.")
elif x < 0:
print("The user entered a negative number.")
else:
print("The user entered 0.")
We use this instruction instead of writing multiple if
statements because if any of the conditions relating to a single if
statement have been met, the rest will not be considered.
break
and continue
keywords
The break
keyword is used to break the continuity of a loop, and continue
can skip particular iterations within it.
while True:
x = int(input())
if x > 0:
break
for x in range(10):
if x == 4 or x == 8:
continue
print(x)
else
in loops
else
also works with loops, but on different principles. The else
block that follows the loop won't be executed only if there is a break
statement inside the loop. This structure is unique to Python, and we shouldn't use it very often because it can be confusing.
x = 3
while x > 4:
x -= 1
print("Loop")
else:
print("Else")
print("Outside")
x = 6
while x > 4:
x -= 1
print("Loop")
else:
print("Else")
print("Outside")
x = 10
while x > 4:
x -= 1
print("Loop")
if x == 7:
break
else:
print("Else")
print("Outside")
for x in range(5):
print("Loop")
else:
print("Else")
print("Outside")
for x in range(5):
print("Loop")
if x == 2:
break
else:
print("Else")
print("Outside")
match
conditional statement (switch
)
match
is an instruction that simplifies complex conditions by replacing multiple if
and elif
statements with a more concise structure.
x = int(input())
match x:
case 0:
print("x equals 0")
case 25:
print("x equals 25")
case 50:
print("x equals 50")
case _: # the default case
print("x doesn't equal 0, 25, or 50")
A parameter of the match
statement is usually an integer or a string (but it could be something else).
Ternary operations
A ternary operation is a simplifed one-line version of the if-else
statement.
x, y = 1, 2 # defining variables in bulk
z = x if x < y else y
print("The smaller numer is:", z)
The walrus operator
The walrus operator :=
allows us to assign a value to a variable as part of an expression. It can make code more concise by combining assignment and condition checks in a single line.
numbers = [1, 2, 3, 4, 5]
total = 0
while (n := len(numbers)) > 0:
total += numbers.pop()
print("Popped number, remaining count:", n)
print("Total sum:", total)