Practice: pass Statement

« Read the chapter

Q1. Empty function

BeginnerCoding

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

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

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

IntermediateCoding

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

AdvancedCoding

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

BeginnerOutput

What does this code print?

Code
for i in range(3):
    pass
print(i)

Goal: Understand loop variable and pass.

Q8. pass does not skip

BeginnerOutput

What does this code print?

Code
for n in [1, 2, 3]:
    if n == 2:
        pass
    print(n)

Goal: Compare pass with continue.

Q9. continue instead of pass

IntermediateOutput

What does this code print?

Code
for n in [1, 2, 3]:
    if n == 2:
        continue
    print(n)

Goal: Compare with continue.

Q10. Return value

IntermediateOutput

What does this code print?

Code
def f():
    pass
print(f())

Goal: Know the result of an empty function.

Q11. Pass in try

AdvancedOutput

What does this code print?

Code
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

BeginnerDebugging

This code raises an IndentationError. Fix it so it prints ok.

Code
def show():
    # TODO

show()
print("ok")

Goal: Fix empty blocks.

Q13. Wrong choice of statement

IntermediateDebugging

The goal is to print 1, 3 only (skip 2). The code prints all. Fix it.

Code
for n in [1, 2, 3]:
    if n == 2:
        pass
    print(n)

Goal: Choose the right statement.

Q14. Why pass?

BeginnerConceptual

Why does Python have a pass statement?

Goal: Explain the purpose of pass.

Q15. pass versus break

IntermediateConceptual

What is the difference between pass and break in a loop?

Goal: Distinguish loop statements.

Q16. Plan a class

IntermediateApplication

You will write a class Student later. Write the class with a placeholder, create an object and print Created.

Goal: Use placeholders in planning.