{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表（list）是什么\n",
    "**Python的列表：list。**list是一种有序的集合，可以随时添加和删除其中的元素。\n",
    "\n",
    "列表由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或\n",
    "所有家庭成员姓名的列表；也可以将任何东西加入列表中，其中的元素之间可以没有任何关系。\n",
    "鉴于列表通常包含多个元素，给列表指定一个表示复数的名称（如letters、 digits或names）是\n",
    "个不错的主意。\n",
    "\n",
    "比如，列出班里所有同学的名字，就可以用一个list表示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "classmates = ['Michael', 'Bob', 'Tracy']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用len()函数可以获得list元素的个数：\n",
    "len(classmates)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.1 **访问列表元素**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表是有序集合，因此要访问列表的任何元素，只需将该元素的位置或索引告诉Python即可。<br/>\n",
    "要访问列表元素，可指出列表的名称，再指出元素的索引，并将其放在方括号内。<br/>\n",
    "例如，下面的代码从列表bicycles中提取第一款自行车："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trek\n"
     ]
    }
   ],
   "source": [
    "bicycles = ['trek', 'cannondale', 'redline', 'specialized']\n",
    "print(bicycles[0].title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用<font color=\"#dd0000\">**索引**</font>访问list中的元素，索引是从$0$开始,而不是1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Michael'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-f871500fb98d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 当索引超出了范围时，Python会报一个IndexError错误，所以，要确保索引不要越界，记得最后一个元素的索引是len(classmates) - 1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mclassmates\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 当索引超出了范围时，Python会报一个IndexError错误，所以，要确保索引不要越界，记得最后一个元素的索引是len(classmates) - 1\n",
    "classmates[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果要**取最后一个元素**，除了计算索引位置外，还可以用$-1$做索引，直接获取最后一个元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tracy'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "取倒数第2个、倒数第3个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bob'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-64527de8b24c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mclassmates\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "classmates[-4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My first bicycle was a Trek.\n"
     ]
    }
   ],
   "source": [
    "bicycles = ['trek', 'cannondale', 'redline', 'specialized']\n",
    "message = \"My first bicycle was a \" + bicycles[0].title() + \".\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **从列表中插入元素**<br/> \n",
    "list是一个可变的有序表，所以，可以往list中追加元素到末尾："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy', 'Adam']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.append('Adam')\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以把元素插入到指定的位置，比如索引号为1的位置："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以把元素插入到指定的位置，比如索引号为1的位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.insert(1, 'Jack')\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **从列表中删除元素**<br/>\n",
    "你经常需要从列表中删除一个或多个元素,你可以根据位置或值来删除列表中的元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "删除list**末尾**的元素，用$pop()$方法：\n",
    "\n",
    "有时候，你要将元素从列表中删除，并接着使用它的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aaa = classmates.pop()\n",
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Adam'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aaa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要删除指定位置的元素，用$pop(i)$方法，其中i是索引位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates = ['Michael', 'Bob', 'Tracy']\n",
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bob\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Michael', 'Tracy']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = classmates.pop(1)\n",
    "print(a)\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用**del语句**删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n",
      "['yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "print(motorcycles)\n",
    "del motorcycles[0]\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n",
      "['honda', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "print(motorcycles)\n",
    "del motorcycles[1]\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first motorcycle I owned was a Honda.\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "first_owned = motorcycles.pop(0)\n",
    "print('The first motorcycle I owned was a ' + first_owned.title() + '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**根据值删除元素**<br/>\n",
    "有时候，你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值，可使\n",
    "用方法remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n",
      "\n",
      "A Ducati is too expensive for me.\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']\n",
    "too_expensive = 'ducati'\n",
    "motorcycles.remove(too_expensive)\n",
    "print(motorcycles)\n",
    "print(\"\\nA \" + too_expensive.title() + \" is too expensive for me.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "motorcycles.index('honda')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：<br/>**方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次，就需要\n",
    "使用循环来判断是否删除了所有这样的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 1, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,1,3]\n",
    "a.remove(1)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,1,3]\n",
    "x = 1\n",
    "for x in a:\n",
    "    a.remove(x)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**把某个元素替换成别的元素，可以直接赋值给对应的索引位置：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Tracy']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[1] = 'Sarah'\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "list里面的**元素的数据类型**也可以不同："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = ['Apple', 123, True]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "list元素也可以是另一个list，比如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = ['python', 'java', ['asp', 'php'], 'scheme']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'asp'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[2][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "s只有4个元素，其中s[2]又是一个list，拆开写："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 'java', ['asp', 'php'], 'scheme']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = ['asp', 'php']\n",
    "s = ['python', 'java', p, 'scheme']\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要拿到'php'可以写$p[1]$或者$s[2][1]$，因此$s$可以看成是一个二维数组，类似的还有三维、四维……数组，不过很少用到。\n",
    "\n",
    "如果一个list中一个元素也没有，就是一个空的list，它的长度为$0$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'asp'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[2][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " # 组织列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在你创建的列表中，元素的排列顺序常常是无法预测的，因为你并非总能控制用户提供数据\n",
    "的顺序。这虽然在大多数情况下都是不可避免的，但你经常需要以特定的顺序呈现信息。 有时候，\n",
    "你希望保留列表元素最初的排列顺序，而有时候又需要调整排列顺序。 Python提供了很多组织列\n",
    "表的方式，可根据具体情况选用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用方法 sort()对列表进行永久性排序**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['audi', 'bmw', 'subaru', 'toyota']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "cars.sort()\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['audi', 'bmw', 'subaru', 'toyota']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你还可以按与字母顺序相反的顺序排列列表元素，为此，只需向sort()方法传递参数\n",
    "reverse=True。下面的示例将汽车列表按与字母顺序相反的顺序排列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['toyota', 'subaru', 'bmw', 'audi']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "cars.sort(reverse=True)\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用函数 sorted()对列表进行临时排序**<br/>\n",
    "要保留列表元素原来的排列顺序，同时以特定的顺序呈现它们，可使用函数sorted()。<br/>函数\n",
    "sorted()让你能够按特定顺序显示列表元素，同时不影响它们在列表中的原始排列顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is the original list:\n",
      "['bmw', 'audi', 'toyota', 'subaru']\n",
      "\n",
      "Here is the sorted list:\n",
      "['audi', 'bmw', 'subaru', 'toyota']\n",
      "\n",
      "Here is the original list again:\n",
      "['bmw', 'audi', 'toyota', 'subaru']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "print(\"Here is the original list:\")\n",
    "print(cars)\n",
    "print(\"\\nHere is the sorted list:\")\n",
    "print(sorted(cars))\n",
    "print(\"\\nHere is the original list again:\")\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**倒着打印列表**\n",
    "要反转列表元素的排列顺序，可使用方法reverse()。 <br/>假设汽车列表是按购买时间排列的，<br/>\n",
    "可轻松地按相反的顺序排列其中的汽车："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bmw', 'audi', 'toyota', 'subaru']\n",
      "['subaru', 'toyota', 'audi', 'bmw']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "print(cars)\n",
    "cars.reverse()\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意， reverse()不是指按与字母顺序相反的顺序排列列表元素，而只是反转列表元素的排\n",
    "列顺序：<br/>\n",
    "['bmw', 'audi', 'toyota', 'subaru']<br/>\n",
    "['subaru', 'toyota', 'audi', 'bmw']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法reverse()永久性地修改列表元素的排列顺序，<br/>但可随时恢复到原来的排列顺序，为此\n",
    "只需对列表再次调用reverse()即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 遍历整个列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alice\n",
      "david\n",
      "carolina\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina']\n",
    "for magician in magicians:\n",
    "    print(magician)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建数值列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "需要存储一组数字的原因有很多，例如，在游戏中，需要跟踪每个角色的位置，还可能需要<br/>\n",
    "跟踪玩家的几个最高得分。在数据可视化中，处理的几乎都是由数字（如温度、距离、人口数量、<br/>\n",
    "经度和纬度等）组成的集合。\n",
    "列表非常适合用于存储数字集合，而Python提供了很多工具，可帮助你高效地处理数字列表。<br/>\n",
    "明白如何有效地使用这些工具后，即便列表包含数百万个元素，你编写的代码也能运行得很好。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用函数 range()**<br/>\n",
    "Python函数range()让你能够轻松地生成一系列的数字。<br/>例如，可以像下面这样使用函数\n",
    "range()来打印一系列的数字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for value in range(1,5):\n",
    "    print(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用 range()创建数字列表**<br/>\n",
    "要创建数字列表，可使用函数list()将range()的结果直接转换为列表。\n",
    "<br/>如果将range()作为list()的参数，输出将为一个数字列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 6)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "numbers = list(range(1,6))\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用函数range()时，还可指定步长。<br/>例如，下面的代码打印1~10内的偶数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "even_numbers = list(range(2,11,2))\n",
    "print(even_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用函数range()几乎能够创建任何需要的数字集，例如，如何创建一个列表，其中包含前\n",
    "10个整数（即1~10）的平方呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "squares = []\n",
    "for value in range(1,11):   #1,2,3.....10\n",
    "    square = value**2\n",
    "    squares.append(square)\n",
    "print(squares)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**列表解析**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "squares = [value**2 for value in range(1,11)]\n",
    "print(squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 10)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对数字列表执行简单的统计计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有几个专门用于处理数字列表的Python函数。\n",
    "<br/>例如，你可以轻松地找出数字列表的最大值、最小值和总和："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\n",
    "print(min(digits))\n",
    "print(max(digits))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用列表的一部分\n",
    "<br/>处理列表的部分元素——Python称之为切片。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charles', 'martina', 'michael']\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(players[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['martina', 'michael', 'florence']\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(players[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你没有指定第一个索引， Python将自动从列表开头开始："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charles', 'martina', 'michael', 'florence']\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(players[:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要让切片终止于列表末尾，也可使用类似的语法。例如，<br/>如果要提取从第3个元素到列表末\n",
    "尾的所有元素，可将起始索引指定为2， 并省略终止索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['michael', 'florence', 'eli']\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(players[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出名单上的最后三名队员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['michael', 'florence', 'eli']\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(players[-3:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**遍历切片**<br/>\n",
    "如果要遍历列表的部分元素，可在for循环中使用切片。<br/>在下面的示例中，我们遍历前三名\n",
    "队员，并打印他们的名字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here are the first three players on my team:\n",
      "Charles\n",
      "Martina\n",
      "Michael\n"
     ]
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "print(\"Here are the first three players on my team:\")\n",
    "for player in players[:3]:\n",
    "    print(player.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 复制列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要复制列表，可创建一个包含整个列表的切片，方法是同时省略起始索引和终止索引（ [:]）。<br/>\n",
    "这让Python创建一个始于第一个元素，终止于最后一个元素的切片，即复制整个列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cake']\n",
      "\n",
      "My friend's favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cake']\n"
     ]
    }
   ],
   "source": [
    "my_foods = ['pizza', 'falafel', 'carrot cake']\n",
    "friend_foods = my_foods[:]\n",
    "# friend_foods = my_foods\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "\n",
    "print(\"\\nMy friend's favorite foods are:\")\n",
    "print(friend_foods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['pizza', 'falafel', 'carrot cake']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_foods[0]='aaa'\n",
    "friend_foods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = a\n",
    "b[0] = \"a\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cake', 'cannoli']\n",
      "\n",
      "My friend's favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cake', 'ice cream']\n"
     ]
    }
   ],
   "source": [
    "my_foods = ['pizza', 'falafel', 'carrot cake']\n",
    "friend_foods = my_foods[:]\n",
    "my_foods.append('cannoli')\n",
    "friend_foods.append('ice cream')\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "print(\"\\nMy friend's favorite foods are:\")\n",
    "print(friend_foods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tuple\n",
    "**tuple(元组)**是另一种有序列表。<br />\n",
    "tuple和list非常类似，但是tuple一旦初始化就不能修改，比如同样是列出同学的名字:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "classmates = ('Michael', 'Bob', 'Tracy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Michael', 'Bob', 'Tracy')"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-58-9ba31880f6d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mclassmates\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'aaa'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "classmates[0] = 'aaa'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里**classmates**这个$tuple$不能变了，它也没有**append()，insert()**这样的方法。<br />\n",
    "其他获取元素的方法和list是一样的，你可以正常地使用**classmates[0]，classmates[-1]**，但不能赋值成另外的元素。<br/>\n",
    "<font color=\"#dd0000\">**不可变的tuple有什么意义？**</font>:因为tuple不可变，所以代码更安全。如果可能，能用tuple代替list就尽量用tuple。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tuple的陷阱：**当你定义一个tuple时，在定义的时候，tuple的元素就必须被确定下来，比如：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1, 2)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义一个空的tuple，可以写成()："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ()\n",
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义的不是tuple，er而是1这个数！ 这是因为括号()既可以表示tuple，又可以表示数学公式中的小括号，这就产生了歧义\n",
    "t = (1)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此，Python规定，这种情况下，按小括号进行计算，计算结果自然是1。\n",
    "所以，只有1个元素的tuple定义时必须加一个逗号,，来消除歧义："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 所以，只有1个元素的tuple定义时必须加一个逗号,，来消除歧义：\n",
    "t = (1,)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**修改元组变量**<br/>\n",
    "虽然不能修改元组的元素，但可以给存储元组的变量赋值。<br/>因此，如果要修改前述矩形的尺\n",
    "寸，可重新定义整个元组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original dimensions:\n",
      "200\n",
      "50\n",
      "\n",
      "Modified dimensions:\n",
      "400\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "dimensions = (200, 50)\n",
    "print(\"Original dimensions:\")\n",
    "for dimension in dimensions:\n",
    "    print(dimension)\n",
    "    \n",
    "dimensions = (400, 100)\n",
    "print(\"\\nModified dimensions:\")\n",
    "for dimension in dimensions:\n",
    "    print(dimension)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**“可变的”tuple：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'b', ['A', 'B'])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('a', 'b', ['A', 'B'])\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'b', ['X', 'Y'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar_1 = t[2][0]\n",
    "t[2][0] = 'X'\n",
    "ar_2 = t[2][0]\n",
    "t[2][1] = 'Y'\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1899709865856"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar_0 = t[2][0]\n",
    "id(ar_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1899709865856"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(t[2][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1899709865856"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[2][0] = 'X'\n",
    "id(t[2][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1899710605216"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[2][0] = 'A'\n",
    "id(t[2][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**原始的内存结构：**</font><br />\n",
    "![alt text](002List-Tuple1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**把list的元素'A'和'B'修改为'X'和'Y'后：**</font><br/>\n",
    "![alt text](002List-Tuple2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "表面上看，tuple的元素确实变了，但其实变的不是tuple的元素，而是list的元素。<br/>\n",
    "tuple一开始指向的list并没有改成别的list，所以，tuple所谓的“不变”是说<font color=\"#dd0000\">**tuple的每个元素，指向永远不变。**</font>即指向'a'，就不能改成指向'b'，指向一个list，就不能改成指向其他对象，**但指向的这个list本身是可变的！**\n",
    "\n",
    "要创建一个内容也不变的tuple怎么做？那就必须保证tuple的每一个元素本身也不能变。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', ['X', 'Y'])\n"
     ]
    }
   ],
   "source": [
    "t = ('a', 'b', ['A', 'B'])\n",
    "ar_1 = t[2][0]\n",
    "t[2][0] = 'X'\n",
    "ar_2 = t[2][0]\n",
    "t[2][1] = 'Y'\n",
    "\n",
    "print(t)\n",
    "\n",
    "# t[2] = [\"t\",\"t\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-bf7849cfffe4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m\"t\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"t\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "t[2] = [\"t\",\"t\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 遍历元组中的所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "像列表一样，也可以使用for循环来遍历元组中的所有值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "dimensions = (200, 50)\n",
    "for dimension in dimensions:\n",
    "    print(dimension)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[200, 50]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[dimension for dimension in dimensions]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "squares = [value**2 for value in range(1,11)]\n",
    "print(squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id(a),id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=3\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=3\n",
    "id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c=2\n",
    "id(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = 'ab'\n",
    "id(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2 = 'ab'\n",
    "id(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s3 = 'a'\n",
    "id(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id('ab')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id('abc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id('abc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id(2.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "id(2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list 练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1. 在列表末尾添加元素<br/>**\n",
    "在列表中添加新元素时，最简单的方式是将元素附加到列表末尾。给列表附加元素时，它将<br/>\n",
    "添加到列表末尾。继续使用前一个示例中的列表，在其末尾添加新元素'ducati'："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "print(motorcycles)\n",
    "motorcycles.append('ducati')\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法append()让动态地创建列表易如反掌，例如，你可以先创建一个空列表，再使用一系列的\n",
    "append()语句添加元素。下面来创建一个空列表，再在其中添加元素'honda'、 'yamaha'和'suzuki'："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "motorcycles = []\n",
    "motorcycles.append('honda')\n",
    "motorcycles.append('yamaha')\n",
    "motorcycles.append('suzuki')\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用方法insert()可在列表的任何位置添加新元素。为此，你需要指定新元素的索引和值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "motorcycles.insert(0, 'ducati')\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = ['ab111','abc','ab1211','abcd']\t\t\t#创建列表并赋值\n",
    "x.sort(key=len,reverse=True)\t\t#使用sort()方法对变量x中元素进行长度逆序排序\n",
    "print(x)\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sorted??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.20"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "406.237px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
