Use built-in functions and library routines

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Programming Basics: Built‑in Functions and Library Routines

Programming Basics – Use Built‑in Functions and Library Routines

What are Built‑in Functions?

Built‑in functions are part of the core language. They are always available without importing any module. They perform common tasks such as I/O, type conversion, and sequence handling.

  • print() – output to the console.
  • input() – read a line of text from the user.
  • len() – return the length of a sequence.
  • range() – generate an immutable sequence of numbers.
  • type() – return the data type of an object.
  • int(), float(), str() – type conversion functions.

What are Library Routines?

Library routines are functions that reside in modules (libraries). To use them you must import the relevant module. They extend the language with specialised capabilities such as mathematics, random number generation, file handling, and system interaction.

Common Standard Library Modules

  1. math – mathematical functions and constants.
  2. random – pseudo‑random number generation.
  3. sys – system-specific parameters and functions.
  4. os – operating‑system interface.
  5. datetime – date and time manipulation.

Example: Using the math Module

The math module provides functions such as sqrt(), sin(), and constants like pi. To use them you must import the module first.

import math

radius = 5

area = math.pi * math.pow(radius, 2) # \$A = \pi r^2\$

print("Area =", area)

Example: Using the random Module

Generating a random integer between 1 and 10:

import random

value = random.randint(1, 10)

print("Random value:", value)

Comparison Table

AspectBuilt‑in FunctionLibrary Routine
AvailabilityAlways availableRequires import
Typical UseBasic I/O, type conversion, simple sequence operationsSpecialised tasks – maths, random numbers, file system, networking
NamespaceGlobal namespaceAccessed via module.function or from module import …
PerformanceGenerally faster (no import overhead)May be slightly slower due to module lookup

Best Practices

  1. Use built‑in functions for simple, common operations – they are concise and efficient.
  2. Import only the modules you need; avoid import * to keep the namespace clear.
  3. Prefer explicit imports: from math import sqrt, pi when only a few functions are required.
  4. Read the module documentation to understand default behaviours (e.g., random.seed()).
  5. Handle exceptions that may arise from library routines, especially I/O and system calls.

Typical Exam Questions

  • Write a program that reads a list of numbers and prints the mean using sum() (built‑in) and len() (built‑in).
  • Explain the difference between math.pow(x, y) and the exponentiation operator x**y.
  • Given a scenario that requires random selection, choose an appropriate function from the random module and justify your choice.

Suggested diagram: Flowchart showing the decision process for choosing a built‑in function versus importing a library routine.

Key Take‑aways

  • Built‑in functions are always available and suited for fundamental tasks.
  • Library routines extend functionality; they must be imported and are accessed via a module namespace.
  • Understanding when to use each leads to clearer, more efficient code and better performance in examinations.