SlideShare a Scribd company logo
1 of 26
Download to read offline
Python
Functional Programming
Haim Michael
November 14th
, 2018
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
lifemichael
Part 1:
https://youtu.be/nhQc-o0dUcM
Part 2:
https://youtu.be/IGeuQ1UBg1c
© 1996-2018 All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More
than 18 years of Practical Experience.
lifemichael
© 1996-2018 All Rights Reserved.
Haim Michael Introduction
● Professional Certifications
Zend Certified Engineer in PHP
Certified Java Professional
Certified Java EE Web Component Developer
OMG Certified UML Professional
● MBA (cum laude) from Tel-Aviv University
Information Systems Management
lifemichael
© 2008 Haim Michael 20151026
Introduction
 The main programming languages paradigms include
the following.
Imperative Paradigm
The program includes statements executed one by one that change the
state.
Structured Paradigm
Based on Imperative. The code has a more logical structure. Avoiding the
goto statement.
© 2008 Haim Michael 20151026
Introduction
Procedural Paradigm
Based on Structured. The code is split into procedures been called in
order to have a shorted code to maintain.
Object Oriented Paradigm
During the execution of our code objects are created in order to represent
the things our code deals with. Objects are connected with each other.
Methods can be invoked on these objects.
© 2008 Haim Michael 20151026
Introduction
Event Driven Paradigm
The control flow is determined mainly by the events (e.g. mouse clicks,
loading of data ends).
Declarative Paradigm
The code defines what we want to get without getting into the logic of it nor
the details.
© 2008 Haim Michael 20151026
Functional Programming
 Functional programming is a programming paradigm
that emphasizes the use of expressions and their
evaluation and especially through the definition of
functions that are treated as expressions. In addition, it
avoids the complexity involved with state changes as
the one when objects and variables.
© 2008 Haim Michael 20151026
Functional Programming
 The use of functions as expressions enable us getting
more expressive code. In many cases we will exploit
the power of recursion in order to get expressive
succinct (expressed in few words) code.
 Python is not a pure functional programming language.
Nevertheless, it has more than a few functional
programming capabilities.
© 2008 Haim Michael 20151026
Recursive Function
def total(numbers):
if len(numbers) == 0:
return 0
else:
return numbers[0] + total(numbers[1:])
print(total([2,5,7]))
© 2008 Haim Michael 20151026
Recursive Function
© 2008 Haim Michael 20151026
Pure Functions
 When we define a function that always returns the same
value for the very same arguments, and it doesn't depend
on any hidden information or state and its evaluation of the
result does not cause any observable side effects nor
output then it is a pure function.
 Pure functions are usually simpler and much easier to test
and are very popular in Python programming.
© 2008 Haim Michael 20151026
Pure Functions
 In order to write a pure function we should make sure that we
write local only code. We should make sure we don't use neither
the global statement nor the nonlocal one.
 Writing a lambda expression as a pure function is the common
approach.
© 2008 Haim Michael 20151026
Lambda Expression
 Using lambda expressions we can define a recursive
function that feels much more as an expression than a
function we define using the def keyword.
total = lambda numbers: 0 if len(numbers)==0 else numbers[0] +
total(numbers[1:])
print(total([5,2,3,6]))
© 2008 Haim Michael 20151026
Lambda Expression
© 2008 Haim Michael 20151026
Higher Order Functions
 When the function we define receives another function (or
functions) as an argument(s) or when its returned value is
another function it will called an higher order function.
 We can use higher order functions for creating new
functions in our code.
© 2008 Haim Michael 20151026
Higher Order Functions
data = [(13225324,"daniel",54), (3452344,"ronen",92),
(98234234,"moshe",80), (65354435,"yael",70)]
beststudent = lambda dat: max(dat, key=lambda ob:ob[2])
print(beststudent(data))
© 2008 Haim Michael 20151026
Higher Order Functions
© 2008 Haim Michael 20151026
Immutable Data
 One of the key characteristics of functional programming is
using immutable objects and constants instead of
variables.
 One of the possible advantages for this approach is the
performance advantage. Functional programming hardly
uses stateful objects.
© 2008 Haim Michael 20151026
Lazy Evaluation
 One of the functional programming characteristics that
improves its performance is the deferred computation till it
is required, also known as lazy evaluation.
 The yield statement is one example for the lazy evaluation
we can find in Python.
© 2008 Haim Michael 20151026
Lazy Evaluation
def numbers():
for num in range(10):
print("num=",num)
yield num
for number in numbers():
print(number)
© 2008 Haim Michael 20151026
Lazy Evaluation
© 2008 Haim Michael 20151026
Recursion instead of Loop
 When writing pure functional code we will avoid using
loops. We will use recursive functions instead.
total = lambda num: 0 if num==0 else num + total(num-1)
print(total(4))
© 2008 Haim Michael 20151026
Recursion instead of Loop
© 2008 Haim Michael 20151026
Currying Functions
 Currying is the technique of breaking down the
