0% found this document useful (0 votes)
19 views8 pages

Day2 Week3 Batch1 (13!05!2025) Jupyter Notebook

The document is a Jupyter Notebook focused on functional programming in Python, covering concepts such as lambda functions, map, filter, and reduce. It provides examples of using these features to perform operations like addition, squaring numbers, and filtering even numbers from a list. Additionally, it introduces iterators and generators, explaining their usage and syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Day2 Week3 Batch1 (13!05!2025) Jupyter Notebook

The document is a Jupyter Notebook focused on functional programming in Python, covering concepts such as lambda functions, map, filter, and reduce. It provides examples of using these features to perform operations like addition, squaring numbers, and filtering even numbers from a list. Additionally, it introduces iterators and generators, explaining their usage and syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

Functional Programming: ¶
Functional programming is a programming paradigm in which we try to bind everything in pre mathematical functional style.
Python support functional programming.

1. Lambda:
It is a anonymous function.
syntax: lambda arg1,agr2:output expression

In [1]: 1 # To find the addition two numbers using normal python fucntions.
2 def add(a,b):
3 return a+b
4 add(12,34)

Out[1]: 46

In [4]: 1 # By using lambda function to find the addition of two numbers?


2 add1 = lambda n1,n2:n1+n2
3 add1(10,20)
4 #print(type(add1))

Out[4]: 30

In [5]: 1 # write a program to find the square of a given number using lambda?
2 s = lambda n:n**2
3 s(3)

Out[5]: 9

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 1/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

In [10]: 1 # using if-else statements in lambda function?


2 # To check whether the given number even or odd?
3 even = lambda a:True if(a%2 == 0) else False
4 even(39)

Out[10]: False

In [12]: 1 # To check whether all three number are equal or to find the biggest three numbers.
2 ​
3 greater=lambda a,b,c:print('a is greater') if(a>b and a>c) else print('b is greater') if(b>c) else print('c is great
4 greater(3,8,1)

 
b is greater

In [21]: 1 # To print 1 to 10 natural numbers using lambda function.


2 ​
3 natural = lambda n:[i for i in range(1,n+1)]
4 natural(10)

Out[21]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

map():
map() function takes two parameters.
syntax: map(function_name,iterable)

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 2/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

In [24]: 1 # To print the square of list of given numbers?


2 def sq(n):
3 return n*n
4 f = map(sq,[10,20,30,40])
5 print(f)
6 print(list(f))

<map object at 0x000001B512D96B80>


[100, 400, 900, 1600]

In [25]: 1 list(map(sq,[i for i in range(1,11)]))

Out[25]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In [42]: 1 # TO return only digits from a string data..


2 s = 'python123@programming78'
3 # output: 1 2 3 7 8
4 def digit(s):
5 for i in s:
6 if i.isdigit():
7 print(i)
8 #digit(s)
9 #dig = lambda s:[i for i in s: if i.isdigit()]
10 print(list(map(digit,'python123@programming78')))

1
2
3
7
8
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, Non
e, None, None, None]

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 3/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

In [36]: 1 for i in s:
2 if i.isdigit():
3 print(i)

1
2
3
7
8

filter:
syntax: filter(function_name,iterable)

In [43]: 1 # To check whether the given number is even or odd


2 def is_even(n):
3 if(n%2 == 0):
4 return True
5 else:
6 return False
7 n = list(filter(is_even,[10,13,45,67,80,100]))
8 print(n)

[10, 80, 100]

reduce:
syntax: reduce(function_name,list)
first we need to import functools module.

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 4/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

In [44]: 1 import functools


2 # TO print the sum of all list elements.
3 L1 = [1,2,3,4]
4 print(functools.reduce(lambda a,b:a+b,L1))

10

Iterators and Generators:


1. Iterator:
A. iter(): It is used to craete a iterator object
B. next(): It return one element at a time.

In [45]: 1 # TO print 1 to 10 natural numbers?


2 for i in range(1,11):
3 print(i)

1
2
3
4
5
6
7
8
9
10

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 5/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

In [52]: 1 # example on Iterator.


2 L1 = [10,20,30,40,50]
3 for i in L1:
4 print(i)
5 # using Iterator
6 I2 = iter(L1)
7 print(I2)
8 print(next(I2))
9 print(next(I2))
10 print(next(I2))
11 print(next(I2))
12 print(next(I2))
13 print(next(I2))

10
20
30
40
50
<list_iterator object at 0x000001B513544D90>
10
20
30
40
50

---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-52-1159d79055e4> in <module>
11 print(next(I2))
12 print(next(I2))
---> 13 print(next(I2))

StopIteration:

Generator:

In generator having one keyword i.e 'yield'

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 6/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

By using 'yield' keyword we can get the list of elements

In [58]: 1 # example on generator.


2 def sample():
3 i = 1
4 while(i<=10):
5 yield i
6 i = i+1
7 print(sample())
8 print(set(sample()))

<generator object sample at 0x000001B512DAF190>


{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

1 ## Modules and packges:


2 - 1. module:
3 - Module is nothing .py python file.
4 - In that file we can write such as functions,variables,classes..
5 - By using import keyword we can import the module.
6 - syntax: import module_name
7 - How to access the module:
8 module_name.function_name
9 module_name.variable_name
10 module_name.class_name
11 - 2. packages:
12 - Package is nothing but it is a directory or folder.
13 - In that package we can store the number of modules.
14 - syntax:
15 import package.module
16 - How to access:
17 package.module.function_name
18 package.module.variable
19 package.module.class

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 7/8
5/13/25, 12:58 PM day2-week3-batch1(13-05-2025) - Jupyter Notebook

localhost:8889/notebooks/SOI-Batch1-(April-2025)/week3/day2-week3-batch1(13-05-2025).ipynb 8/8

You might also like