Download presentation
Presentation is loading. Please wait.
Published byLee Turner Modified over 9 years ago
1
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1
2
Lesson objectives 1. Use arithmetic and binary operators 2. Use the increment and decrement operators 3. Create loops using the for and while statements 4. Control loop execution using the break and continue statements 05/02/09 Python Mini-Course: Day 3 - Lesson 10 2
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) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 3
4
Binary (bitwise) operators AND OR XOR a&b a|b a^b Left shift Right shift Complement a >x ~a 05/02/09 Python Mini-Course: Day 3 - Lesson 10 4
5
Incrementing Updating the value of a variable by adding some value to the current value Example: x = 4 x = x + 3 05/02/09 Python Mini-Course: Day 3 - Lesson 10 5
6
Decrementing Updating the value of a variable by subtracting some value from the current value Example: x = 4 x = x - 3 05/02/09 Python Mini-Course: Day 3 - Lesson 10 6
7
Updating operators Python uses the augmented assignment operators: += Increment -= Decrement *= Update by multiplication /= Update by division 05/02/09 Python Mini-Course: Day 3 - Lesson 10 7
8
Examples x = 5 print x x += 4 print x x -= 3 print x x *= 15 print x x /= 5 print x 05/02/09 Python Mini-Course: Day 3 - Lesson 10 8
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) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 9
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 05/02/09 Python Mini-Course: Day 3 - Lesson 10 10
11
Example 1: blastoff2.py def countdown(n): while n > 0: print n n = n-1 print 'Blastoff!' countdown(10) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 11
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) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 12
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" 05/02/09 Python Mini-Course: Day 3 - Lesson 10 13
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 05/02/09 Python Mini-Course: Day 3 - Lesson 10 14
15
Example: break.py while True: line = raw_input('> ') if line == 'done': break print line print 'Done!' 05/02/09 Python Mini-Course: Day 3 - Lesson 10 15
16
The continue statement Used to restart a loop early Execution immediately goes back to the loop header 05/02/09 Python Mini-Course: Day 3 - Lesson 10 16
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) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 17
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) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.