sequence
In Python, a sequence is a collection of ordered objects where each object has an associated integer index that defines its position in the sequence. Sequences allow you to store multiple values in a single container object. You can access each value by its position (index) within the sequence.
Lists, tuples, and strings are common examples of sequences in Python. Each of these data types supports indexing, slicing, and iteration, making them versatile and powerful tools for managing data collections.
Sequences are fundamental in Python programming because they provide a way to handle collections of data efficiently. With sequences, you can perform operations like concatenation, repetition, and membership testing. You can also use built-in functions like len()
, min()
, and max()
to perform common tasks.
Examples
Here’s a quick example of using a list, which is a sequence data type in Python:
>>> # Creating a list
>>> fruits = ["apple", "banana", "cherry", "grape"]
>>> # Accessing items by index
>>> fruits[0]
'apple'
>>> # Updating an existing item
>>> fruits[3] = "red grape"
>>> # Getting a slice for the list
>>> fruits[1:3]
['banana', 'cherry']
>>> # Adding an item to the list
>>> fruits.append("mango")
>>> # Iterate over the list
>>> for fruit in fruits:
... print(fruit)
...
apple
banana
cherry
red grape
mango
In this example, you perform common sequence operations, such as creating a new list, accessing its items by index, assigning new values to a given item, retrieving a portion of the list, adding items to the list, and iterating over its items.
Note that you can only update values and add new values to mutable sequences like lists. Tuples and strings are immutable and don’t support in-place changes.
Here are some additional examples of using a string, another Python sequence:
>>> # Creating a string
>>> text = "apple"
>>> # Accessing characters by index
>>> text[0]
'a'
>>> # Getting a slice of the string
>>> text[1:4]
'ppl'
>>> # Iterating over the string
>>> for char in text:
... print(char)
...
a
p
p
l
e
>>> # Trying to update a character
>>> text[0] = "A"
Traceback (most recent call last):
...
TypeError: 'str' object does not support item assignment
Strings are immutable. They support indexing, slicing, iteration as well as other common sequence operations. However, the don’t support item assignment as lists do.
Related Resources
Tutorial
Python Sequences: A Comprehensive Guide
This tutorial dives into Python sequences, which is one of the main categories of data types. You'll learn about the properties that make an object a sequence and how to create user-defined sequences.
For additional information on related topics, take a look at the following resources:
- Python's list Data Type: A Deep Dive With Examples (Tutorial)
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Python Sequences: A Comprehensive Guide (Quiz)
- Exploring Python's tuple Data Type With Examples (Course)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
By Leodanis Pozo Ramos • Updated April 25, 2025