{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5.2 Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A list is a fundamental Python data structure that is similar to an array in other programming languages. But, as you'll soon see, it is much more flexible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating a List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You can create a list simply by listing its elements. The elements are enclosed in **square brackets**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = [-45, 6, 0, 72, 1543]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Unlike arrays in many other languages, a Python list can contain elements of different datatypes. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "person = ['Mary', 'Brown', 25, 'Dept. 12', True, 120_000.00]\n", "\n", "person" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing Elements of a List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use **integer** values as subcripts to access individual elements of a list. Enclose the subscript in square brackets (the **subscript operator**). The subscript value 0 access the first element." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Determining a List’s Length " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing Elements from the End of the List with Negative Indices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indices Must Be Integers or Integer Expressions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[a + b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists Are Mutable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[4] = 17\n", "\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Some Sequences Are Immutable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Whereas lists are **mutable** sequences, other sequences such as strings are **immutable**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 'hello'\n", "\n", "s[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s[0] = 'H'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Attempting to Access a Nonexistent Element" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using List Elements in Expressions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c[0] + c[1] + c[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Appending to a List with the `append()` method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_list = []" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for number in range(1, 6):\n", " a_list.append(number)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Appending to a List with +=" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_list = []" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for number in range(1, 6):\n", " a_list += [number]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What's on the right of the `+=` operator must be an iterable, such as another list. Note that we appended `[number]`, not simply `number`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "letters = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A string is iterable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "letters += 'Python'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "letters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concatenating Lists with +" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `+` operator **concatenates** two sequences, such as two lists or two strings. The result is a **new** list or string. The original list or string is unchanged. Both sequences must be of the same datatype (such as list or string)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list1 = [10, 20, 30]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list2 = [40, 50]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "concatenated_list = list1 + list2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "concatenated_list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "greeting = 'Hello'\n", "location = 'world'\n", "\n", "message = greeting + ', ' + location + '!'\n", "message" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using `for` and `range` to Access List Indices and Values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(concatenated_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "range(len(concatenated_list))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "concatenated_list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(len(concatenated_list)): \n", " print(f'{i}: {concatenated_list[i]}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use comparison operators to compare lists element by element." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = [1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a == b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a == c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a < c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List `a` has fewer elements than list `c`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c >= b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE** Create a function `cube_list` that cubes each element of a list. Call the function with the list `numbers` containing 1 through 10. Show `numbers` after the call." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cube_list(lst):\n", " for i in range(len(lst)):\n", " lst[i] **= 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lsst = []\n", "for i in range(1,11):\n", " lsst += [i]\n", " \n", "lsst" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cube_list(lsst)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lsst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE** Use an empty list named `characters` to convert the string `'Birthday'` into a list of its characters. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "characters = []\n", "characters += 'Birthday'\n", "\n", "characters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }