選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Python 教程

Python 主頁 Python 簡介 Python 入門 Python 語法 Python 註釋 Python 變數 Python 資料型別 Python 數字 Python 型別轉換 Python 字串 Python 布林值 Python 運算子 Python 列表 Python 元組 Python 集合 Python 字典 Python If...Else Python While 迴圈 Python For 迴圈 Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Polymorphism Python Scope Python Modules Python Dates Python Math Python JSON Python RegEx Python PIP Python Try...Except Python User Input Python String Formatting

檔案處理

Python 檔案處理 Python 讀取檔案 Python 寫入/建立檔案 Python 刪除檔案

Python 模組

NumPy 教程 Pandas 教程 SciPy 教程 Django 教程

Python Matplotlib

Matplotlib 簡介 Matplotlib 入門 Matplotlib Pyplot Matplotlib 繪圖 Matplotlib 標記 Matplotlib 線條 Matplotlib 標籤 Matplotlib 網格 Matplotlib 子圖 Matplotlib 散點圖 Matplotlib 條形圖 Matplotlib 直方圖 Matplotlib 餅圖

機器學習

Getting Started Mean Median Mode Standard Deviation Percentile Data Distribution Normal Data Distribution Scatter Plot Linear Regression Polynomial Regression Multiple Regression Scale Train/Test Decision Tree Confusion Matrix Hierarchical Clustering Logistic Regression Grid Search Categorical Data K-means Bootstrap Aggregation Cross Validation AUC - ROC Curve K-nearest neighbors

Python MySQL

MySQL 入門 MySQL 建立資料庫 MySQL 建立表 MySQL 插入 MySQL 選擇 MySQL Where MySQL Order By MySQL 刪除 MySQL 刪除表 MySQL 更新 MySQL Limit MySQL Join

Python MongoDB

MongoDB 入門 MongoDB 建立資料庫 MongoDB 集合 MongoDB 插入 MongoDB Find MongoDB Query MongoDB Sort MongoDB 刪除 MongoDB 刪除集合 MongoDB 更新 MongoDB Limit

Python 參考

Python 概述 Python 內建函式 Python 字串方法 Python 列表方法 Python 字典方法 Python 元組方法 Python 集合方法 Python 檔案方法 Python 關鍵字 Python 異常 Python 詞彙表

模組參考

Random 模組 Requests 模組 Statistics 模組 Math 模組 cMath 模組

Python 如何操作

刪除列表重複項 反轉字串 兩個數字相加

Python 示例

Python 示例 Python 編譯器 Python 練習 Python 測驗 Python 伺服器 Python 面試問答 Python 訓練營 Python 證書

Python - 訪問元組項


訪問元組項

您可以透過方括號內的索引號來訪問元組項。

示例

列印元組中的第二個項

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
自己動手試一試 »

注意: 第一個項的索引是 0。


負數索引

負數索引意味著從末尾開始。

-1 指的是最後一項,-2 指的是倒數第二項,依此類推。

示例

列印元組的最後一項

thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
自己動手試一試 »

索引範圍

您可以指定開始和結束範圍來指定一個索引範圍。

當指定範圍時,返回值將是一個包含指定項的新元組。

示例

返回第三、第四和第五項

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
自己動手試一試 »

注意: 搜尋將從索引 2(包含)開始,到索引 5(不包含)結束。

記住第一個項的索引是 0。

透過省略開始值,範圍將從第一項開始

示例

此示例返回從開頭到“kiwi”(不包含)的項

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[:4])
自己動手試一試 »

透過省略結束值,範圍將一直到元組的末尾

示例

此示例返回從“cherry”開始直到末尾的項

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:])
自己動手試一試 »


負數索引範圍

如果您想從元組末尾開始搜尋,請指定負數索引

示例

此示例返回從索引 -4(包含)到索引 -1(不包含)的項

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])
自己動手試一試 »

檢查項是否存在

要確定指定的項是否存在於元組中,請使用 in 關鍵字。

示例

檢查元組中是否存在“apple”

thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple
  print("Yes, 'apple' is in the fruits tuple")
自己動手試一試 »


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援