0% found this document useful (0 votes)
0 views

python41

The document provides an overview of the Python Standard Library, detailing built-in functions and modules such as math and random. It explains how to import modules, alias them, and utilize functions like map, filter, and reduce for sequence manipulation. Additionally, it includes code examples demonstrating the application of these concepts.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

python41

The document provides an overview of the Python Standard Library, detailing built-in functions and modules such as math and random. It explains how to import modules, alias them, and utilize functions like map, filter, and reduce for sequence manipulation. Additionally, it includes code examples demonstrating the application of these concepts.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 1/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

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..


https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 2/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

Working with Standard Library

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


our program.
PYTHON

1 import module_name

Math Module

math module provides us to access some common math functions and


constants.

Code
PYTHON

1 import math
2 print(math.factorial(5))
3 print(math.pi)

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 3/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

Output

120
3.141592653589793

Importing module

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

Code
PYTHON

1 import math as m1
2 print(m1.factorial(5))

Output

120

Importing from a Module

We can import just a specific definition from a module.

Code
PYTHON

1 from math import factorial


2 print(factorial(5))

Output

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 4/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

120

Aliasing Imports

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

Code
PYTHON

1 from math import factorial as fact


2 print(fact(5))

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.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 5/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

Randint

randint() is a function in random module which returns a random integer


in the given interval.

Code
PYTHON

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

Output

Choice

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 6/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

choice() is a function in random module which returns a random element


from the sequence.

Code
PYTHON

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

Output

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.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 7/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

Example - 1

Code
PYTHON

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

Output

[1, 4, 9, 16]

Example - 2

Code
PYTHON

1 numbers = list(map(int, input().split()))


2 print(numbers)

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 8/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

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
PYTHON

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

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 9/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

Output

[1, 3]

Reduce

reduce() function is defined in the functools module.

Code

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d6517… 10/11
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave

PYTHON

1 from functools import reduce


2
3 def sum_of_num(a, b):
4 return a+b
5
6 list_a = [1, 2, 3, 4]
7 sum_of_list = reduce(sum_of_num, list_a)
8 print(sum_of_list)

Output

10

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=4d65173… 11/11

You might also like