🌐
Real Python
realpython.com › python-sequences
Python Sequences: A Comprehensive Guide – Real Python
March 18, 2026 - Even though they’re different types with distinct characteristics, they have some common traits. You can summarize the characteristics that define a Python sequence as follows: A sequence is an iterable, which means you can iterate through it.
People also ask

What are the different types of sequences in Python?
Python provides several sequence types, including lists, tuples, and strings. Lists are mutable, indexed collections of objects. Tuples are immutable, indexed collections. Strings are immutable sequences of characters.
🌐
studysmarter.co.uk
studysmarter.co.uk › python sequence
Python Sequence: Explained & Examples | StudySmarter
How can I modify elements in a Python sequence?
To modify elements in a Python sequence, you can directly assign new values to the elements using their indices if the sequence type is mutable, such as a list. Use the syntax `sequence[index] = new_value`. Immutable sequences like tuples or strings require you to convert them to a mutable type, modify, and then convert back if needed.
🌐
studysmarter.co.uk
studysmarter.co.uk › python sequence
Python Sequence: Explained & Examples | StudySmarter
How can I iterate over a Python sequence?
You can iterate over a Python sequence using a `for` loop, which will process each element in the sequence in order. Alternatively, use list comprehensions for concise inline iteration, or the `map()` function for applying a function to all sequence items. Use `enumerate()` to access both index and value.
🌐
studysmarter.co.uk
studysmarter.co.uk › python sequence
Python Sequence: Explained & Examples | StudySmarter
🌐
StudySmarter
studysmarter.co.uk › python sequence
Python Sequence: Explained & Examples | StudySmarter
Definition of Python Sequence: A linear collection of elements in Python that supports iteration and indexing, maintaining element order. Common Python Sequence Types: Strings, lists, and tuples, each with unique properties such as mutability (lists) or immutability (strings and tuples).
🌐
Python Geeks
pythongeeks.org › python geeks › learn python › sequences in python with types and examples
Sequences in Python with Types and Examples - Python Geeks
June 9, 2021 - Sequences in Python - A sequence is a succession of values bound together by a container that reflects their type. Learn more about it.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python sequences
Python Sequences
March 27, 2025 - Summary: in this tutorial, you’ll learn about the Python sequences and their basic operations. A sequence is a positionally ordered collection of items.
🌐
Real Python
realpython.com › ref › glossary › sequence
sequence | Python Glossary – Real Python
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.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › sequence-and-series-in-python
Sequence and Series in Python - GeeksforGeeks
July 23, 2025 - This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python. An arithmetic sequence is a sequence of numbers in which the difference between consecutive terms is constant.
🌐
NxtWave
ccbp.in › blog › articles › sequence-in-python
Sequence in Python: Types, Methods & Examples
Python for sequence is ordered collections of elements. Each type functions for specific purposes and has unique traits. Here's a detailed guide to sequence data type in Python with examples.
Find elsewhere
🌐
Composingprograms
composingprograms.com › pages › 23-sequences.html
2.3 Sequences
Python includes several native data types that are sequences, the most important of which is the list. A list value is a sequence that can have arbitrary length. Lists have a large set of built-in behaviors, along with specific syntax to express those behaviors.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module2_EssentialsOfPython › SequenceTypes.html
Sequence Types — Python Like You Mean It
In Python, a sequence is any ordered collection of objects whose contents can be accessed via “indexing”. A sub-sequence can be accessed by “slicing” the sequence. You saw, in the required reading, that Python’s lists and strings are both examples of sequences.
🌐
Jsp
jsp.shiksha › index.php › portfolio › bcse101e-computer-programming-python › introduction-python › fundamental-concepts-python-programming › sequences-python
Sequences in Python :: Teaching Resource by JSP
August 11, 2024 - For example, for item in my_list iterates over each element in my_list. Sequences are versatile and powerful data structures in Python, allowing you to work with ordered collections of items. Understanding how to use sequences effectively is essential for writing efficient and readable Python code.
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › sequences in python (data structure categories #2)
Sequences in Python (Data Structure Categories #2)
June 25, 2024 - Let's start with the headline difference between the two terms: A Python sequence is an iterable that you can index using an integer.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › sequences in python
Sequences in Python | Guide To Sequences in Python With Examples
April 18, 2023 - For example, [1,22]*3 will evaluate to [1,22,1,22,1,22]. X in NewSeq returns True if x is an element of NewSeq, otherwise False. This statement can be negated with either not (x in NewSeq) or x, not in NewSeq.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Medium
medium.com › @gauravverma.career › sequence-in-python-705f9904313f
Sequence in Python. Sequence is any ordered set in python… | by Gaurav Verma | Medium
December 7, 2025 - Sequence in Python Sequence is any ordered set in python like List, Tuple, String List: List is mutable and can store any type of object. Tuple: Tuple is immutable and can store any type of …
Top answer
1 of 3
18

Brief introduction to typing in Python

Skip ahead if you know what structural typing, nominal typing and duck typing are.

I think much of the confusion arises from the fact that typing was a provisional module between versions 3.5 and 3.6. And was still subject to change between versions 3.7 and 3.8. This means there has been a lot of flux in how Python has sought to deal with typing through type annotations.

It also doesn't help that python is both duck-typed and nominally typed. That is, when accessing an attribute of an object, Python is duck-typed. The object will only be checked to see if it has an attribute at runtime, and only when immediately requested. However, Python also has nominal typing features. Nominal typing is where one type is declared to be a subclass of another. This can be through inheritance, or with the register() method of ABCMeta.

typing originally introduced its types using the idea of nominal typing. As of 3.8 it is trying to allow for the more pythonic structural typing. Structural typing is related to duck-typing, except that it is taken into consideration at "compile time" rather than runtime. For instance, when a linter is trying to detect possible type errors -- such as if you were to pass a dict to a function that only accepts sequences like tuples or list. With structural typing, a class B should be considered a subtype of A if it implements the all the methods of A, regardless of whether it has been declared to be a subtype of A (as in nominal typing).

Answer

sequences (little s) are a duck type. A sequence is any ordered collection of objects that provides random access to its members. Specifically, if it defines __len__ and __getitem__ and uses integer indices between 0 and n-1 then it is a sequence. A Sequence (big s) is a nominal type. That is, to be a Sequence, a class must be declared as such, either by inheriting from Sequence or being registered as a subclass.

A numpy array is a sequence, but it is not a Sequence as it is not registered as a subclass of Sequence. Nor should it be, as it does not implement the full interface promised by Sequence (things like count() and index() are missing).

It sounds like you want is a structured type for a sequence (small s). As of 3.8 this is possible by using protocols. Protocols define a set of methods which a class must implement to be considered a subclass of the protocol (a la structural typing).

from typing import Protocol
import numpy as np

class MySequence(Protocol):
    def __getitem__(self, index):
        raise NotImplementedError
    def __len__(self):
        raise NotImplementedError
    def __contains__(self, item):
        raise NotImplementedError
    def __iter__(self):
        raise NotImplementedError

def f(s: MySequence):
    for i in range(len(s)):
        print(s[i], end=' ')
    print('end')

f([1, 2, 3, 4]) # should be fine
arr: np.ndarray = np.arange(5)
f(arr) # also fine
f({}) # might be considered fine! Depends on your type checker

Protocols are fairly new, so not all IDEs/type checkers might support them yet. The IDE I use, PyCharm, does. It doesn't like f({}), but it is happy to consider a numpy array a Sequence (big S) though (perhaps not ideal). You can enable runtime checking of protocols by using the runtime_checkable decorator of typing. Be warned, all this does is individually check that each of the Protocols methods can be found on the given object/class. As a result, it can become quite expensive if your protocol has a lot of methods.

2 of 3
1

I think the most practical way to define a sequence in Python is 'A container that supports indexing with integers'.

The Wikipedia definition also holds:

a sequence is an enumerated collection of objects in which repetitions are allowed and order does matter.

To validate if an object is a sequence, I would emulate the logic from the Sequence Protocol:

hasattr(test_obj, "__getitem__") and not isinstance(test_obj, collections.abc.Mapping) 
🌐
TechVidvan
techvidvan.com › tutorials › python-sequences
Python Sequences - Types, Operations, and Functions - TechVidvan
January 13, 2020 - In this article, we have seen the Python sequences. We learned about the six different types of sequences: strings, lists, tuples, byte sequences, byte arrays, and range objects. We saw examples of each sequence on how to create them, then learned about the operations and functions associated with ...
🌐
Patrickwalls
patrickwalls.github.io › mathematicalpython › python › sequences
Sequences - Mathematical Python
It is very inefficient to create a sequence by manually typing the numbers. For example, simply typing out the numbers from 1 to 20 takes a long time! numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] print(numbers) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Python has a beautiful syntax for creating lists called list comprehensions.
🌐
Mimo
mimo.org › glossary › programming-concepts › sequence
Sequence: Concept, Behavior, and Examples
This predictability is what allows algorithms, loops, and string operations to work consistently across languages. In Python, sequences include data types like lists, tuples, ranges, and strings. ... In JavaScript and TypeScript, arrays are the main form of sequence, while strings can also be treated as character sequences: ... In Swift, arrays and strings both conform to the Sequence protocol, meaning you can iterate over them with ...
🌐
DataFlair
data-flair.training › blogs › python-sequence
Python Sequence and Collections - Operations, Functions, Methods - DataFlair
April 21, 2026 - In Python, the umbrella term sequence covers any ordered series that supports length, slicing, and iteration—lists, tuples, strings, and ranges all qualify. A collection is broader, meaning any container that groups objects, ordered or not, ...
🌐
Wikibooks
en.wikibooks.org › wiki › Python_Programming › Sequences
Python Programming/Sequences - Wikibooks, open books for an open world
As demonstrated above, if the first and second number is omitted, then they take their default values, which are 0 and n-1 respectively, corresponding to the beginning and end of the sequence respectively (in this case). Note also that the brackets are inclusive on the left but exclusive on the right: in the first example above with [3:9] the character at index 3, 'l', is included while the character at index 9, 'r', is excluded.