Tutorials / Python Basics / Lesson 3

Variables and Assignment

What You Will Learn

A variable is a name that stores a value. Instead of writing the same value over and over, you store it once and use the name everywhere.


Creating a Variable

Use = to assign a value to a variable:

name = "Alice"
age = 25
height = 1.68

Now you can use those names anywhere in your program:

name = "Alice"
age = 25

print(name)
print(age)

Expected output:

Alice
25

Using Variables in print()

You can print multiple things on one line by separating them with commas:

name = "Alice"
age = 25

print("My name is", name)
print("I am", age, "years old")

Expected output:

My name is Alice
I am 25 years old

Python automatically adds a space between items separated by commas.


Changing a Variable

Variables can change. That is why they are called variables:

score = 0
print(score)

score = 10
print(score)

score = score + 5
print(score)

Expected output:

0
10
15

score = score + 5 means: take the current value of score, add 5, and store the result back into score.


Variable Naming Rules

Variable names can contain letters, numbers, and underscores. They cannot start with a number.

# Valid names
first_name = "Alice"
age2 = 25
total_score = 100

# Invalid names (these will cause errors)
# 2fast = True       ← cannot start with a number
# my-name = "Alice"  ← hyphens are not allowed
# my name = "Alice"  ← spaces are not allowed

Python variable names are case-sensitive. name, Name, and NAME are three different variables.


Descriptive Names

Always choose names that describe what the variable stores:

# Hard to understand
x = 25
y = "Alice"
z = 1000

# Easy to understand
age = 25
name = "Alice"
monthly_salary = 1000

Good variable names make your code readable without needing extra comments.


What You Learned

  • Variables store values under a name
  • Use = to assign a value
  • Variables can be reassigned at any time
  • Names must follow Python’s naming rules
  • Descriptive names make code easier to read

In the next lesson, you will learn about the different data types Python uses to represent numbers, text, and other kinds of information.