Top 10 Python Tricks

Python is one of the most used programming languages. It is right now involved with Data Science, Machine Learning, Artificial intelligence, Robotics, Web development, and many more.  Every day people are trying to learn this simple to use programming language, coming from different backgrounds.

Here in this article, we are going to discuss the top 10 python tricks that every programmer shoud know. These tricks are not in any specific order so read all of them.

Input multiple value using Single Input( ) function

input( ) function is responsible to take input from the standard input device ie keyboard in the form of a string. A normal programmer use this function like this to get 3 values from the keyboard

a= input('Enter first value ")
b= input('Enter second value ")
c= input('Enter third value ")
print('value of a :',a)
print('value of b :',b)
print('value of c :',c)

on the other hand you can use the following method to take 3 input from the single input() function like this

a,b,c = input('Enter space separated thee values ").split()
print('value of a :',a)
print('value of b :',b)
print('value of c :',c)

In the above command input() is able to take three values from a single input() function and using the split() function of string its has been divided into three values.

Swapping the value of Two variables

The old style way of changing the value of two variable is – use a third variable and perform the operation like this

temp = a
b= temp
print('value of a :',a)
print('value of b :',b)

whereas in python, there is no need to introduce a third variable like temp. The trick is something like this

a,b = b,a
print('value of a :',a)
print('value of b :',b)

Shorthand form of if else statement

if else statement is known as a selection statement where one course of action is taken based on the outcome of the associated statement. like this

if( 5>3)

the same condition can be written in python using its shorthand like this

x=1 if (5>3) else 0

Checking multiple conditions using AND operator

The traditional way of checking multiple conditions is – use AND or OR operator. if you are using AND operator for checking the validity of multilpe conditions, the old school way is something like this

a =12
if(a>10 and b>20 and c>30):
  print('awesome trick')

The above code will produce the desired output if and only if all the above conditions are true. but what if there are more than 10 conditions or 20 conditions to check. Python has some cool tricks to solve this issue.

Put all the conditions in a list and then using all functions, we can achieve the same.

condition =[a>10, b>20,c>30]
if all(condition):
  print('awesome trick');

But what is your conditons are joined by OR then inplace of all() function use any() function.

if any(condition):
  print(' I am awesome ');

Finding the unique values

if you have a list of numbers and you need to find out the unique elements present in this list. The old school way of doing this is like this

list1 =[1,2,3,99,3,54,56,6,7,4,3,4,5,6,7,8,89,9,4,5,6,7,4,77]
for x in list1:
    if x not in list2

This is a lots of work and code. here is the simple tricks to so the same

list2= list(set(list1))

set- the data type defined in python can store only the unique values – thus converted the list into the set and then reconverted that set into the list again.

Reversal of string

Reversal of any string is one such common problem that almost all the programmers have done this in his/her programming life. It is very very easy if you use Python. The traditional way and then the python way.

name ="rakesh kumar"
name1 = name[::-1]

Python way to do the same

name ="rakesh kumar"[::-1]

no need to assign and then reassign the same string into another variable.

conclusion of top10 python tricks

These are some of the most common python tricks that I thought, you should know, if you are trying to learn python from scratch otherwise, you will learn python like any other procedural programming language. There are thousands of other python tricks that you people will learn automatically during your journey.

if you know any such Python tricks that is not listed in this list then let us know, we will publish them with full attribution.

Print Friendly, PDF & Email
If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to See your article appearing on the main page and help other students/teachers.