Seven Cool Pythonic Functions :D

Here is an article that tells you the seven functions that are way cool to help you with your time and energy. Fasten your seat belts to take a look at each one of them. The snippet codes are attached to give you essence of the articles that comes handy when you code it yourself with a cup of coffee.

1. Map

Map function is a common tool to start with and powerful tool to lessen your gigantic code into an aesthetic script that makes the code more readable and compact.

Instead of declaring a list and appending elements with an orthodox iterating practice, you can do it an instance with the help of map function.

Output: [10,9,12]

2. Filter

It belong to pretty much the same room as map with a twist. The twist being that you can give a condition to pick the elements from the data structures (list/dictionary/set) that are your need unlike every other.

Remember that filter object, also map, is an iterable that can be accessed once you convert into a data structure of your choice. As suggested, the below snippet tells that filter object is an iterable.

3. Zip

Generally used to pack elements present at the same index in two or more tuples into one unique tuple within the parent tuple is what zip function is good at.

Confusing? Don’t be. The code is pretty easy to understand the working but it will be much more helpful if you practice it once on your own.

Output: [(1,10),(2,20),(3,30)]

4. Getsizeof

Getsizeof (lowercase) comes very handy on determining the size of the object. It is part of the system library and can be called to use.

On a lighter note, it is not some fancy function but the basic and important tool that is frequently used. Finding it interesting? Check how it behaves with generators to learn in depth.

Output: gen:120 list: 824464

5. Dir

Dir function helps in detailing all the methods and attributes present in the object. It is useful in checking what better can we do with the objects or instances.

In the below snippet, magic method, starting and ending with double underscore and a couple of attributes are printed on the screen when user passes sales object in the dir function.

6. Docstrings

It is somewhat near to comment but more structured and purpose filled as docstrings can be written while writing the code and can be accessed by __doc__ magic method.

The text appears in the output that is added by the developer while writing the code. Docstrings are very helpful in coding.

Output: For the animals

7. datetime

Wonder how can you bring dynamic stuff like date and time into your code. It does not get any easier as in Python. Only practice one needs to do is to import the data and time module and if in need, the the option is add a separate class is always on the shelf.

It is viably visible how you can import tons of different module to carry out myriad number of tasks in Python. What comes attached with module is different functions for you served on the platter.

Output: 2021/6

In a similar fashion, you can access browse, send emails, create designs and what not. The adaptability and compatibility is what makes gives Python the spotlight in comparison to other languages. Why don’t you share with us your pythonic experiences in the comments below.



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
Mohammad Alim

Mohammad Alim


Freelance Writer, SEO Expert, Computer Science Enthusiast, Trade Analyst and sometimes a Nutritionist too.