02 Conditions, Loops Flashcards
To make decisions we need an expression that results in a boolean value. Which of the following statements can be used for this purpose?
- x < 9
- x = 9
- 4 > 7
- 4 < 7 < 9
- 4 + 9
- (4 + 9) > 10
- 4 + (9 < 10)
- can be used, returns either True or False.
- can not be used. It is an assignment of a value to a variable that doesn’t return either True or False.
- can be used, returns False.
- can be used, returns True.
- can not be used. It is a calculation that returns the value 13.
- can be used, returns True.
- can not be used. It is a calculation that returns the value 5 (True is interpreted as 1).
What are Logical Operators?
“not”, “or” and “and”.
They’re also called boolean operators.
How does the boolean operator “or” work?
It checks if either the first or the second expression results in True. If so, it returns True. If not, it returns False.
If the first boolean expression is already True, the evaluation of the second is skipped.
How does the boolean operator “and” work?
It checks if both the first and the second expression result in True. If so, it returns True, if not it returns False.
If the first boolean expression is already False, the evaluation of the second is skipped.
Which of the following two checks is potentially less safe, and why?
- len(my_string) >= 1 and my_string[0] == “f”
- my_string[0] == “f” and len(my_string) >= 1
- is safer.
The expression (my_string[0] == “f”) will return an error, if my_string is empty. The expression (len(my_string) >= 1) tells me if my_string is empty without raising an error.
Assuming my_string to be empty and turning the expressions into boolean values, the two checks look like this:
1. False and error
2. error and False
For 1., since the first expression is already False the evaluation of the second is skipped and the error isn’t raised.
For 2., the first expression is an error, which will be raised instantly.
When evaluated in a boolean context, are the following objects interpreted as True or False?
- The number 0.
- The special value None.
- The numeric value 1.
- An empty string.
- A list with two entries.
- A string “False”.
- False
- False
- True
- False
- True
- True
What does the following statement return, and why?
my_string = []
if (my_string):
print(“foo”)
else:
print(“bar”)
bar
The empty list is interpreted as False, so the else-branch is executed.
What are Assertions, how do they work and how should they be used?
An assertion checks a condition and raises an error if the boolean evaluates to False.
They are used by programers to see wether their code behaves as expected.
They should not be used for general error/exception handling since users can disable them before running the code.
How can you run a Python code with assertions disabled?
By adding “-O” to the command line after the keyword “python”:
python -O a5_ex1.py
What happens if an assertion fails?
an AssertionError is raised.
What branching methods did we learn about?
If-elif-else
pattern match
What is if-ifel-else?
Using the keywords “if”, “ifel” and “else”, as well as conditions, we can splice the code into different path branches.
Going from top to bottom, the first condition to return True, is the branch that gets executed. The others are ignored.
What is pattern matching?
The keyword “match” and variable are combined. Using the “case” keyword we check which path branch matches the value of the variable. The first case that matches is the branch that gets executed.
What kind of loops have we learned of?
While-loops and For-loops
How do loops work?
Depending on some condition we can execute part of our code zero or more times (even infinitely often).