lfalanga
2/7/2020 - 12:59 AM

Tuples in Python

# Example 1: Some tuples examples
T1 = 1, 2, 3, 4, 5, 3
print(type(T1)) # <class 'tuple'>
T2 = (1, 2, 3, 4, 5, 3)
print(type(T2)) # <class 'tuple'>

T3 = T1, T2
print(type(T3)) # <class 'tuple'>
print(T3) # ((1, 2, 3, 4, 5, 3), (1, 2, 3, 4, 5, 3))

T4 = T3 + (6, 7)
print(T4) # ((1, 2, 3, 4, 5, 3), (1, 2, 3, 4, 5, 3), 6, 7)
# Determining an item by its index
print(T4[0])  # (1, 2, 3, 4, 5, 3)
print(T4[1])  # (1, 2, 3, 4, 5, 3)
print(T4[2])  # 6
print(T4[3])  # 7
# Determining the position of an element
print(T4.index(6))  # 2
# How many times the item appears in the tuple
print(T4.count(7))  # 1

# Creating an empty tuple
T5 = ()
# Creating a tuple with just one element (see the comma at the end)
T6 = 'Manzana',
print(len(T5))  # 0
print(len(T6))  # 1
print(T6)       # ('Manzana',)