2 Flow Control Flashcards
What are the two values of the Boolean data type? How do you write them?
True
False
What are the three Boolean operators?
And
Or
Not
Write out the truth tables of each Boolean operator (that is, every possible combination of Boolean values for the operator and what they evaluate to).
And: True and True -> True True and False > False False and True -> False False and False -> False
Or: True or True -> True True or False -> True False or True -> True False or False -> False
Not:
Not True -> False
Not False -> True
What are the six comparison operators?
== =! < < >= <=
Explain what a condition is and where you would use one.
E.g. boolean expressiones/ operators.
Conditions always evaluate down to True or False and thus decide which part of the code is executed
Name three flow control statement types and state what they are made of
- If
- While loop -> while true
- For loop -> for a certain number
They often start with a condition, which is always followed by the clause (block of code)
What keys can you press if your program is stuck in an infinite loop?
ctrl + c
What is the difference between break and continue?
Both are used inside loops.
Continue: program execution jumps back to the start of the loop
Break: program execution jumps out of the loop
What does this code evaluate to?
x = [[1, 2], [3, 4]]
for x in x:
for x in x:
print(x)
How could you write this code in one line?
x = [[1, 2], [3, 4]]
for x in x:
for x in x:
print(x)
Evaluates to: 1 2 3 4
One line:
x for x in x for x in x
numbers=[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’]
letters=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
How can you create a list, that has as many list as there are numbers, and as many values as there
numbers=[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’]
letters=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
spam = [] for nr in numbers: spam.append(list()) for letter in letters: spam[-1].append(nr + letter) print(spam)