## Exercise 2.9
birth_year = 37
death_year = 68
reign_start = 54
regin_stop = 64
live_lenght = death_year - birth_year
regin_lenght = regin_stop - reign_start
print( "Nero was in power for ", regin_lenght / live_lenght, "of his life")
Nero was in power for 0.3225806451612903 of his life
## 2.11
price = 250000
area = 75
cost_per_square_meter = price / area
print( cost_per_square_meter )
3333.3333333333335
## 2.12
text = "Coding Social Science is fun."
print( text.lower() )
print( text.upper() )
coding social science is fun. CODING SOCIAL SCIENCE IS FUN.
## 2.13
text = "I'm starting to do and understand computational social science."
print( text[0] )
print( text[19] )
print( text[-1] )
I a .
## 2.14
for i in range(1, 6):
print( i )
for i in range(1, 51):
print( i )
for i in range(1, 101):
print( i )
for i in range(50, 101):
print( i )
1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
## 2.15
for line in open ("emperors_small.csv"):
line = line.split(",")
name = line [0]
birth_year = float( line [1] )
death_year = float( line [2] )
start_of_reign = float( line [3] )
end_of_reign = float( line [4] )
age = death_year - birth_year
print( age )
37.0 54.0 70.0 42.0 45.0 68.0 64.0 62.0 75.0 59.0 31.0 67.0 60.0 66.0 29.0
## 2.16
for line in open ("emperors_small.csv"):
line = line.split(",")
name = line [0]
birth_year = float( line [1] )
death_year = float( line [2] )
start_of_reign = float( line [3] )
end_of_reign = float( line [4] )
life_lenghth = death_year - birth_year
regin_length = end_of_reign - start_of_reign
print( regin_length / life_lenghth )
0.0 0.0 0.14285714285714285 0.047619047619047616 0.3333333333333333 0.029411764705882353 0.296875 0.3387096774193548 0.30666666666666664 0.3220338983050847 0.0 0.0 0.0 0.2727272727272727 0.6551724137931034
## 2.17
## step 1: logic of fizzbuzz
number = 5
if number % 5 == 0 and number % 3 == 0:
print("fizzbuzz")
else:
if number % 5 == 0:
print("buzz")
else:
if number % 3 == 0:
print("fizz")
else:
print( number )
## step 2: repeat command when correct
for number in range(1, 101):
if number % 5 == 0 and number % 3 == 0:
print("fizzbuzz")
else:
if number % 5 == 0:
print("buzz")
else:
if number % 3 == 0:
print("fizz")
else:
print( number )
buzz 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz
## 2.18
## step 1: logic of fizzbuzz
year = 1999
if year % 100 == 0:
if year % 400 == 0: ## if it is divisible by 100, it is a leap year only if it can also be divided by 400.
print( year, "is leap year")
else:
print( year, "is not a leap year")
else:
if year % 4 == 0: ## A year is a leap year if it can be divided by 4.
print( year, "is leap year")
else:
print( year, "is not a leap year")
1999 is not a leap year
## 2.19
for year in range(1990, 2051):
if year % 100 == 0:
if year % 400 == 0: ## if it is divisible by 100, it is a leap year only if it can also be divided by 400.
print( year )
else:
if year % 4 == 0: ## A year is a leap year if it can be divided by 4.
print( year )
1992 1996 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048
## 2.20
most_wanted_holder = 0
for line in open("numbers.txt"):
line = float( line )
if line > most_wanted_holder:
most_wanted_holder = line
print("The highest number is", most_wanted_holder )
The highest number is 9.0
## 2.21
gatherer = 0
for line in open("numbers.txt"):
line = float( line )
gatherer = gatherer + line
print("The sum of the numbers is", gatherer )
The sum of the numbers is 36.0
## 2.22
flag = False
looking_for = 42
for line in open("numbers.txt"):
line = float( line )
if line == looking_for:
flag = True
if flag == True:
print("The file contains the value", looking_for )
else:
print("The file does not contain the value", looking_for )
The file does not contain the value 42
## 2.23
most_wanted_holder = 0
most_wanted_follower = 0
for line in open("numbers.txt"):
line = float( line )
if line > most_wanted_holder:
most_wanted_follower = most_wanted_holder
most_wanted_holder = line
print("The second highest number is", most_wanted_follower )
The second highest number is 7.0
## 2.24
age_most_wanted = 999 ## large number
name_most_wanted = "" ## this is a followe variable
for line in open ("emperors_small.csv"):
line = line.split(",")
name = line [0]
birth_year = float( line [1] )
death_year = float( line [2] )
age = death_year - birth_year
if age < age_most_wanted:
age_most_wanted = age
name_most_wanted = name
print( "Youngest person to become Roman emperor was", name )
Youngest person to become Roman emperor was Caracalla
## 2.35
votes_a1 = 300
votes_a2 = 750
votes_a3 = 345
votes_a4 = 105
votes_a = votes_a1 + votes_a2 + votes_a3 + votes_a4
votes_b1 = 1000
votes_b2 = 300
votes_b = votes_b1 + votes_b2
votes_c1 = 301
votes_c2 = 300
votes_c3 = 299
votes_c = votes_c1 + votes_c2 + votes_c3
votes_d1 = 202
votes_d2 = 201
votes_d3 = 198
votes_d4 = 199
votes_d = votes_d1 + votes_d2 + votes_d3 + votes_d4
print( votes_a , votes_b, votes_c, votes_d )
1500 1300 900 800
## 2.26
total_votes = votes_a + votes_b + votes_c + votes_d
print( votes_a / total_votes )
print( votes_b / total_votes )
print( votes_c / total_votes )
print( votes_d / total_votes )
0.3333333333333333 0.28888888888888886 0.2 0.17777777777777778
## 2.27
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
## 2.28
total_votes_gatherer = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
total_votes_gatherer = total_votes_gatherer + votes
print( total_votes_gatherer )
96589152.0
## 2.29
party_b_votes_gatherer = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if party == "PartyB":
party_b_votes_gatherer = party_b_votes_gatherer + votes
print( party_b_votes_gatherer )
21572206.0
## 2.30
votes_gatherer = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if name.endswith('a'):
votes_gatherer = votes_gatherer + votes
print( votes_gatherer )
20154016.0
## 2.31
votes_A_gatherer = 0
votes_K_gatherer = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if name.startswith('A'):
votes_A_gatherer = votes_A_gatherer + votes
if name.startswith('K'):
votes_K_gatherer = votes_K_gatherer + votes
print( votes_A_gatherer, votes_K_gatherer )
print( votes_A_gatherer / votes_K_gatherer ) ## relative voute counts
14500641.0 6369396.0 2.2766116284809423
## 2.32
candidates_before_1980_collector = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if birth_year < 1980:
candidates_before_1980_collector = candidates_before_1980_collector + 1
print( candidates_before_1980_collector )
113
## 2.33
votes_before_1980_collector = 0
votes_after_1980_collector = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if birth_year < 1980:
votes_before_1980_collector = votes_before_1980_collector + votes
else:
votes_after_1980_collector = votes_after_1980_collector + votes
if votes_before_1980_collector > votes_after_1980_collector:
print("People born before 1980 recieved more votes")
else:
print("People born 1980 or after recieved more votes")
People born before 1980 recieved more votes
## 2.34
votes_before_1980_collector = 0
votes_after_1980_collector = 0
candidates_before_1980_collector = 0
candidates_after_1980_collector = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if birth_year < 1980:
votes_before_1980_collector = votes_before_1980_collector + votes
candidates_before_1980_collector = candidates_before_1980_collector + 1
else:
votes_after_1980_collector = votes_after_1980_collector + votes
candidates_after_1980_collector = candidates_after_1980_collector + 1
if votes_before_1980_collector / candidates_before_1980_collector > votes_after_1980_collector / candidates_after_1980_collector:
print("People born before 1980 recieved more votes")
else:
print("People born 1980 or after recieved more votes")
People born 1980 or after recieved more votes
## 2.35
most_voted_candidate_votes = 0
most_voted_candidate_name = ''
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if party == 'PartyB':
if votes > most_voted_candidate_votes:
most_voted_candidate_votes = votes
most_voted_candidate_name = name
print( most_voted_candidate_name, most_voted_candidate_votes )
Armo Pautamo 895747.0
## 2.37
party_b_candidate_count = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if party == 'PartyB':
party_b_candidate_count = party_b_candidate_count + 1
print( party_b_candidate_count )
46
## 2.38
candidate_count = party_b_candidate_count
party_votes = party_b_votes_gatherer
for i in range( 1, candidate_count ):
print( "Candidate", i, party_votes / i )
Candidate 1 21572206.0 Candidate 2 10786103.0 Candidate 3 7190735.333333333 Candidate 4 5393051.5 Candidate 5 4314441.2 Candidate 6 3595367.6666666665 Candidate 7 3081743.714285714 Candidate 8 2696525.75 Candidate 9 2396911.777777778 Candidate 10 2157220.6 Candidate 11 1961109.6363636365 Candidate 12 1797683.8333333333 Candidate 13 1659400.4615384615 Candidate 14 1540871.857142857 Candidate 15 1438147.0666666667 Candidate 16 1348262.875 Candidate 17 1268953.294117647 Candidate 18 1198455.888888889 Candidate 19 1135379.2631578948 Candidate 20 1078610.3 Candidate 21 1027247.9047619047 Candidate 22 980554.8181818182 Candidate 23 937922.0 Candidate 24 898841.9166666666 Candidate 25 862888.24 Candidate 26 829700.2307692308 Candidate 27 798970.5925925926 Candidate 28 770435.9285714285 Candidate 29 743869.1724137932 Candidate 30 719073.5333333333 Candidate 31 695877.6129032258 Candidate 32 674131.4375 Candidate 33 653703.2121212122 Candidate 34 634476.6470588235 Candidate 35 616348.7428571428 Candidate 36 599227.9444444445 Candidate 37 583032.5945945946 Candidate 38 567689.6315789474 Candidate 39 553133.4871794871 Candidate 40 539305.15 Candidate 41 526151.3658536585 Candidate 42 513623.95238095237 Candidate 43 501679.20930232556 Candidate 44 490277.4090909091 Candidate 45 479382.35555555555
## 2.39
party_a_votes = 0
party_b_votes = 0
party_c_votes = 0
party_d_votes = 0
party_a_candidates = 0
party_b_candidates = 0
party_c_candidates = 0
party_d_candidates = 0
for line in open ("elections.txt"):
line = line.split(",")
name = line[0]
party = line[1]
votes = float( line[2] )
birth_year = float( line [3] )
if party == 'PartyA':
party_a_votes = party_a_votes + votes
party_a_candidates = party_a_candidates + 1
if party == 'PartyB':
party_b_votes = party_b_votes + votes
party_b_candidates = party_b_candidates + 1
if party == 'PartyC':
party_c_votes = party_c_votes + votes
party_c_candidates = party_c_candidates + 1
if party == 'PartyD':
party_d_votes = party_d_votes + votes
party_d_candidates = party_d_candidates + 1
for i in range( 1, party_a_candidates ):
print( "Party A, candidate", i, party_a_votes / i )
for i in range( 1, party_b_candidates ):
print( "Party B, candidate", i, party_b_votes / i )
for i in range( 1, party_c_candidates ):
print( "Party C, candidate", i, party_c_votes / i )
for i in range( 1, party_d_candidates ):
print( "Party D, candidate", i, party_d_votes / i )
Party A, candidate 1 19547763.0 Party A, candidate 2 9773881.5 Party A, candidate 3 6515921.0 Party A, candidate 4 4886940.75 Party A, candidate 5 3909552.6 Party A, candidate 6 3257960.5 Party A, candidate 7 2792537.5714285714 Party A, candidate 8 2443470.375 Party A, candidate 9 2171973.6666666665 Party A, candidate 10 1954776.3 Party A, candidate 11 1777069.3636363635 Party A, candidate 12 1628980.25 Party A, candidate 13 1503674.076923077 Party A, candidate 14 1396268.7857142857 Party A, candidate 15 1303184.2 Party A, candidate 16 1221735.1875 Party A, candidate 17 1149868.4117647058 Party A, candidate 18 1085986.8333333333 Party A, candidate 19 1028829.6315789474 Party A, candidate 20 977388.15 Party A, candidate 21 930845.8571428572 Party A, candidate 22 888534.6818181818 Party A, candidate 23 849902.7391304348 Party A, candidate 24 814490.125 Party A, candidate 25 781910.52 Party A, candidate 26 751837.0384615385 Party A, candidate 27 723991.2222222222 Party A, candidate 28 698134.3928571428 Party A, candidate 29 674060.7931034482 Party A, candidate 30 651592.1 Party A, candidate 31 630573.0 Party A, candidate 32 610867.59375 Party A, candidate 33 592356.4545454546 Party A, candidate 34 574934.2058823529 Party A, candidate 35 558507.5142857142 Party B, candidate 1 21572206.0 Party B, candidate 2 10786103.0 Party B, candidate 3 7190735.333333333 Party B, candidate 4 5393051.5 Party B, candidate 5 4314441.2 Party B, candidate 6 3595367.6666666665 Party B, candidate 7 3081743.714285714 Party B, candidate 8 2696525.75 Party B, candidate 9 2396911.777777778 Party B, candidate 10 2157220.6 Party B, candidate 11 1961109.6363636365 Party B, candidate 12 1797683.8333333333 Party B, candidate 13 1659400.4615384615 Party B, candidate 14 1540871.857142857 Party B, candidate 15 1438147.0666666667 Party B, candidate 16 1348262.875 Party B, candidate 17 1268953.294117647 Party B, candidate 18 1198455.888888889 Party B, candidate 19 1135379.2631578948 Party B, candidate 20 1078610.3 Party B, candidate 21 1027247.9047619047 Party B, candidate 22 980554.8181818182 Party B, candidate 23 937922.0 Party B, candidate 24 898841.9166666666 Party B, candidate 25 862888.24 Party B, candidate 26 829700.2307692308 Party B, candidate 27 798970.5925925926 Party B, candidate 28 770435.9285714285 Party B, candidate 29 743869.1724137932 Party B, candidate 30 719073.5333333333 Party B, candidate 31 695877.6129032258 Party B, candidate 32 674131.4375 Party B, candidate 33 653703.2121212122 Party B, candidate 34 634476.6470588235 Party B, candidate 35 616348.7428571428 Party B, candidate 36 599227.9444444445 Party B, candidate 37 583032.5945945946 Party B, candidate 38 567689.6315789474 Party B, candidate 39 553133.4871794871 Party B, candidate 40 539305.15 Party B, candidate 41 526151.3658536585 Party B, candidate 42 513623.95238095237 Party B, candidate 43 501679.20930232556 Party B, candidate 44 490277.4090909091 Party B, candidate 45 479382.35555555555 Party C, candidate 1 23343976.0 Party C, candidate 2 11671988.0 Party C, candidate 3 7781325.333333333 Party C, candidate 4 5835994.0 Party C, candidate 5 4668795.2 Party C, candidate 6 3890662.6666666665 Party C, candidate 7 3334853.714285714 Party C, candidate 8 2917997.0 Party C, candidate 9 2593775.111111111 Party C, candidate 10 2334397.6 Party C, candidate 11 2122179.6363636362 Party C, candidate 12 1945331.3333333333 Party C, candidate 13 1795690.4615384615 Party C, candidate 14 1667426.857142857 Party C, candidate 15 1556265.0666666667 Party C, candidate 16 1458998.5 Party C, candidate 17 1373175.0588235294 Party C, candidate 18 1296887.5555555555 Party C, candidate 19 1228630.3157894737 Party C, candidate 20 1167198.8 Party C, candidate 21 1111617.9047619049 Party C, candidate 22 1061089.8181818181 Party C, candidate 23 1014955.4782608695 Party C, candidate 24 972665.6666666666 Party C, candidate 25 933759.04 Party C, candidate 26 897845.2307692308 Party C, candidate 27 864591.7037037037 Party C, candidate 28 833713.4285714285 Party C, candidate 29 804964.6896551724 Party C, candidate 30 778132.5333333333 Party C, candidate 31 753031.4838709678 Party C, candidate 32 729499.25 Party C, candidate 33 707393.2121212122 Party C, candidate 34 686587.5294117647 Party C, candidate 35 666970.7428571428 Party C, candidate 36 648443.7777777778 Party C, candidate 37 630918.2702702703 Party C, candidate 38 614315.1578947369 Party C, candidate 39 598563.4871794871 Party C, candidate 40 583599.4 Party C, candidate 41 569365.268292683 Party C, candidate 42 555808.9523809524 Party C, candidate 43 542883.1627906977 Party C, candidate 44 530544.9090909091 Party C, candidate 45 518755.02222222224 Party C, candidate 46 507477.73913043475 Party C, candidate 47 496680.3404255319 Party C, candidate 48 486332.8333333333 Party C, candidate 49 476407.67346938775 Party C, candidate 50 466879.52 Party C, candidate 51 457725.01960784313 Party D, candidate 1 32125207.0 Party D, candidate 2 16062603.5 Party D, candidate 3 10708402.333333334 Party D, candidate 4 8031301.75 Party D, candidate 5 6425041.4 Party D, candidate 6 5354201.166666667 Party D, candidate 7 4589315.285714285 Party D, candidate 8 4015650.875 Party D, candidate 9 3569467.4444444445 Party D, candidate 10 3212520.7 Party D, candidate 11 2920473.3636363638 Party D, candidate 12 2677100.5833333335 Party D, candidate 13 2471169.769230769 Party D, candidate 14 2294657.6428571427 Party D, candidate 15 2141680.466666667 Party D, candidate 16 2007825.4375 Party D, candidate 17 1889718.0588235294 Party D, candidate 18 1784733.7222222222 Party D, candidate 19 1690800.3684210526 Party D, candidate 20 1606260.35 Party D, candidate 21 1529771.761904762 Party D, candidate 22 1460236.6818181819 Party D, candidate 23 1396748.1304347827 Party D, candidate 24 1338550.2916666667 Party D, candidate 25 1285008.28 Party D, candidate 26 1235584.8846153845 Party D, candidate 27 1189822.4814814816 Party D, candidate 28 1147328.8214285714 Party D, candidate 29 1107765.7586206896 Party D, candidate 30 1070840.2333333334 Party D, candidate 31 1036297.0 Party D, candidate 32 1003912.71875 Party D, candidate 33 973491.1212121212 Party D, candidate 34 944859.0294117647 Party D, candidate 35 917863.0571428571 Party D, candidate 36 892366.8611111111 Party D, candidate 37 868248.8378378379 Party D, candidate 38 845400.1842105263 Party D, candidate 39 823723.2564102564 Party D, candidate 40 803130.175 Party D, candidate 41 783541.6341463415 Party D, candidate 42 764885.880952381 Party D, candidate 43 747097.8372093023 Party D, candidate 44 730118.3409090909 Party D, candidate 45 713893.4888888889 Party D, candidate 46 698374.0652173914 Party D, candidate 47 683515.0425531915 Party D, candidate 48 669275.1458333334 Party D, candidate 49 655616.4693877551 Party D, candidate 50 642504.14 Party D, candidate 51 629906.0196078431 Party D, candidate 52 617792.4423076923 Party D, candidate 53 606135.9811320754 Party D, candidate 54 594911.2407407408 Party D, candidate 55 584094.6727272727 Party D, candidate 56 573664.4107142857 Party D, candidate 57 563600.1228070175 Party D, candidate 58 553882.8793103448 Party D, candidate 59 544495.033898305 Party D, candidate 60 535420.1166666667 Party D, candidate 61 526642.737704918 Party D, candidate 62 518148.5 Party D, candidate 63 509923.92063492065 Party D, candidate 64 501956.359375 Party D, candidate 65 494233.95384615386