Introducing IPython magic commands

Magic commands in IPython denote a set of specific syntactic options that allow for considerably convenient APIs that make working with IPython seamless. Here, we will be going over some of these magic commands to get a feel of the options that IPython offers:

  • object_name?Say you are using an IPython console to execute a sequence of Python commands, and at one point, you'd like to inspect the value of a variable that was created earlier. You can use the variable view right next to the console, but you can also take advantage of the object_name? command in IPython to list detailed information about that variable.

    For example, as illustrated in the following screenshot, number 1 was assigned to the a variable, and when we type a? in the IPython console, we obtain an extensive explanation regarding the data type of a, as shown here:

The object_name? command in IPython
  • %precisionStill on the topic of inspecting the value of your variables, if you are working with numerical data that contains many decimal digits (fractional numbers), then the %precision magic command might come in handy. This command is used to specify how many numbers after the decimal point should be displayed in the IPython console.

For example, as the following screenshot illustrates, when I print out the value for e (a famous constant in math) after specifying that the precision should be 4, the printed output is formatted accordingly, as shown here:

The %precision command in IPython
  • %%timeitAs we saw in Chapter 6, Seamless Testing, Debugging, and Profiling, keeping track of the time it takes for a specific command to run is an essential task in any profiling process. For that reason, IPython also offers a quick magic command to time the execution of any code that's entered in the IPython console—the %%timeit command.
    For example, I used the following code to profile the speed of the sort() function in Python with a completely reversed ordered list of numbers, as shown here:

The %%timeit command in IPython

From the output, we obtain an estimation of the speed (with the mean and standard deviation) from the entered block of code to 1 million iterations of the code. Again, this feature is quite useful in profiling tasks.

Here, we have considered three of the most common magic commands in IPython. Of course, there are many other useful commands that you can take advantage of, which can be found in IPython's official documentation: https://ipython.readthedocs.io/en/stable/index.html.

With that said, the main purpose of IPython is not simply the ability to utilize convenient APIs to facilitate specific tasks such as variable inspection, formatting, or profiling—IPython actually uses those functionalities to power its underlying interactive characteristics. In the context of data science projects, IPython, when used in PyCharm, offers a great way for us to inspect and test small blocks of code before using them in a large program.

With that, let's move on to the next section, where we will consider the other notable support PyCharm offers for scientific computing—Jupyter notebooks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.112