Statements
In the section below we will teach you how to use if, else and elif sentences. These sentences are useful for putting conditions on your code. It could for example be used to sort groups into age, gender, race or other features.
If, else, elif statements
A very neat trick in python is that it can conduct decision making sentences. These sentences can be useful for many things.
The first sentence we will focus on, is the if sentence. The if sentence takes an item and does something to it if it meets our conditions. In the following code we create a variable named presentContent, and give it the string value of kitten. The we use the if sentence to create a condition.
presentContent = 'kitten'
if presentContent == 'kitten':
print('Happy!!')
##########Output:##########
Happy!!
In the example above, our condition for printing the word "Happy!!" is that the value in the variable presentContent has to be equal to the word kitten.
Let us see what happens if the variable is not equal to kitten.
presentContent = 'socks'
if presentContent == 'kitten':
print('Happy!!')
##########Output:##########
There is no output! So it only completes the command if the sentence meets the if condition.
Now I'll introduce the if else sentence. This sentence is similar to the one above, however if the variable does not meet the conditions, we can command it to do something else. See below for an example.
presentContent = 'kitten'
if presentContent == 'kitten':
print('Happy!!')
else:
print('Oh no!')
##########Output:##########
Happy!!
Since the variable presentContent is defined to equal kitten in the example, we get a "Happy!!" response. However if we make presentContent equal to socks instead of kitten, we get the "Oh no!" response. See below.
presentContent = 'socks'
if presentContent == 'kitten':
print('Happy!!')
else:
print('Oh no!')
##########Output:##########
Oh no!
We can also add a second condition by adding the elif sentence to the above example.
presentContent = 'socks'
if presentContent == 'kitten':
print('Happy!!')
elif presentContent == 'socks':
print('thaaanks for the socks mum *sigh*')
else:
print('gasp!')`
##########Output:##########
thaaanks for the socks mum *sigh*
Here we can tell Python to do 3 different things, all depending on which variable we're dealing with. We can also use these sentences to put conditions other than equality or variabel. For example a condition of length of variable, or type of variable etc.
Exercises
Exercise 1:
- Create a variable named mouse. Give it the string value of either the word cheese or the word cat.
- Create an if-else sentence that prints out the word yummy! if you give the mouse cheese, and print out the word argh!! if you give the mouse a cat.
Exercise 2:
- Create a variable called lotto rolls and give it a value of 0.
- Create an if-elif-elif statement that states that if there has been 1 lotto roll, print out the word try again. Otherwise if there has been 2 lotto rolls print out the word so close. And last if there have been 3 lotto rolls, print out the word bingo.
Summary of Code
#Here we create a new string variable with the name presentContent and the string value of _kitten_:
presentContent = 'kitten'
#Here we tell Python to print out the word "Happy!!" if the value of the presentContent variable is equal to the string value of 'kitten'. If it isn't equal to it, nothing happens:
if presentContent == 'kitten':
print('Happy!!')
#Here we tell Python to print out the word "Happy!!" if the value of the variable called presentContent is equal to the word 'kitten'. We tell Python that if it is not equal to the word 'kitten' it will then print out the word 'Oh no!':
if presentContent == 'kitten':
print('Happy!!')
else:
print('Oh no!')
#Here we tell Python to print 'Happy!!' if presentContent is equal to 'kitten', and to print 'thaaanks for the socks mum *sigh*' if presentContent is equal to 'socks'. If presentContent isn't equal to 'socks' or 'kitten' then we ask Python to print out 'gasp!'.
if presentContent == 'kitten':
print('Happy!!')
elif presentContent2 == 'socks':
print('thaaanks for the socks mum *sigh*')
else:
print('gasp!')