DEV Community

Cover image for Seven Pythonic Functions You Should Know
Alim Mohammad
Alim Mohammad

Posted on • Updated on

Seven Pythonic Functions You Should Know

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.

Map Function
The above program will print the prices of goods twice and that is how powerful map can be in one go.

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.

Filter Funtion

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.

Zip

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.

Getsizeof

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 methods or dunder methods, starting and ending with double underscore are printed on the screen when user calls the dir function.

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.

Docstring

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.

datetime

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.

Top comments (0)