{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"# Exercises\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"You can easily start a live Jupyter session in the cloud directly from this book. To do this, just click on the Launch Button ({fa}`rocket`) located above on this page.\n",
"\n",
"You have a few options to choose from:\n",
"\n",
"1. Launch on Binder: By selecting this option, you can instantly launch a live Jupyter session using Binder.\n",
"2. Launch on Google Colab: This option allows you to launch a live Jupyter session using Google Colab.\n",
"\n",
"Alternatively, you can also click on the *Jupyter Lite session* link, which will open a new tab where you can freely write and run your code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# ##-On Google colab uncomment and run the following code\n",
"# ## to install and import the function that will be used to check your answers.\n",
"# !pip install learn-python-check\n",
"\n",
"# ##-On Binder just uncomment and run the following line\n",
"# import learn_python_check.check_answers as check"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"`````{admonition} Open the Python Calculator for this page\n",
":class: tip, dropdown\n",
"Click this link and wait until the message \"You may begin!\" is printed to start evaluating code. More information about this tool can be found [here](calculator). \n",
"\n",
"Remember that most pages in this course can also be run interactively using the {fa}rocket icon above (read more about it [here](toolbox)).\n",
"`````"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Exercise 3.1.1\n",
"\n",
"Use the print()
function and an f-string to make a statement about some car using all variables stored in the car_info
dictionary.\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"_Type your code here where the three (*`...`*) dots are placed. Do not change the name of the variables._\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"car_info = {\n",
" 'top_speed' : '229', #km/h\n",
" 'type' : 'Opel Astra'\n",
"}\n",
"\n",
"message = ...\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=0, arguments=[car_info, message])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Exercise 3.2.1\n",
"\n",
"Write a lambda function that converts a number from degrees to radians.\n",
"\n",
"_Type your code here where the three (*`...`*) dots are placed. Do not change the name of the variables._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from math import pi \n",
"\n",
"DegToRad = ...\n",
"\n",
"angle = 20 # Degrees\n",
"print(f\"An angle of {angle} Degrees is equal to {DegToRad(angle):.3f} radians\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=1, arguments=[angle,DegToRad])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Exercise 3.2.2\n",
"\n",
"Write a lambda function that takes four inputs: $(x_1, y_1, x_2, y_2)$ and computes the Euclidian distance between point 1 $(x_1,y_1)$ and point 2 $(x_2,y_2)$.\n",
"\n",
"_Type your code here where the three (*`...`*) dots are placed. Do not change the name of the variables._\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from math import sqrt\n",
"\n",
"distance = ...\n",
"\n",
"\n",
"x1, y1 = 1, 1\n",
"x2, y2 = 4, 4\n",
"\n",
"print(f\"Distance between points ({x1}, {y1}) and ({x2}, {y2}) is {distance(x1, y1, x2, y2):.3f}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=2, arguments=[distance])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## (Fixing) Exercise 3.4.1\n",
"\n",
"Fix the syntax errors so it prints \"AES\" without removing the variable that holds it. You'll need to fix 2 errors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_abbreviation():\n",
" my abbreviation = \"AES\"\n",
" return my abbreviation\n",
" \n",
"print(get_abbreviation())"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=3, arguments=[get_abbreviation])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## (Fixing) Exercise 3.4.2\n",
"\n",
"Solve the runtime error so all values of $B$ are printed. The output after running create_string_from_lists()
should be the following:
\n",
"\n",
"A[0] = 2\n",
"B[0] = 5\n",
"A[1] = 3\n",
"B[1] = 6\n",
"A[2] = 4\n",
"B[2] = 7\n",
"B[3] = 8\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def create_string_from_lists():\n",
" s = \"\"\n",
" \n",
" A = [2, 3, 4]\n",
" B = [5, 6, 7, 8]\n",
" for i in range(4):\n",
" s += f\"A[{i}] = {A[i]}\\n\"\n",
" s += f\"B[{i}] = {B[i]}\\n\"\n",
" print(s)\n",
"\n",
"\n",
"create_string_from_lists()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=4, arguments=[create_string_from_lists])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## (Fixing) Exercise 3.4.3\n",
"\n",
"Find the semantic error in this function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def factorial(x):\n",
" \"returns the factorial of x\"\n",
" if x == 0:\n",
" return 1\n",
" else: \n",
" return x ** factorial(x-1)\n",
"\n",
"factorial(4)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"````{admonition} check your answer!\n",
"To check your answer in a _Jupyter Lite session_, simply run the following line of code immediately after your code implementation. \n",
"\n",
"If your are in _Google Colab_ just run the cell bellow.\n",
"````"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check.notebook_3(question_number=5, arguments=[factorial])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}