0
0
Pythonprogramming~5 mins

If–else execution flow in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does an if statement do in Python?
It checks a condition and runs a block of code only if that condition is true.
Click to reveal answer
beginner
What is the purpose of the else block?
The else block runs code when the if condition is false.
Click to reveal answer
beginner
How does Python decide which block to run in an if–else structure?
Python checks the if condition first. If true, it runs the if block. If false, it runs the else block.
Click to reveal answer
beginner
What happens if the if condition is true and there is an else block?
Only the if block runs. The else block is skipped.
Click to reveal answer
beginner
Can you have an if statement without an else?
Yes. The else block is optional. If missing, nothing happens when the if condition is false.
Click to reveal answer
What will this code print?<br>
if 5 > 3:
    print('Yes')
else:
    print('No')
AYes
BNo
CNothing
DError
If the if condition is false and there is no else, what happens?
AThe program crashes
BNothing happens
CThe code inside <code>if</code> runs
DThe program asks for input
Which keyword starts the block that runs when the if condition is false?
Aelif
Bif
Celse
Dthen
What is the correct way to write an if–else statement in Python?
Aif condition: do_something else: do_something_else
Bif condition then do_something else do_something_else
Cif (condition) { do_something } else { do_something_else }
Dif condition do_something else do_something_else
What will this code print?<br>
if False:
    print('A')
else:
    print('B')
AError
BA
CNothing
DB
Explain how Python decides which code to run in an if–else statement.
Think about the condition and the two possible paths.
You got /4 concepts.
    Describe what happens if the if condition is false and there is no else block.
    Consider what Python does when no alternative code is given.
    You got /4 concepts.