Python Tuples
Tuples
Tuple is a collection of ordered items separated by comma and enclosed within round brackets.
Tuples are indexed and allow duplicate items.
Unlike lists, tuples are immutable.
EntityTuple = ("Body", "Face", "Edge")
print(EntityTuple[0])
Output
Body
Use len() method to find the number of items in the tuple.
Items in the tuple cannot be changed. Add or remove items to tuples are not possible.
Delete Tuple
EntityTuple = ("Body", "Face", "Edge")
del EntityTuple
Tuple Traversal
EntityTuple = ("Body", "Face", "Edge")
for e in EntityTuple:
print(e)
Output
Body
Face
Edge
Check Tuple for an item
EntityTuple = ("Body", "Face", "Edge")
if "Face" in EntityTuple:
print("Face")
Output
Face
Nested Tuple
Create Tuple containing other tuples or lists.EntityCAD = ("Body", "Face", "Edge")
EntityFEM = ("Node", "Element")
EntityTuple = (EntityCAD, EntityFEM)
print(EntityTuple)
Output
(('Body', 'Face', 'Edge'), ('Node', 'Element'))