Python Tuples

In Python, a tuple is a data structure that stores an ordered collection of items. Unlike a list, a tuple is immutable, which means that the items in a tuple cannot be modified. In Python, tuples are represented by round brackets () and the items in a tuple are separated by commas.

Creating a Tuple

To create a tuple in Python, use round brackets () and separate the items in the tuple with commas. Here are some examples of creating tuples in Python:

# Create an empty tuple
my_tuple = ()

# Create a tuple of numbers
numbers = (1, 2, 3, 4, 5)

# Create a tuple of strings
colors = ("red", "green", "blue")

# Create a tuple of mixed data types
mixed = (1, "two", 3.0, (4, 5))

As you can see, a tuple can store items of any data type, including other tuples.

Accessing and Modifying Tuple Items

In Python, you can access the items in a tuple using indexing. To access an item in a tuple, use the square bracket notation [] and specify the index of the item you want to access. In Python, the index of the first item in a tuple is 0, and the index of the last item in a tuple is -1. Here are some examples of accessing items in a tuple:

# Access the first item in a tuple
print(numbers[0])  # Output: 1

# Access the last item in a tuple
print(numbers[-1])  # Output: 5

# Access a range of items in a tuple
print(numbers[1:3])  # Output: (2, 3)

As you can see, you can access the items in a tuple using indexing, just like you would with a list. However, unlike a list, you cannot modify the items in an existing tuple, because tuples are immutable. If you try to modify a tuple, you will get a TypeError. For example:

# Modify an item in a tuple (this will cause an error)
numbers[0] = 10
# Output: TypeError: 'tuple' object does not support item assignment

In contrast, you can modify the items in a list using indexing and the assignment operator =. For example:

# Modify an item in a list
numbers[0] = 10
print(numbers)  # Output: [10, 2, 3, 4, 5]

You can access the items in a tuple using indexing, but you cannot modify the items in an existing tuple.

Common Tuple Methods

In Python, tuples have a few built-in methods that allow you to access and manipulate the items in a tuple. Here are some common tuple methods that you might find useful:

  • index(): returns the index of an item in a tuple
  • count(): returns the number of times an item appears in a tuple
  • len(): returns the number of items in a tuple

Here are some examples of using these tuple methods:

# Get the index of an item in a tuple
print(numbers.index(3))  # Output: 2

# Count the number of times an item appears in a tuple
print(numbers.count(5))  # Output: 1

# Get the number of items in a tuple
print(len(numbers))  # Output: 5

Tuple methods in Python provide a way to access and manipulate the items in a tuple, but you cannot modify the items in an existing tuple.

Conclusion

Tuples are a useful data structure in Python that allows you to store and manipulate collections of items. Tuples are similar to lists, but they are immutable, which means that you cannot modify the items in a tuple.

Tuples are represented by round brackets (), the items in a tuple are separated by commas, and you can access the items in a tuple using indexing. Although tuples and lists have some similarities, they are used in different situations depending on your specific needs.

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete