Lessons

Python Basics

Python Variables

Operators in Python

Conditional Statements in Python

Python Lists

Python Tuples

Python Sets

Python Dictionaries

Loops in Python

Python Arrays and Functions

Conclusion

Tuples in Python

Python Tuples

In Python, tuples are a built-in data type used to store multiple items in a single variable. Tuples are similar to lists but have some key differences, making them a useful choice in specific situations. This lesson will cover the characteristics of tuples, how to create them, and their usage in Python.

1. What is a Tuple?

A tuple is a collection that is both ordered and unchangeable (immutable). This means that once a tuple is created, you cannot change its contents (i.e., you cannot add, remove, or modify items).

Characteristics of Tuples:

  • Ordered: The items in a tuple have a defined order, and that order will not change.
  • Unchangeable: Tuples cannot be modified after creation.
  • Allow Duplicates: Tuples can contain multiple items with the same value.

Example: Create a Tuple

python
1thistuple = ("apple", "banana", "cherry")
2print(thistuple)  # Output: ('apple', 'banana', 'cherry')

2. Tuple Items

Tuple items are indexed, with the first item having index [0], the second item having index [1], and so forth.

Example: Allowing Duplicate Values

python
1thistuple = ("apple", "banana", "cherry", "apple", "cherry")
2print(thistuple)  # Output: ('apple', 'banana', 'cherry', 'apple', 'cherry')

3. Tuple Length

To determine how many items are in a tuple, you can use the len() function.

Example: Print the Number of Items

python
1thistuple = ("apple", "banana", "cherry")
2print(len(thistuple))  # Output: 3

4. Creating a Tuple with One Item

To create a tuple with only one item, you must include a trailing comma after the item. Without the comma, Python will not recognize it as a tuple.

Example: One Item Tuple

python
1thistuple = ("apple",)  # This is a tuple
2print(type(thistuple))  # Output: <class 'tuple'>
3
4# NOT a tuple
5thistuple = ("apple")  # This is a string, not a tuple
6print(type(thistuple))  # Output: <class 'str'>

5. Tuple Items - Data Types

Tuple items can be of any data type. You can have tuples containing strings, integers, booleans, and even other tuples.

Example: Different Data Types

python
1tuple1 = ("apple", "banana", "cherry")  # Tuple of strings
2tuple2 = (1, 5, 7, 9, 3)  # Tuple of integers
3tuple3 = (True, False, False)  # Tuple of booleans
4
5# A tuple with mixed data types
6mixed_tuple = ("abc", 34, True, 40, "male")

6. What is the Data Type of a Tuple?

From Python's perspective, tuples are defined as objects with the data type 'tuple'.

Example: Check the Data Type

python
1mytuple = ("apple", "banana", "cherry")
2print(type(mytuple))  # Output: <class 'tuple'>

7. The tuple() Constructor

You can also create a tuple using the tuple() constructor. This method is useful when you want to create a tuple from an iterable.

Example: Using the tuple() Method

python
1thistuple = tuple(("apple", "banana", "cherry"))  # Note the double round-brackets
2print(thistuple)  # Output: ('apple', 'banana', 'cherry')

Frequently Asked Questions