Open a new Jupyter notebook, and complete the exercises.
Complete simple calculations using Python.
Assign y to be x.
Print y. Before doing so, answer what this will output.
y = x
print(y)
Output:
3.0
Assign x to be x + y.
Print x. Before doing so, answer what this will output.
x = x + y
print(x)
Output:
6.0
Use functions on different types of variables.
Assign n to be 3.7.
Print n's variable type.
Print the rounded value of n.
Assign n to be an integer.
Print n. Before doing so, answer what this will output.
n = 3.7
print(type(n))
print(round(n))
n = int(n)
print(n)
Output:
class 'float'
4
3 (Remember: changing float to integer will always round down.)
Assign founder_name to the string "Jack Bogle".
Print founder_name.
Assign founding_year to a new line and the string "1975".
Print founding_year.
founder_name = "Jack Bogle"
print(founder_name)
founding_year = "\n1975"
print(founding_year)
Output:
Jack Bogle
1975
Assign n to be a string.
Print n's variable type.
Print n.
n = str(n)
print(type(n))
print(n)
Output:
class 'str'
3 (Remember: n looks like an integer, but it is really a string variable)
Use operators and logic to make statements.
Assign a to be 4.
Assign b to be 7.
Print the boolean outcome of the statement: "b equals a".
Print the boolean outcome of the statement: "b is not equal to a".
Print the boolean outcome of the statement: "a is less than or equal to b".
a = 4
b = 7
print(b == a)
print(b != a)
print(a <= b)
Output:
False
True
True
Assign m to be 10.
If m is less than 9, print the boolean True.
Otherwise, print the boolean False.
m = 10
if m < 9:
print(True)
else:
print(False)
Output:
False
Assign z to be 8.
If z is greater than 10, print True.
If it's not, if z is greater than 9, print True.
Otherwise, print False.
z = 8
if z > 10:
print(True)
elif z > 9:
print(True)
else:
print(False)
Output:
False
Assign w to be 2500.
If w is greater than 340,
If w is less than 3400, print "both conditions met".
If w is greater than 340 and greater than 3400, print "one condition met".
Otherwise, print "one condition met or neither condition met".
w = 2500
if w > 340:
if w < 3400:
print("both conditions met")
else:
print("one condition met")
else:
print("one condition met or neither condition met")
Output:
both conditions met
Create list objects and manipulate them using methods.
Create a list called items with the elements: str "okay", int 5, bool True, and float 3.9.
Print the 1st element in the list.
Print the 1st through 3rd elements in the list.
Print the last element in the list.
Print all elements from the 2 element onward.
Print the length of the list.
items = ["okay", int(5), True, 3.9]
print(items[0])
print(items[0:3])
print(items[-1])
print(items[1:])
print(len(items))
Output:
okay (Remember: Python uses zero-based indexing)
['okay', 5, True] (Remember: slicing uses the syntax [start:stop], where 'stop' is not inclusive of the input index.
3.9
[5, True, 3.9]
4
Create an empty list called asset_price.
Append a list called year with elements 2018, 2019, 2020.
Append a list called price with elements 15.28, 18.39, 19.14.
Delete the first elements in both lists within asset_price.
Print the (new) first elements in both lists within asset_price as strings.
Print the list year. Before doing so, answer what this will output.
asset_price = []
year = [2018, 2019, 2020]
asset_price.append(year)
price = [15.28, 18.39, 19.14]
asset_price.append(price)
del asset_price[0][0]
del asset_price[1][0]
print(str(asset_price[0][0]) + ": " + str(asset_price[1][0]))
print(year)
Output:
2019: 18.39
[2019, 2020]
Use for loops to manipulate elements in lists in an iterative process.
Using a for loop, print each element in the list called clients, which consists of: "10453", "10983", "10376".
clients = ["10453", "10983", "10376"]
for client in clients:
print(client)
Output:
10453
10983
10376
Create an empty list called client_assets.
Append the clients list to client_assets.
Append a list called assets to client_assets, which consists of: 70000, 400000, 5500.
Using a for loop, print the client element if the corresponding assets element is greater than 50000.
What do the printed values represent?
client_assets = []
client_assets.append(clients)
assets = [70000, 400000, 5500]
client_assets.append(assets)
index = 0
for asset in client_assets[1]:
if asset > 50000:
print(client_assets[0][index])
index = index + 1
Output:
10453
10983
The printed values are the clients that have in excess of $50,000 worth of assets.
Create your own functions.
Write a function called trading_close that takes no input and returns a string variable called close_statement, which consists of: "Trading day is closed".
Execute the function.
def trading_close():
close_statement = "Trading day is closed"
return close_statement
trading_close()
Output:
'Trading day is closed'
Write a function that passes in a list of prices for an asset across time, and outputs a list of the percentage change in price from each time period to the next.
Pass the list [40.25, 39.50, 41.03, 150.10] to the function and assign the function output to the list variable, per_changes.
Print the list per_changes.
def price_per_change(prices):
per_changes = []
i=0
for price in prices:
if i == 0:
i = i + 1
continue
else:
per_change = ((price - prices[i-1])/prices[i-1])*100
per_changes.append(per_change)
i = i + 1
return per_changes
#Remember: per_changes is not required to be named the same as the variable being returned from the function
per_changes = price_per_change([40.25, 39.50, 41.03, 150.10])
print(per_changes)
Output:
[-1.8633540372670807, 3.87341772151899, 265.82988057518884]