{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 控制流"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "let's go to the park.\n"
     ]
    }
   ],
   "source": [
    "today_weather = \"sunny\"\n",
    "\n",
    "if today_weather == \"sunny\":\n",
    "    print(\"let's go to the park.\")\n",
    "else:\n",
    "    print(\"I can't get out of bed.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "let's go to the park.\n"
     ]
    }
   ],
   "source": [
    "today_weather = \"sunny\"\n",
    "\n",
    "if today_weather == \"sunny\":\n",
    "    print(\"let's go to the park.\")\n",
    "elif today_weather == \"rainy\":\n",
    "    print(\"I can't go to the park.\")\n",
    "elif today_weather == \"cloudy\":\n",
    "    print(\"Maybe we can go to park?\")\n",
    "elif today_weather == \"snowy\":\n",
    "    print(\"Should we stay at home?\")\n",
    "else:\n",
    "    print(\"Ok, I don't know what to do.\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "let's go to the park.\n"
     ]
    }
   ],
   "source": [
    "today_weather = \"sunny\"\n",
    "\n",
    "if today_weather == \"sunny\":\n",
    "    print(\"let's go to the park.\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "`message` string can be printed?\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# default value\n",
    "empty = \"\"\n",
    "names = []\n",
    "message = \"You got!\"\n",
    "\n",
    "if empty:\n",
    "    print(\"`empty` string variable can be printed?\")\n",
    "elif names:\n",
    "    print(\"`names` list can be printed?\")\n",
    "elif message:\n",
    "    print(\"`message` string can be printed?\")\n",
    "else:\n",
    "    print(\"Nothing is printed\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 7, over.\n"
     ]
    }
   ],
   "source": [
    "# complicated if-else\n",
    "\n",
    "import random\n",
    "\n",
    "num = random.randrange(1, 10)\n",
    "target = [2, 8, 10]\n",
    "\n",
    "if (num % 2 == 0) or (num in target):\n",
    "    print(f\"{num} is target num.\")\n",
    "elif num ** 3 == 9:\n",
    "    print(f\"num is 3.\")\n",
    "else:\n",
    "    print(f\"Got {num}, over.\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 循环遍历"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iterate message: H/e/l/l/o/,/ /w/o/r/l/d\n",
      "iterate os: Windows/macOS/Android/Linux/iOS/iPadOS\n"
     ]
    }
   ],
   "source": [
    "from collections import Iterable\n",
    "\n",
    "message = \"Hello, world\"\n",
    "if isinstance(message, Iterable):\n",
    "    print(\"iterate message: \", end=\"\")\n",
    "    print(*message, sep=\"/\")\n",
    "\n",
    "os = [\"Windows\", \"macOS\", \"Android\", \"Linux\", \"iOS\", \"iPadOS\"]\n",
    "if isinstance(os, Iterable):\n",
    "    print(\"iterate os: \", end=\"\")\n",
    "    print(*os, sep=\"/\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## for-loop"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 1*1=1 \n",
      "2: 1*2=2 2*2=4 \n",
      "3: 1*3=3 2*3=6 3*3=9 \n",
      "4: 1*4=4 2*4=8 3*4=12 4*4=16 \n",
      "5: 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 \n",
      "6: 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 \n",
      "7: 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 \n",
      "8: 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",
      "9: 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 outer in range(1, 10):\n",
    "    print(f\"{outer}: \", end=\"\")\n",
    "    for inner in range(outer):\n",
    "        print(f\"{inner+1}*{outer}={outer*(inner+1)}\", end=\" \")\n",
    "    print()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## while-loop"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I can't stop!\n",
      "I can't stop!\n",
      "I can't stop!\n",
      "I can't stop!\n",
      "I can't stop!\n"
     ]
    }
   ],
   "source": [
    "cond = 5\n",
    "while 1:\n",
    "    if cond == 0:\n",
    "        break\n",
    "\n",
    "    print(\"I can't stop!\")\n",
    "    cond -= 1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output 1\n",
      "output 2\n",
      "output 3\n",
      "output 4\n"
     ]
    }
   ],
   "source": [
    "for n in range(1, 6):\n",
    "    if n % 5 == 0:\n",
    "        break\n",
    "    print(f\"output {n}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output 1\n",
      "output 3\n"
     ]
    }
   ],
   "source": [
    "for n in range(1, 4):\n",
    "    if n % 2 == 0:\n",
    "        continue\n",
    "    print(f\"output {n}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 列表推导式与生成器表达式"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "evens = []\n",
    "for n in range(1, 11):\n",
    "    if n % 2 == 0:\n",
    "        evens.append(n)\n",
    "print(evens)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# list comprehension\n",
    "\n",
    "evens = [n for n in range(1, 11) if n % 2 == 0]\n",
    "print(evens)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# generator expression\n",
    "\n",
    "evens = list(n for n in range(1, 11) if n % 2 == 0)\n",
    "print(evens)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# better or not?\n",
    "# comprehension way\n",
    "\n",
    "arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]\n",
    "\n",
    "evens = [n for inner in arr for n in inner if n % 2 == 0]\n",
    "print(evens)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# native for-loop\n",
    "\n",
    "arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]\n",
    "evens = []\n",
    "for inner in arr:\n",
    "    for n in inner:\n",
    "        if n % 2 == 0:\n",
    "            evens.append(n)\n",
    "print(evens)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}