Learn Python – Python Important Tips and Tricks- Basic and advance

For all the Python developers or any other language developers, it is always fascinating to know about the tricks and tips of the programming language that we are learning. As we all know that Python is one of the most preferred programming languages among developers. Therefore, in this tutorial, we bring the essential tips and tricks in Python that every Python developer should know.

10 Essential Tips and Tricks for Python

The tricks or tips of Python that we are going to discuss here will save us from writing many lines of code and will save a lot of time. These Python tips are tricks that will also help us to bring up our coding game and boost us while competing with other programmers.

Here is the list of such ten essential Python tips and tricks that every Python developer should know:

  1. Creating a single string from elements of a list:

We can simply create a single string from all the elements of a given list by using “.” with the join() function inside the print statement with the list variable. So, by this, we can easily get the single string data format from the multiple data elements given in list format.

Example:

# Given data elements in list format  
GivenList = ["Hello", "Python", "Developers!", "Welcome", "to", "JavaTpoint"]   
# printing single string using "." With join() function  
print(" ".join(GivenList))   

Output:

Hello Python Developers! Welcome to JavaTpoint

2. Python use of Enums:

In Python, we can simply use the Enums to check the number of occurrences of a variable inside the given function where it first occurred. We just have to use the word with the function name inside the print statement with the “.” operator to print the number of the first occurrence of that variable inside the function.

Example:

# define a class for Enums  
class EnumExample:   
    Hello, Python, Developers, Welcome, to, JavaTpoint, tutorial, of, Python = range(9)  
# printing first number of occurrences of the given variable  
print("Occurrence of JavaTpoint: ", EnumExample.JavaTpoint)   
print("Occurrence of Hello: ", EnumExample.Hello)   
print("Occurrence of Python: ", EnumExample.Python)  
print("Occurrence of Welcome: ", EnumExample.Welcome)  

Output:

Occurrence of JavaTpoint:  5
Occurrence of Hello:  0
Occurrence of Python:  8
Occurrence of Welcome:  3

3. Printing path of the imported module:

If we need to print the file directory or path for the Python modules we have imported in the programs, then we just have to use the module name inside the print statement simply, and the file directory will be printed in the output.

Example: Looking at the following Python program:

# Importing modules in the program  
import socket  
import numpy  
import os  
# Printing the file directory of module imported in program  
print(socket)  
print(numpy)  
print(os)  

Output:

<module 'socket' from 'C:\\Users\\Manish\\lib\\socket.py'>
<module 'numpy' from 'C:\\Users\\Manish\\lib\\site-packages\\numpy\\__init__.py'>
<module 'os' from 'C:\\Users\\Manish\\lib\\os.py'>

4. Printing the most occurred element of a list:

We have given a list with a number of elements in it, and there are many elements in it that are repeated more than one time. Now, if we want to print the number of elements that are occurred most in the list and it is the same as finding mode in statistics from the given data of numbers. We use max() and count function to get the result of the most occurred element.

Example:

# A list with the number of elements in it  
GivenList = [24, 21, 27, 29, 17, 23, 29, 34, 67, 23, 21, 29, 19, 63, 29, 27, 35, 21, 29]  
# Printing most occurred number or element in list  
print("Most occurred element in the given list: ", max(set(GivenList), key = GivenList.count))   

Output:

Most occurred element in the given list:  29

5. Printing given string number of times:

We can print a given string ‘n’ number of times in the output simply by using ‘String Name * n’ syntax into the print statement. It will print the given string n number of times in continuation in output.

Example:

# Define a string and n number  
GivenString = "Welcome to JavaTpoint, Python developers!"  
n = 4  
# Printing string multiple times  
print("Given string for n number of times: ")  
print(GivenString * n)  

Output:

Given string for n number of times: 
Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!

6. wapping of two variable numbers:

We can also do in-place swapping of two variables numbers so we can use their swapped values in the program.

Example: Look at the following Python program:

# Define two number variables  
m = 24  
n = 26  
print("m before swapping: ", m)  
print("n before swapping: ", n)  
# In-place swapping variables  
m, n = n, m  
print("m after swapping: ", m)  
print("n after swapping: ", n)  

Output:

m before swapping:  24
n before swapping:  26
m after swapping:  26
n after swapping:  24

7. Using chain of comparison operators:

We can use the chain of comparison operator to compare the given variable number with multiple values in a single comparison.

Example:

# Defining a number variable  
num = 31  
# Chaining comparison operators on num variable  
Result1 = 35 > num > 30  
Result2 = 17 > num < 35  
# Printing result of comparison  
print(Result1)  
print(Result2)  

Output:

True
False

8. Reversing a given string:

Sometimes, we have a given string variable, and we may have to print or use the reverse order of that string. Therefore, we should know the easiest way to print the reverse format of a given string.

Example: Look at the following Python program:

# Define a string variable  
GivenString = "Welcome to JavaTpoint Python Developers!"  
print("Given String in program: ", GivenString)  
# Printing reverse of string in the output  
print("Reverse of Given string in program is: ", GivenString[::-1])  

Output:

Given string in program:  Welcome to JavaTpoint Python Developers!
Reverse of Given string in program is: !srepoleveD nohtyP tniopTavaJ to emocleW

9. eturning multiple values from a single function:

We can print the multiple values or elements from a given single function simply by using only one print statement. It will save a lot of time for us from writing multiple lines of code in the program.

Example:

# Define a default functions  
def multival():   
    return 24, 25, 31, 43, 37, 29, 39, 23  
# Defining multiple values from multival() function  
j, k, l, m, n, o, p, q = multival()  
# Printing multiple values in single statement  
print(j, k, l, m, n, o, p, q)  

Output:

24 25 31 43 37 29 39 23

10. Checking for anagram words:

Anagram words are the words in which all the letters are the same in two different words, but the letters of words are arranged in a different order within the word. We can check that if the given two words are pair of anagram words or not.

We can perform this action of checking anagram words, by using the following two methods:

a. Without importing an external module in program:

Look at the following example Python program:

# A default function to check anagram word logic  
def CheckAnagram(mkr1, mkr2):    
    return sorted(mkr1) == sorted(mkr2) # logic  
# Checking anagram words with default function  
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))   
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))  

Output:

Words are anagrams:  True
Words are anagrams:  False

b. By importing an external module in program:

# Importing counter from collection module  
from collections import Counter  
# A default function to check anagram word logic  
def CheckAnagram(mkr1, mkr2):    
    return Counter(mkr1) == Counter(mkr2)  # logic  
# Checking anagram words with default function  
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))   
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))  

Output:

Words are anagrams:  True
Words are anagrams:  False