SythraOpen app

Variables in Python

A variable in Python is a name that refers to a value. You create one with assignment (=), reuse it in expressions, and update it with patterns like score = score + 5 or score += 5.

Sythra

10 min read

XLinkedIn
Variables in Python — cover illustration

Want to understand variables in Python? A variable is a name that refers to a value — a label you can read, update, and pass around so your program remembers things instead of repeating hardcoded numbers and strings.

This page covers both the idea and the day-to-day craft: assignment with =, good names, updates like score = score + 5, shorthand operators, multiple assignment, light scope, and constants. If values and types still feel fuzzy, start with Types, Values, and Errors in Python.

What you will learn

By the end you can:

  • Explain what a variable is (a name pointing at a value)
  • Create variables with assignment and use them in expressions
  • Update values with the score = score + 5 pattern and +=
  • Choose legal, readable names (snake_case, avoid keywords)
  • Assign several names at once and swap with x, y = y, x
  • Recognize light scope ideas and ALL_CAPS “constants”

We are not covering functions in depth, lists/dicts, or advanced memory models. Scope appears only as a beginner map so the word does not surprise you later.

What is a variable?

Imagine keeping score in a game without writing anything down. Every point lives only in your head — until you forget. In programming, a variable is the named place where you write something down so you can use it, change it, and refer to it later.

Without variables, a program can only work with fixed, hardcoded values. With variables, it can remember things, track changes, and respond to different situations. Variables are the memory of your program.

In Python, a variable is simply a name that refers to a value:

  • The value exists somewhere in memory
  • The variable is a label pointing to it
name = "Shantanu"
age = 20
height = 5.9
is_student = True

You are not inventing new kinds of data here — you are assigning names to values. Python quietly records that name refers to a string, age to an integer, height to a float, and is_student to a boolean.

Assignment — creating a variable

Creating a variable is called assignment. You tell Python: store this value and remember it under this name.

The = symbol looks like maths “equals,” but in Python it means something different. It is the assignment operator: take the value on the right and attach the name on the left.

x = 5
# means: take 5 and store it under the name x

A useful picture is an arrow from the name to the value:

# Mental model (not runnable Python):
# name  →  "Shantanu"
# age   →  20

Programmers often draw this as a state diagram — name, arrow, value. If the value changes, the arrow moves. The name is still the same label; it just points somewhere new.

You never declare types up front. Python figures out the type from the value. That is called dynamic typing:

name = "Shantanu"     # str
age = 20             # int
height = 5.9         # float
is_student = True    # bool

A variable does not “own” a type forever. It inherits the type of whatever value it currently refers to. You can always check with type():

message = "hello all"
print(type(message))
# <class 'str'>

Using a variable

Once a variable exists, use it by writing its name. Python substitutes the stored value:

name = "Shantanu"
age = 20

print(name)
print(age)
# Shantanu
# 20

Variables can join expressions and calculations:

print(age + 5)
# 25

And they can drop into text with an f-string:

print(f"Hello, {name}!")
# Hello, Shantanu!

Instead of rewriting the raw value everywhere, you reuse the name — and when the value changes, every use of that name sees the update.

Updating a variable

Variables are not permanent. Reassign whenever you need a new value:

score = 0
print(score)  # 0

score = 10
print(score)  # 10

The old value is replaced. One of the most common patterns in programming is the update:

score = score + 5

In maths that line looks nonsense. In Python, = still means assignment. Python reads it as: look up the current score, add 5, store the result back under score.

score = 10
score = score + 5
print(score)
# 15

Shorthand updates

Because updates happen constantly, Python offers shorter forms that mean the same thing:

score += 5   # score = score + 5
score -= 3   # score = score - 3
score *= 2   # score = score * 2
score /= 2   # score = score / 2

Choosing good variable names

As programs grow, names become documentation. Good names explain why the value exists — not just that a number is sitting there.

Python’s legal-name rules:

  • Must start with a letter (a–z, A–Z) or an underscore _
  • Can contain letters, digits (0–9), and underscores after that
  • Cannot contain spaces or symbols like -, @, !, $
  • Cannot be a Python keyword (class, if, for, …)
  • Names are case-sensitive: age, Age, and AGE are different

Spaces are not allowed, so underscores become Python’s way of spacing words:

your_name = "Hellen"

When names break the rules

Illegal names stop you immediately with a syntax error:

