How can you assign a variable in Python?
assignmentdefinition in Python Terminology.
In Python, the equal sign (=
) assigns a variable to a value:
>>> count = 4
>>> count
4
This is called an assignment statement.
We've pointed the variable count
to the value 4
.
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"
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:
>>> class = "invalid"
File "<stdin>", line 1
class = "invalid"
^
SyntaxError: invalid syntax
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
7
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.
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
7
But there's nothing stopping us from pointing it to a string instead:
>>> amount = "hello"
>>> amount
'hello'
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.
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
2
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
'hello'
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.
=
to assign a variable in PythonAssignment 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.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll explain concepts that new Python programmers often overlook.
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.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.