1 / 14

Chapter 9 IF Statement

Chapter 9 IF Statement. Bernard Chen. If Statement. The main statement used for selecting from alternative actions based on test results It’s the primary selection tool in Python and represents the Logic process. Some Logic Expression. Equal: “==” NOT Equal: “!=” Greater: “>”, “>=”

Download Presentation

Chapter 9 IF Statement

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. Chapter 9 IF Statement Bernard Chen

  2. If Statement • The main statement used for selecting from alternative actions based on test results • It’s the primary selection tool in Python and represents the Logic process

  3. Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”

  4. Outline • Simple If statement • If... else statement • If… else if … else statement

  5. Simple If statement • It takes the form of an if test • if <test>: <statement>

  6. Simple If statement • if 4>3: print “it is true that 4>3” • if 1: print “true” • a=10 • if a==10: print “a=10”

  7. If... else statement • It takes the form of an if test, and ends with an optional else block • if <test>: <statement1> else: <statement2>

  8. If... else statement • if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” • if 0: print “true” else: print “false”

  9. If... else statement • a=10 • if a==10: print “a=10” else: print “a is not equal to 10” • a=10 • if a!=10: print “a=10” else: print “a is not equal to 10” (also try >, < )

  10. If… else if … else statement • It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if <test1>: <statement1> elif <test2>: <statement2> else: <statement3>

  11. If… else if … else statement • a=10 • if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

  12. If… else if … else statement example number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block ... else: print 'No, it is a little lower than that' # you must have guess > number to reach here

  13. Some More Logic Expression • and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False

  14. Some More Logic Expression • or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True

More Related