How variable type changes at runtime in Python - Performance & Efficiency
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how the time it takes to run code changes when a variable changes its type during the program.
How does this affect the speed as the program runs?
Analyze the time complexity of the following code snippet.
x = 10
x = "hello"
x = [1, 2, 3]
for item in x:
print(item)
This code changes the variable x from a number to a string, then to a list, and finally loops over the list to print each item.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the list
xto print each item. - How many times: Once for each item in the list (3 times here).
As the list x gets bigger, the loop runs more times, once per item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The time grows directly with the number of items in the list.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line with the number of items in the list.
[X] Wrong: "Changing the variable type multiple times makes the program slower in a way that depends on the number of changes."
[OK] Correct: Changing the variable type itself is a simple assignment and happens once each time; it does not repeat or grow with input size, so it does not affect the overall time complexity significantly.
Understanding how variable types can change and how loops behave helps you explain how your code runs as data grows, a key skill in many programming tasks.
"What if the variable x was changed to a dictionary instead of a list? How would the time complexity change when looping over it?"
Practice
Solution
Step 1: Understand Python variable typing
Python variables are dynamically typed, meaning their type depends on the current value assigned.Step 2: Assigning a new value changes the type
When you assign a new value of a different type, the variable's type updates to match the new value automatically.Final Answer:
The variable's type changes to the new value's type automatically. -> Option AQuick Check:
Python variables are dynamic = A [OK]
- Thinking variables have fixed types like in some other languages.
- Assuming Python throws an error on type change.
- Believing variable types combine old and new types.
Solution
Step 1: Identify Python assignment syntax
Python uses the single equals sign (=) to assign values to variables.Step 2: Check each option
x = 10 uses correct syntax. x := 10 uses walrus operator which is for expressions, not simple assignment. int x = 10 and var x = 10 are invalid in Python.Final Answer:
x = 10 -> Option AQuick Check:
Assignment uses = in Python = A [OK]
- Confusing ':=' walrus operator with assignment.
- Using typed declarations like other languages.
- Trying to declare variable types explicitly.
var = 5 print(type(var)) var = 'hello' print(type(var))
Solution
Step 1: Check initial assignment and type
var is first assigned 5, an integer, so type(var) is <class 'int'>.Step 2: Reassign and check new type
var is then assigned 'hello', a string, so type(var) is <class 'str'>.Final Answer:
<class 'int'>\n<class 'str'> -> Option CQuick Check:
Type changes with value = C [OK]
- Assuming type stays the same after reassignment.
- Confusing output order of print statements.
- Expecting a runtime error on type change.
value = 10 value = value + '5' print(value)
Solution
Step 1: Analyze the operation between int and str
value is an int (10), then tries to add a string ('5'), which is not allowed.Step 2: Identify the error type
Python raises a TypeError when adding incompatible types like int and str.Final Answer:
TypeError because int and str cannot be added -> Option DQuick Check:
Adding int + str causes TypeError = D [OK]
- Thinking Python auto-converts types in addition.
- Expecting output '15' as string concatenation.
- Confusing SyntaxError with TypeError.
data? data = 100
if isinstance(data, int):
data = str(data)
else:
data = [data]
Solution
Step 1: Check initial type of data
data starts as 100, which is an int.Step 2: Evaluate the if condition
Since data is int, the if block runs, converting data to str('100').Final Answer:
str -> Option BQuick Check:
int converted to str by condition = B [OK]
- Ignoring the if condition and assuming list.
- Thinking data remains int after reassignment.
- Confusing str and bool types.