# 76yourname = "helen"   # SyntaxError — cannot start with a digit
# @yourname = "helen"    # SyntaxError — @ is not allowed
# class = "hellen"       # SyntaxError — class is a keyword

Keywords feel unfair at first. The problem is ownership: words like class, def, return, and if are part of the language’s structure. Python will not let you reuse them as variable names.

Naming conventions — the Python way

A name can be legal and still hard to read. Conventions are shared habits so code looks familiar.

snake_case for regular variables

user_name = "Alice"
total_price = 99.99
number_of_students = 30

Lowercase words separated by underscores — snake_case — is the default style for ordinary variables in Python.

UPPER_SNAKE_CASE for constants

Values meant to stay fixed are usually written in capitals:

MAX_SCORE = 100
GST_RATE = 0.18
PI = 3.14159

Python does not enforce “true” constants. Nothing stops PI = 10. ALL_CAPS is a promise to other programmers: treat this as unchanged. Good programmers keep that promise.

Leading underscore

_internal_counter = 0

A leading underscore signals “internal use.” Python does not enforce it; people reading your code do.

Conventions are not syntax rules. Your program can run without them. Following them makes the code easier for others — and for future you.

Multiple assignment and swapping

Sometimes you want several names at once. Python makes that easy.

Same value for several names

x = y = z = 0
print(x, y, z)
# 0 0 0

Different values in one line

a, b, c = 1, 2, 3
# a → 1, b → 2, c → 3

Swapping without a temp variable

One of Python’s elegant tricks:

x = 10
y = 20
x, y = y, x
print(x, y)
# 20 10

Many languages need a temporary third variable to swap. Python evaluates the right-hand side first, then assigns left to right — so the exchange is one line.

Variable scope — where a name exists

Think of a house with rooms. Something in the living room is visible to everyone. Something in a bedroom is only visible inside that room. Variables work similarly — that idea is called scope.

A name created at the top level of a file (outside any function) is usually a global variable — available broadly in that module:

greeting = "Hello"  # global for this file

A name created inside a function is local. It exists only while that function runs, then disappears. You will use this constantly once functions arrive; for now, remember: not every name is visible everywhere.

You can modify a global from inside a function with the global keyword — but reaching for global often is a smell. Prefer passing values in and returning results out.

counter = 0

def increment():
    global counter
    counter += 1

Deleting a variable

When you no longer need a name, del removes it:

x = 10
del x
# print(x)  → NameError: name 'x' is not defined

Day to day you rarely need del. It matters more when you want to drop a large structure from memory after you are done with it.

Common mistakes

  • Treating = as “equals” instead of assignment
  • Using a name before assigning it (NameError)
  • Illegal names: starting with a digit, spaces, or symbols
  • Shadowing keywords (class, list as a name — confusing even when legal)
  • Writing score + 5 and expecting score to change (you must assign back)
  • Relying on global everywhere instead of clear function inputs/outputs

Why variables matter

Variables let you store information, reuse values, update data over time, and organize programs that would otherwise drown in repeated literals. Once information has a name, a program can remember it, change it, and build something meaningful with it.

Next, practice reading and writing assignments until the update pattern feels natural — then move on to operators and decisions that use those named values.

Common questions

What is a variable in Python?

A variable is a name that refers to a value. The value lives in memory; the variable is the label you use to read or update it.

What does = mean in Python?

In Python, = is the assignment operator. It stores the value on the right under the name on the left. It does not ask whether two sides are equal — that is ==.

How do you update a variable in Python?

Assign a new value to the same name. The common pattern is score = score + 5, which reads the old value, adds 5, and stores the result. Shorthand forms like score += 5 do the same thing.

What are the rules for variable names in Python?

Names must start with a letter or underscore, then may include letters, digits, and underscores. No spaces or special symbols. They cannot be Python keywords, and they are case-sensitive.

What is snake_case in Python?

snake_case is the usual style for ordinary variables: lowercase words separated by underscores, like total_price or number_of_students.

Does Python have constants?

Python has no enforced constants. Writing names in ALL_CAPS (like MAX_SCORE) is a convention that tells other programmers the value should not change.

How do you swap two variables in Python?

Use multiple assignment: x, y = y, x. Python evaluates the right-hand side first, so you do not need a temporary third variable.

Explore

Related topics

Keep going — these sit next to this concept in a real learning path.

Browse all python explainers →