Download Python if statements - School of Information Technologies

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python if statements
Girls’ Programming Network
School of Information Technologies
University of Sydney
Mini-lecture 5
Control Structures
if statements
Summary
2
Outline
1
Control Structures
2
if statements
3
Summary
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
3
End-to-end programs aren’t very useful
ˆ So far our Python programs have been pretty boring
ˆ So far the interpreter has only run a single statement at a time
ˆ Every time we run the program the statements are executed in
the same order
ˆ We need the computer to do more work for us!
ˆ For our programs to be more useful they need to:
ˆ make simple decisions on our behalf to execute certain
statements
ˆ repeat statements over and over again
ˆ Computers are great at doing this until you pull out the plug
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
4
Control structures change program execution
ˆ Control structures control one or more statements by
changing the order (or whether) statements are executed
ˆ That is, they change the flow of control within a program
ˆ The decision control structure is called an if statement
ˆ The repetition control structure is called a loop statement
ˆ All imperative programming languages (like Python) provide
these two
ˆ Most languages provide very similar looking if statements
ˆ But there are many varieties of loop statement
ˆ Today we will cover Python’s if statement
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
5
A Python if statement in action
1
2
3
4
5
6
>>> x = 3
>>> if x == 3:
...
print 'x is equal to 3'
...
x is equal to 3
>>>
ˆ First we create a variable x set to the value 3
ˆ An if statement starts with the keyword if
All Python control structures start with a keyword
ˆ Next is the conditional expression x == 3 and then a colon
ˆ A conditional expression either evaluates to True or False
ˆ Here the comparison operator == tests if x is equal to 3
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
6
Python uses indentation to indicate blocks
1
2
3
4
5
6
>>> x = 3
>>> if x == 3:
...
print 'x is equal to 3'
...
x is equal to 3
>>>
ˆ The conditional expression is True since we set x to 3
ˆ This means the body of the if statement is executed
ˆ The body is a block of code controlled by the if statement
ˆ Python uses indentation to identify blocks
ˆ Here the body is a single print statement
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
7
The if body is not executed on False
ˆ Let’s now set the variable x to 4 and try again:
1
2
3
4
5
>>> x = 4
>>> if x == 3:
...
print 'x is equal to 3'
...
>>>
ˆ Notice this time the print statement does not get run
ˆ That’s because the conditional x == 3 is now False
ˆ So the if statement body is skipped
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
8
A block can contain one or more statements
1
2
3
4
5
6
7
8
>>> x = 10
>>> if x > 3:
...
print 'x is bigger than 3'
...
if x > 6:
...
print 'x is also bigger than 6'
...
x is bigger than 3
x is also bigger than 6
ˆ Here one if statement is controlled by another if statement
ˆ We can tell this because the second if statement is indented
ˆ So the first if statement body consists of two statements:
a print statement and a nested if statement
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
9
The else clause is executed on False
ˆ if statements can have an else clause:
1
2
3
4
5
6
7
>>> x = 4
>>> if x == 3:
...
print 'x is equal to 3'
... else:
...
print 'x is not equal to 3'
x is not equal to 3
>>>
ˆ Notice again, the else keyword is followed by a colon
All Python control structures end with a colon
ˆ We could write this using two if statements, with the second
having x != 3 as the conditional expression
ˆ else is more efficient and avoids redundancy in the logic
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
10
Deeply nested ifs quickly becomes messy
ˆ If we want to consider more alternatives we must nest ifs:
1
2
3
4
5
6
7
8
9
>>> if x < 3:
...
print "x is less than three"
... else:
...
if x == 3:
...
print "x is equal to three"
...
else:
...
print "x is greater than three"
...
>>>
ˆ This can get ugly once there are many alternatives
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
11
elif clauses avoid nested ifs
ˆ We can write the previous example more elegantly as:
1
2
3
4
5
6
7
8
>>> if x < 3:
...
print "x is less than three"
... elif x == 3:
...
print "x is equal to three"
... else:
...
print "x is greater than three"
...
>>>
ˆ Each conditional expression is evaluated until one is True
ˆ The corresponding block is then executed
ˆ If none of the if or elif conditionals are True
the else block is run
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
12
and and or
ˆ There are two additional keywords that are quite useful, and
and or.
1
2
>>> if name == "David" or name == "Paul":
...
print "That's my brother's name!"
ˆ How would you write this without using or?
ˆ You can test conditional expressions in the interpreter:
1
2
3
4
5
6
>>> True and True
True
>>> True or False
True
>>> False and True
False
Girls’ Programming Network
if statements
Mini-lecture 5
Control Structures
if statements
Summary
13
You should now be able to:
ˆ Explain what a control structure is
ˆ Write Python if/elif/else statements
Girls’ Programming Network
if statements
Mini-lecture 5