Library Routines (Cambridge (CIE) IGCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Library Routines

What are library routines?

  • A library routine is reusable code that can be used throughout a program

  • The code has been made public through reusable modules or functions

  • Using library routines saves programmers time by using working code that has already been tested

  • Some examples of pre-existing libraries are:

    • Random

    • Round

Random

  • The random library is a library of code which allows users to make use of 'random' in their programs

  • Examples of random that are most common are:

    • Random choices

    • Random numbers

  • Random number generation is a programming concept that involves a computer generating a random number to be used within a program to add an element of unpredictability

  • Examples of where this concept could be used include:

    • Simulating the roll of a dice

    • Selecting a random question (from a numbered list)

    • National lottery

    • Cryptography 

Concept

Pseudocode

Python

Random numbers

RANDOM(1,6)

import random

number = random.randint(1,10)

number = random.randint(-1.0,10.0)

Random choice

import random

choice = random.choice()

Examples in Python

Random code

# importing random library

import random

# asking user to enter a username and password

user = input("Enter a username: ")
pw = input("Enter a password: ")

# checking if the user and password are correct

if user == "admin" and pw == "1234":

# generating a random 4 digit code
  code = random.randint(1000,9999)


print("Your code is", code)

National lottery

import random

# Create a list of numbers for the national lottery

lottery_numbers = list(range(1, 50))

# Create an empty list to store the chosen numbers

chosen_numbers = []

# Loop to pick 6 numbers from the list

for _ in range(6):

# Use random.choice to pick a number from the list

number = random.choice(lottery_numbers)

# Add the chosen number to the list of chosen numbers

chosen_numbers.append(number)

# Remove the chosen number from the list of available numbers

lottery_numbers.remove(number)

# Sort the chosen numbers in ascending order

chosen_numbers.sort()

# Output the chosen numbers

print("The winning numbers are:", chosen_numbers)

Round

  • The round library is a library of code which allows users to round numerical values to a set amount of decimal places

  • An example of rounding is using REAL values and rounding to 2 decimal places

  • The round library can be written as:


    ROUND(<identifier>, <places>)

Concept

Pseudocode

Python

Round

Cost ← 1.9556

OUTPUT ROUND(Cost,2)

// Outputs 1.95

number = 3.14159 rounded_number = round(number, 2)

print(rounded_number)

// Outputs 3.14

Last updated:

You've read 0 of your 10 free revision notes

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.