Module 3 - Conditionals Flashcards
What are control structures? What are examples of control structures?
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 do statements execute in a sequence control structure?
Line-by-line in the order they appear
How do statements execute in a decision control structure?
Specified action occurs only if a condition exists
What does a diamond represent in a flowchart?
Decision point
Represents true/false condition that must be tested
What is a single alternative decision structure?
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.
if statement in Python
if “condition”:
statement 1
statement 2
- 4 spaces before statements
- NOT If (case sensitive!)
What is a boolean expression? Give an example.
An expression that evalutes to true or false
Tested by if statement
Example: a>b
How do you test if a and b are equal?
a==b
What is a relational operator?
Determines relationship b/w two values/ operands
>,=,<=,>=,==,!=
What happens if you enter “if a=b:” into Python?
= is the assignment operator
writing “if a=b:” will produce a syntax error
to test if they are equal, must use ==
What’s the relational operator for “not equal”?
!=
What is a dual alternative decision structure?
Two possible paths of execution: one if decision is true, one if decision is false
Write an if statement that assigns 0 to x if y is equal to 20.
if y==20:
x=0
How do you align/indent if-then statements?
if condition:
statements
else:
statements
if and else must be aligned
statements in each block must be indented
(4 spaces before the statements)
What is whitespace? How many spaces per indentation?
Whitespace is just characters which are used for spacing, and have an “empty” representation
4 spaces per indentation
When indenting in python, can you mix tab and space?
No NEVER mix them
If you compare a and b in Python, which is bigger?
“b”>”a”
“c”>”b”
Is “a”>”A” in Python?
Yes! ‘A’
How would Python compare the strings hellO and Hello? i.e., “hellO” < “Hello”
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.
What is the syntax for if-elseif-else statement in Python?
If condition: \_\_statements elif condition: \_\_statements else: \_\_statements
What is else if statement?
It’s a nested if statement that’s simpler to write
What are logical operators?
and, or, not
they combine boolean expressions to produce another true/false value
What will be the result? true AND true true AND false false AND true false AND false
true
false
false
false
(it’s true ONLY IF both are true)
What will be the result? true OR true true OR false false OR true false OR false
true
true
true
false
(it’s true if EITHER one is true)
What is the literal that represents a true value? false value?
True
False
*IT IS CAPITALIZED - case sensitive!!!
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
What does the ‘not’ operator do?
Reverses the boolean expression
True –> False
False –> True
Use boolean expressions and logical operators to determine if
- x is between 10 and 20, inclusive
- x is outside of 10 and 20
if x>=10 and x<=20
- can also do if 1020
if x<10 or x>20
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’
What does // do? What’s it called?
Floor division operator
Divides two numbers and rounds down to an integer
7//2 = 3
What does % do? What’s it called?
Modulus operator
Divides two numbers and returns the remainder
7%2 = 1
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)
How do you test if a number is divisible by 2 or 3?
n%2==0 or n%3==0
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
What are the alternatives called in alternative execution? (alternative decision structure)
Branches
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
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
What is base case?
A conditional branch in a recursive function that does not make a recursive call
What is a return statement?
A statement that causes a function to end immediately and return to the caller
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.
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.
How do you line break?
\n
What is a module?
A file containing python code
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’
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)
- worked_overtime == “True”
2. worked_overtime
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
What is the shorthand version of deansList = deansList +1? What about a = a * 2
deansList += 1
a *=2
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)