import numpy as np
from numpy.random import randn
data={i:randn()for i in range(7)}
print data,type(data)
#TAB可以显示一般方法和属性
an_apple=27
b=[1,2,3]
'''用an+Tab会显示出an_apple变量,b.+Tab会显示list相关方法和属性'''
'''?会显示出有关对象的通用信息'''
b?
def add_numbers(a,b):
'''Add two numbers together
Returns
--------
the sum: type of arguments
'''
return a+b
#?显示add_numbers函数docstring,??还可以显示源代码
add_numbers?
add_numbers??
#?还可以进行匹配查找,下面可以列出numpy含有load函数
np.*load*?
{0: -0.062138943844772546, 1: 0.8556806268362996, 2: -0.42712938684082447, 3: -0.3938713006183655, 4: 1.2635806321865928, 5: -0.09553815289310987, 6: -0.7411252471879937} <type 'dict'>
#%run命令运行外部Python文件
%run ipython_script_test.py
# print f??
print c
result
#Ipython可以用%运行魔法命令
'''用%magic可以查看Ipython魔法命令'''
%magic
'''用%MagicCommand?可以查看具体某一魔法命令信息'''
%logstart
Summary of magic functions (from %lsmagic):
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
#让Ipython对类更友好
class Message:
def __init__(self,msg):
self.msg=msg
x=Message('I have a dream')
#直接显示x并不能得到所想要的结果
x
class Message_2:
def __init__(self,msg):
self.msg=msg
'''通过ipython的__repr__可以返回字符串信息'''
def __repr__(self):
return 'Message: %s' % self.msg
y=Message_2('I have a secret')
y
Message: I have a secret
最后是Tim Peter的诗The Zen of Python供勉励
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!