Using Freshworks apps? Check out what we can do for you! Learn More

Back

30 Ways to Write Better Python Code – Part 3

Python Developers for Simplified MVP Development - Part 3

Python developers love the language for its easy readability, coding flexibility and has always been easy to learn as it is not too strict when it comes to the syntax of the language. It has been one of its strengths. It has led to the growth of the language and the support of a growing community. This unique combination of Python makes it ideal for coming up with tricks and innovations.

In this third and final part of the series on Python programming, we will help you understand and practice Python code using relevant examples to enhance your coding skills. Python is also an ideal language for startups.

Just like we mentioned in Part 1 and Part 2, the emphasis is continuous learning and practicing these coding tricks. By doing so, learn to sharpen your coding skills, write better code, and become better Python developers. By the end of this blog, we hope to bring to light the flexibility and simplicity of Python.

Note: Please compile your code here for the best results.

1) Using Lambda with Map Function
Output

[1, 9, 25, 49, 81]

2) Return Multiple Values From a Function

A Python function can return multiple values without any extra help. All you need to do is to separate the values by commas.

For example,

Output

(11, 22, 33, 44, 55)

3) Filtering the Given Values Using the Filter Function

You can use the filter function to filter out some values from an iterable object. The syntax for a filter function,

Syntax: filter(function, iterable)

For example, you need to collect a list of people whose ages are greater than or equal to 26. You can call the filter function on the list object to perform this function.

Output

[34, 32, 27, 29]

Recommended Read: What is Python used for in Startups? Why Should a CTO Choose Python for Business in 2020?

4) Merging Two Dictionaries in Python

In Python, there is no explicit method that is needed to merge two dictionaries. The following snippet will demonstrate the merging of dictionaries in Python.

Case 1 – In Python 3.4 or lower 
Output

{‘Three’: 3, ‘Two’: 2, ‘One’: 1}

Case 2 – In Python 3.5 or Greater 

This is the simpler version and with the update of the Python versions to 3.5 and above, you get this trick to work. The Python compiler suggested will compile accurately Case 1 since it is Python 2.7.

Output

{‘Seven’: 7, ‘Six’: 6, ‘Five’: 5}

5) Getting the Size of an Object

The memory size varies based on the type of object. By importing the sys module, you can access the getsizeof() function. By using this, you can get the memory size of an object. 

Output

24

6) Combining two Lists into a Dictionary

You can use the zip function to combine two lists into a dictionary in Python. For example,

Output

{‘Python’: 7, ‘Hire’: 4, ‘Developers’: 10}

As you can see, the results are ordered in descending order. 

7) Calculate Execution Time for a Program

Time is a module that exists in Python that you can use to calculate the execution time.

Output

0.000487

8) Removing Duplicate Elements in a List

An element that occurs more than once in a list is a duplicate element. You can remove this element using typecasting or set() function.

Output

[2, 3, 4, 5, 6, 8, 9]

9) Printing Monthly Calendar in Python

By importing the calendar module, you can print the calendar and a lot of other date-related stuff in Python. For example, you can print the monthly calendar. 

Output

July 2020

Mo Tu We Th Fr Sa Su

       1  2  3  4  5

 6  7  8  9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

10) Iterating with Zip Functions

You can use the zip function to iterate through more than one iterable using loops. With the help of this trick, you can iterate through two loops simultaneously.

Output

(‘Be’, 2)

(‘Future’, 6)

(‘Ready’, 5)

Final Thoughts

We are now at the end of the series, where we have highlighted to you 30 major tricks that are highly effective in minimizing code complexity. By learning such Python code, you become aware of how Python developers accomplish the given task in the best possible way. By saving time and effort, Python continues to be popular among developers and businesses alike.

Finally, Python has some great functions, modules, and libraries that help developers learn quickly. We hope this series has served you well regardless of where you are in the learning curve. Our efforts are to make sure that you get access to the best of the coding practices as much as possible, and this series has helped fulfill that task.

At TechAffinity, our Python development team updates themselves periodically with every new update and delivers the best Python services to you. If you have any queries regarding MVP/full-fledged app development, share them with media@techaffinity.com or schedule a meeting with our experts.

Subscribe to Our Blog

Stay updated with latest news, updates from us