Python Learning Record _ IPython basics, automatic Tab completion, inner province, % run Command _, _ ipython % run
This is the first time I wrote a blog, and I had a lot of ideas before. I wanted to store what I was in contact with in the form of text, and I never acted in time. This time I made up my mind to share with you the records I 've learned and the problems I 've encountered. At the same time, I am a memo. Thank you for visiting my blog. Thank you for your advice. 12:34:31
Learn the bibliography "using Python for data analysis"
IPython is an interactive computing and development environment.
Install sudo apt-get install ipython in linux
IPython Basics
~$ ipythonasPython 2.7.12 (default, Dec 4 2017, 14:50:18) Type "copyright", "credits" or "license" for more information.IPython 2.4.1 -- An enhanced Interactive Python.? -> Introduction and overview of IPython's features.%quickref -> Quick reference.help -> Python's own help system.object? -> Details about 'object', use 'object??' for extra details.In [1]: from numpy.random import randnIn [2]: import numpy as npIn [3]: data = {i :randn() for i in range(7)}In [4]: dataOut[4]: {0: -0.32421485650349186, 1: 0.752834410829907, 2: 1.0926380203566248, 3: -0.6641241145638012, 4: 1.0580982848912308, 5: -1.3974082670817336, 6: 0.08712617271950605}In [5]: print data{0: -0.32421485650349186, 1: 0.752834410829907, 2: 1.0926380203566248, 3: -0.6641241145638012, 4: 1.0580982848912308, 5: -1.3974082670817336, 6: 0.08712617271950605}
"""
Numpy has some common functions used to generate random numbers. randn () and rand () belong to them.
Numpy. random. randn (d0, d1 ,..., Is to return one or more sample values from the standard normal distribution.
Numpy. random. rand (d0, d1 ,..., The random sample of dn) is located in [0, 1.
"""
The Tab key is automatically completed.
"""
The Automatic completion of the Tab key is one of the major improvements to the standard Python shell.
When you enter an expression in shell, you only need to press the Tab key to find any variable that matches the input string in the current namespace.
"""
In [1]: an_apple = 7In [2]: an_exampel = 42In [3]: anan_apple an_exampel and any In [3]: b = [1, 2 ,3]In [4]: b.<Tab>b.append b.extend b.insert b.remove b.sort b.count b.index b.pop b.reverse In [4]: import datetimeIn [5]: datetime.<Tab>datetime.MAXYEAR datetime.datetime datetime.timedeltadatetime.MINYEAR datetime.datetime_CAPI datetime.tzinfodatetime.date datetime.time
# <Tab> only one button
"IPython hides the methods and attributes starting with an underscore by default. To access the interface, add _ (underline) directly """
In [6]: datetime._datetime.__class__ datetime.__new__datetime.__delattr__ datetime.__package__datetime.__dict__ datetime.__reduce__datetime.__doc__ datetime.__reduce_ex__datetime.__format__ datetime.__repr__datetime.__getattribute__ datetime.__setattr__datetime.__hash__ datetime.__sizeof__datetime.__init__ datetime.__str__datetime.__name__ datetime.__subclasshook__
"""
The Automatic completion of the Tab key can be used not only to search for namespaces and automatically complete objects or template attributes, but also to press the Tab key when you enter something similar to a path, you can find the corresponding content in the system file.
"""
In [6]: /home//home/iostream/ /home/lost+found/
Introspection
In [1]: b = {1, 2 ,3}In [2]: b?Type: setString form: set([1, 2, 3])Length: 3Docstring:set() -> new empty set objectset(iterable) -> new set objectBuild an unordered collection of unique elements.In [3]: b = [1, 2, 3]In [4]: b?Type: listString form: [1, 2, 3]Length: 3Docstring:list() -> new empty listlist(iterable) -> new list initialized from iterable's items
"""
Add a question mark (?) before or after the variable (?) You can display some general information about the object, called the inner of the object.
"""
In [1]: def add_numbers(a, b): ...: """ ...: Add two numbers together ...: ...: Returns ...: ------- ...: the_sum : type of arguments ...: """ ...: return a + b ...:
"""
Use? Show docstring
"""
In [2]: add_numbers?Type: functionString form: <function add_numbers at 0x7fb9308e7aa0>File: /home/iostream/<ipython-input-1-5628bd19e409>Definition: add_numbers(a, b)Docstring:Add two numbers togetherReturns-------the_sum : type of arguments
"""
Use ?? Displays the source code of the function.
"""
In [3]: add_numbers??Type: functionString form: <function add_numbers at 0x7fb9308e7aa0>File: /home/iostream/<ipython-input-1-5628bd19e409>Definition: add_numbers(a, b)Source:def add_numbers(a, b): """ Add two numbers together Returns ------- the_sum : type of arguments """ return a + b
"""
? Another method is to search for the IPython namespace.
Some characters are matched with the wildcard (*) to display all the names that match the wildcard expression.
All functions with "load" contained in the NumPy top-level namespace
"""
In [5]: import numpy as npIn [6]: np.*load*?np.loadnp.loadsnp.loadtxtnp.pkgload
% Run Command
"""
In the IPython session environment, all files can run as Python using the % run Command.
Assume that the script is stored in ipythontest. py.
"""
In [11]: cOut[11]: 7.5In [12]: resultOut[12]: 1.4666666666666666
"""
The script is run in an empty namespace
The behavior should be the same as that in the standard command line environment (started by python *. py ).
After that, all variables defined in this file (as well as various import, functions, and global variables) can be accessed in IPython shell.
"""