 10 Python Tips and Tricks that You Do Not Know

Published at - Oct 27, 2021

Today, I am going to share 10 clever Python tips and tricks that you can use to code smarter. I will explain the best way to do that. If you didn’t already know at least 5 tips before reading this, let me know in the comments.

So let’s get started with Python tips and tricks that will make your life easier and save you some time.

1. Multiple inputs

This tip is about accepting multiple inputs from users in a single line:

a = input('Enter number: ')
b = input('Enter another number: ')
print(a)
print(b)

# Output:
Enter number: 12 # hit enter
Enter another number: 13 # hit enter
12
13

Trick

a, b = input('Enter numbers: ').split()
print(a)
print(b)

# Output:
Enter numbers: 12 13 # enter multiple numbers separate by white space and hit enter.
12
13

So we can see after applying the trick we use one input() statement. It can take n inputs at a time separated by white space.

2. Multiple Conditions (Check for All)

We have n number of conditions to apply in the if statement and to do so we can use a list of conditions. See examples below:

claps = 435
followers = 223

if claps > 300 and followers > 200 and read > 200:

Trick

claps = 435
followers = 223

# list of conditions
conditions = [claps > 300, followers > 200, read > 200]

if all(conditions): # meet all conditions

3. Multiple Conditions (Check for Any One)

claps = 435
followers = 223

if claps > 300 or followers > 200 or read > 200:

Trick

claps = 435
followers = 223

# list of conditions
conditions = [claps > 300, followers > 200, read > 200]

if any(conditions): # meet any one condition

4. Swap Values

a = 1000
b = 500
print(a,b) # 1000 500
temp = a
a = b
b = temp
print(a, b) # 500 1000

Trick

a = 1000
b = 500
print(a,b) # 1000 500
a, b = b, a
print(a, b) # 500 1000

5. Print Unique Values from a List

So in this trick, we will convert list to set because we all know the set is unique, and then convert it to list again. Let’s see.

Example

a = [1,2,1,2,3,24,5,6,7,7,5,3,5,7,8,5,4,5,6,8,9,2,1,3,4,5,7,4,2,8,3,8]
a = list(set(a))
print(a)

6. Print the Most Repeated Element from the List

So in this trick, we will convert the list to set with the key count, and then find max(). Let's see:

Example

a = [1,2,1,2,3,24,5,6,7,7,5,3,5,7,8,5,4,5,6,8,9,2,1,3,4,5,7,4,2,8,3,8]
most_repeted = max(set(a), key = a.count)
print(most_repeted)

7. List Comprehension

Suppose we have to calculate the square of all odd numbers in a list. We do it in the following way:

odd_squares = []
for i in range(11):
if i % 2 == 1:
odd_squares.append(i**2)
print(odd_squares) # [1, 9, 25, 48, 81]

Trick

odd_squares = [i**2 for i in range(11) if i % 2 == 1]
print(odd_squares) # [1, 9, 25, 48, 81]

8. Multiple Arguments in Function

Trick

This trick shows you how you can pass n numbers of arguments in any function. Let’s see an example to get a sum of n numbers:

def sum_of_n_numbers(*a):
result = 0
for i in a:
result += i
return result

res = sum_of_n_numbers(1,2,3,4,5,6,7,8,9)
print(res) # 55

9. Reverse String

Trick

Let’s see how you can reverse a String easily:

name = 'Harendra'[::-1]
print(name) # ardneraH

10. Check for Palindrome

Trick

Let’s see how we can check for palindrome easily in the example below:

word = 'eye'
isPalindrome = word.find(word[::-1]) == 0
print(isPalindrome) # True

Conclusion

So I have listed the top 10 Python tricks with examples and explanations. I hope these tips and tricks will help you code smarter and also save time while coding. Please let me know how many tricks you knew before this article.

Related Posts -

An Introduction to Python m ...

Python map() function is used to execute a function on all elements of an i...  