Resources | Subject Notes | Computer Science | Lesson Plan
Library routines are pre-written blocks of code that perform specific tasks. They are a fundamental part of programming, allowing programmers to reuse code, saving time and effort. Instead of writing the same code repeatedly, we can use library routines, which are often provided by the programming language itself or by external libraries. This promotes code reusability, reduces errors, and improves efficiency.
Library routines can be broadly categorized into:
Here are some examples of common library routines in Python:
Library | Routine | Description | Example |
---|---|---|---|
math | math.sqrt() |
Calculates the square root of a number. | import math; result = math.sqrt(25) # result will be 5.0 |
string | string.upper() |
Converts a string to uppercase. | text = "hello"; upper_case = text.upper() # upper_case will be "HELLO" |
datetime | datetime.now() |
Returns the current date and time. | import datetime; now = datetime.now(); print(now) |
import
statement to make the library's functions available.This example demonstrates how to use the math
library to calculate the area of a circle.
import math
radius = 5
area = math.pi * radius * radius
print("The area of the circle is:", area)
When using library routines, it's important to: