Python: 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:

0 > -2
True
for i in range(3):
    print(1, '>=', i, 1 >= i)
1 >= 0 True
1 >= 1 True
1 >= 2 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):
    print('page number', i, end=': ')
    # if i is even:
    if i % 2 == 0:
        print('left page')
    else:
        print('right page')
page number 2: left page
page number 3: right page
page number 4: left page
page number 5: right page
page number 6: left page
page number 7: right page
page number 8: left page
page number 9: right page

In the example above we use the optional argument `end` inside the first `print()` statement.
Let's inspect it with `help()`:
help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Explanation:

  • value, ... stands for the values we insert into a print() function to be printed
  • sep has a default value of ' ', which means that all comma-separated values that we insert into print() will be separated by a ' '
  • end is by default assigned with a newline character ('\n'), meaning that the next output will be in the next line. We can change that if we want to combine multiple print() functions in one output (as done here with ': ').

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']
Task: Create a calendar that includes all items which contain an 'a'.
# 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.