What Is the Assignment Operator in Python?

A name tag with the title of the article overlayed.

This week, we’re hitting another beginner topic: the assignment operator. While the idea is simple, the concept is rich in related ideas like scope, iterable unpacking, and augmented assignment.

Table of Contents

Concept Overview

Typically, when we talk about assignment in programming languages, we’re talking about binding a value to some name. To show that we want to bind a variable to a name, we use an assignment operator.

In Python, there are many assignment operators, but to keep things simple, we’ll start by talking about the = operator. As mentioned already, we can use the assignment operator to give a name to some value, such is in the following assignment statement:

color = "red"

Now, we have a variable called color assigned to the string "red". If we ever want to retrieve our string, we can call it by its new name.

The cool thing about the assignment operator is that we’re not restricted to such simple statements. As long as everything on the right side of the assignment operator can be evaluated (i.e., an expression), we can store the result in a variable, such as in the following example:

quotient = 21 / 3

That covers the basics of the assignment operator. In the remainder of this article, we’ll look at some questions you might have.

Are There Other Assignment Operators in Python?

While = is typically the operator we think of when talking about assignment, there are actually a variety of assignment operators. According to the Python documentationOpens in a new tab., there is an additional set of Python operators called “augmented assignment operators.”

An augmented assignment operator performs assignment in addition to some other operation. For example, you may be familiar with the += operator. It performs “addition” between the terms on both sides of the assignment operator and then assigns the result into the variable on the left side of the operator. In this case, addition is in quotes because Python allows for operator overloading. For example, the += operator might perform addition as follows:

num = 5
num += 6  # num stores 11

However, for a different data type like strings, the += operator would perform concatenation:

phrase = "Hello"
phrase += ", World"  # num stores Hello, World

With that said, there are many of these augmented operators—basically one for each binary operator.

Of course, you might also be familiar with the walrus operator (i.e., :=), which behaves just like the assignment operator but in contexts where you want the assignment statement to behave like an expression, such as in a loop condition.

Where Can I Use My New Variables?

When you write an assignment statement using the assignment operator, you create a new variable. In Python, the rules that tell you where you can use that variable are dictated by that variable’s scope.

In many programming languages, the norm for variable scope is to define it statically. Basically, that means the variable can only be used within the scope it is declared. If it is defined in the context of a function, it will only be able to be used in the context of that function.

Python is a bit different in that the scoping rules are more relaxed. Specifically, there are four separate namespaces where a variable name can be assigned: built-in, global, enclosing, and local. For the average user, the only two you will really care about are the global and local namespaces.

Global includes any variables defined at the module level, such as a constant you might place at the top of your file. Meanwhile, local refers to any variable defined within a function.

Because Python doesn’t define scope for other blocks, it’s possible to define a variable within a loop or if statement and used it outside of that loop or if statement. This can be quite jarring for folks that come from statically scoped languages, but it’s a really handy feature when you get used to it. No longer will you need to create a variable ahead of a set of if statements. Of course, there are circumstance where you might have a variable that ends up undefined.

Can I Define Multiple Variables With the Assignment Operator?

The short answer: yes! You may have heard of iterable unpacking. If so, then you already know that iterable unpacking allows you to split an iterable into a few variables using the assignment operator:

red, blue = ["red", "blue"]

Python also lets you do this sort of goofy thing where you can assign multiple variables to the save value by chaining the assignment operator:

odd = prime = number = 7

In general, iterable unpacking is probably more common and useful than this latter syntax, but it’s helpful to know it exists.

Assigning Some Reading

Because we’re still in the early stages of this series, there are still a lot of really basic topics to cover, but I’m finding that there’s still plenty of rich depth to discuss. Just by introducing the assignment operator, we were able to hit topics like scope today, which is really exciting.

With that said, while I continue growing the concept map, why don’t you browse some of these related articles:

Likewise, you can build on your knowledge by checking out some of these resources (#ad):

Finally, if you’d like to take your support a step further, check out my list of ways to grow the site. Otherwise, take care!

The Python Concept Map (12 Articles)—Series Navigation

An activity I regularly do with my students is a concept map. Typically, we do it at the start and end of each semester to get an idea of how well our understanding of the material as matured over time. Naturally, I had the idea to extend this concept into its own series, just to see how deeply I can explore my own knowledge of Python. It should be a lot of fun, and I hope you enjoy it!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Code Posts