180 likes | 222 Views
Learn how to use arithmetic and binary operators, create loops using for and while statements, and control loop execution using break and continue statements.
E N D
Day 3 – Lesson 10Iteration: Loops Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 3 - Lesson 10
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
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
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
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
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
Updating operators • Python uses the augmented assignment operators: += Increment -= Decrement *= Update by multiplication /= Update by division Python Mini-Course: Day 3 - Lesson 10
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
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
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
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
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
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
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
Example: break.py while True: line = raw_input('> ') if line == 'done': break print line print 'Done!' Python Mini-Course: Day 3 - Lesson 10
The continue statement • Used to restart a loop early • Execution immediately goes back to the loop header Python Mini-Course: Day 3 - Lesson 10
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
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