MSBA Ex 2 Solution

master yeoda

a long, long time ago in a galaxy far, far away


Back to the spellbook
In [12]:
''' 
MSBA Programming Boot Camp
In-class Exercise 2
'''


############################
#Q5.Loops                  # 
############################

## We have created a list with variables above. Now let's use them!


x = 5
y = 10
character_name = "link"
level = 10
first_name = "master"
last_name = "yeoda"

var_list = [x, y, character_name, level, first_name, last_name]
var_list

for var in var_list:
    print(var)
5
10
link
10
master
yeoda
In [13]:
# Write your answer here
for x in range(5):
    print(x)
0
1
2
3
4
In [14]:
# Write your answer here
for x in range(3):
    print(var_list[x])
5
10
link
In [15]:
## Lastly, let's practice while-loop
num = 0

# Write your answer here
while num < 3:
    print('What is inside of variable num: {}'.format(num))
    num = num + 1


### in case you run into an infite-loop, go to 'Run' and then click on 'Interrupt Kernel'
What is inside of variable num: 0
What is inside of variable num: 1
What is inside of variable num: 2
In [19]:
#########################
#Q1. Logical Operator  #
#########################

# Remove the # and run the codes to see the results. 

a = True
b = True

# Write your answer here
print("a and b", a and b)

# Write your answer here
print("a and b", a or b)


# Write your answer here
if a == False or b == True:
    print('Pizza')
else:
    print('Burger') 


# Write your answer here
if a == False and b == False:
    print('Pizza')
elif a == True or b == True:
            print('Ramen')
else:
    print('Burger')
a and b True
a and b True
Pizza
Ramen
In [29]:
#########################
#Q2. Functions/Methods  #
#########################

def hello_world():
    print('hello world')

# Write your answer here
hello_world()



'''
    As you can see that name parameter has been set to 'yeoda'
    This is called default argument. 
'''
def print_my_name(name='yeoda'):
    print('hello {}'.format(name))
    
print_my_name()
print_my_name("master")
hello world
hello yeoda
hello master
In [30]:
def three_num_multiplication(num_one, num_two, num_three):
    return num_one * num_two * num_three

a = 2
b = 3
c = 4
result = three_num_multiplication(a,b,c)

# Write your answer here
result
print(result)
24
In [31]:
def is_even(val):
    if val % 2 == 0:
        return True
    else:
        return False 

# Write your answer here
print(is_even(5))
False
In [21]:
#########################
#Q3. Built-in Fuctions  #
#########################


a = 2
b = 8

# Write your answer here
print(max(a, b))

# Write your answer here
print(min(a,b))

# Write your answer here
print(pow(b, a))

c = -15

# Write your answer here 
print(abs(c))
8
2
64
15
In [25]:
#########################
#Q4. Using pandas  #
#########################

# Import pandas. You can import it as pandas or jedi or anything. 
# But this will determine how you call the functions later on.

import pandas as pd

# You may need the r before the path if you get an error.
# This is because it is a normal string and it needs to be
# converted to a raw path for this to work.

