1.2 Introducing Variables and Logical Operators
In this chapter we introduce the basic use of variables and logical operators. See the bottom of the page for exercises and an overview of the operators we use.
Throughout the exercises it would be smart to use the # in the code, where ever you want to add a comment, that is not to be run by Python. For example:
Figure 1.2: example of inserting comments whilst coding.
Writing and Operating on Variables
A variable is a name for an object. This object can be anything from a letter to a word, a number, a function, a list and many other things.
The Warehouse Metaphor
Imagine that we have a huge warehouse. This warehouse is equal to the memory of the program. When we create an object in the program it is like we are creating a new box in the warehouse. We can think about a variable as the label of a box in this warehouse. The content of the box is the value of the object.
Figure 1.3: Illustration of how we can think about values written into a variable.
In the code below we have made a numeric object, with the value 9 and the variable name or label named box. Notice that we are using the equal sign = here to put a value into the variable. Once we have defined our variable, Python will remember it and associate box with the value 9.
box = 9
If we wanted to make an object consisting of a character or what we call a string value, we would type:
x = 'Hello'
Remember to put quotes (single ' or double ") around string values. Otherwise Python thinks that we are trying to run a command. As we can see in the figure below, the box label or the variable is called x. The value of the box is the word Hello, and the object is a string-object.
Figure 1.4: Illustration of how to create a variable consisting of a word object
Now we might want to open the box named x to remind ourselves what is inside. We do that simply by typing print(x)
in the console and hitting the green run button to the top right of the console. If we look down at the Output window, we should now see that it has printed the word Hello.
Figure 1.5: Illustration of the Output window after we run a command in the console window.
We can check how many letters are in the word of a variable, using the code len(x)
.
Since we have defined x to be the word Hello
, printing out the length of x would return the number 5 in the output window.
Simple Operations on Variables
Now we can move on to perform some various simple operations on variables.
We can use Python to perform simple mathematic calculations like addition.
print(box + 20)
Which returns the value 29 in the Output window.
We create a new variable, name it cat and give it the value 12.
cat = 12
And then we multiply the two variables box and cat:
print(cat*box)
This returns the number 108 in the Output window.
We can also use Python to check whether a different range of statements are true or false about a variable. For example if I wanted to know whether the variable named cat has the value 17, we could type:
print(cat == 17)
The two ==
signs ask the program whether our phrase is true. The output window would return the phrase False
.
This is because we defined the variable cat to be 12 earlier on. We can also ask whether a variable is not equal to a value or other variable. For example:
print(x != 'Hi')
We defined x to be the string Hello
earlier on. So when we ask Python whether x does not equal Hi
using the !=
code, it returns the word True
in the output window.
We can also as whether one variable has a value that is lesser or higher than another value. We can command this using the <
or >
signs. For example:
print(cat < box)
This will print either True or False depending on whether the sentence is true or false. In our case, we defined cat to be 12 and box to be 9, so the word False should appear in your output window when you run the code.
There are also the operators <=
and >=
. The first one can be used to check whether a value is higher than or equals another value. The second operator works oppositely and checks whether a value is lower than or equals another value.
print(cat <= box)
The above code will return the word True if the value of the variable box is above or equal to the variable cat.
Boolean Operators
There are a bunch of useful boolean operators. They can help us check for different criteria in one sentence.
The and operator tests whether two criteria about a value are both true.
For example if we want to check whether the variable x is 5 letters long, and whether it also equals the word hi, we could use the and operator.
print(len(x) == 5 and x == 'hi' )
This would return a False statement in the output window, since we earlier defined the variable x to be equal to the string Hello.
The boolean operator or only tests whether one of the criteria we set for a value is true. Taking the example above and using the or operator instead of the and one, we would now get a True in the output window, since our variable fulfills the first criteria of the word being 5 letters long.
These operators will be more useful later on when we learn some more advanced coding.
Exercise 1
- Create a variable named MuffinsBaked and give it the value of 12.
- Add another 3 muffins to a new variable called MoreMuffinsBaked. (NB. please use the variable MuffinsBaked in creating the new variable).
- Now your friends come over and only the ones who know the secret password may have a muffin. Create a new variable called SecretPassword and give it the string value of please.
- Create 3 new variables with names of your friends, e.g. Mark, Emily and Nicholas. Give each of their names a string value of either please or give me.
- Now check which friends are allowed to have a muffin. (Hint: print out each variable and see if it's equal to the SecretPassword variable).
- Before you give any of your friends a muffin, you have to check whether any muffins have been stolen! For each name, check whether they have the correct secret password and that the amount of muffins is still 15. (Hint: use the and boolean operator)
Summary of operators used in this section
In the image below you can see a summary of all the code used in this section with a short explanation of what it means.
# Here we define a variable with a numeric value:
x = 2
# Here we define a variable with a string value:
y = "cupcake"
# Here we tell the program to print the value of y in the output window:
print(y)
# Here we ask the program to tell us whether x = 3 or not:
print(x == 3)
# Here we ask the program to tell us how many letters are in the variable y:
print(len(y))
# Here we create a new variable that adds x and 2 together:
xx = x + 2
# Here we multiply the two numeric variables we have made:
xxx = x*xx
# Here we ask the program whether xx does NOT equal 5:
print(xx!= 5)
# Here we ask the program whether xx is bigger than x:
print(xx>x)
# Here we ask the program whether the word in the variable y (cupcake) is shorter than the word "spy":
print(len(y) < len("spy"))
# Here we ask the program whether the variable XX is equal to or smaller than the variable X:
print(xx <= x)
# Here we ask whether the length of the word in Y is equal to 5 letters AND whether the variable contains the word 'cupcake':
print(len(y) == 5 and y == 'cupcake')
# Here we ask whether the word in Y is equal to a length of 5 letters OR whether the variable contains the word 'cupcake':
print(len(y) == 5 or y == 'cupcake')
Figure 1.6: Summary of operations used in this chapter and their output in Python.