In[]: (reverse-1-search)'sqa':square?? At any point,you can add more characters to refine the search,or press Ctrl-r again to search further for another command that matches the query.If you followed along in the previous section,pressing Ctrl-r twice more gives: In[]: return a**2 Once you have found the command you're looking for,press Return and the search will end.We can then use the retrieved command,and carry on with our session return a **2 square of a. In [2]:square(2) 0ut[2]:4 Note that you can also use Ctrl-p/Ctrl-n or the up/down arrow keys to search through history,but only by matching characters at the beginning of the line.That is, if you type def and then press Cp.it would find the most recent cmmand (if any) in your history that begins with the characters def Miscellaneous Shortcuts Finally,there are a few miscellaneous shortcuts that don't fit into any of the preceding categories,but are nevertheless useful to know: Keystroke Action (trl Clear terminal screen Ctrl-c Ctrl-d Exit IPython session The Ctrl-c shortcut in particular can be useful when you inadvertently start a very long-running job While some of the shortcuts discussed here may seem a bit tedious at first,they quickly become automatic with practice.Once you develop that muscle memory,I suspect you will even find yourself wishing they were available in other contexts. IPython Magic Commands The previous two sections showed how IPython lets you use and explore Python eff- ciently and interactively.Here well begin discussing some of the enhancements tha 10 Chapter 1:IPython:Beyond Normal Python
In [1]: (reverse-i-search)`sqa': square?? At any point, you can add more characters to refine the search, or press Ctrl-r again to search further for another command that matches the query. If you followed along in the previous section, pressing Ctrl-r twice more gives: In [1]: (reverse-i-search)`sqa': def square(a): """Return the square of a""" return a ** 2 Once you have found the command you’re looking for, press Return and the search will end. We can then use the retrieved command, and carry on with our session: In [1]: def square(a): """Return the square of a""" return a ** 2 In [2]: square(2) Out[2]: 4 Note that you can also use Ctrl-p/Ctrl-n or the up/down arrow keys to search through history, but only by matching characters at the beginning of the line. That is, if you type def and then press Ctrl-p, it would find the most recent command (if any) in your history that begins with the characters def. Miscellaneous Shortcuts Finally, there are a few miscellaneous shortcuts that don’t fit into any of the preceding categories, but are nevertheless useful to know: Keystroke Action Ctrl-l Clear terminal screen Ctrl-c Interrupt current Python command Ctrl-d Exit IPython session The Ctrl-c shortcut in particular can be useful when you inadvertently start a very long-running job. While some of the shortcuts discussed here may seem a bit tedious at first, they quickly become automatic with practice. Once you develop that muscle memory, I suspect you will even find yourself wishing they were available in other contexts. IPython Magic Commands The previous two sections showed how IPython lets you use and explore Python effi‐ ciently and interactively. Here we’ll begin discussing some of the enhancements that 10 | Chapter 1: IPython: Beyond Normal Python
IPython adds on top of the normal Python syntax.These are known in IPython as magic commands,and are prefixed by the character.These magic commands are designed to succinctly solve various common problems in standard data analysis Magic commands come in two flavors:line magics,which are denoted by a single prefix and operate on a single line of input,and cell magics,which are denoted by a doubleprefix and operaten multipe input.Well demonstrate and di cuss a few brief examples here,and come back to more focused discussion of several useful magic commands later in the chapter. Pasting Code Blocks:%paste and %cpaste When you're working in the IPython interpreter,one common gotcha is that pasting nexpected errors,especially when indentation and nd som example code on a website and want to paste it into your interpreter.Consider the following simple function: >>>def donothing(x): return x The code is formatted as it would appear in the Python interpreter,and if you copy and paste this directly into IPython you get an error: File "<ipython-input-20-5a66c8964687>",line 2 return x SyntaxError:invalid syntax In the direct paste,the interpreter is confused by the additional prompt characters. But never fear- Python's %paste magic function is designed to handle this exact type of multiline,marked-up input: In [3]:%paste >>def donothing(x): return x ##--End pasted text The xpaste command both enters and executes the code,so now the function is ready to be used 6n周:othtng(0 A command with a similar intent is cpaste,which opens up an interactive multiline prompt in which you can paste one or more chunks of code to be executed in a batch: IPython Magic Commands 11
IPython adds on top of the normal Python syntax. These are known in IPython as magic commands, and are prefixed by the % character. These magic commands are designed to succinctly solve various common problems in standard data analysis. Magic commands come in two flavors: line magics, which are denoted by a single % prefix and operate on a single line of input, and cell magics, which are denoted by a double %% prefix and operate on multiple lines of input. We’ll demonstrate and dis‐ cuss a few brief examples here, and come back to more focused discussion of several useful magic commands later in the chapter. Pasting Code Blocks: %paste and %cpaste When you’re working in the IPython interpreter, one common gotcha is that pasting multiline code blocks can lead to unexpected errors, especially when indentation and interpreter markers are involved. A common case is that you find some example code on a website and want to paste it into your interpreter. Consider the following simple function: >>> def donothing(x): ... return x The code is formatted as it would appear in the Python interpreter, and if you copy and paste this directly into IPython you get an error: In [2]: >>> def donothing(x): ...: ... return x ...: File "<ipython-input-20-5a66c8964687>", line 2 ... return x ^ SyntaxError: invalid syntax In the direct paste, the interpreter is confused by the additional prompt characters. But never fear—IPython’s %paste magic function is designed to handle this exact type of multiline, marked-up input: In [3]: %paste >>> def donothing(x): ... return x ## -- End pasted text -- The %paste command both enters and executes the code, so now the function is ready to be used: In [4]: donothing(10) Out[4]: 10 A command with a similar intent is %cpaste, which opens up an interactive multiline prompt in which you can paste one or more chunks of code to be executed in a batch: IPython Magic Commands | 11
In [5]:%cpaste Pasting code;enter-alone on the line to stop or use Ctrl-D. :>>def donothing(x): return x These magic commands,like others we'll see,make available functionality that would be difficult or impossible in a standard Python interpreter. Running External Code:%run As you begin developing more extensive code,you will likely find yourself working in both IPython for interactive exploration,as well as a text editor to store code that you want to reuse.Rather than running this code in a new window,it can be convenient to run it within your IPython session.This can be done with the %run magic. For example,imagine you've created a myscript.py file with the following contents: #file:myscript.py defsqure(x): for N in range(1,4): print(N,"squared is",square(N)) You can execute this from your IPython session as follows: In [6]:%run myscript.py squared Note also that after you've run this script,any functions defined within it are available for use in your IPython session: o() There are several options to fine-tune how your code is run;you can see the docu mentation in the normal way,by typing Xrun?in the IPython interpreter. Timing Code Execution:%timeit Another example of a useful magic function is %timeit,which will automatically determine the execution time of the single-line Python statement that follows it.For example,we may want to check the performance of a list comprehension: 12Chapter 1:IPython:Beyond Normal Python
In [5]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. :>>> def donothing(x): :... return x :-- These magic commands, like others we’ll see, make available functionality that would be difficult or impossible in a standard Python interpreter. Running External Code: %run As you begin developing more extensive code, you will likely find yourself working in both IPython for interactive exploration, as well as a text editor to store code that you want to reuse. Rather than running this code in a new window, it can be convenient to run it within your IPython session. This can be done with the %run magic. For example, imagine you’ve created a myscript.py file with the following contents: #------------------------------------- # file: myscript.py def square(x): """square a number""" return x ** 2 for N in range(1, 4): print(N, "squared is", square(N)) You can execute this from your IPython session as follows: In [6]: %run myscript.py 1 squared is 1 2 squared is 4 3 squared is 9 Note also that after you’ve run this script, any functions defined within it are available for use in your IPython session: In [7]: square(5) Out[7]: 25 There are several options to fine-tune how your code is run; you can see the docu‐ mentation in the normal way, by typing %run? in the IPython interpreter. Timing Code Execution: %timeit Another example of a useful magic function is %timeit, which will automatically determine the execution time of the single-line Python statement that follows it. For example, we may want to check the performance of a list comprehension: In [8]: %timeit L = [n ** 2 for n in range(1000)] 1000 loops, best of 3: 325 µs per loop 12 | Chapter 1: IPython: Beyond Normal Python
The benefit of xtimeit is that for short commands it will automatically perform mul- tiple to attain more robust resultsFor multiline stater ents,adding second%sign will turn this into a cell magic that can handle multiple lines of input For example,here's the equivalent construction with a for loop: In [9]:%xtimeit :L=0 ...for n in range(1000): L.append(n *2) 1000 Loops,best of 3:373 us per Loop We can immediately see that list comprehensions are about 10%faster than the equivalent for loop construction in this case.We'll explore %timeit and other approaches to timing and profiling code in"Profiling and Timing Code"on page 25. Help on Magic Functions:?%magic,and %lsmagic Like normal Python functions,IPython magic functions have docstrings,and this useful documentation can be accessed in the standard manner.So,for example,to read the documentation of thextimeit magic,simply type this In [10]:%timeit? Documentation for other functions can be accessed similarly.To access a general description of available magic functions,including some examples,you can type this: In [1i]:%magic For a quick and simple list of all available magic functions,type this: In [12]:smagic Finally I'll mention that it isquite straightforward to define your own magic func tions if you wish.We won't discuss it here,but if you are interested,see the reference listed in"More IPython Resources"on page 30. Input and Output History Previously we saw that the IPython shell allows you to access previous command with the up and down arrow keys,or equivalently the Ctrl-p/Ctrl-n shortcuts.Addi- tionally,in both the shell and the notebook,IPython exposes several ways to obtain ut of prevno ands,as well as string versions of the comm ands them- IPython's In and Out Objects By now I imagine you're quite familiar with the In[1]:/0ut[1]:style prompts used by IPython.But it turns out that these are not just pretty decoration:they give a clue Input and Output History 13
The benefit of %timeit is that for short commands it will automatically perform mul‐ tiple runs in order to attain more robust results. For multiline statements, adding a second % sign will turn this into a cell magic that can handle multiple lines of input. For example, here’s the equivalent construction with a for loop: In [9]: %%timeit ...: L = [] ...: for n in range(1000): ...: L.append(n ** 2) ...: 1000 loops, best of 3: 373 µs per loop We can immediately see that list comprehensions are about 10% faster than the equivalent for loop construction in this case. We’ll explore %timeit and other approaches to timing and profiling code in “Profiling and Timing Code” on page 25. Help on Magic Functions: ?, %magic, and %lsmagic Like normal Python functions, IPython magic functions have docstrings, and this useful documentation can be accessed in the standard manner. So, for example, to read the documentation of the %timeit magic, simply type this: In [10]: %timeit? Documentation for other functions can be accessed similarly. To access a general description of available magic functions, including some examples, you can type this: In [11]: %magic For a quick and simple list of all available magic functions, type this: In [12]: %lsmagic Finally, I’ll mention that it is quite straightforward to define your own magic func‐ tions if you wish. We won’t discuss it here, but if you are interested, see the references listed in “More IPython Resources” on page 30. Input and Output History Previously we saw that the IPython shell allows you to access previous commands with the up and down arrow keys, or equivalently the Ctrl-p/Ctrl-n shortcuts. Addi‐ tionally, in both the shell and the notebook, IPython exposes several ways to obtain the output of previous commands, as well as string versions of the commands them‐ selves. We’ll explore those here. IPython’s In and Out Objects By now I imagine you’re quite familiar with the In[1]:/Out[1]: style prompts used by IPython. But it turns out that these are not just pretty decoration: they give a clue Input and Output History | 13
as to how you can acce ess prev ious inputs and outputs in your current session.Imag- ine you start a session that looks like this: In [1]:import nath In [3]:math.cos(2) 0ut[3]:-0.4161468365471424 We've imported the built-in math package,then computed the sine and the cosine of the number 2.These inputs and outputs are displayed in the shell with In/out labels, but there's more-IPython actually creates some Python variables called In and out that are automatically updated to reflect this history: pint3a. 'import math','math.sin(2)','math.cos(2)','print(In)'] 6周9.092376256,上B.A616s6n2间 The In object is a list,which keeps track of the commands in order(the first item in the list is a placeholder so that In[]can refer to the first command) In [6]:print(In[1]) import math The Out object is not a list but a dictionary mapping input numbers to their outputs (if any): In [7]:print(Out[2]) 0.9092974268256817 Note that not all operations have outputs:for example,import statements and print statements don't affect the output.The latter may be surprising,but makes sense if you consider that print is a function that returns None;for brevity,any command that returns None is not added to Out. Where this can be useful is if you want to interact with past results.For example,let's check the sum of sin(2)**2 and cos(2)**2 using the previously computed results: a8:82+o]“2 The result is 1.0 as wed expect from the well-known trigonometric identity.In this case,using these previous results probably is not necessary,but it can become very handy if you expensive computation and want to reuse the resut! 14Chapter 1:IPython:Beyond Normal Python
as to how you can access previous inputs and outputs in your current session. Imag‐ ine you start a session that looks like this: In [1]: import math In [2]: math.sin(2) Out[2]: 0.9092974268256817 In [3]: math.cos(2) Out[3]: -0.4161468365471424 We’ve imported the built-in math package, then computed the sine and the cosine of the number 2. These inputs and outputs are displayed in the shell with In/Out labels, but there’s more—IPython actually creates some Python variables called In and Out that are automatically updated to reflect this history: In [4]: print(In) ['', 'import math', 'math.sin(2)', 'math.cos(2)', 'print(In)'] In [5]: Out Out[5]: {2: 0.9092974268256817, 3: -0.4161468365471424} The In object is a list, which keeps track of the commands in order (the first item in the list is a placeholder so that In[1] can refer to the first command): In [6]: print(In[1]) import math The Out object is not a list but a dictionary mapping input numbers to their outputs (if any): In [7]: print(Out[2]) 0.9092974268256817 Note that not all operations have outputs: for example, import statements and print statements don’t affect the output. The latter may be surprising, but makes sense if you consider that print is a function that returns None; for brevity, any command that returns None is not added to Out. Where this can be useful is if you want to interact with past results. For example, let’s check the sum of sin(2) ** 2 and cos(2) ** 2 using the previously computed results: In [8]: Out[2] ** 2 + Out[3] ** 2 Out[8]: 1.0 The result is 1.0 as we’d expect from the well-known trigonometric identity. In this case, using these previous results probably is not necessary, but it can become very handy if you execute a very expensive computation and want to reuse the result! 14 | Chapter 1: IPython: Beyond Normal Python