Practice: pass Statement
Q1. Empty function
Write a function named todo that has no code yet (use pass), call it and print its return value.
Goal: Use pass in a function.
Q2. Empty if branch
Set x = 5. If x is greater than 3, use pass; otherwise print small. Finally print done.
Goal: Use pass in an if.
Q3. Empty class
Create an empty class named Box using pass, make an object and print the class name using type(obj).__name__.
Goal: Use pass in a class.
Q4. Custom exception
Define an empty exception class AgeError based on Exception. Raise and catch it and print Bad age.
Goal: Create custom exceptions.
Q5. Ignore bad numbers
Add up only the valid integers in ["4", "a", "6", "b"] and ignore errors with pass. Print the total.
Goal: Use pass in except.
Q6. Skeleton program
Write functions load() and save() with pass, and a main() that calls both and prints Ready. Then call main().
Goal: Plan a program with placeholders.
Q7. pass in a loop
What does this code print?
for i in range(3):
pass
print(i)Goal: Understand loop variable and pass.
Q8. pass does not skip
What does this code print?
for n in [1, 2, 3]:
if n == 2:
pass
print(n)Goal: Compare pass with continue.
Q9. continue instead of pass
What does this code print?
for n in [1, 2, 3]:
if n == 2:
continue
print(n)Goal: Compare with continue.
Q10. Return value
What does this code print?
def f():
pass
print(f())Goal: Know the result of an empty function.
Q11. Pass in try
What does this code print?
items = ["1", "two", "3"]
total = 0
for s in items:
try:
total += int(s)
except ValueError:
pass
print(total)Goal: Trace exception handling.
Q12. Empty body error
This code raises an IndentationError. Fix it so it prints ok.
def show():
# TODO
show()
print("ok")Goal: Fix empty blocks.
Q13. Wrong choice of statement
The goal is to print 1, 3 only (skip 2). The code prints all. Fix it.
for n in [1, 2, 3]:
if n == 2:
pass
print(n)Goal: Choose the right statement.
Q14. Why pass?
Why does Python have a pass statement?
Goal: Explain the purpose of pass.
Q15. pass versus break
What is the difference between pass and break in a loop?
Goal: Distinguish loop statements.
Q16. Plan a class
You will write a class Student later. Write the class with a placeholder, create an object and print Created.
Goal: Use placeholders in planning.