Last week we saw a “chop” with the NumPy/SciPy and Pandas methods most used, so this week we thought we would continue along the same lines and provide you with some Python programming tricks that will surely make your life as a programmer easier.
If you want to train as a programmer and be a highly qualified expert with the necessary skills to carry out programming work in Python specialized, he Master in Advanced Programming in Python for Big Data, Hacking and Machine Learning will make you get it.
Reverse a list
We can simply reverse a given list using a reverse[] function. Serves for numeric and string data types present in the list.
Print list items in any order
If we need to print the values of a list in different orders, we can assign the list to a series of variables and programmatically decide the order in which we want to print the list.
Using generators inside functions
We can use generators directly inside a function to write shorter and cleaner code. In the following example, we find the sum by using a generator directly as an argument to the sum function.
Using the zip[] function
When we need to join many iterator objects like list to get a single list, we can use zip function. The result shows that each element will be grouped with its respective elements from the other lists.
Exchange of two numbers
Exchanging numbers generally requires storing values in temporary variables. But with this Python trick we can do it using one line of code and without using any temporary variables.
Transposition of a matrix
Transposing a matrix involves converting columns into rows. In Python we can achieve this by designing some loop structure to iterate through the elements in the array and change their places or we can use the following script which involves the zip function along with the * operator to unzip a list which becomes a transpose of matrix.