Part 3 · Variables and types — chapter 1 of 6 · chapter 11 of 50 · 7 min read

Python Variables Explained for Beginners

Variables are one of the first things you learn in Python.

A variable is a name that points to a value. You use variables to store data, reuse it later, and make your programs easier to read.

In Python, you create a variable by assigning a value with the = sign. You do not need a special keyword like var or let.

Let’s walk through how to create variables, name them well, and avoid the mistakes beginners run into most.

Quick example #

name = "Alice"
age = 25

print(name)
print(age)

Use the = sign to assign a value to a variable. Then use the variable name later in your code.

name"Alice"
The name name points to the value "Alice".

What a variable is #

A variable is a name that refers to a value.

This lets you:

  • store data
  • use it later
  • update it when needed

For example, a variable might store:

  • a person’s name
  • a number
  • a price
  • a value entered by the user

In Python, a variable is created when you assign a value to it with =.

message = "Hello"
score = 10
price = 4.99

Here:

  • message refers to "Hello"
  • score refers to 10
  • price refers to 4.99

If you want a shorter definition, see what is a variable in Python.

How to create a variable in Python #

To create a variable:

  • put the variable name on the left
  • put = in the middle
  • put the value on the right

Example:

score = 10

This means: “store the value 10 in the variable named score.”

Python does not require a declaration first. This is valid:

city = "Paris"

You do not need to write anything before it.

Here is another simple example:

user_name = "Maya"
item_count = 3

print(user_name)
print(item_count)

Expected output:

Maya
3

If you are also learning the basic rules of writing Python code, see Python syntax basics explained.

How variables are used #

Variables are useful because they let you reuse values.

You can use them to:

  • print stored values
  • use the same value in multiple places
  • update values
  • do calculations
  • check conditions
language = "Python"
print(language)

Expected output:

Python

You can learn more about this on the Python print() function explained.

Reuse a value #

name = "Ava"

print("Hello,", name)
print("Your name is", name)

Expected output:

Hello, Ava
Your name is Ava

Update a variable #

count = 1
print(count)

count = 2
print(count)

Expected output:

1
2

The second assignment replaces the first value.

Use variables in calculations #

price = 10
tax = 2
total = price + tax

print(total)

Expected output:

12

Variable naming rules #

Python has a few rules for variable names.

Allowed #

Variable names can contain:

  • letters
  • numbers
  • underscores (_)

Examples:

name = "Sam"
score1 = 99
user_name = "sam123"

Not allowed #

Names cannot start with a number #

This is invalid:

# 2score = 10

A valid version would be:

score2 = 10

Names cannot contain spaces #

This is invalid:

# first name = "Lena"

A valid version would be:

first_name = "Lena"

Names are case-sensitive #

age and Age are different variables.

age = 20
Age = 30

print(age)
print(Age)

Expected output:

20
30

Do not use Python keywords #

Words like if, for, and class have special meaning in Python, so you cannot use them as variable names.

Invalid example:

# class = "Math"

Good variable names #

Choose names that clearly describe the value.

Good examples:

  • user_name
  • total_price
  • item_count

These are easier to understand than unclear names like:

  • x
  • a
  • data

Short names are not always wrong, but beginners should usually prefer readable names.

Compare these:

x = 19.99
y = 2
z = x * y
print(z)

This works, but it is not very clear.

A better version:

item_price = 19.99
quantity = 2
total_price = item_price * quantity

print(total_price)

Expected output:

39.98

Good names make your code easier to read and easier to debug.

Variables can change #

Variables are called “variables” because their values can change.

You can assign a new value to the same variable at any time.

count = 1
print(count)

count = 2
print(count)

Expected output:

1
2

The old value is replaced by the new one.

This is useful in programs that track changing data, such as:

  • a score
  • a total
  • a user’s answer
  • the current step in a process

Variables and data types #

A variable can refer to different kinds of data.

Some common Python data types are:

  • str for text
  • int for whole numbers
  • float for numbers with decimals
  • bool for True or False

Examples:

city = "Paris"      # str
age = 20            # int
price = 9.99        # float
is_ready = True     # bool

You can check the type of a value with type():

city = "Paris"
age = 20

print(type(city))
print(type(age))

Expected output:

<class 'str'>
<class 'int'>

If you want to learn these in more detail, see Python data types overview and type conversion in Python.

Simple beginner examples #

Here are a few short examples that show common ways to use variables.

Store text in a variable #

greeting = "Hello, world!"
print(greeting)

Expected output:

Hello, world!

Store numbers and add them #

a = 5
b = 7
total = a + b

print(total)

Expected output:

12

Use variables with print() #

name = "Noah"
score = 95

print("Student:", name)
print("Score:", score)

Expected output:

Student: Noah
Score: 95

Store user input in a variable #

name = input("Enter your name: ")
print("Hello,", name)

The input() function returns text. That means if a user types 25, Python stores it as a string unless you convert it.

You can learn more on the Python input() function explained.

Common beginner mistakes #

These are some of the most common variable mistakes in Python.

Using spaces in variable names #

This is invalid:

# first name = "Ali"

Use an underscore instead:

first_name = "Ali"

Starting a variable name with a number #

This is invalid:

# 2score = 50

Use this instead:

score2 = 50

Confusing = with == #

  • = assigns a value
  • == compares two values

Assignment:

age = 20

Comparison:

print(age == 20)

Expected output:

True

Using a variable before assigning a value #

This causes an error:

print(score)

If score was never created, Python will raise a NameError.

Fix it by assigning a value first:

score = 10
print(score)

If you see this error often, read NameError: name is not defined fix.

Writing a keyword as a variable name #

This is invalid:

# if = 5

Choose another name:

if_count = 5

✍️ Try it yourself

Create two variables, item_price set to 12.5 and quantity set to 4. Store their product in a third variable named total_price, then print it.

Show answer
item_price = 12.5
quantity = 4
total_price = item_price * quantity

print(total_price)
# Output:
# 50.0

What to learn next #

Once you understand variables, the next step is learning what kinds of values they can store and how those values behave.

Good next topics are:

FAQ #

Do I need to declare variables in Python first? #

No. In Python, a variable is created when you assign a value to it.

Can a variable change to a different type? #

Yes. Python lets you assign a new value of a different type to the same variable.

value = 10
print(type(value))

value = "ten"
print(type(value))

Expected output:

<class 'int'>
<class 'str'>

What is the difference between = and == in Python? #

= assigns a value to a variable.

== compares two values to check if they are equal.

x = 5
print(x == 5)

Expected output:

True

Why does Python say a name is not defined? #

Usually because:

  • the variable was used before it was assigned
  • the name was misspelled
  • the capitalization was different, such as age vs Age

Useful debugging checks:

print(my_variable)
print(type(my_variable))
dir()
help(type)

See also #

Press Esc to close