0% found this document useful (0 votes)
7 views3 pages

Chapter 8 - Library Routines

Library routines are reusable code modules or functions that save programmers time by providing tested code. The document discusses two specific libraries: 'random', which generates random numbers and choices, and 'round', which rounds numerical values to specified decimal places. Examples in Python illustrate how to implement these libraries for various applications, such as generating lottery numbers and rounding values.

Uploaded by

chifokoyol
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Chapter 8 - Library Routines

Library routines are reusable code modules or functions that save programmers time by providing tested code. The document discusses two specific libraries: 'random', which generates random numbers and choices, and 'round', which rounds numerical values to specified decimal places. Examples in Python illustrate how to implement these libraries for various applications, such as generating lottery numbers and rounding values.

Uploaded by

chifokoyol
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Library Routines

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 = [Link](1,10)

number = [Link](-1.0, 10.0)


RANDOM CHOICE import random

choice = [Link]()
Function requires a non-empty sequence
argument, such as a list, tuple, or string,
from which to select a random element.

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 =
[Link](1000, 9999)

# printing the code print("Your code is",


code) else: print("Invalid username or
password.")
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 [Link] to pick a number from the list
number = [Link](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
number 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 number = 3.14159
OUTPUT ROUND(Cost,2) rounded_number = round(number, 2)
# Outputs 1.95 print(rounded_number) # Outputs 3.14

You might also like