02 Conditions, Loops Flashcards

1
Q

To make decisions we need an expression that results in a boolean value. Which of the following statements can be used for this purpose?

  1. x < 9
  2. x = 9
  3. 4 > 7
  4. 4 < 7 < 9
  5. 4 + 9
  6. (4 + 9) > 10
  7. 4 + (9 < 10)
A
  1. can be used, returns either True or False.
  2. can not be used. It is an assignment of a value to a variable that doesn’t return either True or False.
  3. can be used, returns False.
  4. can be used, returns True.
  5. can not be used. It is a calculation that returns the value 13.
  6. can be used, returns True.
  7. can not be used. It is a calculation that returns the value 5 (True is interpreted as 1).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Logical Operators?

A

“not”, “or” and “and”.

They’re also called boolean operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does the boolean operator “or” work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the boolean operator “and” work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following two checks is potentially less safe, and why?

  1. len(my_string) >= 1 and my_string[0] == “f”
  2. my_string[0] == “f” and len(my_string) >= 1
A
  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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When evaluated in a boolean context, are the following objects interpreted as True or False?

  1. The number 0.
  2. The special value None.
  3. The numeric value 1.
  4. An empty string.
  5. A list with two entries.
  6. A string “False”.
A
  1. False
  2. False
  3. True
  4. False
  5. True
  6. True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the following statement return, and why?

my_string = []
if (my_string):
print(“foo”)
else:
print(“bar”)

A

bar

The empty list is interpreted as False, so the else-branch is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Assertions, how do they work and how should they be used?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you run a Python code with assertions disabled?

A

By adding “-O” to the command line after the keyword “python”:

python -O a5_ex1.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens if an assertion fails?

A

an AssertionError is raised.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What branching methods did we learn about?

A

If-elif-else
pattern match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is if-ifel-else?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is pattern matching?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What kind of loops have we learned of?

A

While-loops and For-loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do loops work?

A

Depending on some condition we can execute part of our code zero or more times (even infinitely often).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does a while-loop work?

A

A while loop will repeat a part of code as long as the boolean expression of the loop condition is True.
This can also lead to infinite loops, if the boolean expression is never updated to False.

17
Q

How does a for-loop work?

A

A for-loop will repeat a part of code for each element in an iterable of elements.

18
Q

How can a loop be exited before it’s run through completely?

A

with the keyword “break” the loop is exited. Code that is below the “break” keyword inside the loop will be ignored. Code, that is outside and after the loop, will be executed next.

19
Q

What does the keyword “continue” to when put inside a loop?

A

It stops the current loop and jumps to the loop condition again to check it again.