How to Assign Values to Variables in Python

In Python, you can assign values to variables in several ways — from simple single assignments to assigning multiple values simultaneously.
Assigning a single value to a single variable
The most basic form assigns one value to one variable using the =
operator.
# Assigning single values
name = "Alice"
age = 25
height = 5.7
name
stores the string"Alice"
age
stores the integer25
height
stores the float5.7
- The
=
operator tells Python to store the right-side value in the left-side variable
Same Value to Multiple Variables
You can assign one value to multiple variables in a single line:
x = y = z = 0
print(x, y, z) # Output: 0 0 0
All three variables x
, y
, and z
reference the same value 0
.
This is perfect for initializing multiple variables with the same starting value before updating them later in your program.
Multiple Values to Multiple Variables
In Python, you can assign several values to several variables in one line:
a, b, c = 10, 20, 30
print(a, b, c) # Output: 10 20 30
a
stores10
b
stores20
c
stores30
Python automatically matches values to variables in the same order they appear on both sides of the =
sign.
This technique is incredibly handy for initializing multiple variables quickly and cleanly in your programs, especially when working with related values.
Hope this tutorial has been helpful.
To continue building your skills, your next step is to learn about How to Assign Values from Collections in Python.