Python is an amazing programming language that can do many interesting things due to its huge set of libraries. Here are some common hacks and things that will be helpful to you while programming.
Printing repeated character by typing the same character set as many times as we want or looping through if the values are large is commonly used in other programming languages. But python has something else in its trunk to ease this printing of recursive characters.
The below code is used to print recursive characters in python,
print("I love program at tutorials point "+"TP"*4);
I love program at tutorials point TPTPTPTP
List is just like a nonhomogeneous array. And for printing the elements of a list in python there are multiple methods defined. By default, in printing the list in python adds up the square bracket and single quotes. But in python, you have the option to print the list in a more effective manner. This work is done in python using the join() method.
The join methods convert the list into a string. Each element of the list is connected with the string that called the join. Let’s see how this works.
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke'] # traditional method of printing the list print("Bikes are :", bikes) # printing list using join method print("Bikes are : %s" %','.join(bikes)) print('Bikes are : ',(" and ".join(bikes)))
Bikes are : ['thunderbird', 'Pulsar', 'R15', 'Duke'] Bikes are : thunderbird,Pulsar,R15,Duke Bikes are : thunderbird and Pulsar and R15 and Duke
Python provides a method to print elements of more than one list simultaneously in pair form. There's a method called zip that merges two equal length lists into pairs.
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke'] speed = ['142' , '135' , '137' , '145'] for bike, maxspeed in zip(bikes , speed): print(bike, maxspeed)
thunderbird 142 Pulsar 135 R15 137 Duke 145
Python programming language supports an inbuilt shorthand trick to swap two values. This trick provides an easy way to swap values without using any extra variable. Let's see a program that shows how this works −
value1 = 325 value2 = 976 print("value1 = ",value1) print("value2 = ",value2) value1,value2 = value2,value1 print("\nSwapped values") print("value1 = ",value1) print("values = ",value2)
value1 = 325 value2 = 976 Swapped values value1 = 976 values = 325
Python provides a shorthand trick to reverse a string. Let’s see an example of how to reverse a string in python −
value1 = 'Hello! Welcome to tutorials point' print(value1[::-1]) number = 934827165303 print(int(str(number)[::-1]))
tniop slairotut ot emocleW !olleH 303561728439
In python, the function can return multiple values in python i.e. you can return multiple values instead of single.
def multiple() : return 1*3 , 2*3 , 3*3 , 4*3 , 5*3 val1, val2, val3, val4, val5 = multiple() print(val1, val2, val3, val4, val5)
3 6 9 12 15
In python, looping over values using a for-in loop yields values only. But if we want to access the index too we need to use an enumerate which will return index with the value.
Let’s see an example of how it works −
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke'] for i, bike in enumerate(bikes) : print(i, bike)
0 thunderbird 1 Pulsar 2 R15 3 Duke
The slice operation in python is for getting items from a list. Let’s look at an example of how to slice operation works −
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke', 'S1000RR'] print(bikes[0:3]) #print first 3 elements print(bikes[::2]) #print alternate elements print(bikes[::-1]) #prints reversed list print(bikes[::-2]) #prints reversed list with alternate elements
['thunderbird', 'Pulsar', 'R15'] ['thunderbird', 'R15', 'S1000RR'] ['S1000RR', 'Duke', 'R15', 'Pulsar', 'thunderbird'] ['S1000RR', 'R15', 'thunderbird']
Sometimes there is an urge to convert the inputted string into other types. So, here is a method in python that is used to convert string to list in python. Let's see how its done −
name = "3 34 67 12 78" converted_list = list(map(int, name.split())) print(converted_list)
[3, 34, 67, 12, 78]
In python, the multidimensional list can be converted to a one-dimensional list. The method chain.from_iterable() is used for this task. As it returns elements from the list of the list until the last element is encountered. Let’s see an example of how it works −
import itertools dob = [ [3 , 30], [6 , 12] , [8 , 17] ] print(dob) dates = list(itertools.chain.from_iterable(dob)) print(dates)
[[3, 30], [6, 12], [8, 17]] [3, 30, 6, 12, 8, 17]