Key Concepts in Variable Assignment
In programming, assignment
refers to the process of storing a specific value in a declared variable.
For example, x = 5
means that the value 5
is stored in the variable x
.
This act of assigning a value at the time of declaration is often referred to as initialization
, a concept introduced in the previous lesson.
Important Notice
In programming, the =
symbol does not mean "equals" as in mathematics, but rather means assign the value on the right to the variable on the left.
number = 10 message = "Hello, Python!"
In the code above, number = 10
means assigning the number 10
to the variable number
.
Similarly, message = "Hello, Python!"
assigns the string "Hello, Python!"
to the variable message
.
In programming, if you want to compare values rather than assign them, use two equal signs ==
to represent equality.
if number == 10: print("number is 10.")
Variable Reassignment
In Python, you can assign a new value to a variable that already holds data.
When this happens, the old value is overwritten by the new one.
number = 10 # Assign the value 10 to the variable number number = 15 # Reassign the new value 15 to number, which was initially 10
Multiple Assignment
Python allows you to assign values to multiple variables at once using a single line of code.
x, y, z = 5, 10, 15 # Assign 5 to x, 10 to y, and 15 to z print(x) # Outputs 5 print(y) # Outputs 10 print(z) # Outputs 15
Coding Practice
Try reassigning the value of the variable student_name
.
What does the equals sign (=) mean when assigning values to a variable?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result