Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Exercise 23.1

In Exercise 3.5 you calculated the circumference and area of a circle of radius 5.4 cm.

Repeat the exercise but using the value of π\pi in the math module.

import math print( round(2 * math.pi * 5.4, 1) ) print( round(math.pi * 5.4**2, 1) )

Exercise 23.2

Using the statistics module, calculate and print the mean and standard deviation of the following list of bat wingspans to 2dp. The documentation for the statistics module is here.

import statistics wingspans = [20.4, 21.1, 17.1, 16.7, 24.4, 17.8, 20.1, 21.2, 20.8, 18.4, 20.0, 20.8, 19.4, 16.9, 18.0, 18.5, 18.0, 17.5, 18.7, 21.6, 21.6, 20.1, 20.6, 22.0, 20.0, 16.8, 24.2, 15.4, 21.2, 22.2, 26.1, 21.5, 18.9, 18.5, 19.9, 20.9, 18.4] print( round(statistics.mean(wingspans), 2) ) print( round(statistics.stdev(wingspans), 2) )

Exercise 23.3

  1. Search online for a module to randomly choose an item from a list.

  2. Use the module to randomly choose an item from the following list.

  3. Print the chosen item.

import random cities = ['Doha', 'Vancouver', 'Entebbe', 'Pokhara', 'Whangarei'] print(f'A randomly chosen city from the list is {random.choice(cities)}')