AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Ready-made helpers

Week 10 · 1.6 Library programs · 3 lessons
3

Borrowing ready-made code

Computing 8: Topic 1.6, pp. 125–149
GoalSay what a program library is, why programmers use one, and what the benefits and risks are.
New words
library · a collection of ready-made sub-routines that programs can useimport · the Python word that makes a library available to a programtrusted source · a place where code comes from that you or a trusted adult know is safe and reliable
Explanation

A library is a set of ready-made sub-routines that other programmers have written and tested. Python comes with libraries, for example math for number work and random for chance. To use one, you write import at the top of the program, and then call a function with the library name, a dot and the function name, such as math.floor(7.8). Libraries save time because nobody has to write the same code again. The code has usually been tested by many people, so it is likely to work. There are risks too. A library may have mistakes, it may do something different from what you expect, or it may not be maintained. Code from an unknown source could even be harmful. Ask a trusted adult before you add code that you did not write, and always say where code came from.

Worked examples
Python
1import math
2print(math.floor(7.8))
Output
7
Using a library function. The import line comes first. Then math.floor is called with 7.8 and returns 7, which print shows. You will meet more library functions in a later week.
QuestionYour own sub-routineLibrary function
Who wrote it?youother programmers
Has it been tested by many people?only by youusually yes
Time to writelongernone: you import it
Can you see exactly how it works?yesnot always
Writing your own sub-routine compared with using a library function.
BenefitsRisks
saves timemay contain mistakes
already testedmay not do exactly what you need
same code used by many peopleunknown code from an untrusted source could be harmful
programs are shorteryou must trust the author, so ask a trusted adult
Benefits and risks of using a library.
Class activity

Library cards (no computer). Make a card for each of four imaginary library functions, for example round_up, pick_number, add_tax and shout. On each card write the name, what value it needs (the parameter), what it gives back (the return value) and who wrote it. Choose a small problem, such as working out the price of 6 notebooks including tax. Decide which cards you would use and in which order. Then write one benefit and one risk of using ready-made cards. Swap cards with a partner and plan a different problem. If you have a computer or tablet, ask an adult to show you where Python’s math library is described.

Practice
1
Explain in your own words what a program library is and give one example of a library name in Python.
2
The first line of a program is import math. What does the line do, and why is it near the top?
3
Give two benefits and two risks of using code from a library. Say what you would do about the risks.
4
A friend sends you a program file and says it will do your homework in one click. Should you add it to your program? Explain.