Return values in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we look at return values in a function, we want to see how long it takes to get that result.
We ask: How does the time to return a value change as the input grows?
Analyze the time complexity of the following code snippet.
def get_first_element(items):
if items:
return items[0]
return None
This function returns the first item from a list if it exists, otherwise None.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first element of the list.
- How many times: Exactly once, no loops or repeated steps.
Getting the first element takes the same effort no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the list grows larger.
Time Complexity: O(1)
This means the time to get the first item does not change with the size of the list.
[X] Wrong: "Accessing an element always takes longer if the list is bigger."
[OK] Correct: Accessing by index in a list is direct and fast, no matter the list size.
Understanding how simple return statements work helps you explain code efficiency clearly and confidently.
"What if we changed the function to return the last element instead of the first? How would the time complexity change?"
Practice
return statement do in a Python function?Solution
Step 1: Understand the purpose of
Thereturnreturnstatement ends the function and sends a value back to the caller.Step 2: Differentiate from printing or pausing
Printing shows output but does not send a value back; pausing does not return a value.Final Answer:
It sends a value back to where the function was called. -> Option AQuick Check:
Return sends value back [OK]
- Confusing return with print
- Thinking return pauses without value
- Assuming return creates variables
Solution
Step 1: Recall correct return syntax
The correct syntax is simplyreturnfollowed by the value without assignment or colon.Step 2: Check each option
return 10is correct;return(10,)returns a tuple (10,) due to the trailing comma; the others cause syntax errors.Final Answer:
return 10 -> Option BQuick Check:
Return value with 'return value' syntax [OK]
- Using assignment with return
- Adding colon after return
- Using parentheses incorrectly
def add(a, b):
return a + b
result = add(3, 4)
print(result)Solution
Step 1: Understand the function behavior
The functionaddreturns the sum ofaandb.Step 2: Calculate the return value
Callingadd(3, 4)returns 7, which is stored inresultand printed.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings
- Forgetting to return value
- Expecting print inside function
def multiply(x, y):
product = x * y
print(product)Solution
Step 1: Identify the problem
The function prints the product but does not return it, so no value is sent back.Step 2: Fix by returning the product
Addingreturn productsends the result back to the caller.Final Answer:
Addreturn productto send the result back. -> Option DQuick Check:
Return value to send result back [OK]
- Returning print() instead of value
- Not returning any value
- Returning wrong variables
None if all are empty. Which function correctly does this?Solution
Step 1: Understand the goal
The function should return the first string that is not empty (truthy) or None if none found.Step 2: Analyze each option
The version withif not s: return sreturns the first falsy (empty) string;if s == ''returns the first exactly empty string;if s: return sreturns the first truthy (non-empty) string; the version withoutifreturns the first string regardless. Only the truthy check is correct.Final Answer:
def first_non_empty(strings): for s in strings: if s: return s return None -> Option AQuick Check:
Return first truthy string or None [OK]
- Returning empty strings instead of non-empty
- Returning on first loop without condition
- Confusing truthy and falsy values
