This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
Functional Programming in itself is a big topic and there are a lot of concepts in it to understand and master. However, I have a defined goal to learn Python in 30 days, so I rather prefer to understand the key concepts and learn the most commonly used techniques that would come in handy in building practical projects.
I explored the fundamentals of functional programming and how it is implemented in Python yesterday. Today, I explored the missing pieces and I came across quite interesting findings while doing so which I will be sharing in this post.
Lamba Expressions
Lambda is actually a term in computer science which means an anonymous function which is used only once while the expression is being executed. Lambda expressions are quite useful to simplify code and make functions more concise and compact. However, overusing them in complicated evaluations can make the code less readable. Every cool thing has their trade-off as well!
Upon exploring their syntax and use cases, it immediately made me think of arrow functions syntax from the JavaScript universe although they are not exactly similar.
- names = ['John', 'Peter', 'Elon', 'Joseph']
- # make all names upper cased
- uppercased = list(map(lambda name: str.upper(name), names))
- print(uppercased)
Lambda takes any number of arguments (here name) and expression involving the arguments (here str.upper(name)).
- users = [('Mary', 23), ('Emilie', 10), ('Katie', 30)]
- sorted_by_name = sorted(users)
- print(sorted_by_name)
- # [('Emilie', 10), ('Katie', 30), ('Mary', 23)]
- sorted_by_age = sorted(users, key = lambda item: item[1])
- # using age as key for sorting
- print(sorted_by_age)
- # [('Emilie', 10), ('Mary', 23), ('Katie', 30)]
- scores = [23, 55, 20, 90, 34, 53]
- scores_under50 = list(filter(lambda x: x < 50, scores))
- print(scores_under50) # [23, 20, 34]
Comprehensions
Comprehensions are another cool feature of Python which I found really useful. They are used to quickly build data structures such as list, set, dict in a compact syntax and are said to be faster as compared to creating the same data structures using loops.
It can often be tempting to replace loops with comprehensions. However, a cleverly written code is not always good in terms of readability. So lengthy one-liner comprehensions should be avoided as it may look very compact but might hurt someone’s brain 😃
Comprehensions are of the following kind in Python - List comprehension, Set Comprehension and Dict Comprehension.
List Comprehension
Suppose we have to create a list of characters from a string, the simple way would be something like this

Join the conversation! Your thoughts help the community grow.