How to assign a variable in Python

Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video

How can you assign a variable in Python?

An equals sign assigns in Python

In Python, the equal sign (=) assigns a variable to a value:

>>> count = 4
>>> count

This is called an assignment statement. We've pointed the variable count to the value 4.

We don't have declarations or initializations

Some programming languages have an idea of declaring variables.

Declaring a variable says a variable exists with this name, but it doesn't have a value yet. After you've declared the variable, you then have to initialize it with a value.

Python doesn't have the concept of declaring a variable or initializing variables. Other programming languages sometimes do, but we don't.

In Python, a variable either exists or it doesn't:

>>> name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined

If it doesn't exist, assigning to that variable will make it exist:

>>> name = "Trey"

Valid variable names in Python

Variables in Python can be made up of letters, numbers, and underscores:

>>> user_name1 = "trey"

The first character in a variable cannot be a number:

>>> 1user = "uhoh"
  File "<stdin>", line 1
    1user = "uhoh"
SyntaxError: invalid decimal literal

And a small handful of names are reserved, so they can't be used as variable names (as noted in SyntaxError: invalid syntax):

>>> class = "invalid"
  File "<stdin>", line 1
    class = "invalid"
SyntaxError: invalid syntax

Reassigning a variable in Python

What if you want to change the value of a variable?

The equal sign is used to assign a variable to a value, but it's also used to reassign a variable:

>>> amount = 6
>>> amount = 7
>>> amount

In Python, there's no distinction between assignment and reassignment.

Whenever you assign a variable in Python, if a variable with that name doesn't exist yet, Python makes a new variable with that name. But if a variable does exist with that name, Python points that variable to the value that we're assigning it to.

Variables don't have types in Python

Note that in Python, variables don't care about the type of an object.

Our amount variable currently points to an integer:

>>> amount = 7
>>> amount

But there's nothing stopping us from pointing it to a string instead:

>>> amount = "hello"
>>> amount

Variables in Python don't have types associated with them. Objects have types but variables don't. You can point a variable to any object that you'd like.

Type annotations are really type hints

You might have seen a variable that seems to have a type. This is called a type annotation (a.k.a. a "type hint"):

>>> x: int = 2
>>> x

But when you run code like this, Python pretty much ignores these type hints. These are useful as documentation, and they can be introspected at runtime. But type annotations are not enforced by Python. Meaning, if we were to assign this variable to a different type, Python won't care:

>>> x = "hello"
>>> x

What's the point of that?

Well, there's a number of code analysis tools that will check type annotations and show errors if our annotations don't match.

One of these tools is called MyPy.

Type annotations are something that you can opt into in Python, but Python won't do type-checking for you. If you want to enforce type annotations in your code, you'll need to specifically run a type-checker (like MyPy) before your code runs.

Use = to assign a variable in Python

Assignment in Python is pretty simple on its face, but there's a bit of complexity below the surface.

For example, Python's variables are not buckets that contain objects: Python's variables are pointers. Also you can assign into data structures in Python.

Also, it's actually possible to assign without using an equal sign in Python. But the equal sign (=) is the quick and easy way to assign a variable in Python.

Series: Assignment and Mutation

Python's variables aren't buckets that contain things; they're pointers that reference objects.

The way Python's variables work can often confuse folks new to Python, both new programmers and folks moving from other languages like C++ or Java.

To track your progress on this Python Morsels topic trail, sign in or sign up.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video