Python Basics
Variables & Comments Math Operators Sequence Data Types Strings Lists & Tuples Logical Operators Conditional Statements Loops Functions
Data Wrangling
Pandas Introduction Importing Data Select, Drop & Rename Joining Data Cleaning Data Add Columns Dates
Coming Soon
Machine Learning
Coming Soon
Other Tutorials & Content
Python Data Science Reference Blog

Python Sequence Data Types

Python Sequence Data Types come in several forms including Strings, Lists and Tuples. If we think of regular variables like boxes or containers that hold some information then imagine if we placed a line or queue of people inside one of these boxes. Each person in the line takes up a unique position in the line and we can refer to the line of people as a whole by the name we’ve given to the box or we can refer to an individual person by the name of the box and the position that they are standing (1st, 2nd, 3rd etc). Sequence data types are very much like this.

A sequence data type is assigned a variable name just like a regular variable but we can then define what chunks of data or elements to add and what position in the line they should occupy.

Each position within the sequence data type is labelled with a number, know as the Index, which starts at 0 for the 1st position (I know, not very intuitive). Let’s look at an example.

Python Lists

Here we have a variable called “week_days” that contains a sequence data type. The name for each weekday is in it’s own position within the variable’s structure and therefore has it’s own index.

If we call the name of the variable and an index number inside brackets we will get the element that is stored at this position. For example if we call week_days[1] we will get the value “Tuesday”. This is known as Indexing.

> 'Tuesday'

As part of indexing we can use index ranges which means that instead of referencing a single position between brackets, [1], we refer to a range of positions starting at one position and ending at another separated by a colon [1:3]. This is known as Slicing. For example week_days[2:4] will return the weekdays stored at positions 2 and 3.

> ['Wednesday', 'Thursday']

Note that closing values in index ranges are not inclusive so index range [2:4] does not represent position 2, 3 and 4.

All sequence data types allow us to use the Python’s inbuilt LEN function, which will give us the length of a sequence type variable. For example our week_days variable will have length 5 as it contain 5 elements.

> 5

IndexError: index out of range

One of the common errors experienced with sequence data types is the “index out of range” error. This arises if we refer to an index that doesn’t exist, in other words an index that is larger than one less than the number of elements in the variable. In the example above, the elements are stored in positions with indexes 0 to 4. There is no element with index 5 and if we try to call that index we will get an “index out of range” error.

> IndexError: index out of range