+ INFORMATION

Share on social networks!

Python Tricks You Should Be Using

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.

reverse a list python tricks

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.

reverse a list python tricks 1
print elements python tricks

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 generators python tricks

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.

zip function python tricks

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.

zip python tricks

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.

matrix transposition python tricks

Subscribe to our newsletter to stay up to date with all the news

EIP International Business School informs you that the data in this form will be processed by Mainjobs Internacional Educativa y Tecnológica, SA as the person responsible for this website. The purpose of collecting and processing personal data is to respond to the query made as well as to send information about the services of the data controller. Legitimation is the consent of the interested party.
You can exercise your rights of access, rectification, limitation and deletion of data in compliance@grupomainjobs.com as well as the right to file a claim with the supervisory authority. You can consult additional and detailed information on Data Protection in the Privacy Policy that you will find in our Web page
marter-in-python

Leave a comment