Simplicity is Python’s way to solve a problem.

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 syntax to reverse a string, tuple or a list as well

string = 'John…


Kill the messier code with functools elegancy

downloaded from wallpaper cave

functools.partial

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…


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

add = eval("lambda a,b : a + b")
print(add(10,20)) # logs: 30
print(type(add)) # logs: <class 'function'>

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


Embrace the power of sorted function in Python

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.

l = [23,12,35,100,2,43]
#sort list in increasing order
print(sorted(l)) # logs: [2,12,23,35,43,100]

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.

#sorting list in decreasing order
print(sorted(l,reverse = True)) # logs: [100, 43…


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

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.

x, y = 10, 20
print(x + y) # 30
a,b = 'John', 'Wick'
print(a + b) # John Wick

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

dbader.org

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.

import heapqclass PriorityQueue:    def __init__(self):
self._data = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._data, (-priority, self._index, item))
self._index += 1
def pop(self):
return heapq.heappop(self._data)[-1]

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

class Task:


Make your code more functional.

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.

Let’s…


Understand the different usage of function in JavaScript

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

function sayHello(){
return 'Hello World!';
}
console.log(sayHello()); // logs: Hello World!
let getSquare = (value) => value * value;
console.log(getSquare(10)); // logs: 100

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

Method

Function defined in object is known as method.

let obj = {
name: 'John',
sayHello: function(){
return `Hello ${this.name}`
}
}
console.log(obj.sayHello()); …

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.

function square(value){
return value * value;
}
let arr = [1,2,3,4,5];
console.log(arr.map(square)) // logs: [1,4,9,16,25]

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.

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 @https://codeshinobis.com

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