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.
We can simply reverse a given list by using a reverse() function. It handles both numeric and string data types present in the list.
List = ["Shriya", "Lavina","Sampreeti" ] List.reverse() print(List)
Running the above code gives us the following result −
['Sampreeti', 'Lavina', 'Shriya']
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.
List = [1,2,3] w, v, t = List print(v, w, t ) print(t, v, w )
Running the above code gives us the following result −
(2, 1, 3) (3, 2, 1)
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.
sum(i for i in range(10) )
Running the above code gives us the following result −
45
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.
Year = (1999, 2003, 2011, 2017) Month = ("Mar", "Jun", "Jan", "Dec") Day = (11,21,13,5) print zip(Year,Month,Day)
Running the above code gives us the following result −
[(1999, 'Mar', 11), (2003, 'Jun', 21), (2011, 'Jan', 13), (2017, 'Dec', 5)]
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.
x,y = 11, 34 print x print y x,y = y,x print x print y
Running the above code gives us the following result −
11 34 34 11
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.
x = [[31,17], [40 ,51], [13 ,12]] print (zip(*x))
Running the above code gives us the following result −
[(31, 40, 13), (17, 51, 12)]
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.
str ="Point"; print(str * 3);
Running the above code gives us the following result −
PointPointPoint
List slicing is a very powerful technique in python which can also be used to reverse the order of elements in a list.
#Reversing Strings list1 = ["a","b","c","d"] print list1[::-1] # Reversing Numbers list2 = [1,3,6,4,2] print list2[::-1]
Running the above code gives us the following result −
['d', 'c', 'b', 'a'] [2, 4, 6, 3, 1]
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.
f = 32 print "The factors of",x,"are:" for i in range(1, f + 1): if f % i == 0: print(i)
Running the above code gives us the following result −
The factors of 32 are: 1 2 4 8 16 32
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.
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))
Running the above code gives us the following result −
38 35 24 24