1 / 18

Day 3 – Lesson 10 Iteration: Loops

Learn how to use arithmetic and binary operators, create loops using for and while statements, and control loop execution using break and continue statements.

Télécharger la présentation

Day 3 – Lesson 10 Iteration: Loops

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. Day 3 – Lesson 10Iteration: Loops Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 3 - Lesson 10

  2. Lesson objectives • Use arithmetic and binary operators • Use the increment and decrement operators • Create loops using the for and while statements • Control loop execution using the break and continue statements Python Mini-Course: Day 3 - Lesson 10

  3. Arithmetic operators add subtract multiply divide x+y x–y x*y x/y exponent floor divide modulus x**y x//y x%y negation absolute value -x abs(x) Python Mini-Course: Day 3 - Lesson 10

  4. Binary (bitwise) operators AND OR XOR a&b a|b a^b Left shift Right shift Complement a<<x a>>x ~a Python Mini-Course: Day 3 - Lesson 10

  5. Incrementing • Updating the value of a variable by adding some value to the current value • Example: x = 4 x = x + 3 Python Mini-Course: Day 3 - Lesson 10

  6. Decrementing • Updating the value of a variable by subtracting some value from the current value • Example: x = 4 x = x - 3 Python Mini-Course: Day 3 - Lesson 10

  7. Updating operators • Python uses the augmented assignment operators: += Increment -= Decrement *= Update by multiplication /= Update by division Python Mini-Course: Day 3 - Lesson 10

  8. Examples x = 5 print x x += 4 print x x -= 3 print x x *= 15 print x x /= 5 print x Python Mini-Course: Day 3 - Lesson 10

  9. Implicit type conversion • Just like the arithmetic operators, the update operators perform automatic type conversion x = 3 print x, type(x) x += 1.5 print x, type(x) Python Mini-Course: Day 3 - Lesson 10

  10. The while statement • Syntax while conditional: do_something • A general loop that executes code as long as the conditional statement is true • Exits loop when conditional is false Python Mini-Course: Day 3 - Lesson 10

  11. Example 1: blastoff2.py def countdown(n): while n > 0: print n n = n-1 print 'Blastoff!' countdown(10) Python Mini-Course: Day 3 - Lesson 10

  12. Example 2: sequence.py def sequence(n): while n != 1: print n, if n%2 == 0: # n is even n = n/2 else: # n is odd n = n*3+1 sequence(15) Python Mini-Course: Day 3 - Lesson 10

  13. Important notes • The while statement uses negative logic to express the stop condition: • "keep going until that happens" • Also, the loop can only terminate at the beginning of each iteration • Often we want positive logic: • "stop when this happens" Python Mini-Course: Day 3 - Lesson 10

  14. The break statement • Used to break out of a loop (for or while loop) early • Loop stops immediately and execution picks up at the next line outside the loop Python Mini-Course: Day 3 - Lesson 10

  15. Example: break.py while True: line = raw_input('> ') if line == 'done': break print line print 'Done!' Python Mini-Course: Day 3 - Lesson 10

  16. The continue statement • Used to restart a loop early • Execution immediately goes back to the loop header Python Mini-Course: Day 3 - Lesson 10

  17. Example: print_odd.py def print_odd(start=1, stop=1): for x in range(start, stop): if x % 2 == 0: continue print x print_odd(1,7) Python Mini-Course: Day 3 - Lesson 10

  18. Example: print_odd2.py def print_odd(start=1, stop=1): if start % 2 == 0: start += 1 for x in range(start, stop, 2): print x print_odd(4,10) Python Mini-Course: Day 3 - Lesson 10

More Related