10 Interesting Python Cool Tricks


With increase in popularity of python, more and more features are becoming available for python coding. Using this features makes writing the code in fewer lines and cleaner. In this article we will see 10 such python tricks which are very frequently used and most useful.

Reversing a List

We can simply reverse a given list by using a reverse() function. It handles both numeric and string data types present in the list.

Example

List = ["Shriya", "Lavina","Sampreeti" ]
List.reverse()
print(List)

Output

Running the above code gives us the following result −

['Sampreeti', 'Lavina', 'Shriya']

Print list elements in any order

If you need to print the values of a list in different orders, you can assign the list to a series of variables and programmatically decide the order in which you want to print the list.

Example

List = [1,2,3]
w, v, t = List
print(v, w, t )
print(t, v, w )

Output

Running the above code gives us the following result −

(2, 1, 3)
(3, 2, 1)

Using Generators Inside Functions

We can use generators directly inside a function to writer shorter and cleaner code. In the below example we find the sum using a generator directly as an argument to the sum function.

Example

sum(i for i in range(10) )

Output

Running the above code gives us the following result −

45

Using the zip() function

When we need to join many iterator objects like lists to get a single list we can use the zip function. The result shows each item to be grouped with their respective items from the other lists.

Example

Year = (1999, 2003, 2011, 2017)
Month = ("Mar", "Jun", "Jan", "Dec")
Day = (11,21,13,5)
print zip(Year,Month,Day)

Output

Running the above code gives us the following result −

[(1999, 'Mar', 11), (2003, 'Jun', 21), (2011, 'Jan', 13), (2017, 'Dec', 5)]

Swap two numbers using a single line of code

Swapping of numbers usually requires storing of values in temporary variables. But with this python trick we can do that using one line of code and without using any temporary variables.

Example

x,y = 11, 34
print x
print y
x,y = y,x
print x
print y

Output

Running the above code gives us the following result −

11
34
34
11

Transpose a Matrix

Transposing a matrix involves converting columns into rows. In python we can achieve it by designing some loop structure to iterate through the elements in the matrix and change their places or we can use the following script involving zip function in conjunction with the * operator to unzip a list which becomes a transpose of the given matrix.

Example

x = [[31,17],
[40 ,51],
[13 ,12]]
print (zip(*x))

Output

Running the above code gives us the following result −

[(31, 40, 13), (17, 51, 12)]

Print a string N Times

The usual approach in any programming language to print a string multiple times is to design a loop. But python has a simple trick involving a string and a number inside the print function.

Example

str ="Point";
print(str * 3);

Output

Running the above code gives us the following result −

PointPointPoint

Reversing List Elements Using List Slicing

List slicing is a very powerful technique in python which can also be used to reverse the order of elements in a list.

Example

#Reversing Strings
list1 = ["a","b","c","d"]
print list1[::-1]

# Reversing Numbers
list2 = [1,3,6,4,2]
print list2[::-1]

Output

Running the above code gives us the following result −

['d', 'c', 'b', 'a']
[2, 4, 6, 3, 1]

Find the Factors of a Number

When we are need of the factors of a number, required for some calculation or analysis, we can design a small loop which will check the divisibility of that number with the iteration index.

Example

f = 32
print "The factors of",x,"are:"
for i in range(1, f + 1):
   if f % i == 0:
print(i)

Output

Running the above code gives us the following result −

The factors of 32 are:
1
2
4
8
16
32

Checking the Usage of Memory

We can check the amount of memory consumed by each variable that we declare by using the getsizeof() function. As you can see below, different string lengths will consume different amount of memory.

Example

import sys
a, b, c,d = "abcde" ,"xy", 2, 15.06
print(sys.getsizeof(a))
print(sys.getsizeof(b))
print(sys.getsizeof(c))
print(sys.getsizeof(d))

Output

Running the above code gives us the following result −

38
35
24
24

Updated on: 08-Aug-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements