#!/usr/bin/env python # coding: utf-8 # # 5 Iterations # Open In Colab # # - http://openbookproject.net/thinkcs/python/english3e/iteration.html # # ## Topics # - what is iteration/loop and why do we need it? # - types of loops and their syntaxes # - various keywords used with loops # - quick applications # # ## 5.1 Iterations / Loops # - iteratations are also called loops # - life is full of loops; everyday routines such as going to college, cooking food, slicing vegetables, etc. # - loops make computer repeatedly execute some block of code # - working with loop has prep work to start it and some condition to end it # - two types of loops: **for** and **while** # - some keywords that may appear in loops: # - **for, while, break, continue, in, for ... else, while... else** # # ### Slice a carrot # 1. get cutting board # - get knife # - get a carrot # - place carrot on cutting board # - lift knife # - advance carrot # - slice carrot # - if you see only your finger, then quit # - else go to step 4 # # # - steps 4-8 form a loop! # - steps 1-3 preparation # - step 8 checks condition to end the loop # - steps 4-7 are tasks that need to be iterated # # ## 5.2 for loop # - works with range of values # - syntax # ```python # for val in rangeofValues: # # loop body # ``` # - useful when you know exactly how many times the loop should be executed # - commonly used with built-in **range( )** function # - typically, variables **i**, **j**, etc. are used as loop counters # In[1]: # let's learn about range() help(range) # In[2]: # convert range into list of values # learn about list in later chapter list(range(10)) # In[3]: list(range(1, 11)) # In[4]: # let's provide a step of 2 list(range(1, 11, 2)) # In[5]: # print hello world some number of times! for i in range(1, 11): print(i, 'hello world') # ### [Visualize with PythonTutor.com](http://pythontutor.com/visualize.html#code=for%20i%20in%20range%281,%205%29%3A%0A%20%20%20%20print%28i,%20'hello%20world'%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) # In[1]: for num in range(20): print(num) print('done') # ### continue loop # - **continue** statement skips the rest of the current loop when continue is executed # In[7]: # for loop example - continue for i in range(1, 11): # range ... 1...10 if i%2 == 0: continue print(i) # ### [Visualize with PythonTutor.com](http://pythontutor.com/visualize.html#code=%23%20for%20loop%20example%20-%20continue%0Afor%20i%20in%20range%281,%2011%29%3A%20%23%20range%20...%201...10%0A%20%20%20%20if%20i%252%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20continue%0A%20%20%20%20print%28i%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) # # ### break loop # - **break** statement is used to break a loop # - breaks or exits the loop immidiately # - rest of the current loop and other loops will not be executed! # In[8]: # for loop example - break for i in range(10): if i == 10: break print(i) print('done') # ## 5.4 for ... else # - you can write an optional else statement with for loop # - the else clause executes when the loop completes normally # - this means that the loop did not encounter any **break** statement # In[5]: for i in range(5): if i == 2: continue print(i) else: print('end!') # In[6]: for i in range(5): if i == 2: break print(i) else: print('end!') # In[11]: # for... else example # write a program to test whether a given number is prime n = 97 #isPrime = True for i in range(2, n//2+1): if n%i == 0: #isPrime = False print(n, ' is not prime') break else: print(n, 'is prime') # ## 5.5 while loop # - while loop is used when you're not sure how many times the loop should be executed # - may execute 0 or many times! # - for loop and while loop can be used interchangeably # - syntax: # ``` python # while condition: # # loop body # ``` # In[12]: # infinite loop - will never stop while True: print('hello') # In[ ]: i = 1 while i <= 5: print(i, 'Hello World') i += 1 # ### [Visualize with PythonTutor.com](http://pythontutor.com/visualize.html#code=i%20%3D%201%0Awhile%20i%20%3C%3D%205%3A%0A%20%20%20%20print%28i,%20'Hello%20World'%29%0A%20%20%20%20i%20%2B%3D%201&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) # ### 5.6 While loop applications # - loops have many applications in coding # - i.e. a fundamental concept! # # ### input validation # - data types and values need to be often validated for correct results # - while loop can be used for input validation # In[7]: # not sure when the user will get this correct... # at least 1 try; could be more!! while True: number = input('Enter a whole number: ') if not number.isdigit(): print('Not a valid number!') continue number = int(number) break # In[10]: print(f'You entered {number}') # ### countdown # - run countdown as a script # - time.sleep(secs) # - os.system('clear'); os.name == 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos' # In[13]: # while loop - countdown import time import os def clearScreen(): if os.name == 'nt': os.system('cls') else: os.system('clear') i = 10 while i >= 1: print(i) time.sleep(1) # sleep for 1 second clearScreen() i -= 1 #time.sleep(1) print('Hapy new year!') # ## 5.7 while... else # - optional **else** clause executes when the loop the loop executes normally # - this means that the loop didn't encounter any **break** statement # In[14]: while True: print('Hello') break else: print("Look didn't encounter break") # In[15]: count = 1 while count < 5: print(count) count += 1 else: print("Look didn't encounter break") # ## 5.8 Exercises # # Exercise 1. # Write a program to count the number of digits in a positive integer # In[16]: n = 1234 print(len(str(n))) # Exercise 1.1. Convert Exercise 1 to a function and write at least two test cases. # In[17]: def countDigits(number): """ given a positive number the function return number of digits in the number """ pass # In[18]: # test cases def test_countDigits(): assert countDigits(10) == 2 assert countDigits(19923) == 5 assert countDigits(34324324327890) == 14 print('all test cases passed!') # Exercise 2. Write a program to count the number of digits 0 or 5 in a positive ingeger. # In[ ]: # solution 1 # converst positive integer into string and parse one character at a time n = 12345505 n = str(n) count0 = 0 count5 = 0 for c in n: if c == '0': count0 += 1 elif c == '5': count5 += 1 print('There are {} zero(s) and {} five(s) digits in {}.'.format(count0, count5, n)) # In[19]: # solution 2 # repeated divide the positive integer by 10 counting the value of remainder until the number is greater than 0 n = 12345505 savedN = n count0 = 0 count5 = 0 while n > 0: n, rem = divmod(n, 10) if rem == 0: count0 += 1 elif rem == 5: count5 += 1 print('There are {} zero(s) and {} five(s) digits in {}.'.format(count0, count5, savedN)) # Exercise 2.1. Convert Exercise 2 to a function and write at least 2 test cases # Exercise 3. FizzBuzz program - Write a program that prints numbers between 1 and 100 with the following requirements. If the number is divisible by 3, print 'Fizz'. If the number is divisible by 5, print 'Buzz'. If the number is divisible by both 3 and 5, print 'FizzBuzz'. # Exercise 3.1. Convert Exercise 3 into a function and write at least 2 three test cases. # Exercise 4. What are the output(s) of the following code snippets? # In[ ]: a = 11 while a < 10: print(a) a += 1 #break else: print('Done') # In[ ]: for i in range(1, 10): if i == 5: break print(i) else: print('done') # In[ ]: for c in 'hello': print(c, end=' ') # # Kattis problems # - almost every problem involves some form of loops! # In[ ]: