python
Python collection - Tuples in python
#Tuples are unchangable means you can not modify its items
fruits = ("banana", "orange", "apple", "grapes")
print(fruits[2]) #print third element apple
print(fruits[:3]) #print items start index to 0 and less than index 3
print(fruits[1:3]) #print tuple start from index 1 and less than index 3
print(fruits[-1]) #print last element
Output
apple
("banana", "orange")
("orange", "apple")
grapes
("banana", "orange")
("orange", "apple")
grapes
Tuples are python collections that can have multiple items and you can access them using its indexes. Tuples are unchangeable and you can not modify its items. In the code snippet, we have created a tuple named as 'fruits' which contains fruit names and we are printing fruit names by using different indexes.
Was this helpful?
Similar Posts
- Join two tuples in python
- Convert List of Tuples to JSON string in Python
- Python collection - List in python
- Python collection - sets in python
- Python collection - Dictionaries in python
- Convert pandas DataFrame to python collection - dictionary
- Python Measure the execution time of small bits of Python code with the timeit module