1 / 28

Flow Control

Flow Control. Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA. 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.

yeva
Download Presentation

Flow Control

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. Flow Control Dr. Bernard Chen Ph.D. University of Central Arkansas July 9th 2012 CS4HS@UCA

  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 a=10 b=20 if a>b: print ("a is larger than b") else: print ("a is NOT larger than b")

  9. If... else statement a=10 b=20 if a>b: print ("a is larger than b") else: print (“b is larger than a")

  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=15 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 print("please guess a number (between 1~100)") guess = int(input()) if guess == number: print ('Congratulations, you guessed it.') # New block starts here elif guess < number: print ('No, it is a little higher than that') # Another 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

  15. Lists • Lists are Python’s most flexibleordered collection object type • Lists can contain any sort of object: numbers, strings and even other lists

  16. Lists >>> aa=[] #An empty list >>> aa=[1,2,3,4] #4 items, index 0-3 >>> aa [1,2,3,4] >>> aa[0] 1 >>> aa[3] 4

  17. For loop • The for loop is a generic sequence iterator in Python • It can step through the items in ANYordered sequence object

  18. For loop • The Python for loop begins with a header line that specifies an assignment target along with an object that you want to step through for <target> in <object>: <statement> • When Python runs a for loop, it assigns item in the sequence object to the target “one by one”, and then executes the loop body

  19. For loop • The name used as the assignment target in a for header line is usually a variable in the scope where the for statement is coded • After the loop, this variable normally still refers to the last item visited

  20. For loop examples • The name x is assigned to each of the three items in the list in turn, from left to right >>> aa=[1,2,3,4] >>> for i in aa: print (i) 1 2 3 4

  21. For loop examples >>> aa=[1,2,3,4] >>> sum=0 >>> for i in aa: sum = sum + i >>> sum 10

  22. For loop examples >>> aa=[1,2,3,4] >>> product=1 >>> for i in aa: product = product * i >>> product 24

  23. For loop examples • The name x is assigned to each of the three items in the list in turn, from left to right >>> s=“string in python” >>> for i in s: print i

  24. Counter loops: range • The range function is really independent of for loops; although it’s used most often to generate indexes in a for loop >>> range(5) [0, 1, 2, 3, 4]

  25. Counter loops: range • With one argument, range generates a list with integers from zero up to but NOT including the argument’s value >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  26. Counter loops: range • If you pass in two arguments, the first is taken as the lower bound >>> range(2,5) [2, 3, 4]

  27. range in for loop >>> sum=0 >>> for i in range(100): sum = sum + I >>> sum 4950 >>> product=1 >>> for i in range(5,10) product = product * 10 >>> product 15120

  28. Python Official Sites Python Tutorial • http://docs.python.org/tut/ Python Official Site • http://python.org/ Download Python Latest Version • http://python.org/download/

More Related