๐ŸŒ
Real Python
realpython.com โ€บ python-sequences
Python Sequences: A Comprehensive Guide โ€“ Real Python
March 18, 2026 - You create an iterator from the list .points using the built-in iter() function. Although adding .__iter__() is not necessary to ensure a ShapePoints object is a sequence, most iterables implement this special method. You can visualize the shape with the help of the turtle module, which is the simplest way to display graphics using Pythonโ€™s standard library.
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ python-sequences
Python Sequences - Types, Operations, and Functions - TechVidvan
January 13, 2020 - The bytes() function in Python is used to return an immutable bytes sequence.
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 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
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
๐ŸŒ
Art of Problem Solving
artofproblemsolving.com โ€บ wiki โ€บ index.php โ€บ Sequence_(Python)
Sequence (Python) - AoPS Wiki
mySeq[i] will return the i'th character of mySeq. Sequences in Python are zero-indexed, so the first element has index 0, the second has index 1, and so on.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ advanced python โ€บ python sequences
Python Sequences
March 27, 2025 - So the first element is s[0] and the second element is s[1]. If the sequence s has n items, the last item is s[n-1]. Python has the following built-in sequence types: lists, bytearrays, strings, tuples, range, and bytes.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ python-sequence
Python Sequence and Collections - Operations, Functions, Methods - DataFlair
April 21, 2026 - A very common and useful function to pass a sequence to is len(). It returns the length of the Python sequence.
๐ŸŒ
Patrickwalls
patrickwalls.github.io โ€บ mathematicalpython โ€บ python โ€บ sequences
Sequences - Mathematical Python
Create a range object with the built-in function range(). The parameters a, b and step in range(a,b,step) are integers and the function creates an object which represents the sequence of integers from a to b (exclusively) incremented by step.
๐ŸŒ
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 - In the first article in the series, we discussed how an iterable is an object that can return its elements one at a time. With a sequence, we're going further. You can fetch an item based on its position in the sequence.
๐ŸŒ
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 - In this section, we shall demonstrate examples of sequences in python: โ€“ ยท Slicing and dicing, and indexing a string. ... Defining a list and indexing and appending it. ... Various operations on a tuple. var = "me", "you", "them", "Their" var = ("me", "you", "them", "Their") print var ... Apart from these, there are many other methods and functions available that can be implemented on strings, lists, tuple, etc.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
๐ŸŒ
Medium
pythonflood.com โ€บ python-sequences-weaving-the-story-of-order-and-logic-23dff398f83e
Python Sequences - Weaving the Story of Order and Logic. | by Rinu Gour | PythonFlood
July 11, 2023 - Sequences are iterable, meaning you can loop over the elements in a sequence one by one. Python provides several built-in functions to manipulate sequences, such as indexing, slicing, concatenation, and sorting.
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ python sequence
Python Sequence: Explained & Examples | StudySmarter
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.
๐ŸŒ
Python
docs.python.org โ€บ 2.0 โ€บ lib โ€บ typesseq.html
2.1.5 Sequence Types
The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted, use 0. If j is omitted, use len(s).
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ make-a-sequence
How to make a sequence - Python Morsels
April 3, 2023 - Sequences in Python are objects that can be looped over, can be indexed and have a length. So to make our own sequences, we need to make a class with instances that are iterable, indexable, and work with the built-in len function:
๐ŸŒ
Composingprograms
composingprograms.com โ€บ pages โ€บ 23-sequences.html
2.3 Sequences
This pattern is so common that Python has an additional control statement to process sequential data: the for statement. Consider the problem of counting how many times a value appears in a sequence. We can implement a function to compute this count using a while loop.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ sequence
Sequences in Python - Python Morsels
June 10, 2026 - Sign in to your Python Morsels account to save your screencast settings. Don't have an account yet? Sign up here. Sequences are a special type of iterable that can be indexed using square brackets ([...]) to get items by their position.
๐ŸŒ
NxtWave
ccbp.in โ€บ blog โ€บ articles โ€บ sequence-in-python
Sequence in Python: Types, Methods & Examples
Ans. The range() function generates a sequence of numbers, which is often used in loops for iterating over a specific range of values. ... Explore comprehensive Python course duration and fee information.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 documentation
The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, f(a, b, c) is a function call with three arguments, while f((a, b, c)) is a function call with a 3-tuple as the sole argument. Tuples implement all of the common sequence operations.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ c-api โ€บ sequence.html
Sequence Protocol โ€” Python 3.14.6 documentation
Return 1 if the object provides the sequence protocol, and 0 otherwise. Note that it returns 1 for Python classes with a __getitem__() method, unless they are dict subclasses, since in general it is impossible to determine what type of keys the class supports. This function always succeeds.
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) 
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-in-a โ€บ 0596100469 โ€บ ch04s06.html
Sequence Operations - Python in a Nutshell, 2nd Edition [Book]
July 14, 2006 - Sequence OperationsPython supports a variety of operations applicable to all sequences, including strings, lists, and tuples. Some sequence operations apply to all containers... - Selection from Python in a Nutshell, 2nd Edition [Book]
Author: Alex Martelli
Published: 2006
Pages: 734
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ sequence
sequence | Python Glossary โ€“ Real Python
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.