SlideShare a Scribd company logo
Name of the School: School of computer science and engineering
Course Code: OOP Course Name: E2UC201
Faculty Name: Rahul Anjana Programe Name:
Topic:
 FUNCTIONAL PROGRAMMING:
 First class,
 Higher order
 Proxy function
 Lambda function
Functional Programming
Functional Programming
Imperative programming is a paradigm in computer science that uses statements to
change a program's state. It's based on a statement-at-a-time paradigm, and the
order in which operations occur is crucial. Imperative programming languages
require an understanding of the functions necessary to solve a problem, rather than
a reliance on models that are able to solve it.
Example: C, Java, and Python.
Declarative programming is a coding style that tells a program what to do, not how
to do it. It uses a domain-specific language (DSL) that is usually closer to natural
language than pseudocode, making it more readable and easier to learn. The DSL
abstracts much of the boilerplate, leaving fewer lines of code to do the same work.
Example: HTML, SQL, CSS and XML. (eXtensible Markup Language)-
XML stands for Extensible Markup Language. It is a type of markup language and
file format used to store, transport, and recreate arbitrary data.CSS-Cascading Style
Sheets
Functional Programming
Pure and Impure Function:
 Functions that don’t modify their arguments or produce any other side-effects are called pure.
 Functions that modify their arguments or cause other actions to occur are called impure.
