Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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