Python Tuples
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
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 tuplecount()
: returns the number of times an item appears in a tuplelen()
: 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..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete