Simplicity is Python’s way to solve a problem.

Image for post
Image for post
Created on memegenerator
  1. Reverse a string, list or tuple →

Whenever I’d find myself in a situation where I was required to perform a reverse operation, more often than not, I would use a for loop or resort to find some function like reverse() that had do the job for me.

But later on, to my shock I found, python has got another trick up it’s sleeve to solve this in a jiffy — Slicing Operation
You can use slicing [::-1] syntax to reverse a string, tuple or a list as well

Kill the messier code with functools elegancy

Image for post
Image for post
downloaded from wallpaper cave


Partial is a higher order function which allows the partial application of a function. Suppose there is a function with lots of arguments but you need to change only one or two argument every time you use that function, Partial function comes to the rescue. Using Partial function, you can fix any argument value the number of arguments of a function thus removing the necessity of creating another one. What you get as a result is an elegant and easy to read code.

Let’s see it in action →

The logger function above takes 2 arguments — log_level and…

Image for post
Image for post
Photo by Hitesh Choudhary on Unsplash

In Python, functions are first class citizens. But what does this mean?

The first class objects are program entity which have these five characteristics:

1. Can be created at runtime.
2. Can be assigned to a variable.
3. Can be passed as a argument to a function.
4. Can be return as a result from a function.
5. Can have properties and methods

Let’s see each point one by one through code.

1. Can be created at runtime

Here add function is created at runtime and you can also…

Embrace the power of sorted function in Python

Image for post
Image for post
Photo by Chris Ried on Unsplash

As a programmer we all know what sorting is, so I will not write theory here and will go directly into code.

Python has inbuilt function sorted which can be use to sort any iterable. sorted function return the new sorted iterable unlike list.sort function which perform inplace sorting.

If you want to sort list in decreasing order you can pass reverse = True in sorted function as a parameter. By default reverse is set to False.

How to make your object play well with built-in operators

Image for post
Image for post
pic credits LocalNav

Each operator can be used in different way for different types of operands. for example when + is used with integers, it add integers to give result and when + is used with string, it concatenates the provided strings.

This change in behaviour of + operator according to type of operands provided, is called Operator overloading

Operator overloading refers to the ability of operator to behave differently depending on operands it acts on.

Can + operator add any two object…

Take advantage of the heapq module to implement a Priority Queue

Image for post
Image for post

What is a Priority Queue?

A Priority Queue is a type of queue in which every element is associated with priority and it returns the element of highest priority on every pop operation.
if priority is same the elements are return on basis of their insertion order.

We can easily implement priority queue in Python using heapq module.

The above code implement priority queue. Let’s see example of how to use it.

Make your code more functional.

Image for post
Image for post
Photo by Markus Spiske on Unsplash

Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts. — Michael Feathers

map, filter and reduce are essentially just some of the most well-known, easy to use, higher-order functions that run provided callback on each element of an array.

In this article, we will explore how using map(), filter(), and reduce() can help make our code:
1. Easy to comprehend.
2. Less prone to side effects as these function don’t modify the actual array, and instead create a new one.
3. Avoid explicit loops.


Understand the different usage of function in JavaScript

Image for post
Image for post

In other object oriented programming languages Function, Method and Constructor call may be different things but in JavaScript these are just different usage pattern of one programming construct — Function.


Function sayHello and getSquare work as expected. This usage is same as in others programming languages


Function defined in object is known as method.

Improve your code using higher order functions.

Higher order functions may sound some complicated technical term but it is nothing but a normal function which take function as arguments or return a function as their result.
JavaScript uses higher order function very heavily, it generally refer as a callback in JavaScript.

Lets understand this with code.

In above code function square return the square of a passed value. Here map is higher order function which takes function square as a argument.

Let’s take a look…

Step by step guide to understand closures.

Image for post
Image for post
Downloaded photo from Unsplash

Many developers find closures intimidating who come from language which does not support them. But there is nothing to be afraid from closures and the effort you put to understand closures will pay you many times over.

To understand the closures you need to understand four things.
1. Function can access variables which are defined outside the current function.
2. Closures have access to variables defined in function which enclosed it even if it is returned.
3. Functions in JavaScript are first class objects
4. …

Aditya Dhanraj Tiwari

Amor fati, Find my small house for thoughts @

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store