A function is a ready-made tool: you give it a value in brackets, called the argument, it does a set job and returns a result. print itself is a function. round rounds a number to the nearest whole number; give it a second number and it keeps that many decimal places:
print(round(9.6)) print(round(10.3)) print(round(3.14159, 2))
10 10 3.14
| Function | Returns | Example | Result |
|---|---|---|---|
| round(x) | nearest whole number | round(5.7) | 6 |
| max(a, b, ...) | the largest value | max(10, 25, 18) | 25 |
| min(a, b, ...) | the smallest value | min(5, 10, 50) | 5 |
| abs(x) | the value without a minus sign | abs(-7) | 7 |
| len(text) | number of characters | len("Python") | 6 |
print(max(10, 25, 18))
print(min(5, 10, 50))
print(abs(3 - 11))
print(len("Cairo"))25 5 8 5
Round or MAX cause an error. Write them in lowercase.Importing a library: import math
Some functions live in a library, a box of functions you bring in first with import. Then you write the library name, a dot, and the function name:
import math print(math.floor(7.8)) print(math.ceil(7.2)) print(math.sqrt(49)) print(round(9.5)) print(math.floor(9.5))
7 8 7.0 10 9
| Function | Does | Example |
|---|---|---|
| math.floor(x) | always rounds down | math.floor(7.8) → 7 |
| math.ceil(x) | always rounds up | math.ceil(7.2) → 8 |
| math.sqrt(x) | square root | math.sqrt(49) → 7.0 |
When do you need math.floor? A shelf is 87.5 cm wide and a box is 12.3 cm wide. You cannot place part of a box, so round the division down:
import math print(87.5 / 12.3) print(math.floor(87.5 / 12.3))
7.113821138211382 7
round goes to the nearest even number: round(9.5) = 10 but round(8.5) = 8.Random numbers: import random
random.randint(1, 6) returns a random whole number from 1 to 6 (both ends included), like rolling a die. random.random() returns a decimal from 0 up to, but not including, 1. Run the example a few times and the result changes:
import random
print(random.randint(1, 6))
if random.random() >= 0.5:
print("You win!")
else:
print("You lose...")3 You win!
Practice
Import math, then print 5.5 rounded with round, then 8.8 rounded down with math.floor.
Press Run to see the result. Print the larger of 10 and 20, then the smaller one, using max and min.
Press Run to see the result. A shelf is 64.8 cm wide and a prize box is 9.5 cm wide. Print how many whole boxes fit on the shelf, using math.floor.
Press Run to see the result. Complete the condition: if random.random() is 0.7 or more, Grand prize! is printed.
import random
if ___:
print("Grand prize!")
else:
print("Consolation prize")Press Run to see the result. Write a dice program: print a random number from 1 to 6 using random.randint.
Press Run to see the result. A school trip has 230 students and a bus holds 50. No student is left behind, so round up. Print Buses: 5 and let the computer work out the number.
Hint
Usemath.ceil and str().Press Run to see the result. Key points
- A function takes an argument and returns a value:
round,max,min,abs,len. - Import the library first:
import math, thenmath.floor,math.ceil,math.sqrt. random.randint(a, b)is a random whole number from a to b;random.random()is a decimal from 0 up to 1.