16 Python Tricks To Learn Before You Write Your Next Code

Tricks that will make your life easier as a python developer

Muhammad Abdullah Arif
6 min readApr 4, 2023

Python is a versatile programming language that can be used for a wide range of applications, from web development to data analysis. However, as with any programming language, there are certain tricks and techniques that can help you write more efficient, elegant, and maintainable code. In this article, we’ll share 16 Python tricks that you should learn before you write your next code.

1 . List Comprehension

List comprehension is a concise way to create a list in Python. It is faster and more efficient than using loops and conditionals to create a list.
The Syntax
newlist = [expression for item in iterable if condition == True]
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name. Without list comprehension you will have to write a for statement with a conditional test inside:

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
if "a" in x:
newlist.append(x)

print(newlist)

--

--

Muhammad Abdullah Arif
Muhammad Abdullah Arif

Written by Muhammad Abdullah Arif

Python developer. The facts are the facts but opinions are my own.