evaluation of a function that takes multiple arguments
into evaluating a sequence of singe argument
functions.
© 2008 Haim Michael 20151026
Currying Functions
def f(age):
def f1(num):
if age<80:
return num+10
elif age>=80 and age<=100:
return num+5
return f1
temp = f(85)(60)
print(temp)
© 2008 Haim Michael 20151026
Questions & Answers
Haim Michael
0546655837
Haim.Michael@gmail.com
blog.lifemichael.com

More Related Content

What's hot (20)

Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Python ppt
Python pptPython ppt
Python ppt
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
class and objects
class and objectsclass and objects
class and objects
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
 
Python numbers
Python numbersPython numbers
Python numbers
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
Object model
Object modelObject model
Object model
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Python ppt
Python pptPython ppt
Python ppt
 

Similar to Functional Programming in Python

Functional programming in Java
Functional programming in Java  Functional programming in Java
Functional programming in Java Haim Michael
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingHaim Michael
 
Lesson slides for C programming course
Lesson slides for C programming courseLesson slides for C programming course
Lesson slides for C programming courseJean-Louis Gosselin
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in PythonHaim Michael
 
TenYearsCPOptimizer
TenYearsCPOptimizerTenYearsCPOptimizer
TenYearsCPOptimizerPaulShawIBM
 
API automation with JMeter + Bamboo CI
API automation with JMeter + Bamboo CIAPI automation with JMeter + Bamboo CI
API automation with JMeter + Bamboo CIMykola Kovsh
 
Become Jythonic in FDMEE (KSCOPE15)
Become Jythonic in FDMEE (KSCOPE15)Become Jythonic in FDMEE (KSCOPE15)
Become Jythonic in FDMEE (KSCOPE15)Francisco Amores
 
Refactoring to Testable Code
Refactoring to Testable CodeRefactoring to Testable Code
Refactoring to Testable CodeRichard Taylor
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptxFurretMaster
 
Safetty systems intro_embedded_c
Safetty systems intro_embedded_cSafetty systems intro_embedded_c
Safetty systems intro_embedded_cMaria Cida Rosa
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Hammad Rajjoub
 
Agile development with Ruby
Agile development with RubyAgile development with Ruby
Agile development with Rubykhelll
 
Retrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureRetrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureManuel Rivero
 
Flyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoFlyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoKetanUmare
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer. Haim Michael
 
SPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactorySPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactoryNCCOMMS
 
Ds02 flow chart and pseudo code
Ds02 flow chart and pseudo codeDs02 flow chart and pseudo code
Ds02 flow chart and pseudo codejyoti_lakhani
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"GlobalLogic Ukraine
 

Similar to Functional Programming in Python (20)

Functional programming in Java
Functional programming in Java  Functional programming in Java
Functional programming in Java
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Lesson slides for C programming course
Lesson slides for C programming courseLesson slides for C programming course
Lesson slides for C programming course
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
TenYearsCPOptimizer
TenYearsCPOptimizerTenYearsCPOptimizer
TenYearsCPOptimizer
 
API automation with JMeter + Bamboo CI
API automation with JMeter + Bamboo CIAPI automation with JMeter + Bamboo CI
API automation with JMeter + Bamboo CI
 
Become Jythonic in FDMEE (KSCOPE15)
Become Jythonic in FDMEE (KSCOPE15)Become Jythonic in FDMEE (KSCOPE15)
Become Jythonic in FDMEE (KSCOPE15)
 
Refactoring to Testable Code
Refactoring to Testable CodeRefactoring to Testable Code
Refactoring to Testable Code
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptx
 
Safetty systems intro_embedded_c
Safetty systems intro_embedded_cSafetty systems intro_embedded_c
Safetty systems intro_embedded_c
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-
 
Agile development with Ruby
Agile development with RubyAgile development with Ruby
Agile development with Ruby
 
Retrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureRetrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architecture
 
Flyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoFlyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiego
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 
SPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactorySPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint Factory
 
Ds02 flow chart and pseudo code
Ds02 flow chart and pseudo codeDs02 flow chart and pseudo code
Ds02 flow chart and pseudo code
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"
 

More from Haim Michael

Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in JavaHaim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design PatternsHaim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL InjectionsHaim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in JavaHaim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design PatternsHaim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in PythonHaim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScriptHaim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump StartHaim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHPHaim Michael
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on SteroidHaim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib LibraryHaim Michael
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908Haim Michael
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818Haim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728Haim Michael
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Haim Michael
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]Haim Michael
 

More from Haim Michael (20)

Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 

