Python 元組tuple - Python零基礎(chǔ)入門教程

目錄

零基礎(chǔ) Python 學習路線推薦 : Python 學習目錄 >> Python 基礎(chǔ)入門

一.Python 元組 tuple 簡介

在上一篇文章中我們講解了關(guān)于 Python 列表 List 相關(guān)內(nèi)容,今天給大家解釋一下列表 List 的兄弟 – 元組,俗稱: tuple;

元組 tuple 和列表 List 類似,元組有如下特點:

  • 1.由一個或者多個數(shù)據(jù)構(gòu)成,數(shù)據(jù)的類型可以不相同也可以相同;
  • 2.元組中的數(shù)據(jù)需要寫在 () 中括號內(nèi)部,數(shù)據(jù)與數(shù)據(jù)之間用逗號隔開;
  • 3.元組是一個有序的集合,下標索引默認重 0 開始,和字符串類似;
  • 4.元組的數(shù)據(jù)不能被修改;

元組其實也稱為只讀列表,列表支持的函數(shù)元組同樣也支持,唯一區(qū)別是元組 tuple 中的數(shù)據(jù)不能被修改,這就意味著不能刪除元組 tuple 中的數(shù)據(jù),也不能直接給元組 tuple 中的數(shù)據(jù)賦值;

二.Python 元組 tuple 定義

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python元組tuple.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""


tuple1 = tuple() # 定義一個空元組,元組的數(shù)據(jù)不能修改,意味永遠都是一個空元組
print(tuple1)
print(type(tuple1)) # 獲取數(shù)據(jù)類型

tuple2 = ("python","study") # 定義元組tuple2 ,該元組由兩個字符串數(shù)據(jù)構(gòu)成
print(tuple2)

tuple3= ("python","s",False,2.5) # 定義元組tuple3 ,該元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成
print(tuple3)

'''
輸出結(jié)果:
()
<class 'tuple'>
('python', 'study')
('python', 's', False, 2.5)

'''

三.Python 元組 tuple 數(shù)據(jù)查詢

元組 tuple 的查詢和列表 list 的操作類似,同樣也可以直接通過下標查詢元組中的數(shù)據(jù),演示代碼如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python元組tuple.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成
print(type(tuple1)) # 通過內(nèi)置函數(shù)type獲取數(shù)據(jù)類型
print(tuple1)
print(tuple1[1]) # 獲取元組中索引值為1 的數(shù)據(jù)
print(tuple1[len(tuple1)-1]) # 獲取元組中的最后一個元素,注意是len(tuple)-1,并非len(tuple)
print(tuple1[1:4]) # 獲取元組索引值1-4的數(shù)據(jù)
print(tuple1[:5]) # 如果冒號之前沒有設置參數(shù),默認重0開始,表示獲取元組索引值0-5的數(shù)據(jù)
print(tuple1[3:]) # 如果冒號之后沒有設置參數(shù),默認到元組最后一個數(shù)據(jù)介紹,包括最后一個數(shù)據(jù)

'''
輸出結(jié)果:

<class 'tuple'>
('python', 's', False, 2.5, 40, 'tuple')
s
tuple
('s', False, 2.5)
('python', 's', False, 2.5, 40)
(2.5, 40, 'tuple')

'''

四.Python 元組 tuple 不支持刪除/修改數(shù)據(jù)

元組 tuple 中的數(shù)據(jù)只能讀取,不能修改也不能刪除,如果對元組 tuple 中的數(shù)據(jù)刪除或者修改會報錯,代碼演示:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python元組tuple.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

# 測試修改元組數(shù)據(jù)
tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成
tuple1[0] = False

編譯器會報錯:TypeError: ‘tuple’ object does not support item assignment(翻譯:元組 tuple 不支持修改)

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python元組tuple.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

# 測試刪除元組數(shù)據(jù)
tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成
del tuple1[0]

編譯器會報錯:TypeError: ‘tuple’ object doesn’t support item deletion(翻譯:元組 tuple 不支持刪除)

五.Python 元組 tuple 與 列表 list 相互轉(zhuǎn)換

Python 元組 tuple 與 列表 list 之間直接強制轉(zhuǎn)換即可,演示代碼如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python元組tuple.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

# 元組tuple 轉(zhuǎn)為 列表list
tuple1= ("python","s",False,2.5,40,"tuple")
list1 = list(tuple1)

print("tuple1數(shù)據(jù)類型是:",type(tuple1))
print("list1數(shù)據(jù)類型是:",type(list1))

print("***"*20) # 小竅門:直接打印60個*

# 列表list 轉(zhuǎn)為 元組tuple
list2 = [False,"好好學習",0,3.14]
tuple2 = tuple(list2)
print("tuple2數(shù)據(jù)類型是:",type(tuple2))
print("list2數(shù)據(jù)類型是:",type(list2))


'''
輸出結(jié)果:

tuple1數(shù)據(jù)類型是: <class 'tuple'>
list1數(shù)據(jù)類型是: <class 'list'>
************************************************************
tuple2數(shù)據(jù)類型是: <class 'tuple'>
list2數(shù)據(jù)類型是: <class 'list'>

'''

六.重點總結(jié)

  • 1.注意元組 tuple 與列表 list 的區(qū)別,元組的數(shù)據(jù)不能被修改,其他使用和列表一樣;
  • 2.注意元組 tuple / 列表 list / 字符串 str 三者的寫法區(qū)別;

七.猜你喜歡

  1. Python 簡介
  2. Python Pycharm Anacanda 區(qū)別
  3. Python2.x 和 Python3.x,如何選擇?
  4. Python 配置環(huán)境
  5. Python Hello World 入門
  6. Python 代碼注釋
  7. Python 中文編碼
  8. Anaconda 是什么?Anconda 下載安裝教程
  9. Pycharm 提示:this license **** has been cancelled
  10. Pycharm 設置開發(fā)模板/字體大小/背景顏色
  11. Python 列表 list
  12. Python 元組 tuple

未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python 元組 tuple

本文由博客 - 猿說編程 猿說編程 發(fā)布!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容