Tuple

Defining a Tuple

  • A tuple is a collection that:

    • Can be ordered
    • Is indexed
    • Is immutable
    • Allows duplicates

Creating a Tuple

>>> fruits = ('apple', 'banana', 'pear')
>>> print(fruits)
('apple', 'banana', 'pear')

Accessing a Tuple

>>> fruits = ('apple', 'banana', 'pear')
>>> apple = fruits[0]
>>> print(apple)
'apple'

Combining Two Tuples

>>> fruits = ['apple', 'banana', 'pear']
>>> more_fruits = ['orange', 'pear', 'mango']
>>> fruits + more_fruits
('apple', 'banana', 'pear', 'orange', 'pear', 'mango')

References

Previous
Next

List

Set