Python Standard Library

 

Standard Library

Built-in Functions

Built-in functions are Readily available for reuse.

Some of the built Functions are

  1. print()

  2. max()

  3. min()

  4. len()
    and many more..

Standard Library

Python provides several such useful values (constants), classes and functions.

This collection of predefined utilities is referred as the Python Standard Library

All these functionalities are organized into different modules.

  • In Python context, any file containing a Python code is called a module
  • These modules are further organized into folders known as packages

Different modules are:

  1. collections

  2. random

  3. datetime

  4. math and many more..

Working with Standard Library

To use a functionality defined in a module we need to import that module in our program.

import module_name
PYTHON

Math Module

math module provides us to access some common math functions and constants.

Code

import math
print(math.factorial(5))
print(math.pi)
PYTHON

Output

120
3.141592653589793

Importing module

Importing a module and giving it a new name (aliasing)

Code

import math as m1
print(m1.factorial(5))
PYTHON

Output

120

Importing from a Module

We can import just a specific definition from a module.

Code

from math import factorial
print(factorial(5))
PYTHON

Output

120

Aliasing Imports

We can also import a specific definition from a module and alias it

Code

from math import factorial as fact
print(fact(5))
PYTHON

Output

120

Random module

Randomness is useful in whenever uncertainty is required.

For example: Rolling a dice, flipping a coin, etc.,

random
module provides us utilities to create randomness.

Randint

randint()
is a function in random module which returns a random integer in the given interval.

Code

import random
random_integer = random.randint(1, 10)
print(random_integer)
PYTHON

Output

8

Choice

choice()
is a function in random module which returns a random element from the sequence.

Code

import random
random_ele = random.choice(["A","B","C"])
print(random_ele)
PYTHON

Output

B

To know more about Python Standard Library, go through the authentic python documentation - https://docs.python.org/3/library/

Map, Filter and Reduce

We worked with different sequences (list, tuples, etc.)

To simplify working with sequences we can use

map()
,
filter()
and
reduce()
functions.

Map

map()
applies a given function to each item of a sequence (list, tuple etc.) and returns a sequence of the results.

Example - 1

Code

def square(n):
return n * n
numbers = [1, 2, 3, 4]
result = map(square, numbers)
numbers_square = list(result)
print(numbers_square)
PYTHON

Output

[1, 4, 9, 16]

Example - 2

Code

numbers = list(map(int, input().split()))
print(numbers)
PYTHON

Input

1 2 3 4

Output

[1, 2, 3, 4]

Filter

filter()
method filters the elements of a given sequence based on the result of given function.

The function should return True/False

Code

def is_positive_number(num):
return num > 0
list_a = [1, -2, 3, -4]
positive_nums = filter(is_positive_number, list_a)
print(list(positive_nums))
PYTHON

Output

[1, 3]

Reduce

reduce()
function is defined in the functools module.

Code

from functools import reduce
def sum_of_num(a, b):
return a+b
list_a = [1, 2, 3, 4]
sum_of_list = reduce(sum_of_num, list_a)
print(sum_of_list)
PYTHON

Output

10

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form