日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python中乐高积木——函数

發布時間:2024/4/13 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中乐高积木——函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.函數的定義

def 函數名():
函數體
return 返回值1 返回值2
二.函數的調用
函數名()
實現答應返回值:print 函數名()
總結:
定義函數時,函數不執行
定義函數時,函數才執行
1.有參數的函數
(1)必選參數
#形式參數
def add(x,y)
print x + y
#實參,x=1,y=2
add(1, 2)

3

(2)默認參數
def mypow(x, y=2):
print x**y

mypow(2)

(3)可變函數
#形式參數
#args可以改為其他變量名;

def add(*args):
#args實質上是一個元組;

##print args # sum = 0 # for i in args: # sum += i # print sumadd(1, 2, 3, 4, 5, 6)

(4)關鍵字參數

#kwargs可以改為其他變量名; #def inuser(name, age, **kwargs): ##kwargs實質上是一個字典; #print name, age, kwargs inuser("user1" 12 city="xi'an" brith="20180102" 如果必選參數, 默認參數, > 可變參數, > 關鍵字參數 ```; (5)返回值 #函數中如果沒有返回值return時,默認返回值None;

def add(x,y):
return x+y
print add(1,2)

3
None
#返回多個值
def fun(*args):
"""
返回最大值和最小值

: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

![](https://s1.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, 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]: [i
2 for i in range(2, 10, 2)]
Out[10]: [4, 16, 36, 64]

![](https://s1.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 [12]: [i+j for i in 'xyz' for j in '123' ]
Out[12]: ['x1', 'x2', 'x3', 'y1', 'y2', 'y3', 'z1', 'z2', 'z3']

#for嵌套if語句

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]

應用:找出/etc下文件中以.conf結尾的文件; 提示:os.listdir("/etc")s.enswith(".conf")

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']

![](https://s1.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=)未完待續

轉載于:https://blog.51cto.com/13363488/2059268

總結

以上是生活随笔為你收集整理的Python中乐高积木——函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。