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.

Python 3
  1. # shapes.py (the library file)
  2. def area_square(side):
  3. return side * side
  4. # main.py (the program)
  5. # TODO: bring in the shapes library
  6. 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, " + name
print("greet.py is loading")
if __name__ == "__main__":
print(hello("Sam"))
# main.py (the program)
import greet
print(greet.hello("Ali"))

Variables

__name__=__main__

Step 1. You run main.py. Its first line is import greet, so Python now runs greet.py from the top.

Step 1 of 5Called right: 0 of 2

Watch the __name__ value in the state table as the steps move on.

Spot it · extending a library

The extension that broke old programs

Four programs already import shapes and call shapes.area_square(4). A student then extends shapes.py by adding a new function, and now all four programs crash. Which line of the extended library caused it?

The student's extended shapes.py — which line goes wrong?

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.

8 of 8 still to sort.

Write it · design a library

Which functions move out?

def pence_to_pounds(pence): return pence / 100 def add_vat(price): return price * 1.2 def show_menu(): print("1. Add item 2. Pay") def ask_name(): return input("Name? ")

The program below works, but its functions do two different jobs. Say which functions should move into a library, give the library a meaningful file name, write the import statement the program would use, and give two benefits of doing this for this program. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A library of your own = a name.py file of related functions, with a meaningful name.
2import name → call name.function(arguments).
3from name import function → call function(arguments).
4import name as alias → call alias.function(arguments).
5Top-level statements in a library run on import; test code goes under if __name__ == "__main__":.
6Extending: add new functions; keep existing names, parameters and behaviour.

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?
A separate file saved as name.py that holds related functions other programs can import. Create it by writing the function definitions in a new file.
What makes a good library?
It groups functions that belong together and has a meaningful name, so a programmer can tell what it provides.
A library is saved as pricing.py. What do you write to load it?
import pricing, near the top of the program. The import name is the file name without .py.
After import pricing, how do you call its function total with 5?
pricing.total(5): library name, a dot, then the function. The returned value is used like any other function call's.
You wrote from pricing import total. Which call is right: pricing.total(5) or total(5)?
total(5). The function came in under its own name, so there is no library name and no dot.
How do you give a library a shorter name inside one program?
import pricing as p. Calls then use the alias: p.total(5).
Why does a library normally contain only function definitions?
Statements at the top level of a library file run when the library is imported.
What does if __name__ == "__main__": do in a library?
Test code under it runs only when the library file itself is run, not when another program imports it.
How do you extend an existing library of your own?
Add new related function definitions to its file. Every program that imports the library can then call them.
What should stay the same when you extend a library?
The names, parameters and behaviour of the existing functions, so programs that already use the library keep working.
Benefit of reuse: development time
A function written once in a library can be used in many programs without rewriting it.
Benefit of reuse: fewer errors
Library functions that have already been tested can be relied on in new programs, reducing the chance of introducing errors.
Benefit of reuse: maintenance
A correction or improvement made once in the library file is available to every program that imports it.
Benefit of reuse: readability
Moving related functions into a library keeps the main program shorter and easier to read and understand.

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 posted

More Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

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.