Photo by Sean Lim on Unsplash

25 Useful Python Snippets to Help in Your Day-to-Day Work

Python snippets that can be taken as a reference for your daily work

Abhinav Sagar
Oct 23 · 6 min read

Python is a general-purpose and high-level programming language. You can use Python for developing desktop GUI applications, websites, and web applications, for data science, etc. Also, Python, as a high-level programming language, allows you to focus on the core functionality of the application by taking care of common programming tasks. The simple syntax rules of the programming language further make it easier for you to keep the code base readable and application maintainable.

The advantages of using Python when compared to other programming languages are:

  1. Compatible with major platforms and operating systems
  2. Many open-source frameworks and tools
  3. Readable and maintainable code
  4. Robust standard library
  5. Standard test-driven development

Code Snippets

In this piece, I’ll present 25 short code snippets that can help you in your day-to-day tasks.

In other languages, to swap values between two variables without using a third variable, we either have to use arithmetic operators or bitwise XOR. In Python, it is much simpler, as shown below.

a = 5                               
b = 10 a, b = b, a print(a) # 10
print(b) # 5

The following function returns True if the given number is even, False otherwise.

def is_even(num):
return num % 2 == 0
is_even(10) # True

The following function can be used for splitting a multiline string into a list of lines.

def split_lines(s):
return s.split('\n')
split_lines('50\n python\n snippets') # ['50', ' python', ' snippets']

The standard library’s sys module provides the getsizeof() function. That function accepts an object, calls the object’s sizeof() method, and returns the result, so you can make your objects inspectable.

import sys
print(sys.getsizeof(5)) # 28
print(sys.getsizeof("Python")) # 55

Python string library doesn’t support the built-in reverse() as done by other Python containers like list. There are many approaches to reversing a string, of which the easiest way is making use of the slicing operator.

language = "python"                                
reversed_language = language[::-1] print(reversed_language) # nohtyp

It is very easy to print a string n times without using loops, as shown below.

def repeat(string, n):
return (string * n)
repeat('python', 3) # pythonpythonpython

The following function is used for checking if a string is a palindrome or not.

def palindrome(string):
return string == string[::-1]
palindrome('python') # False

The next snippet combines a list of strings into a single string.

strings = ['50', 'python', 'snippets']
print(','.join(strings)) # 50,python,snippets

This function returns the first element of the passed list.

def head(list):
return list[0]
print(head([1, 2, 3, 4, 5])) # 1

This function returns every element that exists in either of the two lists.

def union(a,b):
return list(set(a + b))
union([1, 2, 3, 4, 5], [6, 2, 8, 1, 4]) # [1,2,3,4,5,6,8]

This function returns the unique elements present in a given list.

def unique_elements(numbers):
return list(set(numbers))
unique_elements([1, 2, 3, 2, 4]) # [1, 2, 3, 4]

This function returns the average of two or more numbers present in a list.

def average(*args):
return sum(args, 0.0) / len(args)
average(5, 8, 2) # 5.0

This function checks if all the elements in a list are unique or not.

def unique(list):
if len(list)==len(set(list)):
print("All elements are unique")
print("List has duplicates")
unique([1,2,3,4,5]) # All elements are unique

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

from collections import Counter
list = [1, 2, 3, 2, 4, 3, 2, 3]
count = Counter(list)
print(count) # {2: 3, 3: 3, 1: 1, 4: 1}

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

def most_frequent(list):
return max(set(list), key = list.count)
numbers = [1, 2, 3, 2, 4, 3, 1, 3]
most_frequent(numbers) # 3

The next function is used for converting an angle from degrees to radians.

import math
def degrees_to_radians(deg):
return (deg * math.pi) / 180.0
degrees_to_radians(90) # 1.5707963267948966

The following snippet is used for calculating the time taken to execute a piece of code.

import time
start_time = time.time()
a,b = 5,10
c = a+b
end_time = time.time()
time_taken = (end_time- start_time)*(10**6)
print("Time taken in micro_seconds:", time_taken) # Time taken in micro_seconds: 39.577484130859375

This function calculates the greatest common divisor of a list of numbers.

from functools import reduce
import math
def gcd(numbers):
return reduce(math.gcd, numbers)
gcd([24,108,90]) # 6

This snippet can be used to find all the unique characters present in a string.

string = "abcbcabdb"   
unique = set(string)
new_string = ''.join(unique)
print(new_string) # abcd

Lambda is an anonymous function with the capability of holding a single expression only.

x = lambda a, b, c : a + b + c
print(x(5, 10, 20)) # 35

This function returns a list of the results after applying the given function to each item of a given iterable(list, tuple, etc.)

def multiply(n): 
return n * n

list = (1, 2, 3)
result = map(multiply, list)
print(list(result)) # {1, 4, 9}

This function filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

arr = [1, 2, 3, 4, 5]
arr = list(filter(lambda x : x%2 == 0, arr))
print (arr) # [2, 4]

List comprehensions provide us with a simple way to create a list based on some iterable. During the creation, elements from the iterable can be conditionally included in the new list and transformed as needed.

numbers = [1, 2, 3]
squares = [number**2 for number in numbers]
print(squares) # [1, 4, 9]

Slicing is used to extract a continuous sequence or subsequence of elements from a given sequence. The following function is used for concatenating the results of two slicing operations. First, we are slicing the list from index d to end, then from start to index d.

def rotate(arr, d):
return arr[d:] + arr[:d]

if __name__ == '__main__':
arr = [1, 2, 3, 4, 5]
arr = rotate(arr, 2)
print (arr) # [3, 4, 5, 1, 2]

The final snippet is used to call multiple functions from a single line and evaluate the result.

def add(a, b):
return a + b
def subtract(a, b):
return a - b
a, b = 5, 10
print((subtract if a > b else add)(a, b)) # 15


In this article, I’ve presented 25 short Python code snippets that can be used as a reference in your day-to-day work. Stay tuned for my next piece, “25 Useful SQL Queries to Help in Your Day-to-Day Work.”

References/Further Readings

30-seconds/30-seconds-of-python — Curated collection of useful Python snippets that you can understand in 30 seconds or less.


If you want to keep updated with my latest articles and projects follow me on Medium. These are some of my contacts details:

Happy reading, happy learning, and happy coding!

Better Programming

Advice for programmers.

Abhinav Sagar

Written by

Machine Learning Researcher at VIT. Telling stories with data. All views are my own.

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