Creating and modifying variables

code guest
Python Journey
Published in
5 min readSep 3, 2019

Variables are really important in programming because they make working with data values so much easier — we can store data values in variables for further action and then use these variables in all kinds of operations and computation. Furthermore, variables make our code more readable.

To work with variables — creating variables or updating their data values, we use the assignment operator which is the equal = symbol.

To create a variable, we have to assign a value to it using the = operator.

To modify a variable, we also use the = operator to update its data value.

The = operator takes two operands:

  • The variable to be created or updated is to the left of the = operator
  • while it’s new or updated data value is to the right of the = operator
# Example of assignment of string "hello world" to a variable called hellohello = "hello world"

Note: the assignment operator looks like the traditional mathematical equals sign, this is NOT the case. The equality operator is ‘==’

The assignment operator that create/modify variables is the equal symbol (credits: undraw.co)

= operator’s left operand

The = operator’s left operand must be a variable — this variable will be modified from the operation.

The technical term for the operation is assignment. We say that the variable on the left operand position has been assigned a data value.

Just to emphasise, the left operand in any assignment operation must be a variable — it cannot be a data value. The nature of assignment operations being that a variable is at the heart of the operation.

= operator’s right operand

The = operator’s right operand supplies the data value to the variable in the left operand position.

The right operand can take different forms:

  • The right operand can be an operation like 12*12 (note: operations always evaluate down to a data value)
  • The right operand can be a data value like [], {} or True
  • The right operand can also be another variable (note: variables are just names that refer to data values)
  • The right operand can also be a function object like print

Note that if the right operand is an expression (such as the operation 12*12 above), the expression will be evaluated first.

The assignment operation’s left operand gets modified by the operation. The right operand provides the data value and nothing happens to it (credits: undraw.co)

Variables on both sides

The reason why it is important to differentiate which operand is on the left or on the right is that both can be variables — which can be confusing initially.

The variable on the left is the variable that is being changed.

On the other hand, the variable on the right is just there to supply a data value and this has a few implications:

  • the right variable remains the same after the operation whereas the left variable has been changed.
  • the right variable merely supplies a python Object and isn’t linked with the left variable. If after the assignment operation, we do something to the right variable, the left variable isn’t changed at all.

Augmented assignment

To supplement the = operator, Python supports augmented assignment which uses +=, -=, *=, @=, /=, //=, %=, **=, >>=, <<=, &=, ^= and |=.

Where assignment operations just assign new values to variables, augmented assignment involves an operation followed by assignment.

Notice that augmented assignment comprises two symbols and that the left symbol +, -, / etc resembles operators used for example in arithmetic operations.

And from above we discussed that the left variable is the one that gets modified — what augmented assignment does is to modify the left operand by performing an operation on its data value first (eg. addition or multiplication) and then assigning it the data value obtained after evaluation to it.

# examples
hello = 10 # normal assignment
hello += 10 # augmented assignment
hello *= 2 # augmented assignment
print(hello) # gives us 40

A note about the more uncommon augmented assignment symbols — @= for matrix multiplication, >>=, |= and =<< for bitwise operations (these are not covered in our article series)

Augmented assignment does two things — an operation followed by assignment on its left operand (credits: undraw.co)

Naming variables

There are a couple of naming conventions that we have to follow when choosing a name for our variables and they are:

  • Variables are case sensitive
  • Variables cannot be named after any of Python’s reserved keywords (eg. True , False, for) because Python gives special meaning to these keywords (eg. as boolean values)
  • Variables can’t contain spaces (though we can use the underscore character (“_”) in place of a space)
  • The first character of a variable name must be a letter or the underscore character — this means we can have digits in our variable name but not in as first character

References

Summary

  • The assignment operator is used to assign values to new variables or modify existing variables
  • The left operand of an assignment operation must be a variable
  • The right operand of an assignment operation will be evaluated first before being assigned to the left operand
  • Augmented assignment involves both an operation and assignment
  • There are naming conventions to follow in naming our variables

Quiz

which of the following options are incorrect?

a) x = y makes variable y

b) True = True is an invalid assignment operation

c) 5 = x is invalid — it doesn’t make a variable called 5

d) if variable Hello has been assigned the data value of 20, then Hello //= 3.3 gives hello a value of 3.0

Quiz explanation

a) The operand on the left (which must be a variable) is the one being changed by the assignment operation

b) The operand on the left must be a variable and variables cannot be named after keywords like True

c) The first character of a variable’s name cannot be a digit

d) Hello //= 3.3 does assign Hello the value of 3.0. But Hello isn’t hello and Python is case sensitive

--

--