{ "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.5 Sequence Slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Lists are definitely more flexible than the arrays of other programming languages!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a Slice with Starting and Ending Indices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [2, 3, 5, 7, 11, 13, 17, 19]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[2:6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A slice is a **new** list containing copies of the elements of the original list. The original list is not changed. Note that the slice contains elements whose indices go from the starting index up to but not including the ending index." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a Slice with Only an Ending Index" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[:6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[0:6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a Slice with Only a Starting Index" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[6:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[6:len(numbers)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a Slice with No Indices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This is a **copy** of the original sequence." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing with Steps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Since the the start and end indices are omitted, they are assumed to be 0 and `len(numbers)`, respectively." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing with Negative Indices and Steps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A negative step selects slices in reverse order." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[::-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[-1:-9:-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modifying Lists Via Slices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You can modify a list by assigning to a slice of it. The rest of the list is unchanged. (Remember that a tuple is immutable, so you can't do this with tuples.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [2, 3, 5, 7, 11, 13, 17, 19]\n", "numbers[0:3] = ['two', 'three', 'five']\n", "\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We remove the first three elements from the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[0:3] = []\n", "\n", "numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [2, 3, 5, 7, 11, 13, 17, 19]\n", "numbers[::2] = [100, 100, 100, 100]\n", "\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The built-in `id()` function returns Python's internal identifier for an object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Delete all the elements of list `numbers`, leaving the existing list empty." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[:] = []\n", "\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### It's still the same list object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### But now we assign a **new** empty list to `numbers`. Now it's a different list, since it has a different internal identifier." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = []\n", "\n", "numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE:** Create a list called `numbers` containing the values from 1 through 15." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = []\n", "for i in range(1, 16):\n", " numbers += [i]\n", " \n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Perform the following slicing operations consecutively:\n", "#### (a) Select `number`’s even integers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[1::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (b) Replace the elements at indices 5 through 9 with zeroes. Then show the resulting list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[5:10] = [0, 0, 0, 0, 0]\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (c) Keep only the first five elements. Then show the resulting list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers[5:] = []\n", "numbers" ] }, { "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 2023 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 }