Functional Programming in Python

  • 1. Python Functional Programming Haim Michael November 14th , 2018 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael Part 1: https://youtu.be/nhQc-o0dUcM Part 2: https://youtu.be/IGeuQ1UBg1c
  • 2. © 1996-2018 All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 18 years of Practical Experience. lifemichael
  • 3. © 1996-2018 All Rights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management lifemichael
  • 4. © 2008 Haim Michael 20151026 Introduction  The main programming languages paradigms include the following. Imperative Paradigm The program includes statements executed one by one that change the state. Structured Paradigm Based on Imperative. The code has a more logical structure. Avoiding the goto statement.
  • 5. © 2008 Haim Michael 20151026 Introduction Procedural Paradigm Based on Structured. The code is split into procedures been called in order to have a shorted code to maintain. Object Oriented Paradigm During the execution of our code objects are created in order to represent the things our code deals with. Objects are connected with each other. Methods can be invoked on these objects.
  • 6. © 2008 Haim Michael 20151026 Introduction Event Driven Paradigm The control flow is determined mainly by the events (e.g. mouse clicks, loading of data ends). Declarative Paradigm The code defines what we want to get without getting into the logic of it nor the details.
  • 7. © 2008 Haim Michael 20151026 Functional Programming  Functional programming is a programming paradigm that emphasizes the use of expressions and their evaluation and especially through the definition of functions that are treated as expressions. In addition, it avoids the complexity involved with state changes as the one when objects and variables.
  • 8. © 2008 Haim Michael 20151026 Functional Programming  The use of functions as expressions enable us getting more expressive code. In many cases we will exploit the power of recursion in order to get expressive succinct (expressed in few words) code.  Python is not a pure functional programming language. Nevertheless, it has more than a few functional programming capabilities.
  • 9. © 2008 Haim Michael 20151026 Recursive Function def total(numbers): if len(numbers) == 0: return 0 else: return numbers[0] + total(numbers[1:]) print(total([2,5,7]))
  • 10. © 2008 Haim Michael 20151026 Recursive Function
  • 11. © 2008 Haim Michael 20151026 Pure Functions  When we define a function that always returns the same value for the very same arguments, and it doesn't depend on any hidden information or state and its evaluation of the result does not cause any observable side effects nor output then it is a pure function.  Pure functions are usually simpler and much easier to test and are very popular in Python programming.
  • 12. © 2008 Haim Michael 20151026 Pure Functions  In order to write a pure function we should make sure that we write local only code. We should make sure we don't use neither the global statement nor the nonlocal one.  Writing a lambda expression as a pure function is the common approach.
  • 13. © 2008 Haim Michael 20151026 Lambda Expression  Using lambda expressions we can define a recursive function that feels much more as an expression than a function we define using the def keyword. total = lambda numbers: 0 if len(numbers)==0 else numbers[0] + total(numbers[1:]) print(total([5,2,3,6]))
  • 14. © 2008 Haim Michael 20151026 Lambda Expression
  • 15. © 2008 Haim Michael 20151026 Higher Order Functions  When the function we define receives another function (or functions) as an argument(s) or when its returned value is another function it will called an higher order function.  We can use higher order functions for creating new functions in our code.
  • 16. © 2008 Haim Michael 20151026 Higher Order Functions data = [(13225324,"daniel",54), (3452344,"ronen",92), (98234234,"moshe",80), (65354435,"yael",70)] beststudent = lambda dat: max(dat, key=lambda ob:ob[2]) print(beststudent(data))
  • 17. © 2008 Haim Michael 20151026 Higher Order Functions
  • 18. © 2008 Haim Michael 20151026 Immutable Data  One of the key characteristics of functional programming is using immutable objects and constants instead of variables.  One of the possible advantages for this approach is the performance advantage. Functional programming hardly uses stateful objects.
  • 19. © 2008 Haim Michael 20151026 Lazy Evaluation  One of the functional programming characteristics that improves its performance is the deferred computation till it is required, also known as lazy evaluation.  The yield statement is one example for the lazy evaluation we can find in Python.
  • 20. © 2008 Haim Michael 20151026 Lazy Evaluation def numbers(): for num in range(10): print("num=",num) yield num for number in numbers(): print(number)
  • 21. © 2008 Haim Michael 20151026 Lazy Evaluation
  • 22. © 2008 Haim Michael 20151026 Recursion instead of Loop  When writing pure functional code we will avoid using loops. We will use recursive functions instead. total = lambda num: 0 if num==0 else num + total(num-1) print(total(4))
  • 23. © 2008 Haim Michael 20151026 Recursion instead of Loop
  • 24. © 2008 Haim Michael 20151026 Currying Functions  Currying is the technique of breaking down the evaluation of a function that takes multiple arguments into evaluating a sequence of singe argument functions.
  • 25. © 2008 Haim Michael 20151026 Currying Functions def f(age): def f1(num): if age<80: return num+10 elif age>=80 and age<=100: return num+5 return f1 temp = f(85)(60) print(temp)
  • 26. © 2008 Haim Michael 20151026 Questions & Answers Haim Michael 0546655837 Haim.Michael@gmail.com blog.lifemichael.com