Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #%% Exercise on population 4_2
- """
- Assignment:
- 1 download file
- 2 calculate pop change P[d] 'Population delta'
- 3 calculate pop growth rate 'r' in % on yearly basis (see calculation below)
- r = P[d] / ( P[t-1] * t[d] )
- 4a print a table; headers & data
- 4b print a sequence of lines that read;
- - Time period (initial year (t0) – final year (t1)).
- - Initial population (million) at t0.
- - Population change (million) <-- your results.
- - Population growth rate (%) <-- your results.
- """
- file_name = "your file name.dat"
- file_path1 = "your file path"
- file_path2 = "your alternative file path"
- file_path_complete1 = file_path1 + file_name
- file_path_complete2 = file_path2 + file_name
- line_data = 0 # emty this string before starting
- tab_char = 0 # location of tab
- prev_line_read = str() # empty string befrore starting
- pop_list = [] # list of population
- year_list = [] # list of year
- pop_incr = 0 # population increase per timestep
- a = 0 # iteration value for making lists (used multiple times)
- p_incr_list = [] # list of population increase per timestep
- growth_rate = 0 # speaks for itself
- r_list = [] # list of growthrates
- #open file
- try:
- file_data = open(file_path_complete1, "r")
- except:
- try:
- file_data = open(file_path_complete2, "r")
- except:
- print("Open Failed!")
- # read data into list of population and year (and close file)
- while True:
- line_read = file_data.readline()
- try: # Check if line is data or something else (header or end)
- line_data = int(line_read[:1]) + 1
- tab_char = line_read.find("\t", 1)
- line_read_len = len(line_read) - 1
- population = int(line_read[tab_char + 1:line_read_len])
- pop_list.append(population)
- year = int(line_read[:tab_char])
- year_list.append(year)
- except:
- if line_read == "":
- break
- file_data.close()
- # print lists as check
- #print(year_list, "\n")
- #print(pop_list, "\n")
- #(step 2) make list of population increase per timestep
- for i in pop_list:
- if a != 0:
- b = a - 1
- pop_incr = (pop_list[a]) - (pop_list[b])
- p_incr_list.append(pop_incr)
- else:
- p_incr_list.append(0)
- a = a + 1
- # print list as check
- #print(p_incr_list, "\n")
- #(step 3) calculate growthrate 'r'
- a = 0
- for i in pop_list:
- if a != 0:
- b = a - 1
- growth_rate = p_incr_list[a] / ( pop_list[b] * ( year_list[a] - year_list[b] ) )
- growth_rate = round(growth_rate, 4)
- r_list.append(growth_rate)
- else:
- r_list.append(0)
- a = a + 1
- # print list as check
- #print(r_list, "\n")
- #(step 4a) print a table
- print("Year\tPopulation\tPop. increase\tGrowth rate")
- a = 0
- for i in pop_list:
- print(year_list[a], "\t", pop_list[a], "\t", p_incr_list[a], "\t", r_list[a])
- a = a + 1
- #(step 4b) print line sequence
- print("\n- Time period ", year_list[0], " to ", year_list[len(year_list)-1])
- population = round(float(pop_list[0])/1000,1)
- print("- Initial population", population, " (million) at year ", year_list[0])
- pop_change = round(float((pop_list[len(pop_list)-1] - pop_list[0]))/1000, 1)
- print("- Population change ", pop_change, " (million)")
- growth_rate = round(float( sum(p_incr_list) / ( pop_list[0] * ( year_list[len(year_list)-1] - year_list[0] ) ))*100,2)
- print("- Population growth rate ", growth_rate, "% (yearly average)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement