Bird
Raised Fist0
Pythonprogramming~20 mins

How Python executes code - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Python Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding Python Execution Order
What is the output of this Python code snippet?
Python
def greet():
    print("Hello")

print("Start")
greet()
print("End")
AHello\nStart\nEnd
BStart\nHello\nEnd
CStart\nEnd\nHello
DHello\nEnd\nStart
Attempts:
2 left
💡 Hint
Think about the order in which functions are called and print statements run.
Predict Output
intermediate
2:00remaining
Variable Scope in Python Execution
What will be the output of this code?
Python
x = 5
def change():
    x = 10
change()
print(x)
A5
B10
CNone
DError
Attempts:
2 left
💡 Hint
Consider where the variable x is changed and where it is printed.
Predict Output
advanced
2:00remaining
Order of Execution with List Comprehensions
What is the output of this code?
Python
result = [x**2 for x in range(3)]
print(result)
AError
B[1, 4, 9]
C[0, 1, 2]
D[0, 1, 4]
Attempts:
2 left
💡 Hint
Remember that range(3) produces 0,1,2 and each is squared.
Predict Output
advanced
2:00remaining
Execution Flow with Conditional Statements
What will this code print?
Python
x = 7
if x > 5:
    print("Greater")
else:
    print("Smaller")
AError
BSmaller
CGreater
DNothing
Attempts:
2 left
💡 Hint
Check the condition in the if statement carefully.
🧠 Conceptual
expert
2:00remaining
Python Execution Model: What Happens First?
When Python runs a script, which step happens first?
APython compiles the code to bytecode before running it
BPython immediately runs the code line by line without compilation
CPython converts the code to machine code directly
DPython sends the code to the operating system to execute
Attempts:
2 left
💡 Hint
Think about how Python prepares code before executing it.

Practice

(1/5)
1. What is the first step Python takes when you run a program?
easy
A. It converts the code into bytecode.
B. It immediately executes the code line by line.
C. It compiles the code into machine language.
D. It sends the code to the operating system.

Solution

  1. Step 1: Understand Python's execution process

    Python first translates the source code into bytecode, which is a lower-level, platform-independent representation.
  2. Step 2: Recognize what happens next

    The bytecode is then executed by the Python Virtual Machine (PVM), not the operating system directly.
  3. Final Answer:

    It converts the code into bytecode. -> Option A
  4. Quick Check:

    Python compiles to bytecode first [OK]
Hint: Remember: Python compiles before running [OK]
Common Mistakes:
  • Thinking Python runs code directly without compiling
  • Confusing bytecode with machine code
  • Assuming OS runs Python code directly
2. Which of the following is the correct way to write a simple Python print statement?
easy
A. print "Hello, world!"
B. print('Hello, world!')
C. echo('Hello, world!')
D. printf('Hello, world!')

Solution

  1. Step 1: Recall Python syntax for printing

    Python uses the print() function with parentheses and quotes around the string.
  2. Step 2: Identify the correct syntax

    print('Hello, world!') uses print() with parentheses and quotes correctly.
  3. Final Answer:

    print('Hello, world!') -> Option B
  4. Quick Check:

    print() needs parentheses [OK]
Hint: Use print() with parentheses and quotes [OK]
Common Mistakes:
  • Omitting parentheses in print statement
  • Using echo or printf which are not Python functions
  • Using print without quotes around text
3. What will be the output of this code?
print('Start')
for i in range(2):
    print(i)
print('End')
medium
A. Start\n0\n1\nEnd
B. Start\n1\n2\nEnd
C. 0\n1\nStart\nEnd
D. Start\nEnd

Solution

  1. Step 1: Trace the print and loop execution

    The first print outputs 'Start'. The loop runs with i=0 and i=1, printing each. Then 'End' is printed.
  2. Step 2: Write the output line by line

    Output lines are: Start, 0, 1, End.
  3. Final Answer:

    Start\n0\n1\nEnd -> Option A
  4. Quick Check:

    Loop prints 0 and 1 between Start and End [OK]
Hint: Remember range(2) gives 0 and 1 [OK]
Common Mistakes:
  • Assuming range(2) starts at 1
  • Ignoring the initial and final print statements
  • Confusing loop variable values
4. Find the error in this code snippet:
for i in range(3)
    print(i)
medium
A. print() should be outside the loop
B. Indentation error on print statement
C. Missing colon ':' after range(3)
D. range() function is used incorrectly

Solution

  1. Step 1: Check syntax of for loop

    Python requires a colon ':' at the end of the for statement line.
  2. Step 2: Identify the missing colon

    The code misses ':' after range(3), causing a syntax error.
  3. Final Answer:

    Missing colon ':' after range(3) -> Option C
  4. Quick Check:

    for loops need ':' at the end [OK]
Hint: Look for missing colons in loops and conditionals [OK]
Common Mistakes:
  • Forgetting the colon ':' after for statement
  • Misplacing indentation but here it's correct
  • Thinking range() usage is wrong
5. You want to write a program that prints numbers 1 to 5, but skips number 3. Which code correctly shows how Python executes this?
hard
A. for i in range(1,6): if i == 3: pass print(i)
B. for i in range(1,5): if i == 3: break print(i)
C. for i in range(1,6): if i != 3: break print(i)
D. for i in range(1,6): if i == 3: continue print(i)

Solution

  1. Step 1: Understand the goal and loop behavior

    We want to print numbers 1 to 5 but skip printing 3. Using continue skips the current loop iteration.
  2. Step 2: Analyze each option

    for i in range(1,6): if i == 3: continue print(i) uses continue to skip printing 3 and prints all others. Others either break early or print 3.
  3. Final Answer:

    for i in range(1,6): if i == 3: continue print(i) -> Option D
  4. Quick Check:

    continue skips printing 3 [OK]
Hint: Use continue to skip unwanted loop steps [OK]
Common Mistakes:
  • Using break which stops the loop entirely
  • Using pass which does nothing to skip printing
  • Incorrect range limits causing missing numbers