July 2, 2010 5 Comments
Sometimes life presents you with challenges. Some are large and some are small. How you decide to deal with them has consequences. Those consequences flow from your decision. If I eat all my lollies now, I won’t have any after dinner. If I annoy my friend Ben he won’t invite me over anymore. If I don’t keep my hands to myself I don’t get the DS. If I don’t let the dog out when I get up, I don’t get to play computer games. When these conditions are coupled with consequences, they can guide our lives. Programs are the same. Not exactly the same, but similar.
Within a program you can direct its flow by using “if”. The if statement has a particular structure which needs to be followed (called its “syntax”). The structure of if is as follows:
if <condition>: <consequence>
Don’t worry about the stuff in angle brackets <> at the moment. The important thing is that the first line starts with “if” and ends with a colon “:”. No colon and Python will get upset. Further, the code that follows the colon is indented. That’s important!
>>> time = '5PM' >>> if time == '5PM': ... print 'Time to play the computer!!' ... Time to play the computer!!
So, working through this code… First the string ‘5PM’ is put in the bucket that we’ve called ‘time’. Then, we’ve compared to see if the time is ‘5PM’. If it is, then a message is printed (‘Time to play the computer!!!’).
Notice also that we said “time ==”, not “time =”. That’s because ‘=’ and ‘==’ mean different things to Python. When you write ‘=’ you are saying to Python, “take the thing on the right and put it in the thing on the left”. However, when you write ‘==’ you are saying to Python, “these two things are equal – true or false?”
>>> time = '6PM' >>> if time == '5PM': ... print 'Time to play the computer!!' ... >>>
If the condition (in this case: does time equal ‘5pm’) is not true, then the code is not processed. Here we assigned a different time (actually string) to time. When we ran through the condition, Python said “well, that’s false now” so the code didn’t execute.
You can use other sorts of comparisons (like > and <) in the condition. They won’t work well in the examples above because time is a string (type), not actually a representation of time. So if things like this work:
>>> time = '6PM' >>> if time > '5PM': ... print 'Time to play the computer!!' ... Time to play the computer!!
it’s only because of blind luck. This really won’t work:>>> time = '6AM' >>> if time > '5PM': ... print 'Time to play the computer!!' ... Time to play the computer!!
In this case, it’s a failure because the message printed when it shouldn’t have (6AM is not later than 5PM, but it printed anyway). It’s doing this because Python is comparing them as if they were strings (which they are) not as if they were times. Python is able to compare them as times, but that’s not something we’ll cover here.
We can compare things in lots of ways:
a< b – a is less than b: true or false?>>> a,b = 1,2 >>> if a < b: ... print "a is less than b" ... a is less than b
Did you notice the sneaky way we did two assignments there? Instead of a=1 and b=2 we said a,b = 1,2. You can do this too. Also pay attention to the fact that both a and b are integers, not strings – they have no quotation marks.
a> b – a is greater (or more) than b: true or false?>>> if a > b: ... print "a is greater than b" ...
Nothing printed because a is 1, which is less than b, which is 2.
not – this means “not” so if you have another comparison you can negate it:
not a > b – it is not the case that a is greater than b: true or false? (think it through, it does make sense)>>> if not a > b: ... print "it is not true that a is greater than b" ... it is not true that a is greater than b
a != b the ! (sometimes called a “shriek!” or a “bang!”) here means “not” (strangely this is just “!=”, not “!==” which given what we said about Python and “==” above would make more sense, but also take more typing. Perhaps Python is just a bit schizophrenic?). The translation of this is: a is not equal to b: true or false?>>> if a != b: ... print "They are not equal" ... They are not equal
a <= b – a is less than or equal to b: true or false? The other way around (=<) won’t work:>>> if a <= b: ... print "a is less than or equal to b" ... a is less than or equal to b >>> if a =< b: File "<stdin>", line 1 if a =< b: ^ SyntaxError: invalid syntax
a >= b – a is greater than or equal to b: true or false?
You can also connect a number of conditions together using “and” and “or”:>>> if a==1 and b== 2: ... print 'a equals 1 AND b equals 2' ... a equals 1 AND b equals 2 >>> if a == 1 or b ==5: ... print "a equals 1 or b equals 5 (or both)" ... a equals 1 or b equals 5 (or both)
Try some different combinations.
You can even let Python do some calculations before it compares things:>>> if 5-1 == 3+1: ... print "four equals four" ... four equals four
In fact, Python’s methods of evaluating the conditions you pass it are more many and varied than this. We may meet them later. But for the time being note this:>>> if 1: ... print "1 is true!" ... 1 is true! >>> if 0: ... print "But 0 isn't" ...