---
title: Variables in Python
source: https://app.sythra.ai/learn/python/variables
topic: Python
updated: 2026-08-12
publisher: Sythra (https://app.sythra.ai)
---

# 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.

_Source: [https://app.sythra.ai/learn/python/variables](https://app.sythra.ai/learn/python/variables) — free to read on Sythra._

## Key points

- A variable is a name pointing at a value — not the value itself
- = means assignment, not mathematical equality
- Types come from the value; check with type()
- Update with score = score + 5 or shorthand score += 5
- Use snake_case; ALL_CAPS signals a constant-by-convention
- Multiple assignment and x, y = y, x make common tasks short

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](/learn/python/types-values-errors).

## 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

```python
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.

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

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

```python
# 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**:

```python
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()`:

```python
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:

```python
name = "Shantanu"
age = 20

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

Variables can join expressions and calculations:

```python
print(age + 5)
# 25
```

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

```python
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:

```python
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**:

```python
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`.

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

### Shorthand updates

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

```python
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:

```python
your_name = "Hellen"
```

### When names break the rules

Illegal names stop you immediately with a syntax error:

```python
# 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.

> **Quick assignment rulebook:** 

## 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

```python
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:

```python
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

```python
_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

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

### Different values in one line

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

### Swapping without a temp variable

One of Python’s elegant tricks:

```python
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:

```python
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.

```python
counter = 0

def increment():
    global counter
    counter += 1
```

## Deleting a variable

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

```python
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.

> **Practice with Sythra:**  [Practice with AI tutor](https://app.sythra.ai/pricing)

## FAQ

### 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.

## Related

- [Types, Values, and Errors in Python](https://app.sythra.ai/learn/python/types-values-errors) — What values and types are — the pieces variables point to.
- [Programs, Interpreters & Compilers in Python](https://app.sythra.ai/learn/python/programs-interpreters-compilers) — How programs run once you start naming and storing values.
- [Logical Operators in Python](https://app.sythra.ai/learn/python/logical-operators) — and, or, not — combine conditions stored in variables.
- [Comments in Python](https://app.sythra.ai/learn/python/comments) — # notes for humans — useful comments vs noise, and docstrings.
- [Operators and Operands in Python](https://app.sythra.ai/learn/python/operators-operands) — Arithmetic, comparison, assignment, in, is — the operator map.
- [Statements vs Expressions in Python](https://app.sythra.ai/learn/python/statements-vs-expressions) — REPL vs script — expressions produce values, statements act.
- [Order of Operations in Python](https://app.sythra.ai/learn/python/order-of-operations) — PEMDAS, precedence, parentheses, and left-to-right ties.
- [Data Types in Python](https://app.sythra.ai/learn/python/data-types) — int, float, str, bool, list, tuple, dict, set — the full map.
- [Interactive Mode vs Script Mode in Python](https://app.sythra.ai/learn/python/interactive-mode-vs-script-mode) — Why the same line of code behaves differently in each mode.
- [The input() Function in Python](https://app.sythra.ai/learn/python/python-input-function) — Reading user input and converting it from a string to a number.
- [Type Casting in Python](https://app.sythra.ai/learn/python/type-casting) — Implicit vs explicit conversion between data types.
- [Functions in Python: Definition and How They Work](https://app.sythra.ai/learn/python/functions-in-python) — What functions are, def, parameters, and local scope.
- [Comparison Operators in Python](https://app.sythra.ai/learn/python) — ==, !=, <, > — compare named values before branching.
- [Python course hub](https://app.sythra.ai/learn/python) — All free Python explainers and the path into Agentic practice.
- [Prerequisites to Iteration in Python (Variable Updates)](https://app.sythra.ai/learn/python/prerequisites-to-iteration) — Reassignment and updates — what every loop depends on.

---

Written by Sythra — Learn machine learning by building. Practice this topic with Sythra's AI tutor: https://app.sythra.ai/pricing
