x Python Library Functions | QuiteXams

What is Computer

Module: M3-R5: Python Programming

Chapter: Ch1 Computer Intro

🔹 What are Python Library Functions?

Python library functions are **predefined functions** provided by Python libraries and modules. They save time and effort as you don’t need to write the code from scratch.

🔹 Example 1: Using math Library
import math

print("Square root of 16:", math.sqrt(16))
print("Value of pi:", math.pi)
print("Factorial of 5:", math.factorial(5))

Output:

Square root of 16: 4.0
Value of pi: 3.141592653589793
Factorial of 5: 120
🔹 Example 2: Using random Library
import random

print("Random number between 1 and 10:", random.randint(1, 10))
print("Random choice from list:", random.choice(['Python', 'C++', 'Java']))

Output:

Random number between 1 and 10: 7
Random choice from list: Python
🔹 Example 3: Using datetime Library
import datetime

today = datetime.date.today()
now = datetime.datetime.now()
print("Today's Date:", today)
print("Current Time:", now)

Output:

Today's Date: 2025-10-19
Current Time: 2025-10-19 20:35:12.345678
🔹 Example 4: Using os Library
import os

print("Current Working Directory:", os.getcwd())
print("List of files in current directory:", os.listdir())

Output:

Current Working Directory: /home/user
List of files in current directory: ['file1.py', 'file2.py', 'folder1']
🔹 Example 5: Using sys Library
import sys

print("Python Version:", sys.version)
print("System Path:", sys.path)

Output:

Python Version: 3.11.6 (default, ...)
System Path: ['...', '/usr/lib/python3.11', ...]
✅ Summary
  • Python library functions are pre-written functions from standard libraries.
  • They can be imported using the import statement.
  • Examples: math, random, datetime, os, sys.
  • Saves time and makes code modular and efficient.
Quick Links