When I was a young whippersnapper, I didn’t get dessert unless I finished my dinner. Dinner always consisted of healthy stuff like spinach so it was very hard to finish. That was my introduction to conditional statements. Conditional statements are like saying, “If this, then that.” If I ate dinner, then I got dessert. In my example, eating dinner is the boolean condition and getting dessert is the consequent. if Python, then code?
If Python, then Cake!
We can write my example above in Python:
>>> dinner = True
>>> if dinner == True:
… print('Cake!')
…
Cake!
Sweet! I ate dinner so I get dessert. Unless the cake is a lie…
We don’t have to explicitly evaluate True or False in our condition statement. Python will interpret the value of our variable and proceed accordingly. I can rewrite the above example like this:
>>> if dinner:
… print('Cake!')
Because the variable dinner was assigned True, I get cake.
else
Let’s write the function, dessert(), this time with what is called an alternative.
>>> def dessert(cake):
… if cake == False:
... print('The cake is a lie!')
… else:
... print('Om nom nom.')
…
Piece of cake, right? It gets better. Let’s combine Boolean logic operators with our variables.
>>> dinner = False
>>> if not dinner:
… print('How can you have any pudding, if you don’t eat your meat?')
If you recall, if not dinner is the same as “if dinner is False”.
>>> dinner = True
>>> cake = True
>>> if dinner and cake:
… print('So full!')
You can only get ‘So full!’ if both dinner and cake are True.
>>> dinner = False
>>> cake = True
>>>if dinner or cake:
print('Yay! I choose cake!')
If you want to test a condition, use an if statement. If you learned nothing, start again. Else, proceed to the next tutorial: multiple conditions.