Assignment Ā¶
In Python, an assignment operator is used to assign a value to a variable.
The most common assignment operator is the equal sign
=
.
It is important to note that the equal sign in the context of variable assignment is not a mathematical equality but rather an instruction to assign the value on the right-hand side to the variable on the left-hand side.
When you use the assignment operator (
=
) to create a variable, Python reserves a space in memory to store the value associated with that variable.
class_mascot = "duck"
Let's walk through what is going on under the hood.
Memory allocation Ā¶
All data in your computer is stored either on your hard drive or in your Random Access Memory (RAM).
These hardware components contain a massive amount of places to store data, each with a unique label so the computer can find it again.
This label is called a memory address; for example,
0x6dfed4
,
0xki2zmb
, and
0xeg38d8
could be valid memory addresses.
The first thing Python does, is try to find a drawer that is empty.
Once it finds an empty drawer, Python will put the string
"duck"
inside this drawer with the memory address
0xeg38d8
.
Python will now keep our mascot safe unless we specifically tell Python to remove or change what is inside this drawer.
This is great!
We stored our mascot in our memory and can check in on it by telling Python to go to the drawer with the memory address
0xeg38d8
.
Wait, do I have to remember the memory address
0xeg38d8
each time I want to read our mascot a bedtime story?
That would be annoying.
Instead, we tell Python to give this memory address a name or label that I can remember, like
class_mascot
.
Now, I can tell Python to go get the data stored in
class_mascot
, and it would be there!
print(class_mascot)
duck
Now, suppose that I proposed to change our class mascot.
class_mascot = "penguin"
print(class_mascot)
penguin
Note
Declare before use Ā¶
In Python, you need to explicitly declare and assign a value to a variable before you attempt to use it in your code.
If you try to use a variable before it has been created, Python will raise an error. This includes attempting to use a variable name that hasn't been declared or has been misspelled. For example, if you tried
print(other_example_number)
you would get
NameError: name 'other_example_number' is not defined
.
By enforcing the rule that variables must be created before they are used, Python helps catch potential errors early in the development process, promoting code clarity and preventing accidental use of undefined or misspelled variable names.
Warning
Be aware that it is the order of execution of cells that is important in a Jupyter notebook, not the order in which they appear. Python will remember all the code that was run previously, including any variables you have defined, irrespective of the order in the notebook. Therefore if you define variables lower down the notebook and then (re)run cells further up, those defined further down will still be present.