1 / 23

Selection in Python

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

hang
Download Presentation

Selection in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Selection in Python If statements

  2. Control Structures • Sequence • Selection • Repetition • Module

  3. Selection • If or if / else statement • choosing between mutually exclusive possibilities • Two forms see next slide

  4. Selection • Two forms • if logical expression: statement • if logical expression: statement else: statement

  5. 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)

  6. 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

  7. Syntax of if statement • if x > 0: y = sqrt(x) • if x > y: t = x x = y y = t

  8. 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

  9. 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

  10. 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”

  11. 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

  12. 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!

  13. 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”

  14. 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

  15. Some Answers (taxRate > .25) and (income < 20000) (temperature <= 75) or (humidity < .70) (age > 21) and (age < 60) (age == 21) or (age == 22)

  16. 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

  17. 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”)

  18. 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

  19. 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

  20. Another form of nested if's • if condition: statement; elif condition: statement elif condition: statement else: statement # good for mutually exclusive and exhaustive #conditions

  21. 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”

  22. string myState; string yourState; myState = “Texas”; yourState = “Maryland”; Expression Value myState == yourState false myState > yourState true myState == “Texas” true myState < “texas” true

  23. 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!”)

More Related