The Assignment Operator
In this lesson, we'll explore how to use the assignment operator to store a variable in a computer's memory. We'll go into the details of computer memory in a few lessons.
The assignment operator is denoted by the symbol:
=
(the equal sign). Think of it in terms of math operators
we have already encountered: the addition operator
+
combines the numbers on its left and right,
while the division operator /
divides the number on
its left by the one on its right. Just as the order matters in division,
the order is crucial with the assignment operator.
Let's see this in action.
In the above code snippet, we assign the value 1 to a new variable,
a, using the assignment operator. This operator consists of one
characters: the equal sign; it represents storing the value on its right
into the variable on its left. To view the
value stored in this, write the variable name within a print
function, which we will learn in the next lesson.
What is a variable?
We call the object a a variable, but why, what is a variable? A variable stores a modifiable value into computer memory. And an object is the most general kind of object that stores anything into computer memory. Let's get some examples of these. In the below code area: a and b are variables, and c and d are objects.
a = 1 # a is a variable
b = "hello" # b is a variable
c = [1, 2, 3] # c is an object
d = pd.DataFrame({'y': ["a", "b", "c"]}) # d is an object
In some programming languages, depending on how they are created, a variable or object can not be modified after it is created. In Python, most variable and objects can be modified. In the below code editor let's look at how we can modify - aka update - the value assigned to the variable a.