{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 条件语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "20 >= 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "your age is 20\n",
      "adult\n",
      "end if\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "if age >= 18:\n",
    "    print('your age is', age)\n",
    "    print('adult')\n",
    "print('end if')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "your age is 3\n",
      "teenager\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "age = 3\n",
    "if age >= 18:\n",
    "    print('your age is', age)\n",
    "    print('adult')\n",
    "else:\n",
    "    print('your age is', age)\n",
    "    print('teenager')\n",
    "    \n",
    "print(age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kid\n"
     ]
    }
   ],
   "source": [
    "age = 3\n",
    "if age >= 18:\n",
    "    print('adult')\n",
    "elif age >= 6:\n",
    "    print('teenager')\n",
    "else:\n",
    "    print('kid')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if语句执行有个特点，它是从上往下判断，如果在某个判断上是True，把该判断对应的语句执行后，就忽略掉剩下的elif和else，所以，请测试并解释为什么下面的程序打印的是teenager："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "teenager\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "if age >= 6:\n",
    "    print('teenager')\n",
    "elif age >= 18:\n",
    "    print('adult')\n",
    "else:\n",
    "    print('kid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正确写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adult\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "if age >= 6 and age <=18:\n",
    "    print('teenager')\n",
    "elif age >= 20:\n",
    "    print('adult')\n",
    "else:\n",
    "    print('kid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br />\n",
    "x是非零数值、非空字符串、非空list等，就判断为True，否则为False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, True, True)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0),bool(2),bool(-0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, True)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [];y=[1,2]\n",
    "bool(x),bool(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, True)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(''),bool(' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "if '  ':\n",
    "    print('True')\n",
    "else:\n",
    "    print('Fale')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "birth: 1999\n",
      "00前\n"
     ]
    }
   ],
   "source": [
    "s = input('birth: ')\n",
    "birth = int(s)\n",
    "if birth < 2000:\n",
    "    print('00前')\n",
    "else:\n",
    "    print('00后')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以首字母大写的方式打印其中的汽车名，<br/>但对于汽车名'bmw'，以全大写的方式打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Audi\n",
      "BMW\n",
      "Subaru\n",
      "Toyota\n"
     ]
    }
   ],
   "source": [
    "cars = ['audi', 'bmw', 'subaru', 'toyota']\n",
    "for car in cars:\n",
    "    if car == 'bmw':\n",
    "        print(car.upper())\n",
    "    else:\n",
    "        print(car.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**条件测试**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "大多数条件测试都将一个变量的当前值同特定值进行比较。最简单的条件测试检查变量的值\n",
    "是否与特定值相等：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car = 'bmw'\n",
    "car == 'bmw'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在Python中检查是否相等时区分大小写，例如，两个大小写不同的值会被视为不相等："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car = 'Audi'\n",
    "car == 'audi'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果大小写很重要，这种行为有其优点。但如果大小写无关紧要，而只想检查变量的值，<br/>可\n",
    "将变量的值转换为小写，再进行比较："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car = 'Audi'\n",
    "car.lower() == 'audi'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Audi'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**检查是否不相等**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要判断两个值是否不等，可结合使用惊叹号和等号**（ !=）**，其中的惊叹号表示不，在很多编\n",
    "程语言中都如此"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hold the anchovies!\n"
     ]
    }
   ],
   "source": [
    "requested_topping = 'mushrooms'\n",
    "if requested_topping != 'anchovies':\n",
    "    print(\"Hold the anchovies!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查数值非常简单，例如，下面的代码检查一个人是否是18岁："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age = 18\n",
    "age == 18"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查两个数字是否不等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "That is not the correct answer. Please try again!\n"
     ]
    }
   ],
   "source": [
    "answer = int(input())\n",
    "if answer != 42:\n",
    "    print(\"That is not the correct answer. Please try again!\")\n",
    "else:\n",
    "    print('your input is 42')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**条件语句中可包含各种数学比较，如小于、小于等于、大于、大于等于：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age = 19\n",
    "age < 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age <= 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age > 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age >= 21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**检查多个条件**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用and检查多个条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 = 22\n",
    "age_1 = 18\n",
    "age_0 >= 21 and age_1 >= 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1 = 22\n",
    "age_0 >= 21 and age_1 >= 21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用or检查多个条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 = 22\n",
    "age_1 = 18\n",
    "age_0 >= 21 or age_1 >= 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 = 18\n",
    "age_0 >= 21 or age_1 >= 21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**检查特定值是否包含在列表中**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'onions', 'pineapple']\n",
    "'mushrooms' in requested_toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " 'pepperoni' in requested_toppings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**检查特定值是否不包含在列表中**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marie, you can post a response if you wish.\n"
     ]
    }
   ],
   "source": [
    "banned_users = ['andrew', 'carolina', 'david']\n",
    "user = 'marie'\n",
    "if user not in banned_users:\n",
    "    print(user.title() + \", you can post a response if you wish.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**简单的 if 语句**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are old enough to vote!\n"
     ]
    }
   ],
   "source": [
    "age = 19\n",
    "if age >= 18:\n",
    "    print(\"You are old enough to vote!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are old enough to vote!\n",
      "Have you registered to vote yet?\n"
     ]
    }
   ],
   "source": [
    "age = 19\n",
    "if age >= 18:\n",
    "    print(\"You are old enough to vote!\")\n",
    "print(\"Have you registered to vote yet?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**if-else 语句**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sorry, you are too young to vote.\n",
      "Please register to vote as soon as you turn 18!\n"
     ]
    }
   ],
   "source": [
    "age = 17\n",
    "if age >= 18:\n",
    "    print(\"You are old enough to vote!\")\n",
    "    print(\"Have you registered to vote yet?\")\n",
    "else:\n",
    "    print(\"Sorry, you are too young to vote.\")\n",
    "    print(\"Please register to vote as soon as you turn 18!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**if-elif-else 结构**<br/>\n",
    "经常需要检查超过两个的情形，为此可使用Python提供的if-elif-else结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "if age < 4:\n",
    "    print(\"Your admission cost is $0.\")\n",
    "elif age < 18:\n",
    "    print(\"Your admission cost is $5.\")\n",
    "elif age < 70:\n",
    "    print(\"Your admission cost is $10.\")\n",
    "else:\n",
    "    print(\"Your admission cost is $0.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "if age < 4:\n",
    "    price = 0\n",
    "elif age < 18:\n",
    "    price = 5\n",
    "else:\n",
    "    price = 10\n",
    "print(\"Your admission cost is $\" + str(price) + \".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可根据需要使用任意数量的elif代码块， 例如，假设前述游乐场要给老年人打折，可再添加<br/>\n",
    "一个条件测试，判断顾客是否符合打折条件。下面假设对于65岁（含）以上的老人，<br/>可以半价（即\n",
    "5美元）购买门票："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "if age < 4:\n",
    "    price = 0\n",
    "elif age < 18:\n",
    "    price = 5\n",
    "elif age < 65:\n",
    "    price = 10\n",
    "else:\n",
    "    price = 5\n",
    "print(\"Your admission cost is $\" + str(price) + \".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**省略 else 代码块**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "if age < 4:\n",
    "    price = 0\n",
    "elif age < 18:\n",
    "    price = 5\n",
    "elif age < 65:\n",
    "    price = 10\n",
    "elif age >= 65:\n",
    "    price = 5\n",
    "print(\"Your admission cost is $\" + str(price) + \".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**测试多个条件**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if-elif-else结构功能强大，但仅适合用于只有一个条件满足的情况：遇到通过了的测试后，\n",
    "Python就跳过余下的测试。<br/>\n",
    "有时候必须检查你关心的所有条件。在这种情况下，应使用一系列不包含elif和else<br/>\n",
    "代码块的简单if语句。在可能有多个条件为True，且你需要在每个条件为True时都采取相应措施\n",
    "时，适合使用这种方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "Adding extra cheese.\n",
      "\n",
      "Finished making your pizza!\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'extra cheese']\n",
    "if 'mushrooms' in requested_toppings:\n",
    "    print(\"Adding mushrooms.\")\n",
    "if 'pepperoni' in requested_toppings:\n",
    "    print(\"Adding pepperoni.\")\n",
    "if 'extra cheese' in requested_toppings:\n",
    "    print(\"Adding extra cheese.\")\n",
    "print(\"\\nFinished making your pizza!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果像下面这样转而使用if-elif-else结构， 代码将不能正确地运行，<br/>因为有一个测试通过后，就会跳过余下的测试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "\n",
      "Finished making your pizza!\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'extra cheese']\n",
    "if 'mushrooms' in requested_toppings:\n",
    "    print(\"Adding mushrooms.\")\n",
    "elif 'pepperoni' in requested_toppings:\n",
    "    print(\"Adding pepperoni.\")\n",
    "elif 'extra cheese' in requested_toppings:\n",
    "    print(\"Adding extra cheese.\")\n",
    "print(\"\\nFinished making your pizza!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用 if 语句处理列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查特殊元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出很简单，因为是一个简单的for循"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "Adding green peppers.\n",
      "Adding extra cheese.\n",
      "\n",
      "Finished making your pizza!\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']\n",
    "for requested_topping in requested_toppings:\n",
    "    print(\"Adding \" + requested_topping + \".\")\n",
    "print(\"\\nFinished making your pizza!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "Sorry, we are out of green peppers right now.\n",
      "Adding extra cheese.\n",
      "\n",
      "Finished making your pizza!\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']\n",
    "for requested_topping in requested_toppings:\n",
    "    if requested_topping == 'green peppers':\n",
    "        print(\"Sorry, we are out of green peppers right now.\")\n",
    "    else:\n",
    "        print(\"Adding \" + requested_topping + \".\")\n",
    "print(\"\\nFinished making your pizza!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "确定列表不是空的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "到目前为止，对于处理的每个列表都做了一个简单的假设，即假设它们都至少包含一个元素。<br/>\n",
    "我们马上就要让用户来提供存储在列表中的信息，因此不能再假设循环运行时列表不是空的。<br/>有\n",
    "鉴于此，在运行for循环前确定列表是否为空很重要。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are you sure you want a plain pizza?\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = []\n",
    "if requested_toppings:\n",
    "    for requested_topping in requested_toppings:\n",
    "        print(\"Adding \" + requested_topping + \".\")\n",
    "    print(\"\\nFinished making your pizza!\")\n",
    "else:\n",
    "    print(\"Are you sure you want a plain pizza?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用多个列表**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "Sorry, we don't have french fries.\n",
      "Adding extra cheese.\n"
     ]
    }
   ],
   "source": [
    "available_toppings = ['mushrooms', 'olives', 'green peppers',\n",
    "'pepperoni', 'pineapple', 'extra cheese']\n",
    "\n",
    "requested_toppings = ['mushrooms', 'french fries', 'extra cheese']\n",
    "for requested_topping in requested_toppings:\n",
    "    if requested_topping in available_toppings:\n",
    "        print(\"Adding \" + requested_topping + \".\")\n",
    "    else:\n",
    "        print(\"Sorry, we don't have \" + requested_topping + \".\")"
   ]
  },
  {
   "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": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python的循环有两种，一种是for...in循环，依次把list或tuple中的每个元素迭代出来："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Michael\n",
      "Bob\n",
      "Tracy\n"
     ]
    }
   ],
   "source": [
    "names = ['Michael', 'Bob', 'Tracy']  #序列数据类型，有迭代器next()\n",
    "for name in names: \n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所以for x in ...循环就是把每个元素代入变量x，然后执行缩进块的语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n",
    "    sum = sum + x\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果要计算1-100的整数之和，Python提供一个**range()函数**，可以生成一个整数序列，再通过list()函数可以转换为list。<br/>\n",
    "比如range(5)生成的序列是从0开始小于5的整数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for x in range(1,11):\n",
    "    sum = sum + x\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 11)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# type(range(1,11))\n",
    "list(range(1,11))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# while 循环简介\n",
    "<br/>for循环用于针对集合中的每个元素都一个代码块，而while循环不断地运行，直到指定的条\n",
    "件不满足为止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 "
     ]
    }
   ],
   "source": [
    "current_number = 1\n",
    "while current_number <= 5:\n",
    "    print(current_number,end=' ')\n",
    "    current_number += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(current_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2 让用户选择何时退出**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可使用while循环让程序在用户愿意时不断地运行，如下面的程序。我们在其中\n",
    "定义了一个退出值，只要用户输入的不是这个值，程序就接着运行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. aa\n",
      "aa\n",
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. bb\n",
      "bb\n",
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. quit\n",
      "quit\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\\nTell me something, and I will repeat it back to you:\"\n",
    "prompt += \"\\nEnter 'quit' to end the program. \"\n",
    "message = \"\"\n",
    "while message != 'quit':\n",
    "    message = input(prompt)                    \n",
    "    print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. aa\n",
      "aa\n",
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. bb\n",
      "bb\n",
      "\n",
      "Tell me something, and I will repeat it back to you:\n",
      "Enter 'quit' to end the program. quit\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\\nTell me something, and I will repeat it back to you:\"\n",
    "prompt += \"\\nEnter 'quit' to end the program. \"\n",
    "active = True\n",
    "while active:\n",
    "    message = input(prompt)\n",
    "    if message == 'quit':\n",
    "        active = False\n",
    "    else:\n",
    "        print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**使用 break 退出循环**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要立即退出while循环，不再运行循环中余下的代码，也不管条件测试的结果如何，可使用\n",
    "break语句。 break语句用于控制程序流程，可使用它来控制哪些代码行将执行，哪些代码行不执\n",
    "行，从而让程序按你的要求执行你要执行的代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) aa\n",
      "I'd love to go to Aa!\n",
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) bb\n",
      "I'd love to go to Bb!\n",
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) quit\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\\nPlease enter the name of a city you have visited:\"\n",
    "prompt += \"\\n(Enter 'quit' when you are finished.) \"\n",
    "while True:\n",
    "    city = input(prompt)\n",
    "    if city == 'quit':\n",
    "        break\n",
    "    else:\n",
    "        print(\"I'd love to go to \" + city.title() + \"!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**在循环中使用 continue**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要返回到循环开头，并根据条件测试结果决定是否继续执行循环，可使用continue语句，它\n",
    "不像break语句那样不再执行余下的代码并退出整个循环。例如，来看一个从1数到10，但只打印\n",
    "其中偶数的循环："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 3 5 7 9 "
     ]
    }
   ],
   "source": [
    "current_number = 0\n",
    "while current_number < 10:\n",
    "    current_number += 1\n",
    "    if current_number % 2 == 0:\n",
    "        continue\n",
    "    print(current_number,end=' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**避免无限循环**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 "
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "while x <= 5:\n",
    "    print(x,end=' ')\n",
    "    x += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 1\n",
    "while x <= 5:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1  \n",
      "1 * 2 = 2  2 * 2 = 4  \n",
      "1 * 3 = 3  2 * 3 = 6  3 * 3 = 9  \n",
      "1 * 4 = 4  2 * 4 = 8  3 * 4 = 12  4 * 4 = 16  \n",
      "1 * 5 = 5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25  \n",
      "1 * 6 = 6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36  \n",
      "1 * 7 = 7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49  \n",
      "1 * 8 = 8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64  \n",
      "1 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81  \n"
     ]
    }
   ],
   "source": [
    "for x in range(1,10):  \n",
    "    for y in range(1,x+1):\n",
    "        print(y,\"*\",x,\"=\",x*y,\"\",end=\" \")\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "s=0\n",
    "for i in range(1,101):\n",
    "    s=s+i\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in range(0,5,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p y t h o n "
     ]
    }
   ],
   "source": [
    "for x in 'python':\n",
    "    print(x,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in 'python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
