principles of programming cm10227
play

Principles of Programming CM10227 Lecture D.2.: Conditionals, - PowerPoint PPT Presentation

Conditionals More on Functions Recursion Principles of Programming CM10227 Lecture D.2.: Conditionals, Recursion, Interesting Functions Dr. Marina De Vos University of Bath Ext: 5053 Academic Year 2012-2013 Lecture D.2. (MDV) Programming


  1. Conditionals More on Functions Recursion Principles of Programming CM10227 Lecture D.2.: Conditionals, Recursion, Interesting Functions Dr. Marina De Vos University of Bath Ext: 5053 Academic Year 2012-2013 Lecture D.2. (MDV) Programming I Academic Year 2012-2013 1 / 60

  2. Conditionals More on Functions Recursion Outline Conditionals 1 More on Functions 2 Recursion 3 Lecture D.2. (MDV) Programming I Academic Year 2012-2013 2 / 60

  3. Conditionals More on Functions Recursion Outline Conditionals 1 More on Functions 2 Recursion 3 Lecture D.2. (MDV) Programming I Academic Year 2012-2013 2 / 60

  4. Conditionals More on Functions Recursion Outline Conditionals 1 More on Functions 2 Recursion 3 Lecture D.2. (MDV) Programming I Academic Year 2012-2013 2 / 60

  5. Conditionals More on Functions Recursion Resources How to Think Like a Computer Scientist: Python Version : Chapter 4 -5 http: //greenteapress.com/thinkpython/thinkCSpy/ Introduction to Programming http://eric_rollins. home.mindspring.com/introProgramming/ Introduction to Python http://www.slideshare.net/ amiable_indian/introduction-to-python http://osl.iu.edu/˜lums/swc/www/index.html Python Tutorial http://docs.python.org/tut/tut.html Lecture D.2. (MDV) Programming I Academic Year 2012-2013 3 / 60

  6. Conditionals Operators More on Functions Conditional Execution Recursion Outline Conditionals 1 Operators Conditional Execution More on Functions 2 Recursion 3 Lecture D.2. (MDV) Programming I Academic Year 2012-2013 4 / 60

  7. Conditionals Operators More on Functions Conditional Execution Recursion The Modulo Operator The modulo operator works on integers (and integer expressions) yields the remainder when the first operand is divided by the second. the modulo operator is a percent sign,%. ✞ if x % y is zero, then x is > quotient = 7/3 > > divisible by y > remainder = 7 % 3 > > x % 10 yields the > quotient > > 2 rightmost digit of x (in > remainder > > base 10) 1 ✝ ✆ ✡ x % 100 yields the last two digits Lecture D.2. (MDV) Programming I Academic Year 2012-2013 5 / 60

  8. Conditionals Operators More on Functions Conditional Execution Recursion Booleans True and False are true and false Empty string, 0, and None are equivalent to False (but not the same) Just as 3 is equivalent to 3.0 (Almost) everything else is true Combine Booleans using and, or, not and and or are short-circuit operators Evaluate expressions left to right, and stop as soon as they know the answer Lecture D.2. (MDV) Programming I Academic Year 2012-2013 6 / 60

  9. Conditionals Operators More on Functions Conditional Execution Recursion Booleans II Expression Result Notes True or False True True and False False or is true if either side is true, so it ’a’ or ’b’ ’a’ stops after evaluating ’a’ and is only true if both sides are ’a’ and ’b’ ’b’ true, so it doesn’t stop until it has evaluated ’b’ 0 or ’b’ ’b’ 0 is false, but ’b’ is true Since 0 is false, Python can stop 0 and ’b’ 0 evaluating there 1/0 would be an error, but Python 0 and (1/0) 0 never gets that far If x is true, this expression’s value is (x and ’set’) or ’not set’ It depends ’set’; if x is false, it is ’not set’ Lecture D.2. (MDV) Programming I Academic Year 2012-2013 7 / 60

  10. Conditionals Operators More on Functions Conditional Execution Recursion Comparisons I We can compare values or expressions that evaluate to numbers in the following way: Expression Value 3 < 5 True 3.0 < 5 True 3 ! = 5 True 3 == 5 False 3 < 5 < = 7 True 3 < 5 > = 2 True (but please don’t write this: it’s hard to read) All datatypes can be compared using == and != Lecture D.2. (MDV) Programming I Academic Year 2012-2013 8 / 60

  11. Conditionals Operators More on Functions Conditional Execution Recursion Comparisons II Note the difference between assignment and testing for equality Use a single equals sign = for assignment Use a double equals sign == to test if two things have equal values String comparison may not do what you expect: Characters are encoded as numbers: digits come before uppercase letters, all of which come before lowercase letters, with punctuation is mixed in between, just to make matters difficult Expression Value Strings are compared ’abc’ < ’def’ True character by character: ’abc’ < ’Abc’ False One character is less than ’ab’ < ’abc’ True another ’0’ < ’9’ True One string runs out of ’100’ < ’2’ True characters Lecture D.2. (MDV) Programming I Academic Year 2012-2013 9 / 60

  12. Conditionals Operators More on Functions Conditional Execution Recursion Conditionals Definition Conditional statements allow us to check certain conditions and change the behavior of the program accordingly. The simplest form is the if statement: ✞ i f x > 0: print ” x i s p o s i t i v e ” ✝ ✆ ✡ The expression between the if and the : is called the condition. If it is true, then the indented statement(s) get executed. If it is not true, nothing happens (the indented statements are skipped). Lecture D.2. (MDV) Programming I Academic Year 2012-2013 10 / 60

  13. Conditionals Operators More on Functions Conditional Execution Recursion Conditionals The condition can be any expression that evaluates to a Boolean boolean expressions comparisons functions with a boolean as a return value Lecture D.2. (MDV) Programming I Academic Year 2012-2013 11 / 60

  14. Conditionals Operators More on Functions Conditional Execution Recursion Compound Statements if statements, like function definitions, are compound statements. Their syntax is: ✞ The header begins on a new HEADER: line and ends with a colon. FIRST STATEMENT The indented statements that . . . follow are called a statement LAST STATEMENT ✝ ✆ block. ✡ The first unindented statement marks the end of the block All the statements in the block are treated as a unit. In the case of an if statement, either all of them or none of them are executed. Lecture D.2. (MDV) Programming I Academic Year 2012-2013 12 / 60

  15. Conditionals Operators More on Functions Conditional Execution Recursion Alternative execution A second form of the if statement is alternative execution, in which there are two possibilities, and the condition determines which one gets executed: ✞ i f x%2 == 0: print x , ” i s even ” else : print x , ” i s odd ” ✝ ✆ ✡ This code could be wrapped in a function: ✞ ✞ def p r i n t P a r i t y ( x ) : > p r i n t p a r i t y (17) > > i f x%2 == 0: 17 is odd . print x , ” i s even ” > y = 41 > > else : > p r i n t p a r i t y ( y+1) > > print x , ” i s odd ” 42 is even . ✝ ✆ ✝ ✆ ✡ ✡ Lecture D.2. (MDV) Programming I Academic Year 2012-2013 13 / 60

  16. Conditionals Operators More on Functions Conditional Execution Recursion Multiple branches Another name for conditional execution is conditional branching, because this type of control structure causes the flow of execution to branch off in different directions. if is extended to multiple multiple elif can also be branches with the elif (an repeated: abbreviation for ”else if”). ✞ i f choice == ’A ’ : ✞ functionA ( ) e l i f choice == ’B ’ : i f x < y : functionB ( ) print x , ” i s less than ” , y e l i f choice == ’C ’ : e l i f x > y : functionC ( ) print x , ” i s greater than ” , y e l i f choice == ’D ’ : else : functionD ( ) print x , ” and ” , y , ” are equal ” else : ✝ ✆ ✡ print ” I n v a l i d choice . ” ✝ ✆ ✡ Lecture D.2. (MDV) Programming I Academic Year 2012-2013 14 / 60

  17. Conditionals Operators More on Functions Conditional Execution Recursion Nested conditionals One conditional can also be nested inside another: ✞ i f x == y : print x , ” and ” , y , ” are equal ” else : i f x < y : print x , ” i s less than ” , y else : print x , ” i s greater than ” , y ✝ ✆ ✡ Lecture D.2. (MDV) Programming I Academic Year 2012-2013 15 / 60

  18. Conditionals Operators More on Functions Conditional Execution Recursion Return statements in Conditionals If you put return statements inside a conditional, then you have to guarantee that every possible path through the program hits a return statement. For example: ✞ def absoluteValue ( x ) : i f x < 0: return − x e l i f x > 0: return x # PROBLEM! ! ✝ ✆ ✡ If x is 0, the function will end with out hitting a return statement. The return value will instead be a special value called None: ✞ > print absoluteValue (0) > > None ✝ ✆ ✡ Lecture D.2. (MDV) Programming I Academic Year 2012-2013 16 / 60

Recommend


More recommend