Programming Using Pre-Existing Libraries (Edexcel GCSE Computer Science)

Revision Note

James Woodhouse

Written by: James Woodhouse

Reviewed by: Lucy Kirkham

Programming Using Pre-Existing Libraries

What is a pre-existing library?

  • A pre-existing library is reusable code that can be used throughout a program

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

  • Using pre-existing libraries saves programmers time by using working code that has already been tested

  • Some examples of pre-existing libraries are:

    • Random

    • Math

    • Turtle

Programming Random Library

What is the random library?

  • 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

Python

Random numbers

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)

Programming Math Library

What is the math library?

  • The math library is a library of code which allows users to make use of mathematical operations which can be performed with ease

  • Examples of math that are most common are:

    • math.pi - the constant Pi straight pi

    • math.sqrt (x) - returns the square root of a number

    • math.floor(x) - returns the largest number, not less than x

    • math.ceil (x) - returns the smallest number, not less than x

Concept

Python

Output

Math.pi

import math

radius = 7

circleArea = (radius**2) * math.pi

153.94

Math.sqrt(x)

import random

number = 200

print(math.sqrt(number)

14.14

Math.floor(x)

print(math.floor(32.7))

32

Math.ceil(x)

print(math.ceil(32.7))

33

Programming Turtle Library

What is the turtle library?

  • The turtle library is a pre-built library of code which allows the creation of images and shapes using a 'pen'

  • Examples of turtle that are most common include:

    • Polygons

    • Complex shapes such as spirographs

  • The turtle library uses a canvas which is based on X, Y coordinates

  • Turtle includes a series of simple instructions which can be used along with fundamental programming constructs to create drawings

  • It is important to note that just like all libraries, the user must import turtle at the beginning of the program

Concept

What it does

Python Example

turtle.pendown()

Puts the pen down ready to begin drawing

turtle.pendown()

turtle.penup()

Lifts the pen up to stop drawing if moving the pen

turtle.penup()

turtle.forward(x)

Moves the turtle forward the number steps in the brackets

turtle.pendown()

turtle.right(x)

Turns the turtle right to the number of degrees specified

turtle.right(90)

turtle.left(x)

Turns the turtle left to the number of degrees specified

turtle.left(90)

turtle.setpos(x,y)

Moves the turtle to the position declared

turtle.setpos(0, 30)

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

the (exam) results speak for themselves:

Did this page help you?

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.

Lucy Kirkham

Author: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.