Bird
Raised Fist0
Pythonprogramming~10 mins

Variable assignment in Python - Step-by-Step Execution

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
Concept Flow - Variable assignment in Python
Start
Evaluate right side
Assign value to variable
Variable holds value
End
The program evaluates the value on the right side, then assigns it to the variable on the left side, storing it for later use.
Execution Sample
Python
x = 5
name = "Alice"
pi = 3.14
Assigns values 5, "Alice", and 3.14 to variables x, name, and pi respectively.
Execution Table
StepCode LineActionVariableValue
1x = 5Assign 5 to xx5
2name = "Alice"Assign "Alice" to namename"Alice"
3pi = 3.14Assign 3.14 to pipi3.14
4-No more assignments--
💡 All assignments done, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
xundefined555
nameundefinedundefined"Alice""Alice"
piundefinedundefinedundefined3.14
Key Moments - 3 Insights
Why does the variable 'x' have the value 5 after step 1?
Because at step 1, the code 'x = 5' assigns the value 5 to variable x as shown in the execution_table row 1.
What happens if we assign a new value to 'x' later?
The variable 'x' will update to hold the new value, replacing the old one. Variable assignment always overwrites the previous value.
Is the value on the right side evaluated before or after assignment?
It is evaluated before assignment. The flow shows evaluation first, then assignment, ensuring the variable gets the correct value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what value does 'name' hold?
Aundefined
B5
C"Alice"
D3.14
💡 Hint
Check the 'Value' column for 'name' at step 2 in the execution_table.
At which step does variable 'pi' get its value assigned?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Code Line' column in execution_table where 'pi = 3.14' appears.
If we add a line 'x = 10' after step 3, what will be the value of 'x' after that?
A5
B10
C"Alice"
Dundefined
💡 Hint
Variable assignment overwrites previous values as shown in key_moments explanation.
Concept Snapshot
Variable assignment syntax:
variable = value
The right side is evaluated first.
The variable stores the value for later use.
Assigning again updates the variable.
Variables hold values of any type.
Full Transcript
Variable assignment in Python means giving a name to a value so you can use it later. The program reads the value on the right side of the equals sign, then stores it in the variable on the left side. For example, 'x = 5' stores the number 5 in the variable x. Variables can hold numbers, text, or other data types. If you assign a new value to the same variable, it replaces the old one. This process is simple but very important for programming.

Practice

(1/5)
1. What does the following Python statement do?
age = 25
easy
A. Prints the value 25
B. Checks if age is equal to 25
C. Stores the value 25 in the variable named age
D. Deletes the variable age

Solution

  1. Step 1: Understand the assignment operator

    The = sign in Python assigns the value on the right to the variable on the left.
  2. Step 2: Analyze the statement age = 25

    This means the number 25 is stored inside the variable named age.
  3. Final Answer:

    Stores the value 25 in the variable named age -> Option C
  4. Quick Check:

    Assignment means storing value [OK]
Hint: Remember: '=' means store value, not compare [OK]
Common Mistakes:
  • Confusing '=' with '==' for comparison
  • Thinking '=' prints or deletes variables
  • Assuming variable names are values
2. Which of the following is the correct way to assign the value 10 to a variable named count in Python?
easy
A. count == 10
B. count := 10
C. 10 = count
D. count = 10

Solution

  1. Step 1: Identify the assignment operator

    In Python, the single equals sign = is used to assign values to variables.
  2. 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 to count.
  3. Final Answer:

    count = 10 -> Option D
  4. Quick Check:

    Use '=' for assignment [OK]
Hint: Use single '=' to assign values, not '==' [OK]
Common Mistakes:
  • Using '==' instead of '=' for assignment
  • Trying to assign value to a number
  • Confusing walrus operator with assignment
3. What will be the output of this code?
name = "Alice"
age = 30
name = "Bob"
print(name, age)
medium
A. Alice 30
B. Bob 30
C. Alice 0
D. Bob 0

Solution

  1. Step 1: Trace variable assignments

    Initially, name is set to "Alice" and age to 30. Then name is reassigned to "Bob".
  2. Step 2: Understand print output

    The print statement outputs the current values of name and age. Since name was changed to "Bob", and age remains 30, the output is "Bob 30".
  3. Final Answer:

    Bob 30 -> Option B
  4. Quick Check:

    Last assignment wins [OK]
Hint: Last value assigned to variable is what prints [OK]
Common Mistakes:
  • Thinking first assignment stays unchanged
  • Confusing variable names or values
  • Assuming age changes without assignment
4. Identify the error in this code snippet:
5number = 10
print(5number)
medium
A. Variable names cannot start with a number
B. Missing quotes around 5number
C. Assignment operator is wrong
D. print statement syntax error

Solution

  1. Step 1: Check variable naming rules

    In Python, variable names must start with a letter or underscore, not a number.
  2. Step 2: Analyze the code

    The variable name 5number starts with a digit, which is invalid syntax and causes an error.
  3. Final Answer:

    Variable names cannot start with a number -> Option A
  4. Quick Check:

    Variable names start with letter or underscore [OK]
Hint: Variable names cannot begin with digits [OK]
Common Mistakes:
  • Starting variable names with numbers
  • Confusing strings with variable names
  • Assuming print syntax is wrong
5. You want to swap the values of two variables a and b in Python. Which of the following is the correct way to do it?
hard
A. a, b = b, a
B. a = b b = a
C. temp = a b = temp a = b
D. a = a + b b = a - b a = a - b

Solution

  1. Step 1: Understand swapping variables

    Swapping means exchanging values so that a gets b's value and b gets a's value.
  2. Step 2: Analyze each option

    a = b b = a assigns b to a, then a to b, losing original a. temp = a b = temp a = b incorrectly assigns b before a. 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.
  3. Final Answer:

    a, b = b, a -> Option A
  4. Quick Check:

    Use tuple unpacking for swap [OK]
Hint: Swap with a, b = b, a for clean code [OK]
Common Mistakes:
  • Overwriting variables before swapping
  • Using temporary variable incorrectly
  • Using arithmetic swap with non-numbers