10 Cool and Unique Tricks in Python

How to Write More Concise, Readable, and Expressive Code

Python is a powerful programming language that offers many built-in functions and features to make programming easier and more efficient. As a Python developer, you might be familiar with the basics of the language, such as variables, functions, and control flow statements. However, there are many cool and unique tricks in Python that can help you write more concise, readable, and expressive code. In this article, we’ll explore some of these tricks and show you how to use them in your Python code.


Using the zip() function to combine two lists:

The zip() function can be used to combine two lists and return a list of tuples, where each tuple contains one element from each of the input lists. Here’s an example:

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

result = list(zip(list1, list2))
print(result)

Output

[(1, 'a'), (2, 'b'), (3, 'c')]

Swapping two variables without using a temporary variable:

We can swap the values of two variables without using a temporary variable by using tuple unpacking. Here’s an example:

a = 5
b = 10

a, b = b, a

print('a =', a)
print('b =', b)

Output

a = 10
b = 5

Using the enumerate() function to iterate over a list with indices:

The enumerate() function can be used to iterate over a list with its indices. Here’s an example:

fruits = ['apple', 'banana', 'orange']

for index, fruit in enumerate(fruits):
    print(index, fruit)

Output:

0 apple
1 banana
2 orange

Using the * operator to unpack a list:

The * operator can be used to unpack a list. Here’s an example:

numbers = [1, 2, 3, 4, 5]

print(*numbers)

Output

1 2 3 4 5

Using the map() function to apply a function to each element in a list:

The map() function can be used to apply a function to each element in a list and return a new list with the modified values. Here’s an example:

numbers = [1, 2, 3, 4, 5]

result = list(map(lambda x: x**2, numbers))
print(result)

Output:

[1, 4, 9, 16, 25]

Using list comprehension to create a list of even numbers:

We can use list comprehension to create a list of even numbers. Here’s an example:

even_numbers = [x for x in range(1, 11) if x % 2 == 0]
print(even_numbers)

Output

[2, 4, 6, 8, 10]

Using dictionary comprehension to create a dictionary:

We can use dictionary comprehension to create a dictionary. Here’s an example:

squares = {x: x**2 for x in range(1, 6)}
print(squares)

Output

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Using the any() function to check if any element in a list is true:

The any() function can be used to check if any element in a list is true. Here’s an example:

numbers = [0, 1, 2, 3, 4, 5]

result = any(numbers)
print(result)

Output

True

Using the all() function to check if all elements in a list are true:

The all() function can be used to check if all elements in a list are true. Here’s an example:

numbers = [1, 2, 3, 4, 5]

result = all(numbers)
print(result)

Output

True

Using the filter() function to filter out elements from a list:

The filter() function can be used to filter out elements from a list based on a condition. Here’s an example:

numbers = [1, 2, 3, 4, 5]

result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)

Output

[2, 4]

In this article, we’ve explored some of the cool and unique tricks in Python that can help you write better code.

By mastering these tricks and incorporating them into your Python code, you can become a more efficient and effective Python developer. So, go ahead and try out these tricks in your code and see how they can help you write better Python programs.

For more smart ways to implement algorithms: Visit this link