Important Points When Assigning Values to Variables
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
.
In the previous lesson, initialization
refers to the process of assigning values to a declared variable.
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
.
When using an equal sign to mean "equals," use two equal signs as in ==
.
if number == 10:
print("number is 10.")
Variable Reassignment
In Python, you can assign a new value to an already assigned variable.
In this case, the previous value is replaced with the reassigned value.
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
In Python, you can assign values to multiple variables simultaneously in one line.
x, y, z = 5, 10, 15
print(x) # 5
print(y) # 10
print(z) # 15
Coding Practice
Try reassigning the value of the variable student_name
.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.