Python Tuple - Define, Access, Iterate
Python Tuple ? Define, Access, Iterate
Python Tuple
Python Tuple is a collection of elements where elements can be accessed using index and are unchangeable. What does this mean? A tuple can be defined with a definite number of elements that belong to different datatypes. Each member can be accessed using the index. index starts with 0 for the first element and increments for the subsequent elements. You cannot modify a tuple, hence unchangeable.
How to define Python Tuple?
To define a Python Tuple, use parenthesis () , and the elements inside these parenthesis separated by comma. For example (14, "Raghu") is a Tuple. tuple1 = (14, 'Raghu')
You can access the first element using index as tuple1[0] and the second element as tuple1[1] .
Example 1: Basic Python Tuple
In the following example, we define a tuple with an integer and string. Also, we will access the individual elements using index. example.py myTuple = (14, 'Raghu') print(myTuple[0]) print(myTuple[1])
Output 14 Raghu
Example 2: Python Tuples are unchangeable
In the following example, we define a tuple with an integer and string.
We will try to change the elements of this tuple. example.py
myTuple = (14, 'Raghu') myTuple[0] = 25 myTuple[1] = 'Tim'
Output
Traceback (most recent call last): File "example1.py", line 3, in myTuple[0] = 25
TypeError: 'tuple' object does not support item assignment
Yeah. Python Tuple does not support item assignment and hence unchangeable.
Example 3: Iterate over Tuple
In the following example, we define a tuple with an integer and string. We will try to iterate over the elements of this tuple. example.py
myTuple = (14, 'Raghu') for element in myTuple:
print(element)
Output
14 Raghu
Conclusion
In this Python Tutorial, we learned what a Python Tuple is, how to define a tuple, how to access the elements of a tuple and how to iterate over tuple.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- how to get keys of python dictionary as list tutorial kart
- ppyytthhoonn ddiiccttiioonnaarryy
- dictionaries store connections between pieces of list comprehensions a
- python tuple define access iterate
- cme193 introductiontoscientificpython lecture3 tuples sets
- beginners python cheat sheet pcc dictionaries anarcho copy
- python dictionary tutorial kart
- chapter 12 dictionary hash tables college of engineering
- dictionary case study gktcs
- chapter 2 lists arrays and dictionaries uc davis