a long, long time ago in a galaxy far, far away
'''
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)
# Write your answer here
for x in range(5):
print(x)
# Write your answer here
for x in range(3):
print(var_list[x])
## 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'
#########################
#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')
#########################
#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")
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)
def is_even(val):
if val % 2 == 0:
return True
else:
return False
# Write your answer here
print(is_even(5))
#########################
#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))
#########################
#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
# subset the data for records where population is greater than 10000
high_pop = state[state["Population"] > 10000]
high_pop
# Sort the resultant subset by Area in descending order
high_pop_sorted = high_pop.sort_values("Area",ascending= False)
high_pop_sorted
# Try it in ascending order. Ascending is the default.
high_pop_sorted = high_pop.sort_values("Area")
high_pop_sorted