{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "# Intro to sequences: strings, lists, and tuples" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "A *sequence type* in Python is an *ordered* collection of objects. The term 'ordered' here means that we can retrieve the first object in the sequence, and the second object, and so on.\n", "\n", "There are three main sequence types in Python: strings, lists, and tuples.\n", "\n", "- A *string* is an immutable sequence of characters\n", "- A *list* is a mutable sequence of objects (of any type)\n", "- A *tuple* is an immutable sequence of objects (of any type)\n", "\n", "The term *immutable* means that a value in the sequence **cannot** be changed directly; while *mutable* means that a value can be changed. We will see examples of this below.\n", "\n", "## Finding the length of a sequence\n", "The *len* function can be used to find the length of any sequence." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "word = 'hello'\n", "len(word)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "## Sequence indexing\n", "\n", "Each item in a sequence has a numbered index, which begins at 0. For example, the string \"hello\" has the following indices:\n", "\n", "\n", "\n", "
Index | 0 | 1 | 2 | 3 | 4 | \n", "
Character | h | e | l | l | o | \n", "