Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Using code that others wrote

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

40 minutes

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

WordWhat it meansExample
Pre-written codeCode that somebody else wrote, tested and shared.
User-written codeCode that you write yourself for your own program.
Built-in functionA function that is part of Python. It is always there.len(), max(), round()
LibraryA collection of pre-written code for one kind of job.math, random
importThe Python word that makes a library ready to use.import math
Third-party libraryA library made by another company or group. You install it first.pandas
FunctionA named piece of code that gives back a result.
LicenceThe 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 fromWho wrote itExampleDo you import it?
User-writtenYoudef ticket_cost()No
Built into PythonThe Python teamlen(), print(), round()No
Python libraryThe Python team`math.sqrt()`, `random.randint()`Yes: import math
Third-party libraryAnother grouppandas, matplotlibInstall it, then import it

The cells students fill in are left blank in this preview.

Try it: built-in functions

Code: python
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

Code: python
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 writeIt givesIn words
math.floor(7.6)7Down to the whole number below
math.ceil(7.6)8Up to the whole number above
math.trunc(-7.6)-7Cut off the decimal part
round(7.6)8To the nearest whole number
`math.sqrt(81)`9.0The square root: 9 times 9 is 81
math.pow(2, 5)32.0Five 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

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Fix it: the missing import

Code: python
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.

The lesson goes on

8 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.