Library Routines (Cambridge (CIE) IGCSE Computer Science)
Revision Note
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 choice |
|
Examples in Python
Random code |
---|
# importing random library
# asking user to enter a username and password
# checking if the user and password are correct
# generating a random 4 digit code
|
National lottery |
---|
# Create a list of numbers for the national lottery
# Create an empty list to store the chosen numbers
# Loop to pick 6 numbers from the list
# Use random.choice to pick a number from the list
# Add the chosen number to the list of chosen numbers
# Remove the chosen number from the list of available numbers
# Sort the chosen numbers in ascending order
# Output the 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 |
// Outputs 1.95 |
// Outputs 3.14 |
Last updated:
You've read 0 of your 10 free revision notes
Unlock more, it's free!
Did this page help you?