<jupyter_start><jupyter_text>*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by <NAME>; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).**The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*   IPython Magic Commands The previous two sections showed how IPython lets you use and explore Python efficiently and interactively.Here we'll begin discussing some of the enhancements that 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 discuss 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 working in the IPython interpreter, one common gotcha is that pasting multi-line 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:``` python>>> 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:```ipythonIn [2]: >>> def donothing(x):   ...:     ...     return x   ...:       File "", 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 multi-line, marked-up input:```ipythonIn [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:```ipythonIn [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:```ipythonIn [5]: %cpastePasting 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:```python------------------------------------- file: myscript.pydef square(x):    """square a number"""    return x ** 2for N in range(1, 4):    print(N, "squared is", square(N))```You can execute this from your IPython session as follows:```ipythonIn [6]: %run myscript.py1 squared is 12 squared is 43 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:```ipythonIn [7]: square(5)Out[7]: 25```There are several options to fine-tune how your code is run; you can see the documentation in the normal way, by typing **``%run?``** in the IPython interpreter.<jupyter_code># -------------------------------------
# file: myscript.py
 
def square(x):
    """square a number"""
    return x ** 2
 
for N in range(1, 4):
    print(N, "squared is", square(N))<jupyter_output>1 squared is 1
2 squared is 4
3 squared is 9
{'fraction_non_alphanumeric': 0.1105550500454959, 'fraction_numerical': 0.014103730664240218, 'mean_word_length': 4.142690058479532, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '293692', 'license': 'MIT', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1106, 'n_words': 591, 'path': 'Copy_of_01_03_Magic_Commands.ipynb', 'repo_name': 'BioCogito/sagemakertest', 'size': '4405'}