20 Python Snippets You Should Learn Today

Some tips and tricks to help you code faster

Chaitanya Baweja
Oct 8 · 4 min read
Photo by Chris Ried on Unsplash

Python is a no-BS programming language. Readability and simplicity of design are two of the biggest reasons for its immense popularity.

As the Zen of Python says:

Beautiful is better than ugly.
Explicit is better than implicit.

This is why it is worthwhile to remember some common Python tricks to help improve your code design. These will save you the trouble of surfing Stack Overflow every time you need to do something.

The following tricks will prove handy in your day-to-day coding exercises.


1. Reversing a String

You can read more about this here.


2. Using rhe Title Case (First Letter Caps)


3. Finding Unique Elements in a String


4. Printing a String or a List n Times

An interesting use case of this could be to define a list with a constant value — let’s say zero.

n = 4
my_list = [0]*n # n denotes the length of the required list
# [0, 0, 0, 0]

5. List Comprehension

The following snippet creates a new list by multiplying each element of the old list by two.

You can read more about it here.


6. Swap Values Between Two Variables


7. Split a String Into a List of Substrings


8. Combining a List of Strings Into a Single String


9. Check If a Given String Is a Palindrome or Not


10. Frequency of Elements in a List

Python counter keeps track of the frequency of each element in the container. Counter() returns a dictionary with elements as keys and frequency as values.

We also use the most_common() function to get the most_frequent element in the list.


11. Find Whether Two Strings are Anagrams

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase.

If the Counter objects of two strings are equal, then they are anagrams.


12. Using the try-except-else Block

If you need to run something irrespective of exception, use finally.


13. Using Enumerate to Get Index/Value Pairs


14. Check the Memory Usage of an Object


15. Merging Two Dictionaries

In the script given below, two dictionaries are merged. Values from the second dictionary are used in case of intersections.


16. Time Taken to Execute a Piece of Code

17. Flattening a List of Lists

Here’s how you can get that:

Numpy flatten is a better way to do this if you have a properly formatted array.


18. Sampling From a List

I have been recommended the secrets library for generating random samples for cryptography purposes. The following snippet will work only on Python 3.


19. Digitize


20. Check for Uniqueness


Conclusion

Thank you for reading this story. Hope you enjoyed it.

Better Programming

Advice for programmers.

Chaitanya Baweja

Written by

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade