Functional Programming with Python (1) - PowerPoint PPT Presentation

About This Presentation
Title:

Functional Programming with Python (1)

Description:

A programming paradigm known as functional programming avoids state and changeable data and regards computing as the evaluation of mathematical functions. – PowerPoint PPT presentation

Number of Views:0
Slides: 5
Provided by: MadhuMadhu
Tags:

less

Transcript and Presenter's Notes

Title: Functional Programming with Python (1)


1
Functional Programming with Python A programming
paradigm known as functional programming avoids
state and changeable data and regards computing
as the evaluation of mathematical functions. In
other words, immutable variables and
side-effect-free code are encouraged by
functional programming (FP). It uses a
declarative programming approach. In contrast to
an imperative style, where the primary focus is
"how to solve," it places a greater emphasis on
"what to solve." You can attend the Python
online classes to learn more about Functional
Programming (FP) in Python.
  • Concepts of Functional Programming
  • 1. Pure Functions
  • Two characteristics of pure functions are
  • They have no negative consequences.
  • If given the same input, they always create the
    same output. This indicates that no changeable
    state can be a dependency of the function.

2
  • The following causes of side effects include-
  • altering data that is present outside the
    function's boundaries.
  • Changing the value of arguments in the function.
  • causing an error or throwing an exception.
  • receiving user input or printing to the terminal.
  • examining or adding to a file.
  • Immutability
  • A variable's value cannot be changed in
    functional programming once it has been
    initialised. This means that instead of altering
    the old list when we need to modify values in it,
    we must construct a new list with the updated
    values. The immutability of variables in FP is
    advantageous since it maintains the state as a
    program is run. Python supports immutable data
    types such as int, float, complex, text, tuple,
    frozen set, and bytes.
  • Recursion
  • In FP, if-else clauses and loops are avoided
    because they produce distinct results with each
    iteration. All iteration tasks in functional
    programs employ recursion instead of loops.
    Recursion is a function that repeatedly invokes
    itself up until an exit condition is satisfied.
  • First-Class and higher-order functions are
    available

3
  • Map as a higher-order function.
  • There are two arguments for the map function.
    Iterable and function are the first and second
    arguments, respectively. The passed function is
    subsequently applied to each item in the
    iterable.
  • Filter as a Higher Order Function
  • Based on the criteria you specify, the filter
    function makes it simple to isolate matched
    records from a broader set of data. There must
    be two arguments. Iterable and function are the
    first and second arguments, respectively. For
    each iterable element for which the filter
    function returns true, a sequence is returned.
  • Reduce as a Higher Order Function
  • Reduce takes a function and a sequence as inputs
    and outputs a single value that is calculated as
    follows
  • The function is initially called with the first
    two elements in the sequence, and the output is
    then returned.
  • Using the output from step 1 and the subsequent
    value in the sequence, the function is then
    called once more. Until all of the items in the
    sequence have been iterated, this
  • process is repeated.
  • Functional programming Advantages
  • Pure functions always yield the same results
    they are unaffected by outside variables.
    Programs are simpler to test and debug as a
    result.
  • Because they execute separately without changing
    state, parallel or concurrent programs can be
    written effectively.
  • It supports the idea of lazy evaluation, where
    values are only evaluated and stored when
    necessary.
  • With Pure Functions, FP enables stronger
    encapsulation than OOP.
  • Since FP programs are composed entirely of pure
    functions, we can readily reuse them.
  • Functional programming Disadvantages
  • Recursion and immutable values could have a
    negative impact on performance.
  • Sometimes writing pure functions can make the
    code harder to read.
  • The state in functional programming is absent.
    Instead of altering existing things, they always
    create new ones in order to carry out their
    operations. FP Applications use a lot of memory
    as a result.
  • Writing pure functions is simple the challenge
    comes in putting them together into a
    comprehensive application.

4
Conclusion This article gives you a brief
overview of functional programming and should
help you understand its advantages. Python
enables us to code declaratively and
functionally. Even more, it supports a variety
of widely used functional constructs like lambda,
map(), filter(), and reduce() that may be used
to create parallelizable, compact code. Check out
the Python online training to learn more.
Tags onlinecertificatepython bestpythononlinetr
aining Toptenonlinetrainingpython
PythonCourseOnline PythonCertificationCourse
Write a Comment
User Comments (0)
About PowerShow.com