Variables in Python

Understand how variables work in Python with clear examples. Learn naming rules, variable assignment, dynamic typing, and best practices for writing readable Python code.

Loading...
Variables in Python

What Is a Variable?

A variable is simply a name that stores a value.

For example:

name = "John" 
age = 25

Here:

  • name stores "John"
  • age stores 25

You can then use these variables in your code:

print(name) 
print(age)
 
# Output:
# John 
# 25

Notice you didn't have to declare what type of value name or age would hold. Python figures that out automatically. This is what "dynamically typed" meant.

You can also use variables in calculations:

age = 25
next_year = age + 1
 
print(next_year)
 
# Output:
# 26

Changing a Variable

You can change the value stored in a variable:

name = "John"
print(name)
 
name = "Alice"
print(name)
 
# Output:
 
# John
# Alice

The variable name now stores "Alice" instead of "John".

Variables Can Store Different Values

A variable can store text, numbers, lists, and many other types of values.

name = "John" 
age = 25 
numbers = [1, 2, 3]

Variable Naming Rules

Python has a few rules you need to follow.

1. A variable name must start with a letter or _

These are valid:

name = "John"
_name = "Alice"

But this isn’t:

1name = "John"

A variable name cannot start with a number.

2. You can use letters, numbers, and underscores

For example:

name1 = "John"
user_age = 25
age_30 = 30

But remember: the name cannot start with a number.

3. Variable names are case-sensitive

This means name and Name are treated as two different variables.

name = "John"
Name = "Alice"
 
print(name)
print(Name)
 
# Output:
# John
# Alice

4. You can’t use Python keywords

Python has some words that already have a special meaning.

For example:

# if
# else
# for
# while
# and
# or
# not
# True
# False
# None

You can’t use these as variable names.

if = 10

This will give you an error.

How to Check the Full Keyword List

You don’t need to memorize all the Python keywords.

Python has a keyword module that you can use to see the complete list:

import keyword
print(keyword.kwlist)

This will show all the keywords in Python.

One More Thing: Built-in Names

Some names, such as list, str, print, sum, and input, are not keywords. They are built-in functions or types that Python already provides.

Python won’t stop you from using them as variable names, but it’s better to avoid doing so.

For example:

list = [1, 2, 3]
 
print(list("hello"))

This gives an error because you’ve replaced Python’s built-in list() function with a list.

So as a general rule, avoid using names like list, str, print, sum, and input for your variables.

You don’t need to memorize all of these either. As you learn Python, you’ll become familiar with the commonly used built-in names.

Use Meaningful Names

Try to give your variables names that tell you what they contain.

Instead of:

a = 25

use:

age = 25

Instead of:

fn = "John"

use:

first_name = "John"

For variable names with multiple words, Python commonly uses snake_case:

user_age = 25
first_name = "John"

This makes your code easier to read.

What Types of Values Variables Can Store

Variables can store different kinds of values.

For example:

name = "John"
age = 25
numbers = [1, 2, 3]

These variables contain different types of values:

  • "John" → string
  • 25 → number
  • [1, 2, 3] → list

Python figures out the type automatically.

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

name = "John"
age = 25
 
print(type(name))
print(type(age))
 
# Output:
 
# <class 'str'>
# <class 'int'>

A Variable Can Change its Type

Python also allows you to assign a different type of value to the same variable.

For example:

x = 10
print(x)
 
x = "Hello"
print(x)
 
# Output:
 
# 10
# Hello

x first stored a number and later stored text.

This is one of the things that makes Python flexible.

In languages like Java or C++, you usually have to specify what type of value a variable will store. You can’t simply change a variable from a number to text.

Python is more flexible. A variable can store a number first and later store text without any problem.

That’s because Python figures out the type of the value when the code runs.

Multiple Variable Assignment

Now, let's see how we can assign values to multiple variables in one line.

Instead of writing:

x = 10
y = "Hello"
z = [1, 2, 3]

You can write:

x, y, z = 10, "Hello", [1, 2, 3]

Now:

print(x)
print(y)
print(z)
 
# Output:
 
# 10
# Hello
# [1, 2, 3]

Each value is assigned to the variable in the same position.

Note: The number of variables and values should match. Otherwise, Python will give you an error.

For example:

x, y = 10, "Hello", [1, 2, 3]
 
# Output:
# ValueError: too many values to unpack

Here, we have 2 variables (x and y) but 3 values. Python doesn't know where to put the third value, so it gives an error.

You can also assign the same value to multiple variables

x = y = z = 0

Now all three variables contain 0.

A Useful Python Trick

You can also swap two variables without creating a temporary variable:

a = 10
b = 20
 
print(a)
print(b)
 
# Output:
 
# 10
# 20
 
a, b = b, a
 
print(a)
print(b)
 
# Output:
 
# 20
# 10

Python makes this really simple.


👉 Next tutorial: Data Types in Python