Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Python interpreter and IPython

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand Python interpreter and IPython". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python interpreter

Python has its own interpreter. We can enter the interpreter environment of python by typing python on the command line:

PythonPython 2.7.15 (default, Oct 2 2018, 11:47:18) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.2)] on darwinType "help", "copyright", "credits" or "license" for more information. > > site = "www.flydean.com" > site'www.flydean.com' >

The prompt for the python interpreter is >.

Python provides a very useful command, help, and we can use help to see which commands to use.

HelpType help () for interactive help, or help (object) for help about object.

In Python3, the completion function of tab is also provided:

> site'www.flydean.com' > site.site.capitalize (site.expandtabs (site.isalpha) (site.isprintable (site.lower (site.rindex) (site.upper (site.casefold (site.find) (site.isspace (site.lstrip) (site.startswith (site.zfill (site.center)) (site.format (site.isdigit (site.maketrans (site.maketrans) (site.strip (site.count (site.format_map (site.isidentifier) (site.partition (site.rsplit (site.swapcase) (site.index (site.islower (site.join)) Site.replace (site.rstrip (site.title (site.endswith) (site.isnumeric (site.ljust) (site.split (site.translate))

It is very convenient to use.

In addition to the interpreter that comes with Python, there is a more powerful interpreter called IPython. Let's take a look.

IPython

IPython is a very powerful interpreter and is usually used with jupyter notebook. In IPython3.X, IPython and Jupyter are released together as a whole. But after IPython4.X, Jupyter has been separated from IPython as a separate project.

Using IPython is easy, just enter the IPython command:

$> ipythonPython 3.6.4 | Anaconda, Inc. | (default, Jan 16 2018, 12:04:33) Type 'copyright',' credits' or 'license' for more informationIPython 6.2.1-- An enhanced Interactive Python. Type'?' For help.In [1]: site= "www.flydean.com" In [2]: siteOut [2]: 'www.flydean.com'

The prompt for IPython is In [1]:

Basically, the commands that come with Python can be used in IPython.

IPython provides four very useful commands:

Commanddescription?Introduction and overview of IPython's features.%quickrefQuick reference.helpPython's own help system.object?Details about 'object', use' object??' For extra details. Magic function

There are two magic functions in IPython, one is Line magics and the other is Cell magics.

Line magics receives input from this line as input to the function, starting with%. On the other hand, Cell magics can receive multiple lines of data until you enter blank enter. It starts with%%.

For example, if we want to see the use of a magic function of timeit, we can use Object? To express:

$> In [4]:% timeit?Docstring:Time execution of a Python statement or expressionUsage, in line mode:% timeit [- n-r [- t |-c]-Q-p

-o] statementor in cell mode:% timeit [- n-r [- t |-c]-Q-p

-o] setup_code code code...

Timeit is used to count the execution time of the program. Let's take a look at the use of Line magics and Cell magics respectively:

In [4]:% timeit?In [5]:% timeit range (1000) 199 ns ±3.8ns per loop (mean ±std. Dev. Of 7 runs, 1000000 loops each) In [6]:% timeit range (1000)...: range (1000)...: 208ns ±12.1ns per loop (mean ±std. Dev. Of 7 runs, 1000000 loops each)

In fact, if it's just LIne magics, we can omit the previous%, but for Cell magics, it can't be omitted.

In [7]: timeit range (1000) 200ns ±4.03ns per loop (mean ±std. Dev. Of 7 runs, 10000000 loops each)

Common magic functions are as follows:

Code related:% run,% edit,% save,% macro,% recall, etc.

Shell environment related:% colors,% xmode,% automagic, etc.

Other functions:% reset,% timeit,% writefile,% load, or% paste.

Run and edit

Using% run, you can easily run external python scripts.

In [8]: run?Docstring:Run the named file inside IPython as a program.Usage::% run [- n-I-e-G] [(- t [- N] |-d [- b] |-p [profile options])] (- m mod | file) [args]

Run has several very useful parameters, such as-t, which can be used to count the time of the program. -d can debug the environment, and-p can do profiler analysis.

Use% edit to edit multiple lines of code, and after exiting, IPython will execute them.

If you don't want to do it immediately, you can add the-x parameter.

Debug

You can use% debug or% pdb to enter the debug environment of IPython:

In [11]: debug > / Users/flydean/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/IPython/core/compilerop.py (99) ast_parse () 97 Arguments are exactly the same as ast.parse (in the standard library), 98 and are passed to the built-in compile function. ""-- > 99 return compile (source, filename, symbol, self.flags | PyCF_ONLY_AST 1) 100 101 def reset_compiler_flags (self): ipdb > In [12]: pdbAutomatic pdb calling has been turned ONIn [13]: pdbAutomatic pdb calling has been turned OFF

Or you can use% run-d theprogram.py to debug an external program.

History

IPython can store your input data and program output data, one of the very important functions of IPython is to obtain historical data.

In an interactive environment, a simple way to enter commands through history is to use the up- and down- arrows.

More powerful, IPython stores all inputs and outputs in two variables, In and Out, such as In [4].

In [1]: site = "www.flydean.com" In [2]: siteOut [2]: 'www.flydean.com'In [3]: InOut [3]: [', 'site = "www.flydean.com"', 'site',' In']

You can use _ ih [n] to access a specific input:

In [4]: _ ih [2] Out [4]: 'site'

_ I, _ ii, _ iii can represent the previous, the previous and the previous input, respectively.

In addition, the global variable _ I can also be used to access input, that is to say:

_ I = = _ ih [] = = In [] _ i14 = = _ ih [14] = = In [14]

Similarly, there are three ways to access the output:

_ = _ oh [] = = Out [] _ 12 = = Out [12] = _ oh [12]

The last three outputs can also be obtained through _, _ _ and _ _.

You can also use% history to list previous historical data for selection.

History can be used with% edit,% rerun,% recall,% macro,% save and% pastebin:

By passing in a number, you can select the historical input line number.

% pastebin 3 18-20

The above example selects input on lines 3 and 18-20.

Run system commands

Use! You can run system commands directly:

In [27]:! pwd/Users/flydean/Downloads

You can also use variables to receive the results of the run, such as files =! ls

That's all for "how to understand the Python interpreter and IPython". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report