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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python 元组tuple - Python零基础入门教程

發(fā)布時間:2024/9/27 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 元组tuple - Python零基础入门教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 一.Python 元組 tuple 簡介
  • 二.Python 元組 tuple 定義
  • 三.Python 元組 tuple 數(shù)據(jù)查詢
  • 四.Python 元組 tuple 不支持刪除/修改數(shù)據(jù)
  • 五.Python 元組 tuple 與 列表 list 相互轉(zhuǎn)換
  • 六.重點總結(jié)
  • 七.猜你喜歡

零基礎(chǔ) Python 學(xué)習(xí)路線推薦 : Python 學(xué)習(xí)目錄 >> 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.元組是一個有序的集合,下標(biāo)索引默認(rèn)重 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ù)不能修改,意味永遠(yuǎn)都是一個空元組 print(tuple1) print(type(tuple1)) # 獲取數(shù)據(jù)類型tuple2 = ("python","study") # 定義元組tuple2 ,該元組由兩個字符串?dāng)?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 的操作類似,同樣也可以直接通過下標(biāo)查詢元組中的數(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è)置參數(shù),默認(rèn)重0開始,表示獲取元組索引值0-5的數(shù)據(jù) print(tuple1[3:]) # 如果冒號之后沒有設(shè)置參數(shù),默認(rèn)到元組最后一個數(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,"好好學(xué)習(xí)",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ū)別;

七.猜你喜歡

  • Python 簡介
  • Python Pycharm Anacanda 區(qū)別
  • Python2.x 和 Python3.x,如何選擇?
  • Python 配置環(huán)境
  • Python Hello World 入門
  • Python 代碼注釋
  • Python 中文編碼
  • Anaconda 是什么?Anconda 下載安裝教程
  • Pycharm 提示:this license **** has been cancelled
  • Pycharm 設(shè)置開發(fā)模板/字體大小/背景顏色
  • Python 列表 list
  • Python 元組 tuple
  • 未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python 元組 tuple

    總結(jié)

    以上是生活随笔為你收集整理的Python 元组tuple - Python零基础入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。