Hacks of Python | Python tips and tricks

Contents

Overview

  • Python is an excellent language for data science, but not everyone is an expert in Python
  • Here we present 7 Python tricks to help you navigate your Python code in doubly fast time!

Introduction

“Talking is cheap. Show me the code “. – Linus Torvalds

Let's start with a curious question: Have you considered creating machine learning models using just pen and paper? In reality, it's a great way to learn how an algorithm works. But here's the catch: we live in the middle of a data boom. This means that we cannot calculate or design anything manually.

The data we work with in machine learning projects is huge (and I mean HUGE). To build a machine learning model at that scale, we need two resources that work simultaneously: The data scientist and the machine! And that's where the power of Python programming comes to the fore..

Honestly, Python is one of the most versatile languages ​​I have ever worked with. It is easy to learn and implement and, despite this, is highly effective in complex machine learning tasks. And the fact that there are a large number of Python libraries to help us quickly accomplish these tasks, it's just the icing on the cake.

Python_hacks

Although it is essential to have a decent programming knowledge to do data science, no need to have the experience of a python developer. It is essential to focus on what is important: create better machine learning models.

In this post, I have created 7 Python tricks that will definitely save you a lot of time and effort, and they will make it much more effective in their work.

This is the fourth part of my series of tricks, data science tips and tricks. I recommend reading the previous posts to become a more efficient scientist or data analyst:

I have also turned my learning into a free course that you can consult:

At the same time, if you have your own tricks, data science tips and tricks, you can share them with the open community in this GitHub repository: Tricks, data science tips and tricks on GitHub.

We post these tricks daily on social media platforms like LinkedIn, Twitter, Facebook. Be sure to follow #avhackoftheday to get your daily dose of cheats!, freshly crafted data science tips and tricks!

Table of Contents

In this post, we will cover the tricks, Python tips and tricks for data science below:

  1. Python Data Science Hack # 1 – Zen of Python
  2. Python Data Science Hack # 2 – Format your code using Black
  3. Python Data Science Hack # 3 – Debugging Code with Python Debugger
  4. Python Data Science Hack # 4 – Understanding generator lists v / s
  5. Python Data Science Hack # 5 – Pretty print data structures
  6. Python Data Science Hack # 6 – Time tracking for nested loops
  7. Python Data Science Hack # 7 – Lazy Import Data Science Libraries Using Pyforest

Python Data Science Hack # 1 – The Zen of Python

If at any time you were looking for some tips on how to become a better Python developer, this is the place. This trick itself will give you 20 tips at once.

The Zen of Python, written by Tim Peters, provides a beautifully written set of 20 guiding principles for writing computer programs that influence the design of the Python language.

You will notice that of 20 beginning, solo 19 they have been written. Tim left the beginning number 20 for Guido van Rossum (Python language author) will complete it, but left it empty.

This is included as an easter egg in the Python interpreter that you can enter by typing import this.

Note: These are not hard and fast rules, but it will be beneficial for you to make these principles your habit because they are the guiding principles that influence the design of the Python language.

Python Data Science Hack # 2 – Format your code using Black

Imagine living in a magical house that cleans itself automatically when you wake up.. The sheet is folded, the dishes are ready and you can enjoy your morning coffee. Doesn't that sound great?

That's what black can do with your code!! The black calls himself “The uncompromising code formatter” which I think is because it has simplified my life and my colleagues who read my code.

Black is an automatic code formatter for Python, therefore, just write code in your style and then format it black in consistently formatted code. This really helps to focus on content rather than structure.. At the same time, makes code review faster. You can see this fantastic playground that shows the power of black.

Follow these steps to format your code automatically:

  1. Save your Python file with the extension .py.
  2. Go to the terminal and type: “negro [filename.py]”
  3. Congratulations! Your file is now formatted

Python Data Science Hack # 3 – Debugging Code with Python Debugger

We have all experienced times where we are stuck with a fairly persistent error and spend a lot of time on StackOverflow trying to find a solution. The Python debugger will make your life easier in these situations.

pdp is an interactive debugger for Python programs. Whenever you find a bug, you can use pdb to inspect your code line by line. In this example, he used %pdb, which is a magic command in Jupyter notebooks to invoke the standard python debugger.

Data science hack n. ° 4: Python generators or list comprehensions

Python generator produces one item at the same time and generates them only when there is demand. Generators, therefore, they are much more memory efficient. Have you already used it in your daily programming cycle?

If that is not the case, I will try to give you reasons to incorporate it into your practice by means of this code in which I compared the memory usage and time consumption of list comprehension and Python generators.

Data science hack n. ° 5: nice print data structures in python

pprint is a module built into Python. Gives you the ability to print arbitrary data structures in a format “beautiful” which is well formatted and much more readable.

Let's see it in this example where we try to print a nested library. This is quite a useful Python trick to better understand data structures.

Data science hack n. ° 6: time tracking for nested loops

Most of you must have heard of the tqdm Python library and you may even be using it to track the progress of the permanent execution of 'for' loops. Most of the time we write complex functions with loops “for” nested. tqdm makes it possible to track that in addition.

This is how you can trace nested loops using tdqm in Piton:

Python Data Science Hack # 7: lazy import data science libraries using Pyforest

Have you been in a situation where you feel like you are spending too much time thinking about which libraries to import at the beginning of any data science project? It happens to most of us!

I have incredible news for you: PyForest is the answer to all your library import problems!!

PyForest imports all popular data science libraries into your workbench ONLY when you need them. This is known as lazy import in Python. Do not worry, it doesn't matter the whole list of libraries at the same time. It will not import libraries or functions from libraries that you have not imported.

Watch this video to know more about him:

Final notes

In this post, we cover 7 tricks, Python data science tips and tricks to become a better and more efficient data scientist. Hope these tricks help you with your day-to-day niche tasks and save you a lot of time..

Tell me your tricks, data science tips and tricks in the comment section below!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.