'''
Python version : 3.6
Python program to read the text file containing US population from
year 1950 to 1990
and calculate the change in population and its percent change and
calculate the average
population change, minimum and maximum population change and the
corresponding years
'''
# specify the file name
file = 'USPopulation.txt'
# empty list to contain the population from 1950 to 1990
population = []
# open the file
with open(file) as fp:
# read the contents of the file into the list with
each line being an element of the list
contents = fp.readlines()
fp.close() # close the file
# loop over the contents list
for line in contents:
# strip is used to remove leading
and trailing whitespace from line
# int is used to convert string to
integer and multiply the result by 1000
population.append(int(line.strip())*1000)
# initialize the variables for average, maximum and
minimum
total_population_change = 0
min_population_change = -1
max_popuation_change = -1
min_pop_change_year = 0
max_pop_change_year = 0
# display the header
print('%-10s%-15s%-15s%-s'
%('Year','Population','Change','Percent Change'))
year = 1950 # start year
# loop over the population list
for i in range(len(population)):
# if first record
if i==0:
print('%-10d%-15d%-15s%-s' %(year,population[i],'N/A','N/A'))
else: # for rest of the
records
change =
population[i] - population[i-1] # calculate the change in
population
change_percent
=float(change*100)/(population[i-1]) # calculate percent
change
# display the
record
print('%-10d%-15d%-15d%-.2f%-s'
%(year,population[i],change,change_percent,'%'))
total_population_change += change # add the change to
total_population_change
# determine if
change is the minimum population change
if
min_population_change == -1 or min_population_change >
change:
min_population_change = change
min_pop_change_year = year
# determine if
change is the maximum population change
if
max_popuation_change == -1 or max_popuation_change <
change:
max_popuation_change = change
max_pop_change_year = year
year += 1
# calculate the average population change
avg_change =
float(total_population_change)/(len(population)-1)
# display the average, minimum and maximum population
change
print('\nAverage Change = %.1f' %avg_change)
print('Minimum Change = %d (%d)'
%(min_population_change,min_pop_change_year))
print('Maximum Change = %d (%d)'
%(max_popuation_change,max_pop_change_year))
#end of program
Code Screenshot:
Output:
Input file:
Output:
the language is in Python please help Instructions from your teacher. Instructions The US Population.txt file...
In Python Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Compute the annual change in the population, and the year with the maximum and minimum population change. Using Anaconda, plot a graph for each decade's population change. The attached file USPopulation.txt contains the US population in thousands, during the years 1950 through 1990. The first...