I. Definition of functions
def function name ():
function body
Return value 1 return value 2
Two. Invocation of functions
Function name ()
Implementing the Promised return value: Print function name ()
Summarize:
When defining a function, the function does not perform
function is not executed until the function is defined
1. Function with parameters
(1) Required parameters
#形式参数
def add (x, y)
Print x + y
#实参, x=1,y=2
Add (1, 2)
3
(2) Default parameters
def mypow (x, y=2):
Print X**y
MYPOW (2)
(3) Variable function
#形式参数
#args可以改为其他变量名;
def add (*args):
#args实质上是一个元组;
##print args# sum = 0# for i in args:# sum += i# print sum add(1, 2, 3, 4, 5, 6)
(4) Keyword parameters
#kwargs可以改为其他变量名;#def inuser(name, age, **kwargs):##kwargs实质上是一个字典;#print name, age, kwargsinuser("user1" 12 city="xi‘an" brith="20180102"如果必选参数, 默认参数, > 可变参数, > 关键字参数```;(5)返回值#函数中如果没有返回值return时,默认返回值None;
def add (x, y):
Return X+y
Print Add (+)
3
None
#返回多个值
def fun (*args):
"""
Returns the maximum and minimum values
:param args::return:"""#实质上python只能返回一个值;#间接通过元组返回多个值;return max(args), min(args)
Print Fun (23, 21, 1, 8,12)
/usr/bin/python2.7/root/pycharmprojects/untitled/file/retu.py
(23, 1)
(5)函数作用域1)global关键字必须先声明,再赋值;#全局变量
num = 1
def fun ():
num = 5
Fun ()
Print num
1
num = 1
def fun ():
Global Num #global声明num为全局变量
num = 5 #局部变量
Fun ()
Print num
5
三.高级特性1.切片2.迭代(1)是否可以for循环遍历的对象;(2)isinstance判断是否可迭代;
In [1]: From collections import iterable
In [2]: isinstance (' Hello ', iterable)
OUT[2]: True
In [3]: Isinstance ([1, 2, 3, 4], iterable)
OUT[3]: True
In [4]: Isinstance ((1, 2, 3, 4), iterable)
OUT[4]: True
In [6]: Isinstance ({' C ': 3, ' d ': 4}, Iterable)
OUT[6]: True
In [7]: Isinstance ({1, 2, 3}, Iterable)
OUT[7]: True
![](http://i2.51cto.com/images/blog/201801/10/310edcd8cca1d532fb15e730c3d553ae.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)四.列表生成式(1)生成列表的公式(2)需求:生成一个列表,返回1-100中偶数的平方;[4, 16, 36......]方法1:
Li = []
For I in Range (2,100,2):
...: Li.append (i2)
...: Print Li
...:
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 230 4, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096, 4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056, 7396, 7744, 8100, 8464, 8836, 9216, 9604]
Method 2:
In [ten]: [I2 for I in range (2, 10, 2)]
OUT[10]: [4, 16, 36, 64]
![](http://i2.51cto.com/images/blog/201801/10/65d0356481e47384b351ac8a2855cd51.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)(3)变异的列表生成式#for循环嵌套for循环,两个字符串的全排列
In [K]: [I+j for I in ' XYZ ' for J in ' 123 ']
OUT[12]: [' x1 ', ' x2 ', ' x3 ', ' y1 ', ' y2 ', ' y3 ', ' Z1 ', ' Z2 ', ' Z3 ']
#for嵌套if语句
in [+]: [i**2 for I in range (2, 2) if i%2==0]
OUT[13]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
应用:找出/etc下文件中以.conf结尾的文件;提示: os.listdir("/etc") s.enswith(".conf")
in [+]: Import os
In []: print [i-I in os.listdir ('/etc ') if I.endswith ('. conf ')],
[' host.conf ', ' kdump.conf ', ' sysctl.conf ', ' l D.so.conf ', ' sestatus.conf ', ' nsswitch.conf ', ' nfsmount.conf ', ' man_db.conf ', ' libaudit.conf ', ' dnsmasq.conf ', ' Request-key.conf ', ' krb5.conf ', ' dracut.conf ', ' libuser.conf ', ' rsyslog.conf ', ' logrotate.conf ', ' e2fsck.conf ', ' Yum.conf ', ' mke2fs.conf ', ' idmapd.conf ', ' ovirt-guest-agent.conf ', ' rsyncd.conf ', ' chrony.conf ', ' sudo-ldap.conf ', ' Sudo.conf ', ' vconsole.conf ', ' locale.conf ', ' resolv.conf ', ' grub.conf ', ' asound.conf ', ' fuse.conf ', ' colord.conf ', ' Hba.conf ', ' sos.conf ', ' oddjobd.conf ', ' usb_modeswitch.conf ', ' ipsec.conf ', ' ksmtuned.conf ', ' mtools.conf ', ' Radvd.conf ', ' numad.conf ', ' brltty.conf ', ' fprintd.conf ', ' wvdial.conf ', ' pbm2ppa.conf ', ' pnm2ppa.conf ', ' Updatedb.conf ', ' lftp.conf ', ' trolltech.conf ']
![](http://i2.51cto.com/images/blog/201801/10/ec7cb73b20a5242bc0e9607652147fd9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)未完待续
Lego bricks in Python--functions