Skip to content

Latest commit

 

History

History
83 lines (42 loc) · 4.1 KB

30-python-snippets.md

File metadata and controls

83 lines (42 loc) · 4.1 KB

30 Helpful Python Snippets That You Can Learn in 30 Seconds or Less

Short Python snippets that you can quickly learn and use in your work or personal needs

Fatos Morina

Image for post

Image for post

Photo by Jantine Doornbos on Unsplash

Python represents one of the most popular languages that many people use it in data science and machine learning, web development, scripting, automation, etc.

Part of the reason for this popularity is its simplicity and easiness to learn it.

If you are reading this, then it is highly likely that you already use Python or at least have an interest in it.

In this article, we will briefly see 30 short code snippets that you can understand and learn in 30 seconds or less.

The following method checks whether the given list has duplicate elements. It uses the property of set() which removes duplicate elements from the list.

This method can be used to check if two strings are anagrams. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

This snippet can be used to check the memory usage of an object.

This method returns the length of a string in bytes.

This snippet can be used to print a string n times without having to use loops to do it.

This snippet simply uses the method title() to capitalize first letters of every word in a string.

This method chunks a list into smaller lists of a specified size.

This method removes falsy values (False, None, 0 and “") from a list by using filter().

This snippet can be used to transpose a 2D array.

You can do multiple comparisons with all kinds of operators in a single line.

This snippet can be used to turn a list of strings into a single string with each element from the list separated by commas.

This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.

This method can be used to turn the first letter of the given string into lowercase.

The following methods flatten a potentially deep list using recursion.

This method finds the difference between two iterables by keeping only the values that are in the first one.

The following method returns the difference between two lists after applying a given function to each element of both lists.

You can call multiple functions inside a single line.

The following method checks whether a list has duplicate values by using the fact that set() contains only unique elements.

The following method can be used to merge two dictionaries.

In Python 3.5 and above, you can also do it like the following:

The following method can be used to convert two lists into a dictionary.

This snippet shows that you can use enumerate to get both the values and the indexes of lists.

This snippet can be used to calculate the time it takes to execute a particular code.

You can have an else clause as part of a try/except block, which is executed if no exception is thrown.

This method returns the most frequent element that appears in a list.

This method checks whether a given string is a palindrome.

The following snippet shows how you can write a simple calculator without the need to use if-else conditions.

This snippet can be used to randomize the order of the elements in a list. Note that shuffle works in place, and returns None.

This method flattens a list similarly like [].concat(…arr) in JavaScript.

This is a really quick way for swapping two variables without having to use an additional one.

This snippet shows how you can get a default value in case a key you are looking for is not included in the dictionary.

Source