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
- Are There Other Assignment Operators in Python?
- Where Can I Use My New Variables?
- Can I Define Multiple Variables With the Assignment Operator?
- Assigning Some Reading
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 documentation, 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:
- What Is a Variable in Python?
- What Is an Expression in Python?
- What Is Iterable Unpacking in Python?
Likewise, you can build on your knowledge by checking out some of these resources (#ad):
- Effective Python: 90 Specific Ways to Write Better Python
- Python Tricks: A Buffet of Awesome Python Features
- Python Programming: An Introduction to Computer Science
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!
Recent Code Posts
It's a special day when I cover a Java topic. In this one, we're talking about Enums, and the problem(s) they are intended to solve.
Chances are, if you're reading this article, you've written some Python code and you're wondering how to automate the testing process. Lucky for you, this article covers the concept of unit testing...