Skip to main content
🚧 Work in Progress / Draft

Tuple

Introduction​

Tuple is an ordered and immutable collection data structure. Ordered meaning it keeps the order of elements in which they are inserted, and immutable means that once the tuple is created it cannot change, and you cannot add or remove elements from it.

Lesson Overview​

In this lesson we will cover:

  • How to create tuple
  • Accessing the elements in tuple
  • Useful methods on tuples

Creating Tuple​

A tuple is created by enclosing items in parentheses () separated by comma.

Python Sandbox

Tuples containing a single element still must contain a comma, otherwise python will read the wrong data type:

Python Sandbox

Accessing Elements​

Accessing elements of the tuple is done with index, just like lists.

Python Sandbox

Useful methods​

Unlike functions, as we cannot modify tuples, we don't have a lot of methods available to us, but we will list a few.

  • count() - returns the number of times a specified value appears in tuple.
    Python Sandbox
  • index() - returns index of the first occurrence of the element.
    Python Sandbox

Assignment​

Todo

What's Next​

Todo