Chapter 3 Flashcards

(25 cards)

1
Q

What is a control structure?

A

The logical design that controls the order in which statements execute

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

What is a sequence structure?

A

A set of statements that execute in the order they appear

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

What is a decision (selection) structure?

A

A structure where specific actions are performed only if a condition exists

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

What is the syntax of an if statement in Python?

A

if condition:
statement(s)
The condition is tested; if true, the block executes; otherwise, it is skipped

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

What is a Boolean expression?

A

An expression that evaluates to either True or False

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

What are relational operators in Python?

A

> , <, >=, <=, ==, !=

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

What is the difference between = and == in Python?

A

= is the assignment operator; == tests for equality

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

What is a single-line if statement?

A

A condensed form where one statement executes if the condition is true (e.g., if score > 59: print(‘You passed!’))

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

What is an if-else statement?

A

A dual alternative decision structure with one path if the condition is true and another if false

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

How are strings compared in Python?

A

Using ==, !=, <, >, <=, >=; comparisons are case-sensitive and based on ASCII values

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

What is a nested decision structure?

A

An if statement inside another if statement

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

What is the if-elif-else statement used for?

A

To simplify nested decision structures and allow multiple conditions

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

What are logical operators in Python?

A

and, or, and not

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

When is and true?

A

Only when both sub-expressions are true

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

When is or true?

A

When at least one sub-expression is true

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

What does not do?

A

Reverses the truth value of its operand

17
Q

What is short-circuit evaluation?

A

Evaluating a compound Boolean expression by stopping once the outcome is known (e.g., in or, if the left side is true, Python skips the right)

18
Q

How do you check numeric ranges with logical operators?

A

Inside range: x >= 10 and x <= 20

Outside range: x < 10 or x > 20

19
Q

What is a Boolean variable?

A

A variable referencing either True or False, often used as a flag

20
Q

What is a flag variable?

A

A Boolean variable that signals when a condition exists in a program

21
Q

What is a conditional expression in Python?

A

A shorthand if-else expression: value1 if condition else value2

22
Q

Example of a conditional expression?

A

grade = ‘Pass’ if score > 59 else ‘Fail’

23
Q

What is the walrus operator (:=)

A

An enhanced assignment operator that both assigns a value and returns it

24
Q

Example of walrus operator usage?

A

if (area := width * height) > 100: print(‘The area is too large’)

25
What is the precedence of the walrus operator?
It has the lowest precedence of all Python operators