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.
SSPrinting the same character multiple times in python.
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,
Example
print("I love program at tutorials point "+"TP"*4);
Output
I love program at tutorials point TPTPTPTP
Printing elements of a list in different ways
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.
Example
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)))
Output
Bikes are : ['thunderbird', 'Pulsar', 'R15', 'Duke'] Bikes are : thunderbird,Pulsar,R15,Duke Bikes are : thunderbird and Pulsar and R15 and Duke
Printing multiple lists simultaneously
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.
Example
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke'] speed = ['142' , '135' , '137' , '145'] for bike, maxspeed in zip(bikes , speed): print(bike, maxspeed)
Output
thunderbird 142 Pulsar 135 R15 137 Duke 145
ShortHand trick to swapping values
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 −
Example
value1 = 325 value2 = 976 print("value1 = ",value1) print("value2 = ",value2) value1,value2 = value2,value1 print("\nSwapped values") print("value1 = ",value1) print("values = ",value2)
Output
value1 = 325 value2 = 976 Swapped values value1 = 976 values = 325
Reversing a string in python
Python provides a shorthand trick to reverse a string. Let’s see an example of how to reverse a string in python −
Example
value1 = 'Hello! Welcome to tutorials point' print(value1[::-1]) number = 934827165303 print(int(str(number)[::-1]))
Output
tniop slairotut ot emocleW !olleH 303561728439
Returning multiple values by a function in python
In python, the function can return multiple values in python i.e. you can return multiple values instead of single.
Example
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)
Output
3 6 9 12 15
Printing index along with values in a for-in loop
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 −
Example
bikes = ['thunderbird' , 'Pulsar' , 'R15' , 'Duke'] for i, bike in enumerate(bikes) : print(i, bike)
Output
0 thunderbird 1 Pulsar 2 R15 3 Duke
Slice operation in python
The slice operation in python is for getting items from a list. Let’s look at an example of how to slice operation works −
Example
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
Output
['thunderbird', 'Pulsar', 'R15'] ['thunderbird', 'R15', 'S1000RR'] ['S1000RR', 'Duke', 'R15', 'Pulsar', 'thunderbird'] ['S1000RR', 'R15', 'thunderbird']
Convert a string to list in python
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 −
Example
name = "3 34 67 12 78" converted_list = list(map(int, name.split())) print(converted_list)
Output
[3, 34, 67, 12, 78]
Converting a list of the list into a single list
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 −
Example
import itertools dob = [ [3 , 30], [6 , 12] , [8 , 17] ] print(dob) dates = list(itertools.chain.from_iterable(dob)) print(dates)
Output
[[3, 30], [6, 12], [8, 17]] [3, 30, 6, 12, 8, 17]