Variables & the assignment operator, `=`

The last exercise in particular would have been much cleaner if we had a way of referring to that particular string instead of having to write it all out several times!

This is one of the basic use-cases of variables!

A variable is a way of keeping a handle on data. Variables can hold numerical or string data that we’ve encountered so far, as well any other type of data, as we’ll see later.

In order to create a variable in python, we use the assignment operator, = i.e. the equals sign.

For example

a_number_variable = 10
text1 = "aaaaa"

Naming variables

You are free to choose any name for a variable that you wish. The only exceptions are that the variable name cannot contain spaces or other special characters, and cannot correspond to a special python keyword like if, else, or for, as these are reserved for special operations.

While not being illegal (illegal in programming means that it will give an error), you are also strongly advised to not over-write built-in function names.

For example it is technically legal to name a variable print! However, you would then overwrite the print function and no longer be able to print things to the terminal!

Python variables are case-sensitive, so a variable called a cannot be referred to as A, and a variable called MyNumber is not the same as mynumber!

Note on variables vs the data they hold

New programmers are sometimes confused by variables vs the data they contain, especially when it comes to string variables.

For example, the following are all valid variable assignments

  • one = "1" - a variable called one that holds the single-character string “1”
  • one = 1 - a variable called one that holds the number 1
  • OnE = "one" - a variable called OnE that holds the string “one”

Using variables

Once a variable has been assigned, we can manipulate its data in exactly the same way as if we were dealing with the data (number, string, etc) directly.

For example

print("Joe Bloggs"[-6:])

and

somename = "Joe Bloggs"
print(somename[-6:])

would both output Bloggs.

What happened here?

  1. In the first line we assigned the string "Joe Bloggs" to the variable somename.
  2. Then in the second line, we access the last 6 characters of the string using the slicing that we learned about above, and print it to the terminal.

Exercise : Basic variable usage

Write a script (name the file exercise_variables.py) and create a variable (give it any name you like!) that contains the string

"The quick brown fox jumps over the lazy dog"  

Then create a second variable that contains the text

"lazy cat"

Now use the replace member-function to replace “lazy dog” with the contents of the second variable and assign the result into a third variable. Remember that a member-function is called using the

<OBJECT>.<FUNCTION NAME>(ARGUMENTS...)

syntax.

Lastly print out all three variables.

More assignment operators

Along with the standard assignment operator, =, Python has additional extensions that provide shorthand ways to assign values into a variable.

For example (rhs = right hand side)

  • += : add the rhs to the variable; a += 10 is the same as a = a + 10
  • *= : multiply rhs by the variable; a *= 2 is the same as a = a * 2
  • /= : divide variable by rhs; a /= 4 is the same as a = a/4

Where appropriate, this also applies to string data, e.g.

a = "Some "
a += "text"
print(a)

would output Some text.