Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Get this slide deck:

slides.com/cheukting_ho/python-fuctional

Recap

Beginner topics:

Python objects, Control flows,

Functions, modeuls, classes and decorators

strings operations and regex with re

Testing:

pytest with fixtures and mock

property-based testing

python linters & auto-formatters

TDD, type hinting

Intermedite topics:

Iterators, generators, async

Packaging:

generating docs, pypi

What is functional programming?

 

In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. -wikipedia

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = 0
for num in num_list:
  if num % 2 == 0:
    result += num * 10
from functools import reduce
result = reduce(lambda x,y: x+y,
                map(lambda a: a*10,
                    filter(lambda n: n%2==0,
                           [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))

Why functional programming?

 

Advantages

  • pure functions are simple to understand
  • The arguments and return type of pure functions are given out by their function signature.
  • avoid changing variables or any data outside it
  • lazy evaluation is possible
  • no hidden outputs
  • enhances the comprehension and readability of the code

Why functional programming?

 

Disadvantages

  • reduction in performance
  • in some cases, the code is hard to read
  • combining pure functions could be tough
  • recursive style in place of using loops can be a daunting task

 

https://hackr.io/blog/functional-programming

What do we need?

Lambda function

 

Map

 

Filter

 

Reduce

 

Let's try them out

Next week:
More list and dict

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm

Python Functional Programming

By Cheuk Ting Ho

Python Functional Programming

  • 278
Loading comments...

More from Cheuk Ting Ho