MSBA Ex 1 Solution

master yeoda

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


Back to the spellbook
In [1]:
# %load MSBA_Exercise_1_solution_v2
''' 
MSBA Programming Boot Camp
In-class Exercise 1
'''


#########################
#Q1. Simple Arithmetic  #
#########################

# Note that in many cases, there's no need to use print() to display.
# But for illustration, it's clearer to use print(). 



print(50+10) 
60
In [2]:
print(100-20)
80
In [3]:
print(2*4)
8
In [4]:
print(4/2) 
2.0
In [5]:
print(2**3)
8
In [6]:
print(10%2)
0
In [7]:
print(11%2)
1
In [8]:
print(11/3) 
3.6666666666666665
In [9]:
print(11//3) 
3
In [10]:
#########################
#Q2.Variable Assignment # 
######################### 

# Assign x and y to the values 5 and 10 respectively.
# Add them together and print the result

x = 5
y = 10
# Write your answer here

print(x+y)

result = x + y

print(result)
15
15
In [11]:
# Replace this name with yours
first_name = 'master ' 
last_name = 'yeoda'

# Print your name
# Write your answer here    hint: be careful with whitespace
print(first_name + last_name)
master yeoda
In [12]:
#######################################
#Q3.Boolean and If-statement practice # 
#######################################

# Write an if-then statement to determine if Chewie is hungry.

dog = 'Chewie'
is_hungry = False

# Write your answer here
if is_hungry == True:
    print("{} is hungry!".format(dog))
else:
   print("{} is not hungry :)".format(dog))
Chewie is not hungry :)
In [13]:
# Now try it with 3 conditions.
# If Link's level is < 10, he's not ready for
# the boss. If his level is 10, he needs a new sword.
# If his level is > 10, he's ready for the boss.
# Write your answer here

character_name = "Link"
level = 10

# This is probably a more straightforward if-then-else.

if level < 10:
    print(character_name + " is " + "not ready for the boss")
elif level == 10:
    print(character_name + " should " + "buy a new sword")
else:
    print(character_name + " is " + "ready") 
Link should buy a new sword
In [14]:
############################
#Q4.Data Structure -- List # 
############################

## creating a list
py_list = [1,2,3]
In [15]:
## adding something to the list
py_list.append(10)
In [16]:
## Accessing data inside of the list

# Print the list
# Write your answer here
print(py_list)
[1, 2, 3, 10]
In [17]:
# Access the first element. note that python counts from 0.
# Write your answer here
print(py_list[0])
1
In [18]:
# Access the 3rd element.
# Write your amswer here
print(py_list[2])
3
In [19]:
# Remove the the element "1".
# Remove some data from the list
py_list.remove(1)
In [20]:
# check the list again. Print it.
# Write your answer here
print(py_list)
[2, 3, 10]
In [21]:
## Let's create another list 
## Remember that we declared and assigned values to variables 
#print(x, y, character_name, level, first_name, last_name) 

## We can use variables because we have assgined values earlier
## Let's put these values to a list
# check the data structure. Use type()
var_list = [x, y, character_name, level, first_name, last_name]
var_list
Out[21]:
[5, 10, 'Link', 10, 'master ', 'yeoda']
In [22]:
type(var_list)
Out[22]:
list
In [23]:
00
Out[23]:
0