0
0
Pythonprogramming~5 mins

Pass statement usage in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the pass statement do in Python?
The pass statement does nothing. It is used as a placeholder where code is required syntactically but no action is needed.
Click to reveal answer
beginner
Why would you use pass inside a function or loop?
You use pass to create an empty function or loop body temporarily, so the program runs without errors while you plan to add code later.
Click to reveal answer
beginner
Can pass be used in an if statement? Show an example.
Yes, pass can be used in an if statement to do nothing when the condition is true.<br>
if x > 0:
    pass  # do nothing for now
Click to reveal answer
intermediate
What happens if you omit code inside a block without using pass?
Python will give an IndentationError because it expects some code inside the block. pass prevents this error by acting as a placeholder.
Click to reveal answer
intermediate
Is pass the same as continue or break?
pass does nothing and just moves on. continue skips to the next loop iteration, and break exits the loop entirely.
Click to reveal answer
What is the purpose of the pass statement in Python?
ATo act as a placeholder and do nothing
BTo exit a loop immediately
CTo skip the current loop iteration
DTo raise an error
Which of these will cause an error in Python?
ALeaving a function body empty without <code>pass</code>
BUsing <code>pass</code> inside an empty function
CUsing <code>pass</code> inside an <code>if</code> block
DUsing <code>pass</code> inside a loop
What will this code print?<br>
for i in range(3):
    if i == 1:
        pass
    print(i)
A0 2
B0 1 2
C1
DError
Which statement is true about pass?
AIt skips the rest of the current loop iteration
BIt exits the current function
CIt raises an exception
DIt is a no-operation placeholder
When might you use pass?
ATo skip a line of code
BTo stop a loop
CTo temporarily create an empty class or function
DTo print output
Explain the role of the pass statement in Python and give an example where it is useful.
Think about writing code you plan to complete later.
You got /4 concepts.
    Describe what happens if you leave a code block empty without using pass.
    Python expects some code inside blocks.
    You got /4 concepts.