{ "cells": [ { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "orig_str = \"There once was a town named Blooth\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 1" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'T'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 2" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'h'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 3" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Teeoc a onnmdBot'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 4" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hol ea wtaswen rh'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[::-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 5" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'htoolB deman nwot a saw ecno erehT'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 6\n", "There are a couple of ways to solve this using slicing.\n", "Your answer may be perfectly valid as well." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Blooth'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orig_str[-6:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 7" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There once was a city named Blooth\n" ] } ], "source": [ "orig_str = orig_str[0:17] + \"city\" + orig_str[21:]\n", "print(orig_str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 8" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There once was a city named Blooth\n" ] } ], "source": [ "new_str = orig_str[:]\n", "print(new_str)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140352887728152" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(orig_str)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140352887728152" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(new_str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking a slice of the original string does not create a new object because strings are immutable and Python won't let you change the string without creating a new object on its own. Thus, to optimize memory, Python sets the original and new string to point to the same object in memory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 9" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['T', 'h', 'e', 'r', 'e', ' ', 'o', 'n', 'c', 'e', ' ', 'w', 'a', 's', ' ', 'a', ' ', 'c', 'i', 't', 'y', ' ', 'n', 'a', 'm', 'e', 'd', ' ', 'B', 'l', 'o', 'o', 't', 'h']\n" ] } ], "source": [ "orig_list = list(orig_str)\n", "print(orig_list)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "new_list = orig_list[:]" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140352771384648" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(orig_list)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140352771386504" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(new_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking a slice of the original list results in a new object because lists are mutable and can be modified without resulting in Python creating a new object on it's own." ] } ], "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.4.6" } }, "nbformat": 4, "nbformat_minor": 2 }