GCSE · Computer Science · Eduqas · Spec C500QS
Creating and extending Python 3 libraries
Copied the same function into three programs, then fixed the same bug three times? Put it in a library of your own and write it once.
Try it · Python 3
Break it, read it, fix it
The function area_square used to sit inside main.py. It now lives in its own file, shapes.py, saved in the same folder as main.py, and main.py has to print 16. Press Run and read what Python says. Then pick a replacement for the flagged line and run again. Keep going until 16 prints, then come back and try the other options too.
- # shapes.py (the library file)
- def area_square(side):
- return side * side
- # main.py (the program)
- # TODO: bring in the shapes library
- print(area_square(4))
Messages are what Python 3 prints for main.py. The gutter numbers count down this whole listing; Python numbers each file from line 1.
Predict it · what runs on import
Same file, two ways to run it
Call each printed line before Python shows it, then switch input and run the same code the other way.
Step through, and commit to each printed line before it appears.
# greet.py (the library)def hello(name):return "Hello, " + nameprint("greet.py is loading")if __name__ == "__main__":print(hello("Sam"))# main.py (the program)import greetprint(greet.hello("Ali"))
Variables
Step 1. You run main.py. Its first line is import greet, so Python now runs greet.py from the top.
Watch the __name__ value in the state table as the steps move on.
Sort it · why bother with a library?
Which benefit is this?
Each programmer is enjoying one benefit of reuse. Which one?
Still to sort
Saves development time (0)
Written once, used in many programs without rewriting.
Where the line is: This is about not having to write the function again, not about whether the function is correct.
Relies on tested code (0)
Already-tested functions mean fewer new errors.
Where the line is: This is about trusting code that has already been used and checked. Fixing a mistake later belongs with the next category.
One fix reaches every program (0)
Correct or improve the library file once; every importing program gets it.
Where the line is: This is about changing the library file afterwards. Trusting code you have not changed belongs with the tested category.
Shorter, clearer main program (0)
The details live in the library, so the main program is easier to read.
Where the line is: This is about how the main program reads, not about time or correctness.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Your own .py file of functions, and how the import you choose decides how you call them
What you need to know
- A library of your own is a separate .py file holding related functions that other programs can import and use.
- Create one by writing the function definitions in a new file saved as name.py. The file name without .py is what you import.
- Group functions that belong together, and give the library a meaningful name so a programmer can tell what it provides.
- Load it with import name near the top. In the simplest case, the library file sits in the same folder as the program.
- After import name, call functions with dot notation: name.function(arguments). The returned value works like any other function call's.
Have a goJo wrote import shapes, then print(area_square(4)). She is sure the library is broken. Is it, and what is the one change she needs?
The library is fine. Jo needs the library name and a dot: shapes.area_square(4).
import shapes loads the library under the name shapes, so its functions are reached through that name. Jo's bare name was never defined in her program.
- from name import function lets you call it by its name alone. import name as alias means you call alias.function(arguments).
Have a goYou write import shapes as sh. Write the line that calls area_square with the value 3.
sh.area_square(3)
The alias replaces the library name for this program, so the call starts with sh, not shapes.
- Statements at the top level of a library file run when it is imported, so a library normally holds only function definitions.
Have a gotools.py contains only the line print("hi") at the top level, outside any function. A program runs import tools and nothing else. Does anything print?
Yes. hi is printed during the import.
Top-level statements in a library file run when it is imported, even if no function is ever called. That is why libraries normally contain only function definitions.
- Test code under if __name__ == "__main__": runs only when the library file itself is run, not when another program imports it.
- Extend a library by adding new related function definitions to its file. Keep existing functions' names, parameters and behaviour so older programs still work.
- Reuse pays off four ways: less rewriting, already-tested code, one fix reaching every program, and a shorter, easier-to-read main program.
The big picture
A library of your own is a .py file of related functions that other programs import. The import statement decides how each call is written, and you can extend the file later without breaking the programs that already use it.
Key points
Worked example
Problem
A program converts 100 °C to °F with its own function c_to_f. You want other programs to reuse that function. Move it into a library, use it from main.py, then extend the library with f_to_c without breaking anything.
⚠ Watch out
Writing area_square(4) on its own after import shapes. Python only knows the library under the name shapes, so the call needs the library name and a dot in front. A second slip is writing import shapes.py: the import name is the file name without .py.
Memory hook
You call a function by the name you imported it under. import shapes → shapes.area_square(). from shapes import area_square → area_square(). import shapes as sh → sh.area_square().
Check yourself
Cover the page. pricing.py holds a function total(items). Write all three import forms, each with the matching call to total. Then explain why test code in pricing.py should sit under if __name__ == "__main__":.
Flashcards
(14)What is a library of your own in Python 3, and how do you create one?
What makes a good library?
A library is saved as pricing.py. What do you write to load it?
After import pricing, how do you call its function total with 5?
You wrote from pricing import total. Which call is right: pricing.total(5) or total(5)?
How do you give a library a shorter name inside one program?
Why does a library normally contain only function definitions?
What does if __name__ == "__main__": do in a library?
How do you extend an existing library of your own?
What should stay the same when you extend a library?
Benefit of reuse: development time
Benefit of reuse: fewer errors
Benefit of reuse: maintenance
Benefit of reuse: readability
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Eduqas GCSE Computer Science topics
- Abstraction
- Analyse a set of requirements
- Authentication management routines
- Comparing approaches to requirements
- Consistent programming style
- Creating and extending Python 3 functions/methods
- Decomposition
- Designing a testing strategy
- Explaining test outcomes
- File handling: open a file
- File handling: read into a variable
- File handling: read into an array
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 16 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.