Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course
3.5 Python Libraries: Using Code Others Have Written
Unit 3 – Programming Fundamentals · 60 min read · 9 MCQs to check yourself
Modules with math, random and pip
1. Modules, packages and libraries
- Module: a single Python file of reusable code (e.g.
math). - Package: a folder of related modules.
- Library: a collection of modules/packages for a purpose (e.g. matplotlib for charts).
- Standard library: comes with Python –
math,random,statistics,datetime,turtle,time. - Third-party libraries: written by others, installed with pip:
pip install matplotlib(search on pypi.org).
Why use libraries? Save time (do not reinvent the wheel), reliable and tested code, complex tasks in a few lines (charts, AI, web), focus on solving your problem – this is abstraction: you call math.sqrt(49) without knowing the square-root algorithm inside.
2. Ways to import
import math
from random import randint, choice
import statistics as st
print(math.sqrt(49), math.pi > 3.14, math.factorial(5))
print(math.ceil(4.2), math.floor(4.8), round(3.14159, 2))
print(st.mean([70, 80, 90]), st.median([3, 9, 4]))
Output:
7.0 True 120
5 4 3.14
80 4
3. Useful standard modules
| Module | Useful functions | Use |
|---|---|---|
math |
sqrt, pow, pi, ceil, floor, factorial, sin, cos, log |
Mathematics |
random |
random(), randint(a, b), choice(list), shuffle(list) |
Games, dice, quizzes, sampling |
statistics |
mean, median, mode, stdev |
Data analysis (Unit 4) |
datetime |
date.today(), datetime.now() |
Dates and ages |
time |
sleep(s), time() |
Delays and timing |
turtle |
drawing | Graphics |
Dice game with random:
import random
random.seed(1) # fixed seed so the result is repeatable
roll = random.randint(1, 6)
print("You rolled", roll)
Output:
You rolled 2
4. Third-party libraries – simple to use, complex inside
| Library | Install | What it does in a few lines |
|---|---|---|
| matplotlib | pip install matplotlib |
Bar, line, pie charts (used in Unit 4) |
| qrcode | pip install qrcode |
Makes a QR code image |
| pyttsx3 | pip install pyttsx3 |
Text-to-speech |
| requests | pip install requests |
Download data from websites/APIs |
| pandas | pip install pandas |
Tables of data like a spreadsheet |
| emoji | pip install emoji |
Emojis in text |
Example – a bar chart with matplotlib (5 lines!):
import matplotlib.pyplot as plt
subjects = ["CS", "Math", "Physics", "English"]
marks = [78, 65, 70, 82]
plt.bar(subjects, marks, color="teal")
plt.title("My marks")
plt.show()
Example – QR code for the college website:
import qrcode
img = qrcode.make("https://www.fbise.edu.pk")
img.save("fbise_qr.png")
Inside, qrcode handles error-correction codes, masking patterns and image drawing – thousands of lines you never see. That is abstraction.
Import libraries and random numbers – Python for beginners
Short and long questions
1. What is a library in Python? Why do programmers use libraries?
Model answer
A library is a collection of pre-written, tested modules (functions and classes) that can be imported and reused. Reasons: saves time (no need to write complex code), reliable, makes difficult tasks easy in a few lines (charts, AI, web), and lets programmers focus on their own problem – abstraction hides the internal implementation.
2. Differentiate between standard and third-party libraries with two examples each.
Model answer
Standard libraries come built into Python and need no installation: math, random, statistics, datetime, turtle. Third-party libraries are developed by others and installed with pip from PyPI: matplotlib, pandas, qrcode, requests.
3. Write a program that uses the random module to simulate rolling two dice and prints their sum.
Model answer
import random
d1 = random.randint(1, 6)
d2 = random.randint(1, 6)
print("Dice:", d1, d2, "Sum:", d1 + d2)
4. Write the output: import math; print(math.sqrt(64), math.factorial(4), math.ceil(2.1))
Model answer
8.0 24 3 – sqrt returns a float (8.0), 4! = 24, ceil rounds 2.1 up to 3.
This lesson ends with a quiz of 9 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 3.4 Turtle Graphics: Drawing Shapes with Python · Next: 3.6 Lists: Sequence and Repetition with Collections
All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course