Skip to content

Instantly share code, notes, and snippets.

@zenwattage
Last active June 13, 2017 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenwattage/8d2b3150fe8311309c3babe850d9a3ef to your computer and use it in GitHub Desktop.
Save zenwattage/8d2b3150fe8311309c3babe850d9a3ef to your computer and use it in GitHub Desktop.
World pop
#8 - World Population
#Program that reads data from a file and shows a report to the user.
#open and read datafile
datafile = open('WorldData2012.txt','r')
countrylist = []
population = []
area = []
line = datafile.readline()
denselist = []
while line != '':
datalist = line.split(',')
countrylist.append(datalist[0].strip())
population.append(int(datalist[1].strip()))
area.append(float(datalist[2].strip()))
line = datafile.readline()
#display data
print('*'*50)
print('*** This program will display World Data ***')
print('-'*50)
#total countries in the list
print("Total number of countries is:",len(countrylist))
#total world population
#sum of population
print("The total world population is: ",sum(population))
print("The lowest population is:",min(population),"and the lowest area is:",min(area))
#name and pop of the country with highest pop
#find element with max pop
#if element has max value - store which index
#get item with that index
maxpop = 0
minpop = 99999999999999999999999
indexmax = -1
indexmin = -1
maxarea = 0
minarea = 99999999999999999999
indexmaxarea = -1
indexminarea = -1
#get min and max population
for i in range(len(population)):
if population[i]> maxpop:
maxpop = population[i]
indexmax = i
if population[i] < minpop:
minpop = population[i]
indexmin = i
print(countrylist[indexmax] +" has the highest population with: ", + population[indexmax],"people.")
#name and country with lowest population
print(countrylist[indexmin] +" has the lowest population with: ", + population[indexmin],"people.")
#the name and land area of the country that has the greatest land area
#area calc
for i in range(len(area)):
if area[i]> maxarea:
maxarea = area[i]
indexmaxarea = i
#
if area[i]< minarea:
minarea = area[i]
indexminarea = i
print("The",countrylist[indexmaxarea],"has the greatest land area with: ",+ area[indexmaxarea],"sq.km")
print("The",countrylist[indexminarea],"has the lowest land area with: ", + area[indexminarea],"sq.km")
#the name and population density of the country that has the highest population density
denselist = [0]* len(countrylist)
for i in range(len(countrylist)-1):
denselist[i] = population[i]/ area[i]
#i'm stuck in both finding the average density and figuring out how to find which country[] index is associated with
#the density number
#testing results
print(max(denselist),"people/sq.km")
print(min(denselist[:-1]),"people/sq.km")
'''
• the name and population density of the country that has the lowest population density
• average population density (the average of the population densities of all the countries, not the population density of the planet as a whole)
• a list of "densely populated countries" (those with a population density that is more than two times the average)
• a list of "very sparsely populated countries" (those with a population density that is less than 1% of the average)
'''
'''
Example for Testing
Total number of countries is 229
Total world population is 6950773863.000000 people
China has the highest population - 1347565324 people
Holy See has the lowest population - 459 people
Russian Federation has the greatest land area - 17075200.00 sq.km
Holy See has the smallest land area - 0.44 sq.km
Macao SAR has the highest population density - 19847.54 people/sq.km
Greenland has the lowest population density - 0.03 people/sq.km
****************************************
Average population density is 398.70
----------------------------------------
A list of densely populated countries (people/sq.km)
(those with a population density that is more than two times the average)
----------------------------------------
Bahrain 1990.28
Bangladesh 1045.09
Bermuda 1227.87
Gibraltar 4302.06
Holy See 1043.18
Hong Kong SAR 6522.15
Macao SAR 19847.54
Maldives 1066.94
Malta 1322.33
Monaco 17713.50
Singapore 7486.19
****************************************
A list of sparsely populated countries (people/sq.km)
(those with a population density that is less than 1% of the average
----------------------------------------
Australia 2.94
Botswana 3.38
Canada 3.44
Falkland Islands (Malvinas) 0.25
French Guiana 2.84
Greenland 0.03
Guyana 3.52
Iceland 3.15
Libyan Arab Jamahiriya 3.65
Mauritania 3.44
Mongolia 1.79
Namibia 2.82
Suriname 3.24
Western Sahara 2.06
'''
Afghanistan,32358260,647500
Albania,3215988,28748
Algeria,35980193,2381740
American Samoa,69543,200
Andorra,86165,468
Angola,19618432,1246700
Anguilla,15645,102
Antigua and Barbuda,89612,443
Argentina,40764561,2766890
Armenia,3100236,29800
Aruba,108141,193
Asc. and Tristan and Saint Helena,4055,413
Australia,22605732,7686850
Austria,8413429,83870
Azerbaijan,9306023,86600
Bahamas,347176,13940
Bahrain,1323535,665
Bangladesh,150493658,144000
Barbados,273925,431
Belarus,9559441,207600
Belgium,10754056,30528
Belize,317928,22966
Benin,9099922,112620
Bermuda,65077,53
Bhutan,738267,47000
Bolivarian Republic of Venezuela,29436891,912050
Bosnia and Herzegovina,3752228,51129
Botswana,2030738,600370
Brazil,196655014,8511965
British Virgin Islands,23478,153
Brunei Darussalam,405938,5770
Bulgaria,7446135,110910
Burkina Faso,16967845,274200
Burundi,8575172,27830
Cambodia,14305183,181040
Cameroon,20030362,475440
Canada,34349561,9984670
Cape Verde,500585,4033
Cayman Islands,56729,259
Central African Republic,4486837,622984
Chad,11525496,1284000
Channel Islands,153876,194
Chile,17269525,756950
China,1347565324,9596960
Colombia,46927125,1138910
Comoros,753943,2170
Congo,4139748,342000
Cook Islands,20414,237
Costa Rica,4726575,51100
Cote d'Ivoire,20152894,322460
Croatia,4395560,56542
Cuba,11253665,110860
Cyprus,1116564,9250
Czech Republic,10534293,78866
Dem. People's Republic of Korea,24451285,120540
Democratic Republic of the Congo,67757577,2345410
Denmark,5572594,43094
Djibouti,905564,23000
Dominica,67675,754
Dominican Republic,10056181,48730
Ecuador,14666055,283560
Egypt,82536770,1001450
El Salvador,6227491,21040
Equatorial Guinea,720213,28051
Eritrea,5415280,121320
Estonia,1340537,45226
Ethiopia,84734262,1127127
Faeroe Islands,48863,1399
Falkland Islands (Malvinas),3027,12173
Fed. States of Micronesia,111542,702
Fiji,868406,18270
Finland,5384770,338145
France,63125894,643427
French Guiana,237080,83534
French Polynesia,273777,4167
Gabon,1534262,267667
Gambia,1776103,11300
Georgia,4329026,69700
Germany,82162512,357021
Ghana,24965816,239460
Gibraltar,29254,6.8
Greece,11390031,131940
Greenland,57296,2166086
Grenada,104890,344
Guadeloupe,463069,1628
Guam,182111,541
Guatemala,14757316,108890
Guinea,10221808,245857
Guinea-Bissau,1547061,36120
Guyana,756040,214970
Haiti,10123787,27750
Holy See,459,0.44
Honduras,7754687,112090
Hong Kong SAR,7122187,1092
Hungary,9966116,93030
Iceland,324366,103000
India,1241491960,3287590
Indonesia,242325638,1919440
Iraq,32664942,437072
Ireland,4525802,70280
Islamic Republic of Iran,74798599,1648000
Isle of Man,83327,572
Israel,7562194,20770
Italy,60788694,301230
Jamaica,2751273,10991
Japan,126497241,377835
Jordan,6330169,92300
Kazakhstan,16206750,2717300
Kenya,41609728,582650
Kiribati,101093,811
Kuwait,2818042,17820
Kyrgyzstan,5392580,198500
Lao People's Democratic Republic,6288037,236800
Latvia,2243142,64589
Lebanon,4259405,10400
Lesotho,2193843,30355
Liberia,4128572,111370
Libyan Arab Jamahiriya,6422772,1759540
Liechtenstein,36304,160
Lithuania,3307481,65200
Luxembourg,515941,2586
Macao SAR,555731,28
Macedonia,2063893,25333
Madagascar,21315135,587040
Malawi,15380888,118480
Malaysia,28859154,329750
Maldives,320081,300
Mali,15839538,1240000
Malta,417855,316
Marshall Islands,54816,181
Martinique,407121,1128
Mauritania,3541540,1030700
Mauritius,1306593,2040
Mayotte,210552,374
Mexico,114793341,1972550
Monaco,35427,2
Mongolia,2800114,1564116
Montenegro,632261,14026
Montserrat,5990,102
Morocco,32272974,446550
Mozambique,23929708,801590
Myanmar,48336763,678500
Namibia,2324004,825418
Nauru,10308,21
Nepal,30485798,147181
Netherlands,16664746,41526
Netherlands Antilles,202847,800
New Caledonia,254816,19060
New Zealand,4414509,268680
Nicaragua,5869859,129494
Niger,16068994,1267000
Nigeria,162470737,923768
Niue,1426,260
Northern Mariana Islands,61174,477
Norway,4924848,323802
Oman,2846145,212460
Pakistan,176745364,803940
Palau,20609,458
Palestinian Territory,4152369,6220
Panama,3571185,78200
Papua New Guinea,7013829,462840
Paraguay,6568290,406750
Peru,29399817,1285220
Philippines,94852030,300000
Plurinational State of Bolivia,10088108,1098580
Poland,38298949,312685
Portugal,10689663,92391
Puerto Rico,3745526,13790
Qatar,1870041,11437
Republic of Korea,48391343,98480
Republic of Moldova,3544864,33843
Reunion,855849,2504
Romania,21436495,237500
Russian Federation,142835555,17075200
Rwanda,10942950,26338
Saint Kitts and Nevis,53051,261
Saint Lucia,176044,616
Saint Pierre and Miquelon,6041,242
Saint Vincent and the Grenadines,109365,389
Samoa,183874,2944
San Marino,31735,61
Sao Tome and Principe,168526,1001
Saudi Arabia,28082541,2149690
Senegal,12767556,196190
Serbia,9853969,77474
Seychelles,86879,455
Sierra Leone,5997486,71740
Singapore,5187933,693
Slovakia,5471502,48845
Slovenia,2035012,20273
Solomon Islands,552267,28450
Somalia,9556873,637657
South Africa,50459978,1219912
Spain,46454895,504782
Sri Lanka,21045394,65610
Sudan,44632406,2505810
Suriname,529419,163270
Swaziland,1203330,17363
Sweden,9440747,449964
Switzerland,7701690,41290
Syrian Arab Republic,20766037,185180
Tajikistan,6976958,143100
Thailand,69518555,514000
Timor-Leste,1153834,15007
Togo,6154813,56785
Tokelau,1134,10
Tonga,104509,748
Trinidad and Tobago,1346350,5128
Tunisia,10594057,163610
Turkey,73639596,780580
Turkmenistan,5105301,488100
Turks and Caicos Islands,39184,430
Tuvalu,9847,26
Uganda,34509205,236040
Ukraine,45190180,603700
United Arab Emirates,7890924,83600
United Kingdom,62417431,244820
United Republic of Tanzania,46218486,945087
United States of America,313085380,9826630
United States Virgin Islands,108846,1910
Uruguay,3380008,176220
Uzbekistan,27760267,447400
Vanuatu,245619,12200
Viet Nam,88791996,329560
Wallis and Futuna Islands,13450,274
Western Sahara,548377,266000
Yemen,24799880,527970
Zambia,13474959,752614
Zimbabwe,12754378,390580
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment