{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "W1_S2.ipynb",
"provenance": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# (Exercises) Simple Data Structures"
],
"metadata": {
"id": "9kmxOQ6qHj8X"
}
},
{
"cell_type": "markdown",
"source": [
"## Warm-up: Lists"
],
"metadata": {
"id": "iyJBbPOSAC3a"
}
},
{
"cell_type": "markdown",
"source": [
"1. Write out the following code without using a list comprehension:\\\n",
"`plus_thirteen = [number + 13 for number in range(1,11)]`\n",
"2. Use list comprehension or loops to create a list of first twenty cubes (i.e., `x^3 from 0-19`)"
],
"metadata": {
"id": "t4SvHV60ALnR"
}
},
{
"cell_type": "code",
"source": [
"# Question 1\n",
"plus_thirteen = []\n",
"for ______ in range(1,11):\n",
" plus_thirteen._______(________)\n",
"print(plus_thirteen)"
],
"metadata": {
"id": "c88sWFwuyiy9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title ##### Your answer should look like this\n",
"plus_thirteen = []\n",
"for number in range(1,11):\n",
" plus_thirteen.append(number+13)\n",
"print(plus_thirteen)"
],
"metadata": {
"cellView": "form",
"id": "u6lOYAOfXk1I"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Question 2\n",
"[_____________________________________ range(20)]"
],
"metadata": {
"id": "fwev9Agux5NM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title ##### Your answer should look like this\n",
"[number**3 for number in range(20)]"
],
"metadata": {
"cellView": "form",
"id": "ub-XfKBZXgFR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Main Exercise: Creating and Manipulating Simple Data Structures\n",
"\n",
"**Learning Goals**\n",
"\n",
"This assignment will verify that you have the following skills:\n",
"\n",
"* Create lists and dictionaries\n",
"* Iterate through lists, tuples, and dictionaries\n",
"* Index sequences (e.g. lists and tuples)\n",
"* Define functions\n",
"* Use optional keyword arguments in functions\n",
"* Define classes\n",
"* Add a custom method to a class"
],
"metadata": {
"id": "ATTpqOa0DS5w"
}
},
{
"cell_type": "markdown",
"source": [
"\n"
],
"metadata": {
"id": "4oicCV-uDjFE"
}
},
{
"cell_type": "markdown",
"source": [
"### Part I: Lists and Loops\n",
"\n",
"\n",
"Q1. Create a list with the names of every planet in the solar system (in order)\\\n",
"Q2. Have Python tell you how many planets there are by examining your list\\\n",
"Q3. Use slicing to display the first four planets (the rocky planets)\\\n",
"Q4. Iterate through your planets and print the planet name only if it has an `s` at the end\n"
],
"metadata": {
"id": "PakvfDxNDqoh"
}
},
{
"cell_type": "markdown",
"source": [
"**Q1. Create a list with the names of every planet in the solar system (in order)**"
],
"metadata": {
"id": "i7Rdq-V_thPi"
}
},
{
"cell_type": "code",
"source": [
"# Create your list here\n",
"planetlist = ['mercury','_',__________________________]"
],
"metadata": {
"id": "kBk7sj8stf4q"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q2. Have Python tell you how many planets there are by examining your list**"
],
"metadata": {
"id": "E_p79oZ4tmjp"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"# Hint: Use len() function\n",
"# Answer: you should get a single number of 8\n",
"print(___(______________))"
],
"metadata": {
"id": "INbwC3OSttIQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q3. Use slicing to display the first four planets (the rocky planets)**\n"
],
"metadata": {
"id": "RCdn-rBMtvhP"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"____________________[:_]"
],
"metadata": {
"id": "tIArigiRt0X_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q4. Iterate through your planets and print the planet name only if it has an `s` at the end**\n"
],
"metadata": {
"id": "8Oy6N-VWt5PH"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"# Hint: Access the final letter of every planet (planet[-1]), print planet name if the letter equals to \"s\", continue with the loop otherwise.\n",
"# Answers:\n",
"# venus\n",
"# mars\n",
"# uranus\n",
"\n",
"for ________________ in ____________:\n",
" if ___________[__]=='s':\n",
" print(__________)\n",
" else:\n",
" ____________"
],
"metadata": {
"id": "l1jiMBNFt2uu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Part II: Dictionary"
],
"metadata": {
"id": "t22s92xwDuE5"
}
},
{
"cell_type": "markdown",
"source": [
"**Q5) Now create a dictionary that maps each planet name to its mass**\n",
"\n",
"You can use values from this [NASA](https://nssdc.gsfc.nasa.gov/planetary/factsheet/) fact sheet. You can use whatever units you want, but please be consistent."
],
"metadata": {
"id": "D8JGILoCDzQS"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"#planetdict = {'mercury':______,'venus':_____,'earth':_____,\n",
"# 'mars':__xx___,'jupiter':____,'saturn':____,'uranus':_____,'nepturn':___}"
],
"metadata": {
"id": "jCtX4V2ot_o-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q6) Use your dictionary to look up Earth’s mass**"
],
"metadata": {
"id": "Ek5Se2lrD5Iz"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"print(________[___])"
],
"metadata": {
"id": "yTTxpnnPuAof"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q7) Loop through the data to create and display a list of planets whose mass is greater than $100 \\times 10^{24}$ kg**"
],
"metadata": {
"id": "BkHvojwuD777"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"# In the parentheses, you will access the names (keys) of each entry in the dictionary.\n",
"# You should see this: ['jupiter', 'saturn', 'nepturn']\n",
"q7list = []\n",
"for key in list(_______.keys()):\n",
" # Now you wil access the values associated with each key-value pair\n",
" if _____________[key]>100:\n",
" q7list.append(___) # the name of each planet\n",
" else:\n",
" _________\n",
"print(q7list)"
],
"metadata": {
"id": "UmQfeDqruBPm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q8) Now add Pluto to your dictionary**"
],
"metadata": {
"id": "n-dsPPdSEBoo"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"# You should see this: ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'nepturn', 'pluto']\n",
"_____________['pluto'] = ______\n",
"print(planetdict.keys())"
],
"metadata": {
"id": "h6D5L6JtuB3W"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Part III: Functions"
],
"metadata": {
"id": "mdAToOSvEG6f"
}
},
{
"cell_type": "markdown",
"source": [
"**Q9) Write a function to convert from the unit you chose for planetary masses to $M_{Earth}$, the mass of the Earth**\n",
"\n",
"For example, the mass of Jupiter is:\n",
"\n",
"$M_{Jupiter} \\approx 1898\\times10^{24}kg \\approx 318 M_{Earth}$"
],
"metadata": {
"id": "ROIndYqvEOeH"
}
},
{
"cell_type": "code",
"source": [
"#write function here\n",
"# Write your function here\n",
"# The function should take in the any planet mass you want (e.g., planetdict['mars']) and divide by the mass of Earth\n",
"# (planetdict['earth'])\n",
"def convert_mass_Mearth(______=____):\n",
" return _________/planetdict['earth']\n",
"\n",
"#test it works for jupiter (planetdict['jupiter']), the function should output 317.92294807370183\n",
"convert_mass_Mearth(____________)"
],
"metadata": {
"id": "2q8fjUWmERVM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q10) Now write a single function that can convert from the unit you chose for planetary masses to both $M_{Earth}$ or $M_{Jupiter}$ depending on the keyword argument you specify**"
],
"metadata": {
"id": "0sMvjuphETEM"
}
},
{
"cell_type": "code",
"source": [
"# Write your function here\n",
"def convert_m_Mearthjupiter(______=____,ourplanet='jupiter'):\n",
" return ___________/planetdict[ourplanet]\n",
"\n",
"# Check if your function work: convert Mjupiter to Mearth and Mjupiter,\n",
"# You should see 317.923 (Mearth) and 1.0 (Mjupiter)\n",
"print(convert_m_Mearthjupiter(planetdict['jupiter'],_____),convert_m_Mearthjupiter(planetdict['jupiter'],______))"
],
"metadata": {
"id": "1sMWhXR8DrbQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Store the Mjupiter for Q11\n",
"Mjupiter = convert_m_Mearthjupiter(planetdict['jupiter'],'jupiter')"
],
"metadata": {
"id": "-jLtgEdVsuAk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q11) Write a function takes one argument (mass in $M_{Jupiter}$) and returns two arguments (mass in $M_{Earth}$ and mass in the unit you chose [whichever planet you want])**"
],
"metadata": {
"id": "I4P1udcPEXrc"
}
},
{
"cell_type": "code",
"source": [
"# Write your function here\n",
"def mass_conversion_two_arguments(mass,planet=____):\n",
" a = mass/planetdict[_____] # Earth\n",
" b = mass/planetdict[______] # The planet you want to use as a reference\n",
" return a,b\n",
"# Check that it works to convert Jupiter's mass to $M_{Earth}$ and to\n",
"# the unit you chose (e.g., Mars' planet)\n",
"print(mass_conversion_two_arguments(_____,'mars'))"
],
"metadata": {
"id": "ysKxqLEdDkVE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Bonus: Use the function from Q10 to convert Neptune's mass to $M_{Jupiter}$\n",
"# and then the function from Q11 to convert it back to the unit you chose\n",
"# Do you get the original value back?"
],
"metadata": {
"id": "HYEOb0_MEZ6R"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Part IV: Classes"
],
"metadata": {
"id": "nDLl3L13EhcT"
}
},
{
"cell_type": "markdown",
"source": [
"**Q12) Write a class `planet` with two attributes: `name` and `mass`**"
],
"metadata": {
"id": "aLPRglSDEl5r"
}
},
{
"cell_type": "code",
"source": [
"# Write your class here\n",
"class planet:\n",
" def __init__(____,inname=____,inmass=____):\n",
" self.name = inname\n",
" self.mass = inmass\n",
"# Create a new instance of that class to represent the Earth (mass=5.97)\n",
"earth = planet(_______,_____)\n",
"# Create a new instance of that class to represent Jupiter (mass=1898)\n",
"jupiter = planet(_______,____)"
],
"metadata": {
"id": "aG9kqSo0EsSL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# How to check that you did the right thing?\n",
"print(earth._____,earth.____)"
],
"metadata": {
"id": "mRBpmVbJvSrZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Q13) Add a custom method `is_light` to the class `planet` that returns `True` if the planet is strictly lighter than Jupiter, `False` strictly heavier than Jupiter, and 'same mass!' if the planet weighs the same as jupiter**"
],
"metadata": {
"id": "cdgJ21kNEtI3"
}
},
{
"cell_type": "code",
"source": [
"# Change your class here\n",
"class planet:\n",
" def __init__(____,inname=____,inmass=____):\n",
" self.name = inname\n",
" self.mass = inmass\n",
" def is_light(____):\n",
" if ___________planetdict['jupiter']:\n",
" print('False')\n",
" else:\n",
" print('same mass!')\n",
"\n",
"# Ask Python whether the Earth is lighter than Jupiter, you should see True for this one!\n",
"planet(_____,planetdict[_____]).is_light()\n",
"# Ask Python whether Jupiter is lighter than itself, you should see same mass! for this one!\n",
"planet(_____,planetdict[_____]).is_light()\n"
],
"metadata": {
"id": "8-me3TTDEyFZ"
},
"execution_count": null,
"outputs": []
}
]
}