Better Programming

Advice for programmers.

Follow publication

Member-only story

7 Commands in Python to Make Your Life Easier

Chaitanya Baweja
Better Programming
Published in
6 min readApr 30, 2020

Photo by Greg Rakozy on Unsplash

When Python first appeared in 1991, it was seen more as an “at your own risk” computing language. Today it is the predominant language for data science, machine learning, and software development.

A key reason behind Python’s popularity is its flexibility when it comes to adding new features and technologies like magic commands.

So, what exactly is a magic command in Python?

Magic commands are enhancements or shortcuts over the usual Python syntax designed to facilitate routine tasks.

These special commands enable us to easily control the behavior of the IPython system and solve various common problems in standard data analysis, for example, running an external script or calculating the execution time of a piece of code.

In this tutorial, I will teach you some very useful spells. These will help you to become better programming wizards.

All these commands were tested beforehand on a Jupyter Notebook running Python version 3.7.4. These commands should run perfectly in any IPython shell on your IDE, like Spyder, Pycharm, etc.

Magic commands come in two different forms:

  • Line magic — is denoted by a single % prefix and operates on a single line of input
  • Cell magic — is denoted by a double %% prefix and operates on the entire cell or multiple lines of input.

Let’s look at some of the most popular magic commands.

Run an External Script

Suppose you are working on a new project on Jupyter, and you wish to use a script that you wrote earlier. Rather than copying the entire code into a Jupyter cell, you can use some Python magic.

Any script can be run inside the environment of our IPython session using the %run command.

Suppose I have a file called test_script.py with the following code:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Chaitanya Baweja
Chaitanya Baweja

Written by Chaitanya Baweja

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

Responses (5)

Write a response