#!/usr/bin/env python # coding: utf-8 # # Foundations of Computational Economics #5 # # by Fedor Iskhakov, ANU # # # ## Python essentials: control flow and functions # # # # # [https://youtu.be/KDOH2R-Ambk](https://youtu.be/KDOH2R-Ambk) # # Description: Flow control, user defined functions. Sieve of Eratosthenes example. # ### Plan for the lecture # # 1. Flow control # 1. User defined functions # # # 📖 Kevin Sheppard “Introduction to Python for Econometrics, Statistics # and Data Analysis.” *Chapters: 3, 5, 10, 11, 13, 18, 22* # ### Flow control: conditional expression and loops # # Branch the program depending on a condition # # - if # - if .. else # - if .. elif # - if .. elif .. else # # # **Remember about indentation!**- Should be 4 spaces according to PEP8 - Better not use Tab # #### Examples of if-then-else # In[1]: x = 2 y = 2.0 z = [1, 2.1, 3.0, 0.0] if y==2 and z[-1] >= 0.0: print("Condition 1") elif y<2: print("Condition 2") else: print("Condition 3") # In[2]: x = True y = False if x and y: print("Condition 1") elif x and not y: print("Condition 2") elif not x and y: print("Condition 3") elif not x and not y: print("Condition 4") else: print("Condition 5") # #### Pass statement # # Do nothing, but have correct indent # In[3]: if True: pass else: print("check") print("done") # #### Ternary conditional operator # In[4]: a, b, condition = 1, 3, True a if condition else b # expression, not a statement c = a if condition else b # therefore this is possible # #### Flow control: loops # # - for # - while # - break # - continue # - for .. else, while .. else # # # **Remember about indent**- Should be 4 spaces according to PEP8 - Better not use Tab # #### Examples of for # In[5]: for i in [0,1,2,3,4,5]: print("A iteration %d" % i) print() for i in range(5): print("B iteration %d" % i) # #### List comprehensions # In[6]: x = [] for i in range(15): if i%3==0: x.append("item %d"%i) x # In[7]: x = ["item %d"%i for i in range(15) if i%3==0] x # #### Multiple indexes in list comprehensions # In[8]: for i,j in zip(range(4),["a","b","c","d"]): print("i=%d j=%s"%(i,j)) # In[9]: p= [x**y for x in (2,3,5) for y in range(5)] #three power series p # #### For .. else # # Very useful to check if for loop did not **break** # In[10]: k=0 for i in range(100): if k>15: break if i%5==0: k+=1 else: print("i went all the way up to %d"%i) #only runs if loop completed print("loop complete with k=%d i=%d"%(k,i)) # #### While .. break .. continue example # # **Sieve of Eratosthenes** to find prime numbers up to a certain value # # Algorithm: # - initialize the list of primes with all integers # - go through a list of divisors (have to be in the list of primes) # - cross all candidates divisible by the considered divisor # - stop when all divisors up to the boundary are considered # # Exercise: how can the algorithm be improved? # In[11]: upper = 23 primes = list(range(1,upper+1)) # all numbers between 1 and upper divisor = 1 # initial divisor while True: divisor+=1 # next divisor if divisor>upper: # checked all divisors (?) break if not divisor in primes: continue # skip divisor which is not itself prime i=0 while i