Python中乐高积木——函数
一.函數(shù)的定義
def 函數(shù)名():
函數(shù)體
return 返回值1 返回值2
二.函數(shù)的調(diào)用
函數(shù)名()
實(shí)現(xiàn)答應(yīng)返回值:print 函數(shù)名()
總結(jié):
定義函數(shù)時(shí),函數(shù)不執(zhí)行
定義函數(shù)時(shí),函數(shù)才執(zhí)行
1.有參數(shù)的函數(shù)
(1)必選參數(shù)
#形式參數(shù)
def add(x,y)
print x + y
#實(shí)參,x=1,y=2
add(1, 2)
3
(2)默認(rèn)參數(shù)
def mypow(x, y=2):
print x**y
mypow(2)
(3)可變函數(shù)
#形式參數(shù)
#args可以改為其他變量名;
def add(*args):
#args實(shí)質(zhì)上是一個(gè)元組;
(4)關(guān)鍵字參數(shù)
#kwargs可以改為其他變量名; #def inuser(name, age, **kwargs): ##kwargs實(shí)質(zhì)上是一個(gè)字典; #print name, age, kwargs inuser("user1" 12 city="xi'an" brith="20180102" 如果必選參數(shù), 默認(rèn)參數(shù), > 可變參數(shù), > 關(guān)鍵字參數(shù) ```; (5)返回值 #函數(shù)中如果沒有返回值return時(shí),默認(rèn)返回值None;def add(x,y):
return x+y
print add(1,2)
3
None
#返回多個(gè)值
def fun(*args):
"""
返回最大值和最小值
print fun(23, 21, 1, 8,12)
/usr/bin/python2.7 /root/PycharmProjects/untitled/file/retu.py
(23, 1)
num = 1
def fun():
num = 5
fun()
print num
1
num = 1
def fun():
global num #global聲明num為全局變量
num = 5 #局部變量
fun()
print num
5
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
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, 2304, 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]
方法2:
In [10]: [i2 for i in range(2, 10, 2)]
Out[10]: [4, 16, 36, 64]
In [12]: [i+j for i in 'xyz' for j in '123' ]
Out[12]: ['x1', 'x2', 'x3', 'y1', 'y2', 'y3', 'z1', 'z2', 'z3']
In [13]: [i**2 for i in range(2, 20, 2) if i%2==0]
Out[13]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
In [14]: import os
In [17]: print [i for i in os.listdir('/etc') if i.endswith('.conf')],
['host.conf', 'kdump.conf', 'sysctl.conf', 'ld.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']
轉(zhuǎn)載于:https://blog.51cto.com/13363488/2059268
總結(jié)
以上是生活随笔為你收集整理的Python中乐高积木——函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET分布式缓存Redis从入门到实战
- 下一篇: 1月11日学习内容整理:请求库selen