{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extra List Slicing Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The list slicing syntax:\n", "\n", "`sample_list[ start : end : step]`\n", "\n", "`start` is inclusive, `end` is exclusive.\n", "\n", "The default `start` is `0`, default `end` is the end, default `step` is `1`.\n", "\n", "Given the following list, please finish the exercises:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "sample_list = [0,1,2,3,4,5,6,7,8,9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get the first item?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get the last item?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get the first 3 items?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get the last 3 items?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get a copy of the list?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get a copy of the list without first item?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we get a copy of the list without last item?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }