Phone makers buy their parts
A phone maker does not make the screen, the camera and the battery from nothing. They buy parts that other people made and tested.
Programmers do the same. Other people wrote and tested code for common jobs. You can use it. This lesson shows where it comes from, and its good and bad points.
Key words
| Word | What it means | Example |
|---|---|---|
| Pre-written code | Code that somebody else wrote, tested and shared. | |
| User-written code | Code that you write yourself for your own program. | |
| Built-in function | A function that is part of Python. It is always there. | len(), max(), round() |
| Library | A collection of pre-written code for one kind of job. | math, random |
| import | The Python word that makes a library ready to use. | import math |
| Third-party library | A library made by another company or group. You install it first. | pandas |
| Function | A named piece of code that gives back a result. | |
| Licence | The rules for how you may use somebody else's code. | free to use, but you must name the author |
The cells students fill in are left blank in this preview.
Four places code comes from
| Where from | Who wrote it | Example | Do you import it? |
|---|---|---|---|
| User-written | You | def ticket_cost() | No |
| Built into Python | The Python team | len(), print(), round() | No |
| Python library | The Python team | `math.sqrt()`, `random.randint()` | Yes: import math |
| Third-party library | Another group | pandas, matplotlib | Install it, then import it |
The cells students fill in are left blank in this preview.
Try it: built-in functions
prices = [3.5, 1.2, 2.0]
print(len(prices))
print(max(prices))
print(min(prices))
print(round(7.6))
print(prices.count(2.0))
print(ord("A"))
print(chr(66))
# Add another price to the list and run it again.
Try it: the math library
import math
print(math.floor(7.6))
print(math.ceil(7.6))
print(math.trunc(-7.6))
print(math.sqrt(81))
print(math.pow(2, 5))
print(math.pi)
# Try math.floor(-7.6). What do you expect?
What the math functions give
| You write | It gives | In words |
|---|---|---|
| math.floor(7.6) | 7 | Down to the whole number below |
| math.ceil(7.6) | 8 | Up to the whole number above |
| math.trunc(-7.6) | -7 | Cut off the decimal part |
| round(7.6) | 8 | To the nearest whole number |
| `math.sqrt(81)` | 9.0 | The square root: 9 times 9 is 81 |
| math.pow(2, 5) | 32.0 | Five 2s multiplied: 2 x 2 x 2 x 2 x 2 |
The cells students fill in are left blank in this preview.
Try it: the random library
import random
print(random.randint(1, 6))
print(random.random())
print(random.uniform(1, 10))
print(random.sample(["Sam", "Aisha", "Tom", "Mei"], 2))
# Run it three times. The answers change.
Quick check: where does it come from?
Have a go
Quick check: where does it come from?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Fix it: the missing import
print(math.ceil(2.1))
# Run it. Read the error. Python does not know the name math.
# Add one line at the top to fix it.