首页 > 编程语言 > 详细

python 基础语法

时间:2019-06-22 23:00:50      阅读:141      评论:0      收藏:0      [点我收藏+]

匿名函数

In [1]: a = lambda x,y:x+y

In [2]: a(1,2)
Out[2]: 3

 正则表达式

match如果第一个字符不匹配,则终止;search则是一个个字符里寻找。

In [3]: import re

In [4]: re.search("hao","da jia hao")
Out[4]: <_sre.SRE_Match at 0x7eff99911308>

In [5]: re.search("hao","da jia h")

In [6]: re.match("hao","da jia hao")

In [7]: re.match("da","da jia hao")
Out[7]: <_sre.SRE_Match at 0x7eff999115e0>

 小括号中写上要输出的内容,结果以元组输出

In [10]: re.match("[0-9]{3}(go)","123go").groups()
Out[10]: (go,)

单独输出

In [14]: re.match("[0-9]{3}(go)","123go").group(0)
Out[14]: 123go

In [15]: re.match("[0-9]{3}(go)","123go").group(1)
Out[15]: go
In [29]: re.search("(go)","just go go go !").group(1)
Out[29]: go

 模块导入导出

import module

from module import m1,m2,m3

import module_name as m4

通过sys.path可以查找python从目录中查找模块的顺序

In [31]: import sys

In [32]: sys.path
Out[32]: 
[‘‘,
 /usr/bin,
 /usr/lib64/python27.zip,
 /usr/lib64/python2.7,
 /usr/lib64/python2.7/plat-linux2,
 /usr/lib64/python2.7/lib-tk,
 /usr/lib64/python2.7/lib-old,
 /usr/lib64/python2.7/lib-dynload,
 /usr/lib64/python2.7/site-packages,
 /usr/lib64/python2.7/site-packages/mysqlclient-1.3.13-py2.7-linux-x86_64.egg,
 /usr/lib/python2.7/site-packages,
 /usr/lib/python2.7/site-packages/setuptools-41.0.1-py2.7.egg,
 /usr/lib/python2.7/site-packages/IPython/extensions,
 /root/.ipython]

第一个目录为空,表示当前目录,即优先从当前目录查找

然后从/usr/bin目录中查找是否存在所导入的模块,依次排查剩下的所有目录

as通常用于同一文件中从不同模块引入的方法重名的时候,或者换一个更贴切业务含义的名字

In [34]: from re import search as sea

In [35]: sea("ka","wa ka ka ")
Out[35]: <_sre.SRE_Match at 0x7eff98800cc8>

 

装饰器

In [40]: def addLogging(func):
    ...:     def log(*args, **kwargs):
    ...:         print "%s is running"%func.__name__
    ...:         return func(*args)
    ...:     return log
    ...: 

In [41]: def aa():
    ...:     print 123
    ...:     

In [42]: aa = addLogging(aa)

In [43]: aa()
aa is running
123

In [44]: @addLogging
    ...: def bb():
    ...:     print 111
    ...:     

In [45]: bb()
bb is running
111

 

python 基础语法

原文:https://www.cnblogs.com/perfei/p/11070601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!