Variable assignment in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how fast variable assignment happens in Python.
We want to know how the time to assign a value changes as we assign more variables.
Analyze the time complexity of the following code snippet.
for i in range(n):
x = i
This code assigns the value of i to x repeatedly in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Variable assignment
x = iinside the loop. - How many times: Exactly
ntimes, once per loop cycle.
Each time we increase n, the number of assignments grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The number of assignments grows directly with n.
Time Complexity: O(n)
This means the time to finish grows in a straight line as you assign more variables.
[X] Wrong: "Variable assignment is always instant and does not depend on how many times it runs."
[OK] Correct: While one assignment is very fast, doing it many times adds up, so total time grows with the number of assignments.
Understanding how simple steps add up helps you explain code speed clearly and confidently.
"What if we assigned a list of size n to x inside the loop? How would the time complexity change?"
Practice
age = 25Solution
Step 1: Understand the assignment operator
The=sign in Python assigns the value on the right to the variable on the left.Step 2: Analyze the statement
This means the number 25 is stored inside the variable namedage = 25age.Final Answer:
Stores the value 25 in the variable named age -> Option CQuick Check:
Assignment means storing value [OK]
- Confusing '=' with '==' for comparison
- Thinking '=' prints or deletes variables
- Assuming variable names are values
count in Python?Solution
Step 1: Identify the assignment operator
In Python, the single equals sign=is used to assign values to variables.Step 2: Check each option
count == 10 uses==which is a comparison, not assignment. 10 = count tries to assign to a value, which is invalid. count := 10 uses walrus operator:=which is valid in Python 3.8+, but not standard for simple assignment. count = 10 correctly assigns 10 tocount.Final Answer:
count = 10 -> Option DQuick Check:
Use '=' for assignment [OK]
- Using '==' instead of '=' for assignment
- Trying to assign value to a number
- Confusing walrus operator with assignment
name = "Alice" age = 30 name = "Bob" print(name, age)
Solution
Step 1: Trace variable assignments
Initially,nameis set to "Alice" andageto 30. Thennameis reassigned to "Bob".Step 2: Understand print output
The print statement outputs the current values ofnameandage. Sincenamewas changed to "Bob", andageremains 30, the output is "Bob 30".Final Answer:
Bob 30 -> Option BQuick Check:
Last assignment wins [OK]
- Thinking first assignment stays unchanged
- Confusing variable names or values
- Assuming age changes without assignment
5number = 10 print(5number)
Solution
Step 1: Check variable naming rules
In Python, variable names must start with a letter or underscore, not a number.Step 2: Analyze the code
The variable name5numberstarts with a digit, which is invalid syntax and causes an error.Final Answer:
Variable names cannot start with a number -> Option AQuick Check:
Variable names start with letter or underscore [OK]
- Starting variable names with numbers
- Confusing strings with variable names
- Assuming print syntax is wrong
a and b in Python. Which of the following is the correct way to do it?Solution
Step 1: Understand swapping variables
Swapping means exchanging values so thatagetsb's value andbgetsa's value.Step 2: Analyze each option
a = b b = a assignsbtoa, thenatob, losing originala. temp = a b = temp a = b incorrectly assignsbbeforea. a = a + b b = a - b a = a - b uses arithmetic swap which works but is less clear and can cause errors with non-numbers. a, b = b, a uses Python's tuple unpacking to swap values correctly and clearly.Final Answer:
a, b = b, a -> Option AQuick Check:
Use tuple unpacking for swap [OK]
- Overwriting variables before swapping
- Using temporary variable incorrectly
- Using arithmetic swap with non-numbers
