Python / Functions

Built-in functions and import

Use ready-made functions such as round, max, min, abs and len, and import math and random to round, calculate and make random numbers. Baccalaureate

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:

Python
print(round(9.6))
print(round(10.3))
print(round(3.14159, 2))
Console
10
10
3.14
FunctionReturnsExampleResult
round(x)nearest whole numberround(5.7)6
max(a, b, ...)the largest valuemax(10, 25, 18)25
min(a, b, ...)the smallest valuemin(5, 10, 50)5
abs(x)the value without a minus signabs(-7)7
len(text)number of characterslen("Python")6
Python
print(max(10, 25, 18))
print(min(5, 10, 50))
print(abs(3 - 11))
print(len("Cairo"))
Console
25
5
8
5
Function names are case-sensitive: 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:

Python
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))
Console
7
8
7.0
10
9
FunctionDoesExample
math.floor(x)always rounds downmath.floor(7.8) → 7
math.ceil(x)always rounds upmath.ceil(7.2) → 8
math.sqrt(x)square rootmath.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:

Python
import math
print(87.5 / 12.3)
print(math.floor(87.5 / 12.3))
Console
7.113821138211382
7
When the fraction is exactly .5, 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:

Python
import random
print(random.randint(1, 6))
if random.random() >= 0.5:
    print("You win!")
else:
    print("You lose...")
Console
3
You win!

Practice

1Write the programCurriculum practice

Import math, then print 5.5 rounded with round, then 8.8 rounded down with math.floor.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Print the larger of 10 and 20, then the smaller one, using max and min.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

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.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Complete the condition: if random.random() is 0.7 or more, Grand prize! is printed.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import random
if ___:
    print("Grand prize!")
else:
    print("Consolation prize")
Console
Press Run to see the result.
5Write the programExtra practice

Write a dice program: print a random number from 1 to 6 using random.randint.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

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.

HintUse math.ceil and str().
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
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, then math.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.