Exercises#
You can easily start a live Jupyter session in the cloud directly from this book. To do this, just click on the Launch Button () located above on this page.
You have a few options to choose from:
Launch on Binder: By selecting this option, you can instantly launch a live Jupyter session using Binder.
Launch on Google Colab: This option allows you to launch a live Jupyter session using Google Colab.
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.
Open the Python Calculator for this page
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.
Remember that all pages in this course can also be opened as an interactive notebook via Binder or Colab using the {fa}rocket icon above (read more about it here).
Exercise 3.1.1#
Use the print()
function and an f-string to make a statement about some car using all variables stored in the car_info
dictionary.
Type your code here where the three (...
) dots are placed. Do not change the name of the variables.
car_info = {
'top_speed' : '229', #km/h
'type' : 'Opel Astra'
}
message = ...
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=0, arguments=[car_info, message])
Exercise 3.2.1#
Write a lambda function that converts a number from degrees to radians.
Type your code here where the three (...
) dots are placed. Do not change the name of the variables.
from math import pi
DegToRad = ...
angle = 20 # Degrees
print(f"An angle of {angle} Degrees is equal to {DegToRad(angle):.3f} radians")
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=1, arguments=[angle,DegToRad])
Exercise 3.2.2#
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)\).
Type your code here where the three (...
) dots are placed. Do not change the name of the variables.
from math import sqrt
distance = ...
x1, y1 = 1, 1
x2, y2 = 4, 4
print(f"Distance between points ({x1}, {y1}) and ({x2}, {y2}) is {distance(x1, y1, x2, y2):.3f}")
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=2, arguments=[distance])
(Fixing) Exercise 3.4.1#
Fix the syntax errors so it prints “AES” without removing the variable that holds it. You’ll need to fix 2 errors.
def get_abbreviation():
my abbreviation = "AES"
return my abbreviation
print(get_abbreviation())
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=3, arguments=[get_abbreviation])
(Fixing) Exercise 3.4.2#
Solve the runtime error so all values of \(B\) are printed. The output after running create_string_from_lists()
should be the following:
A[0] = 2
B[0] = 5
A[1] = 3
B[1] = 6
A[2] = 4
B[2] = 7
B[3] = 8
def create_string_from_lists():
s = ""
A = [2, 3, 4]
B = [5, 6, 7, 8]
for i in range(4):
s += f"A[{i}] = {A[i]}\n"
s += f"B[{i}] = {B[i]}\n"
print(s)
create_string_from_lists()
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=4, arguments=[create_string_from_lists])
(Fixing) Exercise 3.4.3#
Find the semantic error in this function:
def factorial(x):
"returns the factorial of x"
if x == 0:
return 1
else:
return x ** factorial(x-1)
factorial(4)
check your answer!
To check your answer in a Jupyter Lite session, simply run the following line of code immediately after your code implementation.
If your are in Google Colab just run the cell bellow.
check.notebook_3(question_number=5, arguments=[factorial])