260 likes | 446 Views
Selection in Python. If statements. Control Structures. Sequence Selection Repetition Module. Selection. If or if / else statement choosing between mutually exclusive possibilities Two forms see next slide. Selection. Two forms if logical expression: statement
E N D
Selection in Python If statements
Control Structures • Sequence • Selection • Repetition • Module
Selection • If or if / else statement • choosing between mutually exclusive possibilities • Two forms see next slide
Selection • Two forms • if logical expression: statement • if logical expression: statement else: statement
Logical Expressions • Relational Operators ==, !=, <=, >=, >, < • They are binary operators – have two operands • work on any type of data, be careful about matching types • produce a bool result (True or False)
Syntax of if statement • if logical expression: statement • Indentation required • if expression is true, then statement is executed • if expression is false, statement is NOT executed
Syntax of if statement • if x > 0: y = sqrt(x) • if x > y: t = x x = y y = t
If-Then Statement Determine whether or not to execute a statement (which can be a single statement or an entire block) TRUE expression FALSE statement
Examples • output larger of two numbers • don't allow sqrt of negative number • don't allow overflow • determining even or odd • give student another chance at a question
Visual Aid • Decision tree • “if person is 18 or over and state is KY, they can have regular license” • “if person is 16 or over and under 18 and state is TN or VA, they can have learner’s permit” • “if person is under 16, in any state, they can’t have license” • “if person is over 65 in VA, they have modified license”
Logical Operators • and or not • and, or are binary operators, not is unary • used to combine bool values • produce a bool result • truth tables • operator precedence • not then and then or • not, and, or are very low, below other operators
Precedence of operators • See http://docs.python.org/py3k/reference/expressions.html#evaluation-order • Note that this table is “upside down”, lowest precedence is on top!
Converting English to logic • "0 is less than x is less than 5" • "x is 5 or 6" • "x is bigger than 5 and less than 10“ • 5 < x < 10 IS valid in Python! • impossible situations "dead code" • always true – “x < x + 1” “x > 5 or x < 8” • always false – “x < 5 and x > 10”
Write an expression for each taxRate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22
Some Answers (taxRate > .25) and (income < 20000) (temperature <= 75) or (humidity < .70) (age > 21) and (age < 60) (age == 21) or (age == 22)
Nested if's • the statement to be executed in an if statement can be another if • an else branch goes with the if which matches its indentation
what’s the difference? if a > 5: if b < 10: print(“green”) else: print(“blue”) • if a > 5: if b < 10: print(“green”) else: print(“blue”)
if .. else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clause else clause
Example • Given x, y on Cartesian plane, which quadrant is it in? (I, II, III, IV) if x > 0: if y > 0: quadrant = 1 else: quadrant = 4 else: if y > 0: # NOT a redundant test! quadrant = 2 else: quadrant = 3
Another form of nested if's • if condition: statement; elif condition: statement elif condition: statement else: statement # good for mutually exclusive and exhaustive #conditions
Comparing Strings • Two objects of type string (or a string object and a C string) can be compared using the relational operators • A character-by-character comparison is made using the ASCII character set values • If all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string • space < digits < uppercase < lowercase • “ “ < “0” < … < “9” < “A” < “B” < … < “Z” < “a” < … < “z”
string myState; string yourState; myState = “Texas”; yourState = “Maryland”; Expression Value myState == yourState false myState > yourState true myState == “Texas” true myState < “texas” true
Comparing Real Values Do not compare floating point values for equality, compare them for near-equality. a = 0 for i in range(5000000): a = a + 0.001; if abs(a – 5000) < 0.00001: print( “They are close enough!”)