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

Exercise 3.1

In your head, or on paper, work out the following arithmetic as you would expect Python to do it using the rules of precedence.

  • 5 * 5 - 5 = ?

  • 10 - 2 * 3 = ?

  • 20 + 5 * 2 - 10 = ?

  • 4 + 4 / 2 = ?

  • 2 * 5 ** 2 = ?

  • 9 / 3 ** 2 = ?

Check your answers by typing them into the code cell below.

print(5 * 5 - 5) print(10 - 2 * 3) print(20 + 5 * 2) print(4 + 4 / 2) print(2 * 5 ** 2) print(9 / 3 ** 2)

Exercise 3.2

What are the types of each of the following numbers?

  • -6

  • 10.3

  • 99.0

  • -1.0

Check your answers in the code cell below.

print( type(-6) ) print( type(10.3) ) print( type(99.0) ) print( type(-1.0) )

Exercise 3.3

  1. What type of number do you get if you do 4 / 2?

  2. What type of number do you get if you do 4 // 2?

Check your answer below.

print( type(4/2) ) print( type(4//2) )

Exercise 3.4

Round the following floats to the given number of decimal places.

  • 34.2694 to 2dp

  • -1.4 to 0dp

  • 1024.234 to 1dp

print( round(34.2694, 2) ) print( round(-1.4, 0)) print( round(1024.234, 1) )

Exercise 3.5

What are the circumference and area of a circle of radius 5.4 cm rounded to 1 decimal place?

If you've forgotten what the circumference and area of a circle are use google.

# Circumference is 2 * pi * radius print( round(2 * 3.142 * 5.4, 1) ) # Area is pi * radius**2 print( round(3.142 * 5.4**2, 1) )

Exercise 3.6

In the code cell below calculate the average of the following set of numbers to 2dp:

11, 11, 13.5, 7.5, 13.5, 22.5
print( round( (11 + 11 + 13.5 + 7.5 + 13.5 + 22.5)/6, 2 ) )

Exercise 3.7

Using Python code calculate what 321 minutes is in hours and minutes? (E.g., 61 minutes is 1 hour and 1 minute.)

Find the remainder of the following:

  • 592\frac{59}{2}

  • 1002\frac{100}{2}

print( 321 // 60 ) print( 321 % 60) print( 59 % 2 ) print( 100 % 2 )