Python的6大基本資料型別之元組

2021-09-25 02:23:13 字數 3100 閱讀 8384

標籤: python

() 小括號建立

eg.

tup1 = ()

print(tup1,type(tup1))

tuple 函式建立(函式中新增的需是可迭代物件)

eg.

tup2=tuple()

print(tup2,type(tup2))

tup3 = ("姓名",":","楊過","  ","qq",9999999,true,[1,2],tup1)

print(tup3,type(tup3))

t1=("python")

print(t1,type(t1))

t2=("python",)

print(t2,type(t2))

可迭代物件都可以被轉換成列表和元組,能被for迴圈遍歷的就是可迭代物件

比如字串可以for迴圈遍歷

d = "wearethworld"

for i in d:

print(i)

所以字串可迭代,用元組轉換字串,會將字串拆分開

tup4=tuple("wearethworld")

print(tup4,type(tup4))

數字不可迭代,不能轉換成元組,下列**不可執行

tup5=tuple(314134)

print(tup5,type(tup5))

其他資料型別轉換回顧

a = str("hello world")

print(a,type(a))

b = int(100)

print(b,type(b))

c = list("wearethworld")

print(c,type(c))

d = "wearethworld"

for i in d:

print(i)

tup4 =(1,2,3,4)

for i in tup4:

print(i)

與列表相同

tup = (1,2,3,4,5)

print(tup[2])

print(tup[3:]) #切片訪問

#元組的拆包

1.在python中使用「 * 」的形式對元組進行拆包

eg.

tup = (1,2,3)

print(*(1,2,3),sep="-")

tup = ("岳雲鵬","楊過","張三丰","徐風年")

print(len(tup)) #顯示元組元素個數,可以應用於列表

def one(*args):

print(*args)

one(tup)

也可以用下列方法拆包

2.定義和元組元素個數相同的變數

people1,people2,people3,people4 = tup

print(people1,people2,people3,people4)

a,b=[1,2]

print(a,b)

a,b=(3,4)

print(a,b)

a,b=

print(a,b)

a,b=

print(a,b)

查 in not in count index

成員運算子 in not in

元組是不可變資料型別,只能進行檢視,不能刪改

建立元組

tup1=()

print(tup1,type(tup1))

tup2=tuple()

print(tup2,type(tup2))

元組的語法(元素,元素)

元組裡面可放除字典所有型別元素

tup3 = ("姓名",":","馬化騰"," ","qq",123456789,true,[1,2],tup1)

print(tup3,type(tup3))

當元組只有乙個元素時,必須在這唯一乙個元素後面在逗號

t1=("python")

print(t1,type(t1))

t2=("python",)

print(t2,type(t2))

資料型別的轉換

a = str("hello world")

print(a,type(a))

b = int(100)

print(b,type(b))

c = list("wearethworld")

print(c,type(c))

d = "wearethworld"

for i in d:

print(i)

print("------" * 3)

tuple()

tup4 =(1,2,3,4)

for i in tup4:

print(i)

print("------"*3)

tup = ("宋先生","楊過","張無機","馬雲")

print(len(tup)) #顯示元組元素個數,可以應用於列表

def one(*args):

print(*args)

one(tup)

people1,people2,people3,people4 = tup

print(people1,people2,people3,people4)

print("------"*3)

yxt = ("王者榮耀","刺激戰場","pubg")

t = ("54088",)

print(t,type(t))

def one(*args):

print(*args)

one(yxt)

like1,like2,like3 = yxt

print(like2)

print(like1,like2,like3)

pyhon的6大基本資料型別

整型包括所有的正整數,負整數還有0。在python中所有的整型資料全部預設採用十進位制進行表示,但我們還可以手動表示其他進製的整型,具體表示如下 表示十進位制整型,不能使用0d來表示,python不支援這種寫法 a 100 表示二進位制整型 a 0b100010 表示八進位制整型 a 0o12333...

Python中6種基本資料型別

基本資料型別包括 不可變資料 number 數字 string 字串 tuple 元組 可變資料 list 列表 set 集合 dictionary 字典 有序序列 string,tuple,list 無序序列 set,dictionary number int整數型別 float 浮點型別 boo...

python基本資料型別

物件是python中最基本的概念,python中資料以物件的形式出現 無論是python提供的內建物件,還是使用python或是像c擴充套件庫這樣的擴充套件語言工具建立的物件。物件時記憶體中的一部分,包括數值和相關操作的集合。python程式可以分解成模組 語句 表示式以及物件,如下 1 程式由模組...