5 easy-peasy hacks to speed up your coding in Python

#5. To delete is human, to restore divine

Adith - The Data Guy
3 min readJul 22, 2022

Tips and Tricks, especially in the programming world, are useful. Sometimes a minor hack can be time and saving. An add-on can sometimes prove to be a Godsend and can be a productivity booster. So, here are some tips and tricks. Some may be known and some may be new but I am sure they would come in handy the next time you work on a project.

So, without any further due let’s get started!!!

1. Finding and Eliminating Errors

The interactive debugger is a magic function. If you get an exception while running the code cell, type %debug in a new line and run it.

This opens an interactive debugging environment that brings you to the position where the exception has occurred. To exit the debugger hit q.

2. Make printing pretty

If you want to produce beautiful representations of your data structures, pprint is the to-go module. It is especially useful when printing dictionaries or JSON data. Let’s have a look at an example that uses both print and pprint to display the output.

3. Printing all the outputs of a single cell

Consider a cell of Jupyter Notebook containing the following lines of code:

In  [1]: 10+5          
11+6Out [1]: 17

It is a normal property of the cell that only the last output gets printed and for the others, we need to add the print() function. Well, it turns out that we can print all the outputs just by adding the following snippet at the top of the notebook.

from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = “all”#Now all the outputs get printed one after the other.In  [1]: 10+5          
11+6
12+7Out [1]: 15
Out [1]: 17
Out [1]: 19

4. Commenting out your code automatically

Ctrl/Cmd + / comments out selected lines in the cell automatically. Hitting the combination again will uncomment the same line of code.

5. To delete is human, to restore divine

Have you ever accidentally deleted a cell in a Jupyter Notebook? If yes then here is a shortcut that can undo that delete action.

  • In case you have deleted the contents of a cell, you can easily recover it by hitting CTRL/CMD+Z
  • If you need to recover an entire deleted cell hit ESC+Z or EDIT > Undo Delete Cells

Conclusion

In this article, I’ve listed the main tips I have gathered while working with Python and Jupyter Notebooks. I am sure these simple hacks will be of use to you and you will take back something from this article.

Don’t forget to leave your responses.✌

Everyone stay tuned!! To get my stories in your mailbox kindly subscribe to my newsletter.

Thank you for reading! Do not forget to give your claps and to share your responses and share with a friend!

--

--

Adith - The Data Guy

Passionate about sharing knowledge through blogs. Turning data into narratives. Data enthusiast. Content Curator with AI. https://www.linkedin.com/in/asr373/