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.
Jump into concepts and practice - no test required
x = 5 name = "Alice" pi = 3.14
| Step | Code Line | Action | Variable | Value |
|---|---|---|---|---|
| 1 | x = 5 | Assign 5 to x | x | 5 |
| 2 | name = "Alice" | Assign "Alice" to name | name | "Alice" |
| 3 | pi = 3.14 | Assign 3.14 to pi | pi | 3.14 |
| 4 | - | No more assignments | - | - |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 |
|---|---|---|---|---|
| x | undefined | 5 | 5 | 5 |
| name | undefined | undefined | "Alice" | "Alice" |
| pi | undefined | undefined | undefined | 3.14 |
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.
age = 25= sign in Python assigns the value on the right to the variable on the left.age = 25age.count in Python?= is used to assign values to variables.== 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.name = "Alice" age = 30 name = "Bob" print(name, age)
name is set to "Alice" and age to 30. Then name is reassigned to "Bob".name and age. Since name was changed to "Bob", and age remains 30, the output is "Bob 30".5number = 10 print(5number)
5number starts with a digit, which is invalid syntax and causes an error.a and b in Python. Which of the following is the correct way to do it?a gets b's value and b gets a's value.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.