Chapter 1.6 - Assignment Operators

Chapter 1.6 - Assignment Operators

How do we store the score in our game? Or keep track of the health of the enemy? What we need to do this is the assignment operator

An operator is a symbol like + or -. An assignment operator is the = symbol. It stores a value into a variable to be used later on. The code below will assign 10 to the variable x, and then print the value stored in x. 

# Create a variable x
# Store the value 10 into it.
x = 10
# This prints the value stored in x.
print(x)
# This prints the letter x, but not the value in x
print("x")
# This prints "x= 10"
print("x=", 10)

An assignment statement (a line of code using the = operator) is different than the algebraic equality your learned about in math. Do not think of them as the same. On the left side of an assignment operator must be exactly one variable. Nothing else may be there.

On the right of the equals sign/assignment operator is an expression. An expression is anything that evaluates to a value. Examine the code below.

x = x + 1

There are also assignment operators for addition, subtraction, multiplication and division.