Top 10 important Python Tips and Tricks What you Don’t know

Python is a high-level and general-purpose programming language. It is also one of the most popular and demanding programming languages in 2021.

Python used in Web Apps, Automation, Web scraping, Machine Learning, game, Android Apps, and lots of other sectors so you can use these python tips and tricks that will help you.

One of the most important things is that most people start their programming journey with Python as it is very easy to learn.

So today in this article I am going to tell you the top 10 Python tips and tricks that you don’t know. I am sure that most of the tricks you don’t know. 

let’s make a bet, If you didn’t know at least 5 tricks out of these tricks before reading this article, you have to share this article with your social media account. So let’s start.


1. Store conditions in a list.

The first trick is to store conditions in a list. for example, you have 3 variable:

number_of_apple = 50
number_of_mango = 100
number_of_banana= 90

Now if you want to write these variables in the If-else condition, You simply write this like this:

number_of_apple = 50
number_of_mango = 100
number_of_banana= 90
 
if number_of_apple > 40 and number_of_mango == 100 and number_of_banana < 200:
    print('Yo, Hello World')

But, now the trick is you can store any conditions in the list. Like below given examples:

number_of_apple = 50
number_of_mango = 100
number_of_banana= 90
 
conditions = [
     number_of_apple > 40, 
     number_of_mango == 100,
     number_of_banana < 200,
]
if all(conditions):
    print('Yo, Hello World')

Now if you want to execute only one condition, then you can write the code like this:

conditions = [
     number_of_apple > 40,
     number_of_mango == 100,
     number_of_banana < 200,
]
 
if any(conditions):
    print('Yo, Hello World')

2. Take more than one user input.

Trick number two is to take more than one input with a single line of python code. Guys, As you know that if you want to take any user input in python, you just need to type like this:

a = input('Give me an Input')
print(a)

Now the trick is if you want to take more than one user input, then simply type like this:

a , b , c , d = input('Give me an Input ').split()
print(a)
print(b)
print(c)
print(d)

3. Print multiple variables in one single line.

The third trick is if you guys want to print multiple variables in one line, Then First type “print(variables)” and just add a comma in each variable. An example is given below:

print(variable1 , variable2 , variable3 , variable4 , variable5)

4. Switch Variable.

The fourth trick is to Switch variable, For example, you have 2 variables

variable1 = 300
variable2 = 10

and you want to switch these variables’ values that’s mean the value of variable1 will go to variable2 and the value of variable2 will go to variable1. A normal programmer will write something like this:-

variable1 = 300
variable2 = 10
print(variable1, variable2)
temp_variable = variable1 #first store varible1 value in temp_variable
variable1 = variable2 #then asign the variable2 value in variable1
variable2 = temp_variable #and asign the temp_variable value to variable2
print(variable1, variable2)
Output:
300 10
10 300

But the trick is you can write it in only one line of Python code, Like this:–

Variable1 , variable2 = variable2 , variable1

5. Type Variable under a string.

Number five trick is ‘f’ string. Most of the time we write any variable under the string. We type variable under the string something like this:

variable = "share"
print("Hello, Please " + variable + " this article")

But this is not the recommended way. The simplest way is by writing f string. Type ‘f’ before quotation marks. Something like this example given below:

variable = "share" #easy way
print(f"Hello, Please {variable} this article")

6. Remove Duplicate Item.

Trick number six is to remove the duplicate items from any list. For example, you have a list where stored lots of duplicates items and you want to remove the duplicate items. Then the easiest way to do this given bellow:

items_list = [1,2,2,3,2,1,2,4,5,6,6,6,7,7,7,8,9,8]
remove_duplicates = list(set(items_list)))

As you can see we have a list of numbers with lots of duplicate numbers. Now to remove duplicates, first of all, we convert the list to a set (A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements). Then we just convert our set to a list. 

7. Use Modern IDEs.

Trick number seven is to use modern IDEs (Integrated Development Environment). There is a lot of modern code editor available in the market to boost up your coding. A lot of code editors have one functionality that if you write code then the code editor recommended the code snippet to boost up your work. (My Recommendation for Python Programmers: Use Visual Studio Code to write code. Because there are lots of interesting things you can do with Microsoft VS Code.) 

8. Find the repeated numbers.

Trick number eight is to find the most repeated number of items in a list. In our previous trick, we had been seen that how to remove duplicate or repeated items from a list. This is also similar to this. An example is given below:-

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

9. Run anything forever.

Trick number nine is to run anything unlimited. For example, you have a function something like this:-

def Myf():
    print('Share this article and follow us')

Now you want to run this function for unlimited times. Then you can write the code something like this:-

def Myf(): #Function
    print('Share this article and follow us')
 
while True:
    Myf() #call the function

10. Count words from the list.

The last trick is that if you want to count the number of words of a string. Then the easiest way to count is that example is given below:-

string = "Share this article to your friends and follow us" #this is our string
s = string.split() #Split each word
lenght = len(s) #count the splited words
print(lenght) 

So, as we decided if you don’t know at least 5 python tips and tricks share this article, and if you know more python tips and tricks comment below, we will share your tips and tricks and feature you in our post. Thank you for reading this article, comment below if you have any queries about python. Also, check out the top 5 best hosting for bloggers. Guys if you like these tips and tricks, please share this article and follow us for our future updates. 

Leave a Comment