How Python executes code - Performance & Efficiency
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how Python runs code and how the time it takes changes as the code gets bigger or more complex.
Our question: How does the work Python does grow when the input or code size grows?
Analyze the time complexity of the following code snippet.
for i in range(n):
print(i)
sum = 0
for j in range(n):
sum += j
print(sum)
This code prints numbers from 0 to n-1, then sums numbers from 0 to n-1 and prints the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops each running from 0 to n-1.
- How many times: Each loop runs n times, so total loops run about 2n times.
As n grows, the total work grows roughly twice as fast because of two loops.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (2 loops x 10 each) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The work grows in a straight line as n grows, doubling the input doubles the work.
Time Complexity: O(n)
This means the time Python takes grows directly in proportion to the size of the input n.
[X] Wrong: "Two loops mean the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not inside each other, so their times add up, not multiply.
Understanding how Python runs code and how time grows with input helps you explain your solutions clearly and confidently in interviews.
"What if the second loop was inside the first loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand Python's execution process
Python first translates the source code into bytecode, which is a lower-level, platform-independent representation.Step 2: Recognize what happens next
The bytecode is then executed by the Python Virtual Machine (PVM), not the operating system directly.Final Answer:
It converts the code into bytecode. -> Option AQuick Check:
Python compiles to bytecode first [OK]
- Thinking Python runs code directly without compiling
- Confusing bytecode with machine code
- Assuming OS runs Python code directly
Solution
Step 1: Recall Python syntax for printing
Python uses the print() function with parentheses and quotes around the string.Step 2: Identify the correct syntax
print('Hello, world!') uses print() with parentheses and quotes correctly.Final Answer:
print('Hello, world!') -> Option BQuick Check:
print() needs parentheses [OK]
- Omitting parentheses in print statement
- Using echo or printf which are not Python functions
- Using print without quotes around text
print('Start')
for i in range(2):
print(i)
print('End')Solution
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.Step 2: Write the output line by line
Output lines are: Start, 0, 1, End.Final Answer:
Start\n0\n1\nEnd -> Option AQuick Check:
Loop prints 0 and 1 between Start and End [OK]
- Assuming range(2) starts at 1
- Ignoring the initial and final print statements
- Confusing loop variable values
for i in range(3)
print(i)Solution
Step 1: Check syntax of for loop
Python requires a colon ':' at the end of the for statement line.Step 2: Identify the missing colon
The code misses ':' after range(3), causing a syntax error.Final Answer:
Missing colon ':' after range(3) -> Option CQuick Check:
for loops need ':' at the end [OK]
- Forgetting the colon ':' after for statement
- Misplacing indentation but here it's correct
- Thinking range() usage is wrong
Solution
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.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.Final Answer:
for i in range(1,6): if i == 3: continue print(i) -> Option DQuick Check:
continue skips printing 3 [OK]
- Using break which stops the loop entirely
- Using pass which does nothing to skip printing
- Incorrect range limits causing missing numbers
