Booleans, if-elif-else, while-loop
Contents
Booleans, if-elif-else, while-loop¶
Booleans¶
A boolean
is another data type. It is very simple but also very important.
A boolean has two possible values:
True
# or
False
Commonly this data type is returned when you execute a conditional statement.
Relational operators¶
a > b # a bigger b
a < b # a smaller b
a <= b # a smaller or equal b
a >= b # a bigger or equal b
a == b # a equal b
a != b # a not equal b
0 > -2
True
for i in range(3):
print(f'1 >= {i} is {1 >= i}')
1 >= 0 is True
1 >= 1 is True
1 >= 2 is False
1 == 1
True
1 != 2
True
'rose' == 'rose'
True
'Rose' != 'rose'
True
Multiple conditions¶
It’s possible to combine multiple conditions with the keywords and
and or
:
10 > 5 and 5 > 4
True
-124 > 0 or 1e-4 > 0
True
True or False
True
True and False
False
With the keyword in
we can evaluate if an item is included in a list or a string.
advent_calendar = ['apple', 'berry', 'date', 'eel', 'firebird', 'mango']
'app' in 'apple'
True
'apple' in advent_calendar
True
'catfish' not in advent_calendar
True
If we want to check multiple conditions, we have to separate them:
# Does not work as expected:
'catfish' and 'firebird' in advent_calendar
True
'catfish' in advent_calendar and 'firebird' in advent_calendar
False
'catfish' in advent_calendar or 'firebird' in advent_calendar
True
With is
we can ask for the identity of an object.
s = 'text'
type(s) is str
True
i, f = 486, 24.1
print(type(i) is int)
print(type(f) is float)
True
True
if elif else (Decision Making)¶
With if
, elif
(optional) and else
you can specify to execute a block of code based on the boolean (True
and False
) of a condition.
if True:
print('code block is executed')
print('still inside')
else:
print('this is not executed')
print('executed in any case')
code block is executed
still inside
executed in any case
The syntax is similar to writing a for-loop. The statement is finished with a :
, followed by lines of code with indent. The first line without indent marks the end of the block of code.
It’s also possible to create a variable of type boolean:
i_like_coffee = True
if i_like_coffee:
print('☕')
else:
print('🫖')
☕
We can swap booleans with the not
keyword:
i_like_coffee = True
print(i_like_coffee)
i_like_coffee = not i_like_coffee
print(i_like_coffee)
True
False
We can use if elif else
if we want to evaluate multiple conditions.
a, b = 4, 8
if a > b:
print(a, '>', b)
elif a < b:
print(a, '<', b)
else:
print(a, '=', b)
4 < 8
for i in range(2,10):
# if i is even:
if i % 2 == 0:
print(f'{i} is an even number')
else:
print(f'{i} is an odd number')
2 is an even number
3 is an odd number
4 is an even number
5 is an odd number
6 is an even number
7 is an odd number
8 is an even number
9 is an odd number
if-expressions in list comprehension¶
numbers = [n for n in range(10)]
numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers = list(range(10))
numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
advent_calendar = ['apple', 'berry', 'date', 'eel', 'firebird', 'mango']
# Add all items to the new list if their length is smaller than or equal 4.
short_calendar = [item for item in advent_calendar if len(item) <= 4]
print(short_calendar)
['date', 'eel']
# Create a list with a loop. Loop over all items in
# advent_calendar and append them to the list
# if an 'a' is in the item.
a = [item for item in advent_calendar if 'a' in item]
print(a)
['apple', 'date', 'mango']
while-loop¶
We know the for-loop already, but there’s another type of loop, which depends on conditions. The loop is executed as long (while) the condition is true. It is used if you don’t know the amount of iterations, instead know the changing conditions.
a = 0
while a < 10:
print(a)
a += 1
0
1
2
3
4
5
6
7
8
9
Take care with while-loops, because they can run forever if the condition stays True
forever, like
while True:
print('I print this text forever')
# or
a = 0
while a < 10:
print(a)
a = 1
# or
while ('a rose' is 'a rose'):
print('a rose is ', end='')
# Although it's not recommended to use 'is' for string comparison.