Assigning Values to Variables in Python

Assigning Values to Variables in Python

9. Assigning Values to Variables

In Python, you can assign values to variables in different ways — from simple single assignments to assigning multiple values at once.

Let’s explore the most common patterns 👇

1️⃣ Assigning a single value to a single variable.

The most basic form is assigning one value to one variable using the = operator.

# Assigning a single value
name = "Alice"
age = 25
height = 5.7

Explanation:

  • name stores the string “Alice”
  • age stores the integer 25
  • height stores the float 5.7
  • The = operator tells Python to store the value on the right inside the variable on the left

2️⃣. Assigning the Same Value to Multiple Variables.

You can assign the one value to several variables in a single line:

x = y = z = 0

print(x, y, z)  # Output: 0 0 0

Here, all three variables - x, y, and z store the same value 0.

💡 Tip: This approach is handy for initializing multiple variables before updating them later in your program.

3️⃣. Assigning Multiple Values to Multiple Variables.

Python also allows multiple assignments — assigning several values to several variables in one line:

a, b, c = 10, 20, 30

print(a, b, c)  # Output: 10 20 30

Here:

  • a stores 10
  • b stores 20
  • c stores 30

Python automatically matches values to variables in the same order they appear on both sides of the = sign.

Tip : This can be very handy when you want to initialize multiple variables quickly in your programs.

You NOW understand Python data types, your next step is to learn about Updating Variable Values in Python in detail.