A library in Python is a collection of pre-written modules and functions that programmers can use to perform specific tasks without writing the code from scratch. Libraries save time, reduce errors, and allow programmers to build on existing, tested code.
Why do we need libraries? Writing every function from scratch (e.g., calculating square roots or generating random numbers) would be time-consuming and error-prone. Libraries provide ready-made, reliable solutions.
To use a library in Python, you must first import it using the import keyword.
import library_name
Once imported, you access its functions using dot notation:
import math
result = math.sqrt(25)
print(result) # Output: 5.0
You can import only a specific function from a library:
from math import sqrt
print(sqrt(49)) # Output: 7.0
This allows you to call sqrt() directly without the math. prefix.
math ModuleThe math module provides mathematical functions and constants.
| Function / Constant | Description | Example |
|---|---|---|
math.sqrt(x) | Square root of x | math.sqrt(16) → 4.0 |
math.pow(x, y) | x raised to the power y | math.pow(2, 3) → 8.0 |
math.floor(x) | Rounds down to nearest integer | math.floor(4.7) → 4 |
math.ceil(x) | Rounds up to nearest integer | math.ceil(4.2) → 5 |
math.pi | Value of π | 3.141592653589793 |
math.factorial(n) | Factorial of n | math.factorial(5) → 120 |
Example:
import math
radius = 7
area = math.pi * math.pow(radius, 2)
print("Area of circle:", area)
# Output: Area of circle: 153.93804002589985
random ModuleThe random module is used to generate pseudo-random numbers.
| Function | Description | Example |
|---|---|---|
random.random() | Random float between 0.0 and 1.0 | 0.573... |
random.randint(a, b) | Random integer between a and b (inclusive) | random.randint(1, 6) → dice roll |
random.choice(list) | Random element from a list | random.choice(['a','b','c']) |
random.shuffle(list) | Shuffles a list in place | Randomises order |
Example:
import random
# Simulate a dice roll
dice = random.randint(1, 6)
print("You rolled:", dice)
# Pick a random colour
colours = ["red", "green", "blue", "yellow"]
print("Random colour:", random.choice(colours))
turtle ModuleThe turtle module is a built-in Python library used for drawing graphics. It is covered in detail in the Turtle Graphics topic.
import turtle
turtle.forward(100)
turtle.right(90)
import vs from ... import| Method | Syntax | How to call function |
|---|---|---|
| Import whole module | import math | math.sqrt(4) |
| Import specific function | from math import sqrt | sqrt(4) |
| Import with alias | import math as m | m.sqrt(4) |
Problem: Write a program that asks the user for a number and prints its square root rounded to 2 decimal places.
import math
num = float(input("Enter a number: "))
result = math.sqrt(num)
print("Square root:", round(result, 2))
Output (if user enters 2):
Enter a number: 2
Square root: 1.41
import keyword to include a library.math module provides mathematical functions like sqrt(), pow(), and the constant pi.random module generates random numbers using functions like randint() and choice().from module import function to import a specific function directly.