Functional Programming
#Example Impure:
def
remove_last(input_list):
input_list.pop()
names = [‘A', ‘B', ‘C']
remove_last(names)
print(names)
remove_last(names)
print(names)
#Example Pure:
def remove_last(input_list):
new_list = input_list.copy()
new_list.pop()
return new_list
names = ['A', 'B', 'C']
new_names = remove_last(names)
print(names)
print(new_names)
[‘A', ‘B']
[‘A']
[‘A', ‘B‘,’C’]
[‘A‘,’B’]
Functional Programming
Functional programming is a declarative programming paradigm style where one applies
pure functions in sequence to solve complex problems.
Functions take an input value and produce an output value without being affected by
the program.
Functional Programming Concepts
Functional Programming Concepts:
 First-class functions
 Recursion
 Immutability
 Pure functions
 High order functions
Functional Programming Concepts
1. Functions are objects: Python functions are first class objects. In the example below, we
are assigning function to a variable. This assignment doesn’t call the function. It takes the
function object referenced by shout and creates a second name pointing to it, yell.
Functional Programming Concepts
Functional Programming Concepts:
 Recursion: Functional programming languages rely on recursion rather than iteration.
Instead of iterating over a loop, a function in a functional programming language will call
itself.
# Solving for a factorial using recursion
def recursiveFactorial(n):
if (n == 0):
return 1;
# recursion call
return n *recursiveFactorial(n - 1);
print(recursiveFactorial(5))
Functional Programming Concepts
First-class functions First-class functions
In Python, the term “first-class function” refers to a function’s ability to be treated as an :
 object that can be assigned to a variable,
 used as an argument for other functions, and
 returned as a value.
As a result, functions in Python are identical to other objects like strings, integers, and lists.
Functional Programming Concepts
First-class functions First-class functions
 Function can be assigned to a variable:This allows for easy manipulation and reuse of
functions.
Example:
def square(x):
return x ** 2
my_func = square
print(my_func(3)) # Output: 9
Functional Programming Concepts
First-class functions First-class functions
 Functions can be passed as arguments to other functions: This is helpful for writing more
modular, reusable code as well as higher-order functions.
Exmple:
def apply_operation(func, x):
return func(x)
def square(x):
return x ** 2
print(apply_operation(square, 3)) # Output: 9
Functional Programming Concepts
First-class functions First-class functions
 Functions can also return values from other functions: This is useful for returning functions
based on specific criteria or for creating functions on the fly.
Example:
def get_operation(op):
if op == '+':
def add(x, y):
return x + y
return add
elif op == '-':
def subtract(x, y):
return x - y
return subtract
add_func = get_operation('+')
subtract_func = get_operation('-')
print(add_func(3, 4)) # Output: 7
print(subtract_func(10, 5)) # Output: 5
Functional Programming Concepts
Higher order functions
 A function that accepts other functions as parameters or returns functions as outputs is
called a high order function.
 This process applies a function to its parameters at each iteration while returning a new
function that accepts the next parameter.
 Common examples of higher-order functions include filter, map, and reduce.
 The idea of first-class functions in Python makes higher-order functions possible.
 Higher-order functions operate by accepting a function as an argument, altering it, and then
returning the altered function.
 More modular and reusable code can be produced as a result.
Built-in Higher-Order Functions in Python
Map, Filter, and Reduce
Functional Programming Concepts
Higher order functions
 Example
def apply_func(func, lst):
return [func(x) for x in lst]
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = apply_func(square,
numbers) print(squared_numbers)
# Output: [1, 4, 9, 16, 25]
Example 2
def make_adder(n):
def adder(x):
return x + n
return adder
add_five = make_adder(5)
print(add_five(10))
# Output: 15
Functional Programming Concepts
Proxy function
 The Proxy method is Structural design pattern that allows you to provide the replacement
for an another object.
 Here, we use different classes to represent the functionalities of another class.
 The most important part is that here we create an object having original object functionality
to provide to the outer world.
Functional Programming Concepts
Lambda function: What is a Lambda Function?
 Lambda functions are similar to user-defined functions but without a name.
 They're commonly referred to as anonymous functions.
 Lambda functions are efficient whenever you want to create a function that will only
contain simple expressions – that is, expressions that are usually a single line of a
statement.
 They're also useful when you want to use the function once.
Note: The anonymous function does not have a return keyword. This is because the
anonymous function will automatically return the result of the expression in the function once
it is executed.
Functional Programming Concepts
Lambda function: When Should You Use a Lambda Function?
You should use the lambda function to create simple expressions. For example,
expressions that do not include complex structures such as if-else, for-loops, and so
on.
So, for example, if you want to create a function with a for-loop, you should use a user-
defined function.
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
lambda argument(s) : expression
1.lambda is a keyword in Python for defining the anonymous function.
2.argument(s) is a placeholder, that is a variable that will be used to hold the value you
want to pass into the function expression. A lambda function can have multiple variables
depending on what you want to achieve.
3.expression is the code you want to execute in the lambda function.
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
lambda argument(s) : expression
Example:
(lambda x : x * 2)(3)
>> 6
def f(x):
return x * 2
print(f(3))
x=lambda x:x*3
print(x(2))
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
Immediately Invoked Function Expression
(lambda x, y: x + y)(2, 3)
(lambda x: x + 1)(2)
>>>high_ord_func = lambda x, func: x + func(x)
>>> high_ord_func(2, lambda x: x * x)
6
>>> high_ord_func(2, lambda x: x + 3)
7
Functional Programming Concepts
Examples:
(lambda x, y, z: x + y + z)(3, 8, 1)
12
print((lambda x: x if(x > 10) else 10)(5))
10
print((lambda x: x if(x > 10) else 10)(12))
12
Functional Programming Concepts
Examples:
(lambda x: x * 10 if x > 10 else (x * 5 if x < 5 else x))(11)
>>> high_ord_func = lambda x, func: x + func(x)
>>> high_ord_func(2, lambda x: x * x)
(lambda x, y, z=3: x + y + z)(1, 2)
(lambda x, y, z=3: x + y + z)(1, y=2)
(lambda *args: sum(args))(1,2,3)
y = 6
z = lambda x: x * y
print (z(8))
Functional Programming Concepts
Examples:
def myfunc(n):
return lambda a : a * n
Functional Programming Concepts
Examples:
min = (lambda x, y: x if x < y else y)
print(min(2, 3))
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
The functions map(), filter(), and reduce() all do the same thing: They
each take a function and a list of elements, and then return the result of
applying the function to each element in the list. As previously stated,
Python has built-in functions like map(), filter(), and reduce().
Functional Programming Concepts
we have three main functions:
•map()
•filter()
•reduce()
The map() function-The map() function or map and filter in Python (also
called as map filter in Python) is a higher-order function.
SYNTAX: map(function, iterables)
EXAMPLE-
def function(a):
return a*a
x = map(function, (1,2,3,4)) #x is the map object
print(set(x))
Functional Programming Concepts
x is a map object, as you can see. The map function is displayed next, which takes
“function()” as a parameter and then applies “a * a” to all ‘iterables’. As a result, all iterables’
values are multiplied by themselves before being returned.
The filter() function-
The filter() function is used to generate an output list of values that return true when the
function is called. It has the following syntax:
SYNTAX: filter (function, iterables)
This function like python map function map(), can take user-defined functions and lambda
functions as parameters.
EXAMPLE-
def func(x):
if x>=3:
return x
y = filter(func, (1,2,3,4))
print(y)
OUTPUT-[3, 4]
As you can see, y is the filter object, and the list is a collection of true values for the condition
(x>=3).
The reduce() function-
The reduce() function applies a provided function to ‘iterables’ and returns a single value, as the name
implies.
SYNTAX: reduce(function, iterables)
The function specifies which expression should be applied to the ‘iterables’ in this case. The function tools
module must be used to import this function.
EXAMPLE-
from functools import reduce
reduce(lambda a,b: a+b,[23,21,45,98])
OUTPUT-187
The reduce function in the preceding example adds each iterable in the list one by one and returns a single
result.

More Related Content

Similar to OOPS Object oriented Programming PPT Tutorial (20)

Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
Svetlin Nakov
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
 
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
 
JNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptxJNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
function ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptxfunction ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
Jagdish Chavan
 
Scala qq
Scala qqScala qq
Scala qq
羽祈 張
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
Chapter 2 Python Functions
Chapter 2               Python FunctionsChapter 2               Python Functions
Chapter 2 Python Functions
11210208
 
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
Rethink programming: a functional approach
Rethink programming: a functional approachRethink programming: a functional approach
Rethink programming: a functional approach
Francesco Bruni
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
RohitYadav830391
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
kimmidalboc0
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
An Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User GroupAn Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User Group
Andreas Pauley
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
Svetlin Nakov
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
 
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
function ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptxfunction ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
Jagdish Chavan
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
Chapter 2 Python Functions
Chapter 2               Python FunctionsChapter 2               Python Functions
Chapter 2 Python Functions
11210208
 
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
Rethink programming: a functional approach
Rethink programming: a functional approachRethink programming: a functional approach
Rethink programming: a functional approach
Francesco Bruni
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
kimmidalboc0
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
An Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User GroupAn Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User Group
Andreas Pauley
 

Recently uploaded (20)

Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
Forecasting Road Accidents Using Deep Learning Approach: Policies to Improve ...
Forecasting Road Accidents Using Deep Learning Approach: Policies to Improve ...Forecasting Road Accidents Using Deep Learning Approach: Policies to Improve ...
Forecasting Road Accidents Using Deep Learning Approach: Policies to Improve ...
Journal of Soft Computing in Civil Engineering
 
Presentación Tomografía Axial Computarizada
Presentación Tomografía Axial ComputarizadaPresentación Tomografía Axial Computarizada
Presentación Tomografía Axial Computarizada
Juliana Ovalle Jiménez
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
International Journal of Advance Robotics & Expert Systems (JARES)
International Journal of Advance Robotics & Expert Systems (JARES)International Journal of Advance Robotics & Expert Systems (JARES)
International Journal of Advance Robotics & Expert Systems (JARES)
jaresjournal868
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
MODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational AutoencoderMODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational Autoencoder
DivyaMeenaS
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
Presentación Tomografía Axial Computarizada
Presentación Tomografía Axial ComputarizadaPresentación Tomografía Axial Computarizada
Presentación Tomografía Axial Computarizada
Juliana Ovalle Jiménez
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
International Journal of Advance Robotics & Expert Systems (JARES)
International Journal of Advance Robotics & Expert Systems (JARES)International Journal of Advance Robotics & Expert Systems (JARES)
International Journal of Advance Robotics & Expert Systems (JARES)
jaresjournal868
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
MODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational AutoencoderMODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational Autoencoder
DivyaMeenaS
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Ad

OOPS Object oriented Programming PPT Tutorial

  • 1. Name of the School: School of computer science and engineering Course Code: OOP Course Name: E2UC201 Faculty Name: Rahul Anjana Programe Name: Topic:  FUNCTIONAL PROGRAMMING:  First class,  Higher order  Proxy function  Lambda function
  • 3. Functional Programming Imperative programming is a paradigm in computer science that uses statements to change a program's state. It's based on a statement-at-a-time paradigm, and the order in which operations occur is crucial. Imperative programming languages require an understanding of the functions necessary to solve a problem, rather than a reliance on models that are able to solve it. Example: C, Java, and Python. Declarative programming is a coding style that tells a program what to do, not how to do it. It uses a domain-specific language (DSL) that is usually closer to natural language than pseudocode, making it more readable and easier to learn. The DSL abstracts much of the boilerplate, leaving fewer lines of code to do the same work. Example: HTML, SQL, CSS and XML. (eXtensible Markup Language)- XML stands for Extensible Markup Language. It is a type of markup language and file format used to store, transport, and recreate arbitrary data.CSS-Cascading Style Sheets
  • 4. Functional Programming Pure and Impure Function:  Functions that don’t modify their arguments or produce any other side-effects are called pure.  Functions that modify their arguments or cause other actions to occur are called impure.
  • 5. Functional Programming #Example Impure: def remove_last(input_list): input_list.pop() names = [‘A', ‘B', ‘C'] remove_last(names) print(names) remove_last(names) print(names) #Example Pure: def remove_last(input_list): new_list = input_list.copy() new_list.pop() return new_list names = ['A', 'B', 'C'] new_names = remove_last(names) print(names) print(new_names) [‘A', ‘B'] [‘A'] [‘A', ‘B‘,’C’] [‘A‘,’B’]
  • 6. Functional Programming Functional programming is a declarative programming paradigm style where one applies pure functions in sequence to solve complex problems. Functions take an input value and produce an output value without being affected by the program.
  • 7. Functional Programming Concepts Functional Programming Concepts:  First-class functions  Recursion  Immutability  Pure functions  High order functions
  • 8. Functional Programming Concepts 1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
  • 9. Functional Programming Concepts Functional Programming Concepts:  Recursion: Functional programming languages rely on recursion rather than iteration. Instead of iterating over a loop, a function in a functional programming language will call itself. # Solving for a factorial using recursion def recursiveFactorial(n): if (n == 0): return 1; # recursion call return n *recursiveFactorial(n - 1); print(recursiveFactorial(5))
  • 10. Functional Programming Concepts First-class functions First-class functions In Python, the term “first-class function” refers to a function’s ability to be treated as an :  object that can be assigned to a variable,  used as an argument for other functions, and  returned as a value. As a result, functions in Python are identical to other objects like strings, integers, and lists.
  • 11. Functional Programming Concepts First-class functions First-class functions  Function can be assigned to a variable:This allows for easy manipulation and reuse of functions. Example: def square(x): return x ** 2 my_func = square print(my_func(3)) # Output: 9
  • 12. Functional Programming Concepts First-class functions First-class functions  Functions can be passed as arguments to other functions: This is helpful for writing more modular, reusable code as well as higher-order functions. Exmple: def apply_operation(func, x): return func(x) def square(x): return x ** 2 print(apply_operation(square, 3)) # Output: 9
  • 13. Functional Programming Concepts First-class functions First-class functions  Functions can also return values from other functions: This is useful for returning functions based on specific criteria or for creating functions on the fly. Example: def get_operation(op): if op == '+': def add(x, y): return x + y return add elif op == '-': def subtract(x, y): return x - y return subtract add_func = get_operation('+') subtract_func = get_operation('-') print(add_func(3, 4)) # Output: 7 print(subtract_func(10, 5)) # Output: 5
  • 14. Functional Programming Concepts Higher order functions  A function that accepts other functions as parameters or returns functions as outputs is called a high order function.  This process applies a function to its parameters at each iteration while returning a new function that accepts the next parameter.  Common examples of higher-order functions include filter, map, and reduce.  The idea of first-class functions in Python makes higher-order functions possible.  Higher-order functions operate by accepting a function as an argument, altering it, and then returning the altered function.  More modular and reusable code can be produced as a result. Built-in Higher-Order Functions in Python Map, Filter, and Reduce
  • 15. Functional Programming Concepts Higher order functions  Example def apply_func(func, lst): return [func(x) for x in lst] def square(x): return x ** 2 numbers = [1, 2, 3, 4, 5] squared_numbers = apply_func(square, numbers) print(squared_numbers) # Output: [1, 4, 9, 16, 25] Example 2 def make_adder(n): def adder(x): return x + n return adder add_five = make_adder(5) print(add_five(10)) # Output: 15
  • 16. Functional Programming Concepts Proxy function  The Proxy method is Structural design pattern that allows you to provide the replacement for an another object.  Here, we use different classes to represent the functionalities of another class.  The most important part is that here we create an object having original object functionality to provide to the outer world.
  • 17. Functional Programming Concepts Lambda function: What is a Lambda Function?  Lambda functions are similar to user-defined functions but without a name.  They're commonly referred to as anonymous functions.  Lambda functions are efficient whenever you want to create a function that will only contain simple expressions – that is, expressions that are usually a single line of a statement.  They're also useful when you want to use the function once. Note: The anonymous function does not have a return keyword. This is because the anonymous function will automatically return the result of the expression in the function once it is executed.
  • 18. Functional Programming Concepts Lambda function: When Should You Use a Lambda Function? You should use the lambda function to create simple expressions. For example, expressions that do not include complex structures such as if-else, for-loops, and so on. So, for example, if you want to create a function with a for-loop, you should use a user- defined function.
  • 19. Functional Programming Concepts Lambda function: How to Define a Lambda Function? lambda argument(s) : expression 1.lambda is a keyword in Python for defining the anonymous function. 2.argument(s) is a placeholder, that is a variable that will be used to hold the value you want to pass into the function expression. A lambda function can have multiple variables depending on what you want to achieve. 3.expression is the code you want to execute in the lambda function.
  • 20. Functional Programming Concepts Lambda function: How to Define a Lambda Function? lambda argument(s) : expression Example: (lambda x : x * 2)(3) >> 6 def f(x): return x * 2 print(f(3)) x=lambda x:x*3 print(x(2))
  • 21. Functional Programming Concepts Lambda function: How to Define a Lambda Function? Immediately Invoked Function Expression (lambda x, y: x + y)(2, 3) (lambda x: x + 1)(2) >>>high_ord_func = lambda x, func: x + func(x) >>> high_ord_func(2, lambda x: x * x) 6 >>> high_ord_func(2, lambda x: x + 3) 7
  • 22. Functional Programming Concepts Examples: (lambda x, y, z: x + y + z)(3, 8, 1) 12 print((lambda x: x if(x > 10) else 10)(5)) 10 print((lambda x: x if(x > 10) else 10)(12)) 12
  • 23. Functional Programming Concepts Examples: (lambda x: x * 10 if x > 10 else (x * 5 if x < 5 else x))(11) >>> high_ord_func = lambda x, func: x + func(x) >>> high_ord_func(2, lambda x: x * x) (lambda x, y, z=3: x + y + z)(1, 2) (lambda x, y, z=3: x + y + z)(1, y=2) (lambda *args: sum(args))(1,2,3) y = 6 z = lambda x: x * y print (z(8))
  • 24. Functional Programming Concepts Examples: def myfunc(n): return lambda a : a * n
  • 25. Functional Programming Concepts Examples: min = (lambda x, y: x if x < y else y) print(min(2, 3)) def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11))
  • 26. The functions map(), filter(), and reduce() all do the same thing: They each take a function and a list of elements, and then return the result of applying the function to each element in the list. As previously stated, Python has built-in functions like map(), filter(), and reduce().
  • 27. Functional Programming Concepts we have three main functions: •map() •filter() •reduce() The map() function-The map() function or map and filter in Python (also called as map filter in Python) is a higher-order function. SYNTAX: map(function, iterables) EXAMPLE- def function(a): return a*a x = map(function, (1,2,3,4)) #x is the map object print(set(x))
  • 28. Functional Programming Concepts x is a map object, as you can see. The map function is displayed next, which takes “function()” as a parameter and then applies “a * a” to all ‘iterables’. As a result, all iterables’ values are multiplied by themselves before being returned. The filter() function- The filter() function is used to generate an output list of values that return true when the function is called. It has the following syntax: SYNTAX: filter (function, iterables) This function like python map function map(), can take user-defined functions and lambda functions as parameters. EXAMPLE- def func(x): if x>=3: return x y = filter(func, (1,2,3,4)) print(y)
  • 29. OUTPUT-[3, 4] As you can see, y is the filter object, and the list is a collection of true values for the condition (x>=3). The reduce() function- The reduce() function applies a provided function to ‘iterables’ and returns a single value, as the name implies. SYNTAX: reduce(function, iterables) The function specifies which expression should be applied to the ‘iterables’ in this case. The function tools module must be used to import this function. EXAMPLE- from functools import reduce reduce(lambda a,b: a+b,[23,21,45,98]) OUTPUT-187
  • 30. The reduce function in the preceding example adds each iterable in the list one by one and returns a single result.