7 Python Tips and Tricks

There are lots of features in Python that help us a lot and make our work easier. In this article, we are going to know some of them.

These seven tips and tricks are randomly ordered.

help()

help() is a Python built-in function. And it helps us a lot when getting stuck or we need to know more about a module, object, class, etc. For that, we need to pass them inside the function as an argument. It gives us detailed documentation about that. See the example below.

help(str) # As an output, we will see the details
import math

help(math) # As an output, we will see the details of the Math module.

As an output, we will see the details of the math module in our terminal.

dir()

dir() is very useful function. If we need to know about an object’s methods and properties then dir() helps us a lot. Suppose, If we want to know what methods and properties are in the math object, then just put math inside the dir() function. See the example below.

import math
dir(math) 

# We can see the square root method in the picture below as sqrt. 

print(math.sqrt(25)) # Output = 5.0
All methods and properties of math
dir(str)
All methods and properties of str

id()

This function helps us to know the memory address of any data. Your memory address may not be the same as mine. See the following example.

num = 3.14
id(num) # 2071970255208

char = 'A'
id(char) # 2072000491904

ord()

This function helps us to know the ASCII value of any character. Try the below codes in your terminal.

ord('A') # Output =  65

ord('a') # Output = 97

ord('!') # Output = 33

This function accepts only one character as a parameter. If you put two or more characters, you get a type error. To know more about ASCII.

Swap

Swapping is very easy in Python from the general procedure. Value can be assigned to many variables in one line in Python. That made it easy to swap. The following example will make the matter clearer.

a = 5
b = 10
# We can also assign this way
a, b = 5, 10

# Swap example 1

# temp = a
# a = b
# b = temp

# Swap example 2 (Pythonic Way)
a, b = b, a

print(a, b) # Output = 10 5

In the general procedure, we have to create the third variable. In python, we don’t need to do that.

f strings

F strings introduced in Python version 3.6. This feature will not work in the previous version. Basically, this feature is for formatting string. It helps us to concatenate string easily. We can understand it better in the example. So, let’s take a look.

# Variables
a, b = 5, 10

# concatenation using plus sign
print('The result of '  + str(a) + ' + ' + str(b) + ' = ' + str(a + b) ) # The result of 5 + 10 = 15

# f strings 
print(f'The result of {a} + {b} = {a + b}') # The result of 5 + 10 = 15

So, I hope you can understand the code. We can use variable and any other python expression inside the curly braces. And of course, don’t forget to put f before start the quotes.

enumerate()

enumerate() is very useful function. We can use it for getting index and element at the same time from a list in the loop. Generally, how we print index and element from a list? Let’s see an example.

week_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

for i in range(len(week_days)):
	print(f'index = {i} and value = {week_days[i]}')
	
'''
Output

index = 0 and value = Monday
index = 1 and value = Tuesday
index = 2 and value = Wednesday
index = 3 and value = Thursday
index = 4 and value = Friday
index = 5 and value = Saturday
index = 6 and value = Sunday
'''

On the other hand, using enumerate function we can do this very easily. Actually enumerate() function pairs every element of the list with their corresponding index. Let’s see another example of our previous code. But we will use this time enumerate() function.

week_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for index, val in enumerate(week_days):
  print(f'index = {index} and value = {val}')
  
# The output will be the same as the previous example

Don’t forget to pass the list inside the enumerate() function.

That’s all. I will try to cover some more of these types of python tips and tricks in the future. Have a nice day.