day 21 : understanding builtins model in python
Introduction:
Welcome to Day 21 of my Python journey!
Today, I ventured into the realm of builtins modules .
These concepts are very essential in python.
built-in functions
map : -
. map is a predefined class in python belongs to builtins module
. Syntax to create map object
mapObj = map ( function , sequence/collection )
. map is used to apply a specific function on each individual element avaliable in collection
. to fetch the value from mapObject we have to implement type conversion mechanism
print ( list ( mapObj ))
Example :
def factorial ( no ) :
fact = 1
while no > 0 :
fact = fact * no
no = no - 1
return fact
print ( factorial ( 5 ) )
collection = [ 1, 5, 3, 2, 4 ]
# i want to call factorial function for each individual element avaliable in
# collection
mapObj = map ( factorial , collection )
print ( collection )
print ( list ( mapObj ) )
Example :
collection = [ 1, 2, 3, 4, 5 ]
print ( collection )
print ( list ( map ( lambda number : Even if number % 2 == 0 else Odd , collection ) ) )
filter : -
. filter is a predefined class avaliable in python belongs to builtins module
. Syntax to crate the object of filter class,
filterObj = filter ( function , collection/sequence/iterable )
. filter is used to filter a collection as per the demand of a function .
what the function we are going to use for filter that function must return True or False .
if for an element the result is True that element will be selected .
if the result is False then that element will be rejected
. to get the values from filter object we have to implement type conversion mechanism
print ( list ( filterObj ))
example
collection = [ 1, 2, 3, 4, 5 ]
print ( collection )
print ( list ( map ( lambda number : True if number % 2 == 0 else False ,collection ) ) )
print ( list ( filter ( lambda number : True if number % 2 == 0 else False ,collection ) ) )
reduce : -
. reduce is a predefined function in python belongs to functools module
. from functools import reduce # compulsory
. to call reduce function param are reduce ( function , sequence/collection/iterable )
example
from functools import reduce
max = lambda x, y : x if x > y else y
collection = [ 1, 3, 2, 5, 4 ]
print ( reduce ( max , collection ) )
print ( reduce ( lambda x, y : x if x > y else y , collection )
Challenges :
Understanding builtin modules.
Handling errors.
Resources :
Official Python Documentation: Functions
W3Schools' Python Tutorial: Functions
Scaler's Python Course: Functions
Goals for Tomorrow :
Explore something more on artguments.
that is command line arguments .
Conclusion :
Day 20’s a success!
What are your favorite Python resources? Share in the comments below.
Connect with me :
GitHub: [ https://github.com/p-archana1 ]
LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]
Join the conversation :
Share your own learning experiences or ask questions in the comments.
HAPPY LEARNING!!
THANK YOU!!
Subscribe to my newsletter
Read articles from Archana Prusty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Archana Prusty
Archana Prusty
I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.