Tuesday, 12 September 2017

A Quick Look at Variables

In the earlier post about a for loop I casually mentioned that it included a variable, i, to represent each time it went through the loop. So what are variables?
The simplest way I can put it is a variable is a holding space (a virtual pigeonhole?) with a name, for holding a piece of information, such as a number, a string (computer term for words, letters & other text) or maybe something more complicated.

Variable Names

The name of a variable is important, as it is how both the computer and humans looking at the program refer to that pigeonhole. If the name is changed (even accidentally), the computer will assume the new name refers to a different pigeonhole of information.
In some languages (Java, C++ and to a lesser extent Perl) a variable can only hold one type of information (number, string, list or something else). Python is more relaxed and flexible - a variable can only hold one piece of information at a time, but the type can change from number to text or back.
If you tell Python to print the variable using the variable name, it will print whatever information is stored as that variable. Going back to that earlier script:
#!/usr/bin/python3.5
for i in range(10):
    print (i, "Hello World")
looking at  i you should be able to work out that it contains a number, and that with each loop the number changes. The last line in the script prints out whatever value is in i, followed by the string "Hello World"
If there is one problem with this variable, it is that the variable name does not give any clue as to what it represents. As some of my programs can use a lot of variables, I need to be careful about naming them so that I don't get confused.
Variable names in Python can be most words (except keywords, such as for, print, in and about 30 others, which all have a very specific meaning in Python), using upper or lower case, and can have numbers in the middle or end. Punctuation/symbols are risky - I generally avoid them though I understand that underscore _ can be used, typically to join two words together such as max_amount or running_total. Note that both of these examples give some idea as to what they represent.

Assigning Values to Variables

Putting a value into a variable is simple - you use an assignment statement, such as x = 4 which tells Python that the variable with the name x now holds the value 4. 
Although maths will be covered later on, here's a quick example using variables to represent numbers.
#!/usr/bin/python3.5
x = 4
y = 6
print ("x is ", x)
print ("y is ", y)
print ("x + y = ", x + y)
gives the output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test03c_variables.py
x is  4
y is  6
x + y =  10

>>>
 Just as an aside, the phrase "x is "  is an example of a string, as is "Hello World" from the first script. The quotes on either side tell the computer (and humans reading the program) that this is a string, and can be printed as it is. Even the "x + y = " is a string - it looks like a formula, but because it is enclosed in quotes it is treated as a string. The quotes can be single 'like this' or double "like this" - Python recognises both as strings. 
Assignment statements can use other variables, maths, or other even more complicated ways of determining what a variable should hold. For example, lets extend the script a bit.
#!/usr/bin/python3.5
x = 4 # Assignment statement!
y = 6 # Another assignment statement
print ("x is ", x)
print ("y is ", y)
print ("x + y = ", x + y)
w = x * 2
print ("w is ", w)
z = x + y + w
print ("z is ", z)
gives the output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test03c_variables.py
x is  4
y is  6
x + y =  10
w is  8
z is  18

>>>
 I've been a bit creative and added some comments to the earlier lines. As I mentioned in an earlier post, comments start with the hash # sign, and anything after the # is ignored by Python - comments are for human readers, not the computer.
Back to variables, this newer script shows that the values of both w and z are derived from the values of other variables - here x & y. If the value of x changes, the value of w will change, and if either x or y changes, the value of z will change.

A variable can be changed, with the new value replacing the old value. Sometimes this is dependent on what the old value is and here the assignment expression can have the variable on both sides of the = sign.
If I add the following lines to the script:
x = x * 3
print ("x has now changed to ", x)
y +=1
print ("y has now changed to ", y)
the output is now:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test03c_variables.py
x is  4
y is  6
x + y =  10
w is  8
z is  18
x has now changed to  12
y has now changed to  7

>>>
The line x = x * 3 you can probably work out for yourself, given that the asterisk * is the multiply sign in Python. It calculates what x * 3 is (x = 4, 4 * 3 = 12) and then assigns this new value to x.
 The second from last line is more sneaky. It is a shortcut for saying y = y +1. This shortcut has arisen due to programmers so often wanting to increase or decrease values by a steady amount, typically in a loop of some sort.

Final script of this post should demonstrate:
#!/usr/bin/python3
x = 10
y = 10
z = 10
for count in range(6):
    x += 1
    y -= 2
    z *= 2
    print ("Count is", count, ", x = ", x, ", y = ", y, ", z = ", z)
This gives the output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test03d_variablesincrement.py
Count is 0 , x =  11 , y =  8 , z =  20
Count is 1 , x =  12 , y =  6 , z =  40
Count is 2 , x =  13 , y =  4 , z =  80
Count is 3 , x =  14 , y =  2 , z =  160
Count is 4 , x =  15 , y =  0 , z =  320
Count is 5 , x =  16 , y =  -2 , z =  640

>>>
 As you can see, the variables are given their initial values (initialisation!) at the start. Then a for loop runs (I've taken my own advice, and used the variable count rather than i).
For every time it goes through the loop, x is increased by 1, y is decreased by 2 and z is multiplied by 2. Incidentally, count also increases by 1, but this is because of the for loop working through the range(6) (which, as you can see, is actually 0-5) rather than any explicit assignment.

No comments:

Post a Comment