python雜記(3) 元組與函式

2021-09-25 08:21:05 字數 4583 閱讀 2172

1、元組

#元組tuple,與列表有很多類似。區別主要是元組中的元素是不可改變的;它的定義使用(),而不是[ ]

tuple1=(1

,2,3

,4,5

,6)print

(tuple1[2]

)tuple2=

('123'

,'abc'

,'pht'

,123

,456

)tuple2=tuple2[:2

]+('哈哈',)

+tuple2[2:

]#元組的插入,哈哈 的後面一定要加, 因為,才是元組的「本體」,才表示它是乙個元組

print

(tuple2)

#-------------------------

temp=

'i am years old'

#字串的插入,與元組類似

temp=temp[:4

]+'20'

+temp[4:

]print

(temp)

temp=

'i am years old'

#如果split()不設定引數,則預設從空格分割

temp=temp.split(

)print

(temp)

#------------------字串格式化format-------------

temp=

' old '

.format

('how'

,'are'

,'you'

)#花括號表示replacement欄位,設定位置引數用來替換

print

(temp)

temp=

' old '

.format

('how'

,haha=

'are'

,emmm=

'you'

)#可以綜合位置引數與關鍵字引數,但位置引數放在關鍵字引數之前

print

(temp)

temp=''.

format

(1.2768

,'kb'

)#:將0代表的數字取小數點後一位小數,四捨五入

print

(temp)

#-------------list函式:列表-------------------------------

a=list()

print

(a)#生成乙個空列表

b='how are you'

b=list

(b)print

(b)# 輸出:['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u']c=(

1,1,

2,3,

5,8,

13,21,

34)#c為乙個元組。斐波那契數列

c=list

(c)print

(c)#把乙個元組變成乙個列表,輸出:[1, 1, 2, 3, 5, 8, 13, 21, 34]

#--------------tuple函式:把乙個可迭代物件轉換為元組-----------------------

#把其他形式轉換為元組,與list類似,具體略過

#len() str()

max=

max(b)

#max與min的引數中必須是同一型別,如:不能有字元型又有整型

min=

min(b)

print

(min)

#返回空格

print

(max)

#輸出為y,ascii碼的最大值

s=sum

(c)#不能是字元型的

print

(s)#88

syz=

sum(c,10)

#10為可選引數,加在後面

print

(syz)

#98d=[45

,12,3

,69,88

,5,100]n=

sorted

(d)#從小到大排序

print

(n,d)

#d的元素順序不做改變,sorted,以及下面的reversed都只是拷貝。而list.sort()等則不同

e=reversed

(d)print

(e)#返回迭代器物件?

print

(list

(reversed

(d))

)#強制轉換為list,翻**[100, 5, 88, 69, 3, 12, 45]

print

(d)#[45, 12, 3, 69, 88, 5, 100]

f=enumerate

(d)#返回包含index值和item 值的元組,並列舉

print

(f)#,返回乙個物件,

print

(list

(enumerate

(d))

)#[(0, 45), (1, 12), (2, 3), (3, 69), (4, 88), (5, 5), (6, 100)]a=[

1,2,

3,4,

5,7]

b=[2,

5,7,

8]print

(list

(zip

(a,b)))

#zip返回各個引數的序列組成的元組,並用list轉換,最後輸出:[(1, 2), (2, 5), (3, 7), (4, 8)]

2、函式的基本操作

def

afunction()

:#定義函式

print

('hello world'

)afunction(

)#往上尋找,並呼叫函式

defbfunction

(name)

:'加油的函式'

#這是乙個函式文件

print

(name+

'加油'

)bfunction(

'pht'

)#pht必須為字串型的,與加油保持一致

print

(bfunction.__doc__)

#列印出函式文件,雙下劃線

defadd_

(a,b)

: c=a+b #c為函式中的區域性變數

return c

print

(add_(3,

2))# print(c) #這裡會報錯,因為c為區域性變數,函式外無法訪問

#關鍵字引數

defsaysome

(name,words)

:print

(name+words)

saysome(words=

'hahaha'

,name=

'pht'

)#當引數太多時,可以使用關鍵字引數,防止引數順序出錯

defhhhh

(*a)

:#加*表示收集引數(可變引數)

print

('a的長度:'

,len

(a))

print

(a[0])

hhhh(

354,

657,

789,

'hahaaha'

)#函式的閉包.................

#lambda關鍵字建立匿名函式;過濾器filter;以及map()

defabc

(x):

return x**

2#等價於下面的匿名函式

a=lambda x:x**

2# lambda 函式引數:函式返回值,它返回乙個函式物件a

print

(a(3))

print

(list

(filter

(none,[

1,0,

true

,false])

))#filter有兩個引數,1、funcion或者none型別,2、篩選物件(可迭代),返回true的物件

#用過濾器返回0-9中的奇數

print

(list

(filter

(lambda x:x%2,

range(10

))))

#lambda構建了匿名函式,屬於function型別

#map函式,有兩個引數。1、function 2、乙個序列。它可以將序列的數代入函式中,並返回乙個序列

print

(list

(map

(lambda x:x*5,

range(15

))))

3、遞迴:

def

digui

(n):

if n==1:

return

1else

:return n*digui(n-1)

print

(digui(5)

)

python3元組 Python3元組

python的元組與列表相似,不同之處在於元組的元素不能修改 元組使用小括號,列表使用方括號 元組建立很簡單,只需要在括號中新增元素,並使用逗號隔開即可。建立空元組 tup1 tup2 1,元組只包含乙個元素時,需要在元素後面新增逗號,否則括號會被當作運算子使用 元組與字串類似,下標索引從0開始,可...

Python筆記 3 元組學習

usr bin env python coding utf 8 author lingchongshi 檢視原始碼ctrl 左鍵 tuple 以圓括號 括起來,以 分隔 1 有序,建立後不可變的 2 元組中元素的資料是可以變的 tuple 1,2,a b 中文 3,python 5 中文 檢視物件的...

Python學習筆記 3 元組

元組也是乙個列表,但是不能修改。元組大部分時候是小括號,list是中括號,字典是大括號。建立 mysql 118.124.33.40 3306 root 123456 建立乙個元組 my 建立乙個空元組 也可以用分片的方式複製乙個元組 mysql1 mysql print mysql1 查 和列表方...