Module 3 - Conditionals Flashcards

1
Q

What are control structures? What are examples of control structures?

A
A logical design that controls the order in which a set of statements execute
A structure (block of code) that CONTROLS the flow of the program
For example, sequential (sequence), decision (selection) = if/then, iteration (loop)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do statements execute in a sequence control structure?

A

Line-by-line in the order they appear

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

How do statements execute in a decision control structure?

A

Specified action occurs only if a condition exists

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

What does a diamond represent in a flowchart?

A

Decision point

Represents true/false condition that must be tested

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

What is a single alternative decision structure?

A

If condition is not true, exit structure
A decision structure that provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.

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

if statement in Python

A

if “condition”:
statement 1
statement 2

  • 4 spaces before statements
  • NOT If (case sensitive!)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a boolean expression? Give an example.

A

An expression that evalutes to true or false
Tested by if statement
Example: a>b

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

How do you test if a and b are equal?

A

a==b

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

What is a relational operator?

A

Determines relationship b/w two values/ operands

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

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

What happens if you enter “if a=b:” into Python?

A

= is the assignment operator
writing “if a=b:” will produce a syntax error

to test if they are equal, must use ==

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

What’s the relational operator for “not equal”?

A

!=

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

What is a dual alternative decision structure?

A

Two possible paths of execution: one if decision is true, one if decision is false

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

Write an if statement that assigns 0 to x if y is equal to 20.

A

if y==20:

x=0

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

How do you align/indent if-then statements?

A

if condition:
statements
else:
statements

if and else must be aligned
statements in each block must be indented
(4 spaces before the statements)

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

What is whitespace? How many spaces per indentation?

A

Whitespace is just characters which are used for spacing, and have an “empty” representation
4 spaces per indentation

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

When indenting in python, can you mix tab and space?

A

No NEVER mix them

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

If you compare a and b in Python, which is bigger?

A

“b”>”a”

“c”>”b”

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

Is “a”>”A” in Python?

A

Yes! ‘A’

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

How would Python compare the strings hellO and Hello? i.e., “hellO” < “Hello”

A

It goes letter by letter (in the corresponding location)

The FIRST comparison that fails stops the whole comparison and returns the false
h < H –> FALSE. it should stop there and return false.

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

What is the syntax for if-elseif-else statement in Python?

A
If condition: 
\_\_statements
elif condition: 
\_\_statements
else: 
\_\_statements
21
Q

What is else if statement?

A

It’s a nested if statement that’s simpler to write

22
Q

What are logical operators?

A

and, or, not

they combine boolean expressions to produce another true/false value

23
Q
What will be the result? 
true AND true
true AND false
false AND true
false AND false
A

true
false
false
false

(it’s true ONLY IF both are true)

24
Q
What will be the result? 
true OR true
true OR false
false OR true
false OR false
A

true
true
true
false

(it’s true if EITHER one is true)

25
What is the literal that represents a true value? false value?
True False *IT IS CAPITALIZED - case sensitive!!!
26
What is a short circuit evaluation in a compound boolean expression?
Determining the value after evaluating only one subexpression Or - if left operand is true, compound expression is true And - if left operand is false, compound expression is false Otherwise, evaluate right operand
27
What does the 'not' operator do?
Reverses the boolean expression True --> False False --> True
28
Use boolean expressions and logical operators to determine if 1. x is between 10 and 20, inclusive 2. x is outside of 10 and 20
if x>=10 and x<=20 - can also do if 1020 if x<10 or x>20
29
What is a boolean variable? What is a flag?
bool - references one of two values: True or False flag is a variable that signals when a condition exists in a program GoAhead = 'A'
30
What does // do? What's it called?
Floor division operator Divides two numbers and rounds down to an integer 7//2 = 3
31
What does % do? What's it called?
Modulus operator Divides two numbers and returns the remainder 7%2 = 1
32
What are some uses for %?
If one number is divisible by another Odd or even Can extract the right-most digit (x%10) or digits (x%100)
33
How do you test if a number is divisible by 2 or 3?
n%2==0 or n%3==0
34
What can you use as a placeholder if you haven't written the code yet and want to come back to it?
pass it does nothing if x<0: pass
35
What are the alternatives called in alternative execution? (alternative decision structure)
Branches
36
What is a chained conditional?
elif (which is the if-elseif-else statement) can use if there's more than two branches else is the end, but there doesn't HAVE to be an else statement
37
What is a function that calls itself called? What's the process of executing called?
A function that calls itself is recursive | The process of executing it is called recursion
38
What is base case?
A conditional branch in a recursive function that does not make a recursive call
39
What is a return statement?
A statement that causes a function to end immediately and return to the caller
40
What is a compound statement?
A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.
41
What is infinite recursion?
A recursion that doesn’t have a base case, or never reaches it. Eventually, an infinite recursion causes a runtime error.
42
How do you line break?
\n
43
What is a module?
A file containing python code
44
Assume that c is a variable has been assigned a string value. Write an expression whose value is true if and only if c is a tab character.
c=='\t'
45
Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True. (there are two ways to answer this question)
1. worked_overtime == "True" | 2. worked_overtime
46
Write an expression that evaluates to True if the int associated with number_of_prizes is divisible (with no remainder) by the int associated with number_of_participants. Assume that the latter is not zero.
number_of_prizes%number_of_participants==0
47
What is the shorthand version of deansList = deansList +1? What about a = a * 2
deansList += 1 | a *=2
48
ispremiumcustomer is a boolean variable with value True. what is the result of the code and why? if ispremiumcustomer: print("it worked!")
it worked! because ispremiumcustomer is a condition that was met (it was True)