u02-slides-conditions-loops-flashcards

1
Q

What are the three main logical operations in Python?

A

not, or, and

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

What is short-circuit evaluation in Python?

A

For ‘or’, if first expression is True, second is skipped. For ‘and’, if first is False, second is skipped.

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

What objects are interpreted as False in Python?

A

The special value None, the value 0 of all numeric types, and empty sequences

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

What is the purpose of assertions in Python?

A

To implement sanity checks that raise an error if a condition evaluates to False - useful for debugging

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

How can assertions be disabled in Python?

A

By running Python with the -O option (python -O …)

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

What are the main branching statements in Python?

A

if, elif, and else statements

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

How is code within branches assigned in Python?

A

Through indentation (typically 4 spaces) rather than braces

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

What is pattern matching in Python?

A

A feature introduced in Python 3.10 that allows structural pattern matching using the match statement

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

What are the two main types of loops in Python?

A

while loops and for loops

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

What is the difference between while and for loops?

A

while loops repeat based on a condition being True, for loops iterate over elements in an iterable

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

What are common use cases for while loops?

A

Asking for input until correct, running main routines until stopped, optimization until convergence

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

What is a potential danger with while loops?

A

They can lead to endless/infinite loops if the condition is never False

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

What is an iterable in Python?

A

A sequence of elements that can be iterated over (e.g., strings, data structures)

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

What keywords can manually control loop execution?

A

break (to exit the loop) and continue (to jump to next iteration)

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

Why should manual loop control keywords be used sparingly?

A

They break the normal control flow of the program by creating sudden jumps in code

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

How do you write a chained comparison in Python?

A

You can chain operators, e.g., 3 < 4 < 5

17
Q

What happens if there’s no else statement in an if-elif chain?

A

No branch might be executed at all

18
Q

What’s the syntax for adding a message to an assertion?

A

assert condition, “message”

19
Q

When should assertions not be used?

A

For general error/exception handling (since users can disable them)

20
Q

How do you write a for loop to iterate over a string?

A

“for char in my_string:”

21
Q

What is the typical indentation size in Python?

A

4 spaces

22
Q

How are multiple conditions evaluated in if-elif-else chains?

A

From top to bottom

23
Q

What is the default case in pattern matching called?

A

The underscore (_) case

24
Q

What happens to code after a break or continue statement?

A

It is ignored within that loop iteration