#!/usr/bin/env python # coding: utf-8 # # Iteration/Loop # 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 # - 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 # - 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! # # # # ## 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 # In[12]: for i in range(1, 11): print(i, 'hello world') # In[18]: for num in range(0, 100): if num == 10: continue # exit the loop print(num+1) print('done') # In[ ]: # for loop example - range() for i in range(1, 21): # range ... 1...20 if i%2 == 0: continue print(i) # In[ ]: # for loop example - range() for i in range(1, 21): if i%2 == 0: #break continue print(i) else: print('end') # In[ ]: help(range) # ## for ... else # - The else clause executes when the loop completes normally. # - This means that the loop did not encounter any break. # In[ ]: # for... else example # write a program to test whether a given number is prime n = 50 #isPrime = True for i in range(2, n): if n%i == 0: #isPrime = False print(n, ' is not prime') break else: print(n, 'is prime') # ## while loop # - while loop is used when we typically not sure how many times the loop should be executed # - syntax: # ``` python # while condition: # # loop body # ``` # - demonstrate countdown as a script # - time.sleep(secs) # - os.system(‘clear’); os.name - 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos' # # In[ ]: # while loop - countdown import time import os i = 10 while i >= 1: print(i) time.sleep(1) os.system('clear') i -= 1 #time.sleep(1) else: print('Hapy new year!') # ## while... else # - else clause executes when the loop condition becomes false, i.e., the loop executes normally # - this means that the loop didn't encounter any break # ## exercise 1 # Write a program to count the number of digits in a positive integer # In[ ]: n = 1234 print(len(str(n))) # ## exercise 2 # Write a program to count the number of digits 0 or 5 in a positive ingeger. # In[ ]: n = 123450 n =str(n) for c in n: print(c) # ## 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 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=' ')