# Note that the path will be different for you.
state = pd.read_csv('statex77.csv')
state
Out[25]:
Unnamed: 0 Population Income Illiteracy Life Exp Murder HS Grad Frost Area
0 Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
1 Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
2 Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
3 Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
4 California 21198 5114 1.1 71.71 10.3 62.6 20 156361
5 Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
6 Connecticut 3100 5348 1.1 72.48 3.1 56.0 139 4862
7 Delaware 579 4809 0.9 70.06 6.2 54.6 103 1982
8 Florida 8277 4815 1.3 70.66 10.7 52.6 11 54090
9 Georgia 4931 4091 2.0 68.54 13.9 40.6 60 58073
10 Hawaii 868 4963 1.9 73.60 6.2 61.9 0 6425
11 Idaho 813 4119 0.6 71.87 5.3 59.5 126 82677
12 Illinois 11197 5107 0.9 70.14 10.3 52.6 127 55748
13 Indiana 5313 4458 0.7 70.88 7.1 52.9 122 36097
14 Iowa 2861 4628 0.5 72.56 2.3 59.0 140 55941
15 Kansas 2280 4669 0.6 72.58 4.5 59.9 114 81787
16 Kentucky 3387 3712 1.6 70.10 10.6 38.5 95 39650
17 Louisiana 3806 3545 2.8 68.76 13.2 42.2 12 44930
18 Maine 1058 3694 0.7 70.39 2.7 54.7 161 30920
19 Maryland 4122 5299 0.9 70.22 8.5 52.3 101 9891
20 Massachusetts 5814 4755 1.1 71.83 3.3 58.5 103 7826
21 Michigan 9111 4751 0.9 70.63 11.1 52.8 125 56817
22 Minnesota 3921 4675 0.6 72.96 2.3 57.6 160 79289
23 Mississippi 2341 3098 2.4 68.09 12.5 41.0 50 47296
24 Missouri 4767 4254 0.8 70.69 9.3 48.8 108 68995
25 Montana 746 4347 0.6 70.56 5.0 59.2 155 145587
26 Nebraska 1544 4508 0.6 72.60 2.9 59.3 139 76483
27 Nevada 590 5149 0.5 69.03 11.5 65.2 188 109889
28 New Hampshire 812 4281 0.7 71.23 3.3 57.6 174 9027
29 New Jersey 7333 5237 1.1 70.93 5.2 52.5 115 7521
30 New Mexico 1144 3601 2.2 70.32 9.7 55.2 120 121412
31 New York 18076 4903 1.4 70.55 10.9 52.7 82 47831
32 North Carolina 5441 3875 1.8 69.21 11.1 38.5 80 48798
33 North Dakota 637 5087 0.8 72.78 1.4 50.3 186 69273
34 Ohio 10735 4561 0.8 70.82 7.4 53.2 124 40975
35 Oklahoma 2715 3983 1.1 71.42 6.4 51.6 82 68782
36 Oregon 2284 4660 0.6 72.13 4.2 60.0 44 96184
37 Pennsylvania 11860 4449 1.0 70.43 6.1 50.2 126 44966
38 Rhode Island 931 4558 1.3 71.90 2.4 46.4 127 1049
39 South Carolina 2816 3635 2.3 67.96 11.6 37.8 65 30225
40 South Dakota 681 4167 0.5 72.08 1.7 53.3 172 75955
41 Tennessee 4173 3821 1.7 70.11 11.0 41.8 70 41328
42 Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134
43 Utah 1203 4022 0.6 72.90 4.5 67.3 137 82096
44 Vermont 472 3907 0.6 71.64 5.5 57.1 168 9267
45 Virginia 4981 4701 1.4 70.08 9.5 47.8 85 39780
46 Washington 3559 4864 0.6 71.72 4.3 63.5 32 66570
47 West Virginia 1799 3617 1.4 69.48 6.7 41.6 100 24070
48 Wisconsin 4589 4468 0.7 72.48 3.0 54.5 149 54464
49 Wyoming 376 4566 0.6 70.29 6.9 62.9 173 97203
In [26]:
# subset the data for records where population is greater than 10000
high_pop = state[state["Population"] > 10000]
high_pop
Out[26]:
Unnamed: 0 Population Income Illiteracy Life Exp Murder HS Grad Frost Area
4 California 21198 5114 1.1 71.71 10.3 62.6 20 156361
12 Illinois 11197 5107 0.9 70.14 10.3 52.6 127 55748
31 New York 18076 4903 1.4 70.55 10.9 52.7 82 47831
34 Ohio 10735 4561 0.8 70.82 7.4 53.2 124 40975
37 Pennsylvania 11860 4449 1.0 70.43 6.1 50.2 126 44966
42 Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134
In [27]:
# Sort the resultant subset by Area in descending order

high_pop_sorted = high_pop.sort_values("Area",ascending= False)
high_pop_sorted
Out[27]:
Unnamed: 0 Population Income Illiteracy Life Exp Murder HS Grad Frost Area
42 Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134
4 California 21198 5114 1.1 71.71 10.3 62.6 20 156361
12 Illinois 11197 5107 0.9 70.14 10.3 52.6 127 55748
31 New York 18076 4903 1.4 70.55 10.9 52.7 82 47831
37 Pennsylvania 11860 4449 1.0 70.43 6.1 50.2 126 44966
34 Ohio 10735 4561 0.8 70.82 7.4 53.2 124 40975
In [28]:
# Try it in ascending order. Ascending is the default.
high_pop_sorted = high_pop.sort_values("Area")
high_pop_sorted
Out[28]:
Unnamed: 0 Population Income Illiteracy Life Exp Murder HS Grad Frost Area
34 Ohio 10735 4561 0.8 70.82 7.4 53.2 124 40975
37 Pennsylvania 11860 4449 1.0 70.43 6.1 50.2 126 44966
31 New York 18076 4903 1.4 70.55 10.9 52.7 82 47831
12 Illinois 11197 5107 0.9 70.14 10.3 52.6 127 55748
4 California 21198 5114 1.1 71.71 10.3 62.6 20 156361
42 Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134
In [ ]: