35 Best Python Tips and Tricks For Writing Better Code

15 min read
Share on:

Python is one of the most used programming languages in the world. It has a lot of features that will help a programmer to write better code. A lot of people think that writing code is all about getting the right answer. But in reality, it’s only a small part of the battle. The real challenge is writing code that is easy to understand, maintain, and extend.

Do you think in a similar way? But confused,

  • How you should write better python code?
  • What to improve and how to improve?

If so then you have reached the perfect place. In this article, I’m going to share with you the top 35 python tips and tricks that are going to help you to write better and easy to understandable python code.

This post is a compilation of python coding industry standards and a list of Python tips and tricks that every Python developer should know.

1. Python Tips for Conditional Expressions / Ternary Expression

Many times we use the if…else statement to check a condition and based on that we assign a value to a variable. It requires few lines of code and if we have to repeat the process multiple times in the program then it becomes lengthy. If you are facing the same thing then you are in luck I got a solution for you. You can use a ternary expression.

A ternary expression checks the condition and completes the assignment with just one line of code. As it’s a one-liner, it makes your code even more concise.

# Normal Programmers
if num % 2 == 0:
      result = "Number is divided by 2"
else:
      result = "Number is not divided by 2"

# Mentos Programmers
result = "Number is divided by 2" if num % 2 == 0 else  "Number is not divided by 2"

2. Use Python for multiple variable assignments

You can assign multiple variables with the same value or multiple values in a single line in python. In the first example, we are assigning 10 to the x, y, and z variables. We can do this by using this syntax.

 variable 1 = variable 2 = variable 3 = value

Similarly, if you are assigning different values then you try out this syntax.

 variable 1, variable 2, variable 3 = value1, value2, value3
# Normal Programmers
x = 10
y = 10
z = 10 

# Mentos Programmers
x = y = z = 10

x, y, z = 10, 20, 30

3. Working with Large Numbers with python

Now let us discuss my favorite trick. Have you ever worked with large numbers? 1000000000 or 1,000,000,000; Which one is more readable? The second one, right? But if we try to do that in Python like num1=1,000,000,000, we would get an error.

But there’s another method to do the same. We can use _ to separate the digits which do not affect our program. And we can use: inside the f string to separate the output digits by a comma. The following code demonstrates the same.

num1 = 1_000_000_000     # 1 billion
num2 = 10_000_000        # 10 million
total = num1 + num2
print(f'{total:,}')      # To separate the output digits with comma

# Output
# 1,010,000,000

4. Use list comprehension instead of raw for-loops

Have you ever worked with a list or tuple where you need to perform some operations on them?

But in the end, you wanted a list as an output? Then the next trick can definitely help you next time to save time and write concise code.

arr = [1, 2, 3, 4, 5, 6]
res = []

# Instead of this
for num in arr:
    if num % 2 == 0:
        res.append(num * 2)
    else:
        res.append(num)

# Use this
res = [(num * 2 if num % 2 == 0 else num) for num in arr]

5. Iterate with enumerate() instead of range(len())

All of us sometimes needed to iterate over a list while also needing to track the current item and index value. For that, we can use range(len()) but it’s hefty as we have to fetch the values separately. An easier solution for this problem is to use enumerate().

Enumerate() takes a list or tuple as a parameter and returns the result’s current index, value.

data = [1,2,3,-4,5]
# Normal Programmers
for i in range(len(data)):
      if data[i] < 0:
           data[i] = 0

# Mentos Programmers
for i, num in enumerate(data):
      if num < 0:
          data[i] = 0

6. Inputting Secret Information

Let’s say you are taking a username and password as input from the user. You would surely go with the following approach.

uname = input('Enter Username: ')
pwd = input('Enter password: ')
print('Logging In....')

#output
"""
Enter Username: Anup
Enter password: 12345
Loggin In..
"""

But anyone can see that password, and that breaches the security. So to make it more secure, we would be using the getpass() module.

from getpass import getpass
uname = input('Enter Username: ')
pwd = getpass('Enter password: ')
print('Logging In....')

#output
"""
Enter Username: Anup
Enter password: *****
Loggin In..
"""

7. List embedding with a nested loop in python

If you need to create a list while iterating through a list or tuple you can use this approach.

list1 = [1,2,3]
list2 = ['a', 'b']

result = [(x, y) for x in list1 for y in list2]

for x,y in result: print(x, y)

Output:
1 a
1 b
2 a
2 b
3 a
3 b

8. Finding cities with above-average pollution peaks from a set of data

Suppose you have this NumPy array that holds the sample pollution data for cities Hong Kong, New York, Berlin, and Montreal.

# Dependencies
import numpy as np

X = np.array(
       [[ 42, 40, 41, 43, 44, 43 ], # Hong Kong
       [ 30, 31, 29, 29, 29, 30 ], # New York 
       [ 8, 13, 31, 11, 11, 9 ], # Berlin 
       [ 11, 11, 12, 13, 11, 12 ]]) # Montreal

From this data, we want to get the above-average pollution peaks. For that let’s create a mask using the below condition.

print(X > np.average(X)) 
""" 
[[ True True True True True True]
 [ True True True True True True]
 [False False True False False False]
 [False False False False False False]]
""" 

Now let’s convert all of these boolean values into nonzero’s.

print(np.nonzero(X > np.average(X))) 
"""
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2], dtype=int64), 
array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 2], dtype=int64)) 
"""

Now, there is only one thing left to do; remove duplicates and give columns a name.

cities = np.array(["Hong Kong", "New York", "Berlin", "Montreal"])

print(cities[np.nonzero(X > np.average(X))[0]]) 

polluted = set(cities[np.nonzero(X > np.average(X))[0]])

# Result
print(polluted)
""" ['Hong Kong' 'Hong Kong' 'Hong Kong' 'Hong Kong' 'Hong Kong' 'Hong Kong' 'New York' 'New York' 'New York' 'New York' 'New York' 'New York' 'Berlin'] 
"""

Now let’s conclude everything and we can perform everything in a single line.

polluted = set(cities[np.nonzero(X > np.average(X))[0]])

# Result
print(polluted)

9. Make your search faster

If you need to search for a list of items. Searching with a set is faster (O(1)) compared to a list (O(n)).

# Instead of this 
l = ['a', 'e', 'i', 'o', 'u']

def is_vowel(char):
    if char in l:
        print("Vowel")

# Use this
s = {'a', 'e', 'i', 'o', 'u'}

def is_vowel(char):
    if char in s:
        print("Vowel")

Also Read: Zero to Mastery Decision Tree Guide to Move in Data Science

10. Variable Unpacking

In python, variable unpacking is the process of assigning an iterable of values to a list or tuple of variables using a single assignment statement.

x, y = [1, 2]
# x = 1, y = 2

x, *y = [1, 2, 3, 4, 5]
# x = 1, y = [2, 3, 4, 5]

x, *y, z = [1, 2, 3, 4, 5]
# x = 1, y = [2, 3, 4], z = 5

11. Save Memory with Generators

Let’s say we have a very large list with 100000 items and we want to calculate the sum over all the items. The list is not always the best choice. Of course, you can do this with a list but you might waste lots of RAM.

This can make a huge difference when working with large data, so it’s always good to keep the generator in mind!

import sys 

my_list = [i for i in range(100000)]
print(sys.getsizeof(my_list), 'bytes') # 824456 bytes

my_gen = (i for i in range(100000))
print(sys.getsizeof(my_gen), 'bytes') # 112 bytes

12. Lambda Function to filter NumPy array

We can easily filter a NumPy array or DataFrame series using the lambda function. In the below example, I’m filtering a NumPy array of books and ratings which have ratings more than 3.9.

## Dependencies
import numpy as np

## Data looks like [title, rating]
books = np.array([['Coffee Break NumPy', 4.6],
['Lord of the Rings', 5.0], ['The Alien Artifact', 1.8], 
['Winnie-the-Pooh', 3.9],
['The Betrothed by Kiera Cass',2.9],
 ['Forbidden', 3.0], 
['Coffee Break Python', 4.7]]) 

predict_bestseller = lambda x, y : x[x[:,1].astype(float) > y] 

print(predict_bestseller(books, 3.9))

# Output
"""
[['Coffee Break NumPy' '4.6']
 ['Lord of the Rings' '5.0']
 ['Coffee Break Python' '4.7']]
"""

13. Swapping values between two variables

Let’s say you have a variable x hold 10 and y holds 20. Now you want to swap the values between x and y. So, y should hold 20 and x should hold 10.

To do this python have an easy trick on its sleeve.

# Normal Programmers
temp = x
x = y
y = temp

# Mentos Programmers
x, y = y, x

14. Filter unique values with Sets

If you have a list of values that could contain duplicate values. But you want only unique values from the list. One simple way of doing this is converting your list to a set. As the set will only contain unique values.

my_list = [1,2,3,3,'3',4,5,6,7,7,7,'a','a','a']
my_set = set(my_list) # removes duplicates
print(my_set) 

# Output
"""
{1, 2, 3, 4, 5, 6, 7, 'a', '3'}
"""

15. Finding Palindromes with Lambda Functions and Negative Slicing

A palindrome is a number/string that remains the same when its characters or digits are reversed. For instance, 131 is a palindrome but 123 is not.

Most palindrome strings will have an even number of characters and similarly, most palindrome numbers are even.

is_palindrome = lambda phrase: phrase == phrase[::-1] 

print(is_palindrome("dad"))
print(is_palindrome("mom"))
print(is_palindrome("python"))

# Output
"""
True
True
False

"""

16. Calculating the Fibonacci Series with the reduce() Function

The idea behind Python’s <a href="https://docs.python.org/3/library/functools.html#functools.reduce" target="_blank" rel="noreferrer noopener nofollow"><code>reduce() is to take an existing function, apply it to all the items in an iterable, and generate a single final value. It’s used with an implementation of a mathematical technique called reduction or folding. It’s handy for processing iterable without writing loops.

Syntax:

functools.reduce(function, iterable[, initializer])

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the updated value from the iterable.

Using the reduce() function we can easily calculate the n number of Fibonacci numbers.

# imports
from functools import reduce #functools for reduce()

# How many numbers
n = 10 

# calculating fibonacci
fibs = reduce(lambda x, _: x + [x[-2] + x[-1]], [0] * (n-2), [0, 1]) 
print(fibs)

# Output
"""
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
"""

17. Multiple Comparisons without logical operator

Using comparison operators we compare two values. Its return is true or false based on the comparison. But sometimes if you need to find a value between two or more conditions then we use logical operators like and, or, not to combine conditions.

For example, Our value x should be between 0 to 100. So, we need two conditions, 0<x, and x< 100. Two combine both of the conditions we can use logical and. In python, we have a simple approach we can just write it as 0 < x < 100.

# Instead of this
if 0 < x and x < 100:
    print(f"{x} is a 2 digit number")

# Use this
if 0 < x < 100:
    print(f"{x} is a 2 digit number")

Similarly, if you need a bunch of conditions then there is no need to combine them using logical operators. You can use a list or tuple with these elements and using it you can check all of them.

# Instead of this
if b == "Mon" or b == "Wed" or b == "Fri" or b == "Sun":
    # do something here

# Use this 
if b in "Mon Wed Fri Sun".split():
    # do something here

Till now we are comparing using only one variable. But suppose you need to compare multiple variables. For that, you can use all() or any() methods. From the function name you can guess, that if you need all the conditions true then all() is going to return true just like the “and” operator. In case any() conditions are true it’s going to return just like or operator.

# Instead of this
if a < 10 and b > 5 and c == 4:
    # do something

# Use this all()
if all([a < 10, b > 5, c == 4]):
    # do something

# Instead of this
if a < 10 or b > 5 or c == 4:
    # do something

# Use this any()
if any([a < 10, b > 5, c == 4]):
    # do something

18. the best way to Iterating dictionary in python

If you have a dictionary of fruit prices. If you want to iterate through it then follow the below approach.

fruit_price = {
    "apple": 200,
    "banna": 20
}

# Instead of this
for key in fruit_price:
    print(key, fruit_price[key])

# Do this
for fruit, price in fruit_price.items():
    print(fruit, price)

19. Better way of String Formatting

Let’s say we have different variables which have different data types. Now if we want to create a string value for all of these variables then we can either convert all of these values to strings. Or we can be using String formatting.

There are three ways to do string formatting. One of them is F-Strings which are introduced in python 3. String formatting is a great new way to format strings and is more readable, faster, concise, and less prone to error than other ways of formatting!

name = "3080ti"
price = 97000

# Instead of this
print(name + " costs "+str(price))

# Use this
print(f"{name} costs {price}.")

# Or this
print("{} costs {}.".format(name, price))

# Or this
print("%s costs %d." %(name, price))

20. The Dir Function

It is a powerful inbuilt function in python, which returns a list of the attributes and methods of any object i.e. functions, modules, strings, lists, dictionaries, etc.

This can be very useful when having little to no information about the module and helps to learn new modules faster.

numbers = [1, 2, 3, 4, 5]
print(dir(numbers))

# Output
"""
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
"""

21. Use a Counter for Element Counting

If you want to count how many times unique elements occur; you can use Counter from collections. Counter returns a counter object which stores key-value pairs. The Key will be the word and the value will be the frequency of the word.

words = ['an', 'boy', 'girl', 'an', 'boy', 'dog', 'cat', 'Dog', 'CAT', 'an','GIRL', 'AN', 'dog', 'cat', 'cat', 'bag', 'BAG', 'BOY', 'boy', 'an']

# Instead of this
unique_words = {x.lower() for x in set(words)}

for word in unique_words:
     print(f"* Count of {word}: {words.count(word)}")
 
# Output
"""
Count of girl: 1
Count of boy: 3
Count of dog: 2
Count of an: 4
Count of cat: 3
Count of bag: 1
"""

# Use this
from collections import Counter

word_counter = Counter(x.lower() for x in words)
print("Word Counts:", word_counter)

# Output
"""
Word Counts: Counter({'an': 5, 'boy': 4, 'cat': 4, 'dog': 3, 'girl': 2, 'bag': 2})
"""

22. Merge two dictionary

Using **kwargs like an object we can easily merge 2 or more dictionaries.

d1={"A": 10, "B": 20, "C": 30}
d2={"X": 100, "Y": 200, "Z": 300}

d3={**d1, **d2}
print(d3)

# Output
"""
{'A': 10, 'B': 20, 'C': 30, 'X': 100, 'Y': 200, 'Z': 300}
"""

23. Find the most common item

Let’s say you want to find the most common item on a list. We can use the counter which is going to give us a counter object. From the counter object, you can call the most_common() method; which is going to return a list sorted by frequency.

# import
from collections import Counter

words = ['an', 'boy', 'girl', 'an', 'boy', 'dog', 'cat', 'Dog', 'CAT', 'an','GIRL', 'AN', 'dog', 'cat', 'cat', 'bag', 'BAG', 'BOY', 'boy', 'an']

word_counter = Counter(x.lower() for x in words)

# Find out the most common item
print("Most Common:", word_counter.most_common(1))

# Find out the most common 2 items
print("Most Common 2 :", word_counter.most_common(2))

# Output
"""
Most Common: [('an', 5)]
Most Common 2 : [('an', 5), ('boy', 4)]
"""

24. Reverse a string with negative slicing

If you want to reverse a string with the help of negative slicing; you can easily do it.

sentence = "This is just a test"
reversed = sentence[::-1]
print(reversed)

# tset a tsuj si sihT

25. Squash a list of strings into one string

Let’s say you have a list of words and you want to join all of them together as a string. Using the string join() method we can do that. Also, we can easily add space between two words.

words =['The', 'quick', 'brown', 'fox']
combined = " ".join(words)
print(combined)

# Output
"""
The quick brown fox
"""

26. Find the Most Frequent Element in a List

If you need to find out the most frequent element in a list then you can use this trick.

test = [6, 2, 2, 3, 4, 2, 2, 90, 2, 41]
most_frequent = max(set(test), key = test.count)
print(most_frequent)

# Output
"""
2
"""

27. Duplicate Strings without looping

Suppose you have a string and you want to duplicate it 20 times. Then you can either use a loop or use this simple trick.

name = "ATT"
print(name * 5)

# Output
"""
ATTATTATTATTATT
"""

28. Using map()

One of Python’s built-in functions is called map(). The syntax for map() is:

map(function, something_iterable)

In the example below we will convert a string of numbers into int and then convert them into a list of items.

# Convert a string representation of
# a number into a list of ints.
list_int = list(map(int, "1234567"))
print(list_int)
# Output
"""
[1, 2, 3, 4, 5, 6, 7]
"""

29. Create a progress bar

Let’s say you are building a command-line Interface(CLI) and performing a certain operation takes some time. If we don’t show any update to the user, he might get confused that the system is hanged or something wrong happened.

We can easily overcome it just by creating a progress bar. It will be a tremendous task to do. But there is a quicker way of doing it; by using the process package.

68747470733a2f2f7261772e6769746875622e636f6d2f7665726967616b2f70726f67726573732f6d61737465722f64656d6f2e676966

You can install the process by using a pip.

pip install progress

You can create a progress bar with minimal effort:

from progress.bar import Bar

bar = Bar('Processing', max=20)
for i in range(20):
    # Do some thing
    bar.next()
bar.finish()

30. Customize the terminal/cmd with colors

While creating a CLI application if you wanted to give your application a unique look and better usability. Then you should use the pythons Colorama package. Using this package you can add some color to your terminal.

You can install the Colorama by using pip.

pip install colorama

Now we can easily print some colorful text on the terminal

from colorama import Fore, Back, Style

print(Fore.RED + 'ATT in red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
customize terminal with colorama python
output

31. Getting the Date and time from the log file

If you ever have to deal with log files then you must know it’s always a headache to get dates and times from a log file. But there is an easy solution to this problem. The python-dateutil module provides powerful extensions to the standard DateTime module.

You can install it with:

pip install python-dateutil

Now, you can just use particularly useful: fuzzy parsing of dates from log files and such.

from dateutil.parser import parse

logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.'
timestamp = parse(logline, fuzzy=True)
print(timestamp)

# Output
"""
2020-01-01 00:00:01
"""

32. Sorting objects by multiple keys

If we have a list of dictionaries have two keys ‘name’, and ‘age’. Now if we don’t just want to sort it by name or age; we want to sort it by both fields.

In SQL, this would be a query like SELECT * FROM people ORDER by name, age.

We can easily do this using operator.itemgetter(). In the below example we are shorting a dictionary based on name and age.

people = [
{ 'name': 'John', "age": 64 },
{ 'name': 'Janet', "age": 34 },
{ 'name': 'Ed', "age": 24 },
{ 'name': 'Sara', "age": 64 },
{ 'name': 'John', "age": 32 },
{ 'name': 'Jane', "age": 34 },
{ 'name': 'John', "age": 99 },
]

# Sorting by name and age

import operator
people.sort(key=operator.itemgetter('age'))
people.sort(key=operator.itemgetter('name'))

#Output
"""
[
 {'name': 'Ed',   'age': 24},
 {'name': 'Jane', 'age': 34},
 {'name': 'Janet','age': 34},
 {'name': 'John', 'age': 32},
 {'name': 'John', 'age': 64},
 {'name': 'John', 'age': 99},
 {'name': 'Sara', 'age': 64}
]
"""

33. Sort Complex Iterables With Sorted()

Using the python sorted() function we can sort a list or tuple without modifying the original sequence.

Syntax:

sorted(iterable, key, reverse)
key it's an optional value which by default None.
reverse by default it is  false. If it's set true, then the iterable would be sorted in descending order.

Below we have two examples in the first example we are sorting the list by numbers. In the second example, the dictionary is sorted by age.

num_list = [3,2,6,33,11,64,99,15]
num_sorted = sorted(num_list)

data = [{"name" : "Joy", "Age" : 19}, 
        { "name" : "Lisa", "Age" : 17},
        { "name" : "Jack", "Age" : 21}]
sorted_data = sorted(data, key = lambda x : x['Age'])

print("Sorted list:", num_sorted)
print("Sorted dictionary list:", sorted_data)

"""
Output:Sorted list: [2, 3, 6, 11, 15, 33, 64, 99]
Sorted dictionary list: [{'name': 'Lisa', 'Age': 17}, {'name': 'Joy', 'Age': 19}, {'name': 'Jack', 'Age': 21}]
"""

34. Use of the get() method in Dictionary

If we want to extract a value from a dictionary then using [] we can get it. But if the key does not exist then it’s going to return an error.

We can extract a value from a dictionary in a much better way using the get() method.

Syntax:

dictionary.get(key, value)
key - key of the item you are looking for.
value - default value if specified key does not exits.

In the below example we have a dictionary called the car.

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

print("Brand:", car.get("brand"))
print("Type:", car.get("type", "Not Found"))

"""
Output:Brand: Ford
Type: Not Found
"""

35. Use of Enum in Python

Python enum short for enumerations module, a powerful tool for handling sets of data that are not going to change.

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

print(Color.RED.name)
print(Color.RED.value)
print(list(Color))

# Output
"""
RED
1
[<Colors.RED: 1>, <Colors.GREEN: 2>, <Colors.BLUE: 3>]
"""

# We can also create a Enum in one line
Colors = Enum('Colors', 'RED GREEN BLUE')
print(Colors.RED.name)
print(Colors.RED.value)
print(list(Colors))

# Output
"""
RED
1
[<Colors.RED: 1>, <Colors.GREEN: 2>, <Colors.BLUE: 3>]
"""

Conclusion

Tried my best to keep this article as comprehensive as possible, but it could possible that some of the useful tips are missing. If so, please share it in the comment section, I will be happy to know about it. Be curious, keep learning and stay willing to learn new things until we meet next time! 

If you like this article and wish to connect with me follow on Linkedin. Share your thoughts in the comments section below, what did you find interesting in this article.

Now I’m leaving you with an interesting thought from Lao Tzu. Until next time, take care of yourself, your family, and your friends, and stay safe and healthy!

“To attain knowledge, add things every day. To attain wisdom, remove things every day.”

Lao Tzu.
Anup-Das-Anuptechtips

Anup Das

I'm obsessed with python and write articles about python tutorials for Django, Data Science, and Automation.

1 thought on “35 Best Python Tips and Tricks For Writing Better Code”

Comments are closed.