IF statements
Syntax
if |
a |
> |
59 |
: |
|
print |
'True block' |
||
else |
: |
|||
|
print |
'False block' |
||
About IF
If statements allow you to change the flow of your program. It allows you to test a condition, test what the user has entered or evaluate the current state of your variables. Fundamentally an if statement will allow you to run specific code if a condition is true or not. The condition must evaluate to either true or false. There are no other possible choices.
Here is the sample code from the video. Try this out before moving on.
age = int(raw_input("how old are you?"))
if age > 18:
print "your an adult"
print "hai"
else:
print "your not an adult"
Key term
Control statements – Changes the flow of the program. Also known as flow control.
Helpful links
http://www.ibiblio.org/g2swap/byteofpython/read/if-statement.html
http://www.pwnict.co.uk/webpages/python/syntax/lesson%205/lesson5.html
http://www.pwnict.co.uk/webpages/python/problemSolve/lesson%204/Lesson4.html – NOTE this link shows you how you can decide on what conditions a IF statement should have.
http://www.tutorialspoint.com/python/python_if_else.htm
Test your skill
1. What will be printed by the following code?
a = 21 b = 21 if a>b: print "hi" else: print "bye"
2. Write a short program which will ask the user for how much they want to get paid. If they enter over 50,000 then it should say that they are insane otherwise it should say OK.
3. Try the HackIT task – Print or not to print











