{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "strs ='abcdefg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gfedcba'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strs[6::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "中国古代数学史上著名的“百鸡问题”：鸡翁一，值钱五，鸡母一，值钱三，鸡雏三，值钱一。百钱买百鸡，问鸡翁、母、雏各几何？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鸡翁: 0 鸡母: 25 鸡雏: 75\n",
      "鸡翁: 4 鸡母: 18 鸡雏: 78\n",
      "鸡翁: 8 鸡母: 11 鸡雏: 81\n",
      "鸡翁: 12 鸡母: 4 鸡雏: 84\n"
     ]
    }
   ],
   "source": [
    "for cock in range(0,20+1):                   #鸡翁范围在0到20之间\n",
    "    for hen in range(0,33+1):                #鸡母范围在0到33之间\n",
    "        for biddy in range(3,99+1):          #鸡雏范围在3到99之间\n",
    "            if (5*cock+3*hen+biddy/3)==100:  #判断钱数是否等于100\n",
    "                if (cock+hen+biddy)==100:    #判断购买的鸡数是否等于100\n",
    "                    if biddy%3==0:           #判断鸡雏数是否能被3整除\n",
    "                        print (\"鸡翁:\",cock,\"鸡母:\",hen,\"鸡雏:\",biddy)    #输出\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鸡翁: 0 鸡母: 25 鸡雏: 75\n",
      "鸡翁: 4 鸡母: 18 鸡雏: 78\n",
      "鸡翁: 8 鸡母: 11 鸡雏: 81\n",
      "鸡翁: 12 鸡母: 4 鸡雏: 84\n"
     ]
    }
   ],
   "source": [
    "for cock in range(0,20+1):                     #鸡翁范围在0到20之间\n",
    "    for hen in range(0,33+1):                  #鸡母范围在0到33之间\n",
    "        for biddy in range(3,99+1,3):          #鸡雏范围在3到99之间\n",
    "            if (5*cock+3*hen+biddy/3)==100:    #判断钱数是否等于100\n",
    "                if (cock+hen+biddy)==100:      #判断购买的鸡数是否等于100\n",
    "                        print (\"鸡翁:\",cock,\"鸡母:\",hen,\"鸡雏:\",biddy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鸡翁: 0 鸡母: 25 鸡雏: 75\n",
      "鸡翁: 4 鸡母: 18 鸡雏: 78\n",
      "鸡翁: 8 鸡母: 11 鸡雏: 81\n",
      "鸡翁: 12 鸡母: 4 鸡雏: 84\n",
      "鸡翁: 16 鸡母: -3 鸡雏: 87\n",
      "鸡翁: 20 鸡母: -10 鸡雏: 90\n"
     ]
    }
   ],
   "source": [
    "for biddy in range(0,99+1,3):                  #鸡翁范围在0到20之间\n",
    "    for cock in range(0,20+1):                 #鸡母范围在0到33之间\n",
    "        hen = 100 - cock - biddy\n",
    "        if (5*cock+3*hen+biddy/3)==100:\n",
    "            print (\"鸡翁:\",cock,\"鸡母:\",hen,\"鸡雏:\",biddy)              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鸡翁: 0 鸡母: 25 鸡雏: 75\n",
      "鸡翁: 4 鸡母: 18 鸡雏: 78\n",
      "鸡翁: 8 鸡母: 11 鸡雏: 81\n",
      "鸡翁: 12 鸡母: 4 鸡雏: 84\n"
     ]
    }
   ],
   "source": [
    "for biddy in range(0,100,3):\n",
    "    for cock in range(0,min(21,100-biddy)):\n",
    "        hen = 100-cock-biddy\n",
    "        if (5*cock+3*hen+biddy/3)==100:\n",
    "            print (\"鸡翁:\",cock,\"鸡母:\",hen,\"鸡雏:\",biddy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5%2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-35-53cacbc667a3>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-35-53cacbc667a3>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    'I'm OK!'\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'I'm OK!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I told my friend,\"Python is my favorite language!\"'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'I told my friend,\"Python is my favorite language!\"'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\tb\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'a\\tb'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('a\\tb')\n",
    "'a\\tb'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm learning\n",
      "Python.\n",
      "\\\n",
      "\\\n",
      "\\\\\\n\\\\\n"
     ]
    }
   ],
   "source": [
    "print('I\\'m learning\\nPython.')\n",
    "print('\\\\\\n\\\\')\n",
    "print(r'\\\\\\n\\\\')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个整数:4\n",
      "请输入第二个整数:6\n",
      "两数之和为: 10\n"
     ]
    }
   ],
   "source": [
    "a=input(\"请输入第一个整数:\")     #输入变量a的值\n",
    "    \n",
    "b=input(\"请输入第二个整数:\")    #输入变量b的值\n",
    "a=int(a)                        #将变量a转换为整型\n",
    "b=int(b)\n",
    "c=a+b                            #两数相加赋给c\n",
    "print(\"两数之和为:\",c)           #输出c的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "# 数据类型\n",
    "计算机程序理所可以处理各种数值【数值，还可以处理文本、图形、音频、视频、网页等各种各样的数据】。不同的数据，需要定义不同的数据类型。在Python中，能够直接处理的数据类型有以下几种："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**整数**</font><br />\n",
    "Python可以处理任意大小的整数，当然包括负整数，在程序中的表示方法和数学上的写法一模一样，例如：1，100，-8080，0，等等。\n",
    "计算机由于使用二进制，所以，有时候用十六进制表示整数比较方便，**十六进制**用$\\boldsymbol{0x}$前缀,0-9，a-f表示，例如：0xff00，0xa5b4c3d2，等等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**浮点数**</font><br />\n",
    "浮点数也就是小数，之所以称为浮点数，是因为按照科学记数法表示时，一个浮点数的小数点位置是可变的，比如，1.23x109和12.3x108是完全相等的。\n",
    "浮点数可以用数学写法，如1.23，3.14，-9.01，等等。但是对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，1.23x109就是1.23e9，\n",
    "或者12.3e8，0.000012可以写成1.2e-5，等等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**字符串**</font><br />\n",
    "字符串是以单引号**\\'**或双引号**\\\"**括起来的任意文本，比如'abc'，\"xyz\"等等。请注意，''或\" \"本身只是一种表示方式，不是字符串的一部分，\n",
    "因此，字符串'abc'只有a，b，c这3个字符。如果**\\'**本身也是一个字符，\n",
    "那就可以用**\" \"**括起来，比如**\"I'm OK\"**包含的字符是**I，'，m，空格，O，K**这6个字符。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.1运行 hello_world.py 时发生的情况"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行hello_world.py时， Python都做了些什么呢？\n",
    "\n",
    "下面来深入研究一下。实际上，即便是运行简单的程序， Python所做的工作也相当多："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python world!\n"
     ]
    }
   ],
   "source": [
    "# 这里print()为函数，用绿色的字符给与标明。\n",
    "print(\"Hello Python world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.2 变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**变量**</font>的概念基本上和初中代数的方程变量是一致的，只是在计算机程序中，变量不仅可以是数字，还可以是任意数据类型。比如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "t_007 = 'T007'\n",
    "Answer = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python world!\n"
     ]
    }
   ],
   "source": [
    "message = \"Hello Python world!\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们添加了一个名为message的变量。每个变量都存储了一个值——与变量相关联的信息。\n",
    "在这里，存储的值为文本“Hello Python world!”。\n",
    "\n",
    "添加变量导致Python解释器需要做更多工作。处理第1行代码时，它将文本“Hello Python\n",
    "world!”与变量message关联起来；而处理第2行代码时，它将与变量message关联的值打印到屏幕。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python world!\n",
      "Hello Python Crash Course world!\n"
     ]
    }
   ],
   "source": [
    "# 在程序中可随时修改变量的值，而Python将始终记录变量的最新值。\n",
    "message = \"Hello Python world!\"\n",
    "print(message)\n",
    "message = \"Hello Python Crash Course world!\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "message = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "变量的id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(140729470998288, 140729470998288)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a),id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(140729470998320, 140729470998288)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=3\n",
    "print(a,b)\n",
    "id(a),id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(140729470998320, 140729470998320)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=3\n",
    "id(a),id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140729470998288"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=2\n",
    "id(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1857352206512, 1857352206512)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = 'ab';s2 = 'ab'\n",
    "id(s1),id(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1857348343600, 1857348343600, 1857352206512)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = 'a'\n",
    "id(s3),id('a'),id('ab')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2489941114360, 2489941114360)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id('abc'),id('abc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1857414752944, 1857414752944)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(2.0),id(2.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**变量的命名和使用**\n",
    "\n",
    "在Python中使用变量时，需要遵守一些规则和指南。违反这些规则将引发错误，而指南旨在\n",
    "让你编写的代码更容易阅读和理解。\n",
    "\n",
    " * 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头，但不能以数字打\n",
    "头，例如，可将变量命名为message_1，但不能将其命名为1_message。\n",
    " * 变量名不能包含空格，但可使用下划线来分隔其中的单词。例如，变量名greeting_message\n",
    "可行，但变量名greeting message会引发错误。\n",
    " * 不要将Python关键字和函数名用作变量名，即不要使用Python保留用于特殊用途的单词，\n",
    "如print\n",
    " * 变量名应既简短又具有描述性。例如， name比n好， student_name比s_n好， name_length\n",
    "比length_of_persons_name好。\n",
    " * 慎用小写字母l和大写字母O，因为它们可能被人错看成数字1和0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'mesage' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-29bb9c7df0e2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 解释器会提供一个traceback。 traceback是一条记录，指出了解释器尝试运行代码时，在什么地方陷入了困境。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Hello Python Crash Course reader!\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmesage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'mesage' is not defined"
     ]
    }
   ],
   "source": [
    "# Python解释器将竭尽所能地帮助你找出问题所在。程序无法成功地运行时，\n",
    "# 解释器会提供一个traceback。 traceback是一条记录，指出了解释器尝试运行代码时，在什么地方陷入了困境。\n",
    "message = \"Hello Python Crash Course reader!\"\n",
    "print(mesage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**变量赋值时**Python解释器干了两件事情：<br />\n",
    "1.在内存中创建了一个'ABC'的字符串；<br />\n",
    "2.在内存中创建了一个名为a的变量，并把它指向'ABC'。<br />\n",
    "也可以把一个变量a赋值给另一个变量b，这个操作实际上是把变量b指向变量a所指向的数据，例如下面的代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABC\n"
     ]
    }
   ],
   "source": [
    "a = 'ABC'\n",
    "b = a\n",
    "a = 'XYZ'\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 7,
     "metadata": {
      "image/png": {
       "height": 200,
       "width": 400
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Image\n",
    "#PATH = \"C:\\Users\\tangH\\th\\python程序设计\\第二章python基础\"\n",
    "Image(\"python变量赋值1.png\", width=400, height=200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](python变量赋值1.png);![alt text](python变量赋值2.png);![alt text](python变量赋值3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.3 常量\n",
    "\n",
    "所谓常量就是不能变的变量，比如常用的数学常数π就是一个常量。在Python中，通常用全部大写的变量名表示常量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "PI = 3.14159265359"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但事实上PI仍然是一个变量，Python根本没有任何机制保证PI不会被改变，所以，用全部大写的变量名表示常量只是一个习惯上的用法，如果你一定要改变变量PI的值，也没人能拦住你。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.4 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "大多数程序都定义并收集某种数据，然后使用它们来做些有意义的事情。\n",
    "\n",
    "鉴于此，对数据进行分类大有裨益。我们将介绍的第一种数据类型是字符串。\n",
    "\n",
    "字符串虽然看似简单，但能够以很多不同的方式使用它们。\n",
    "\n",
    "**字符串**就是一系列字符。在Python中，用引号括起的都是字符串，其中的引号可以是单引号，\n",
    "也可以是双引号，如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**puthon字符串能够在字符串中包含引号和撇号：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I told my friend, \"Python is my favorite language!\"'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'I told my friend, \"Python is my favorite language!\"'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"The language 'Python' is named after Monty Python, not the snake.\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"The language 'Python' is named after Monty Python, not the snake.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"One of Python's strengths is its diverse and supportive community.\""
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"One of Python's strengths is its diverse and supportive community.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "**使用方法修改字符串的大小写**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ada Lovelace\n",
      "ada lovelace\n"
     ]
    }
   ],
   "source": [
    "name = \"ada lovelace\"\n",
    "print(name.title())\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个示例中，小写的字符串\"ada lovelace\"存储到了变量name中。在print()语句中，方法title()出现在这个变量的后面。 <br/> \n",
    "方法是Python可对数据执行的操作。在name.title()中， name后面的句点（ .）让Python对变量name执行方法title()指定的操作。 <br/> \n",
    "每个方法后面都跟着一对括号，这是因为方法通常需要额外的信息来完成其工作。这种信息是在括号内提供的。函数title()不 <br/> \n",
    "需要额外的信息，因此它后面的括号是空的。title()以首字母大写的方式显示每个单词，即将每个单词的首字母都改为大写。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要将字符串改为全部大写或全部小写，可以\n",
    "像下面这样做："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ada lovelace\n"
     ]
    }
   ],
   "source": [
    "name = \"Ada Lovelace\"\n",
    "# print(name.upper())\n",
    "print(name.lower())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**合并（拼接）字符串**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ada lovelace\n"
     ]
    }
   ],
   "source": [
    "# Python使用加号（ +）来合并字符串。在这个示例中，我们使用+来合并first_name、空格和\n",
    "# last_name，以得到完整的姓名\n",
    "first_name = \"ada\"\n",
    "last_name = \"lovelace\"\n",
    "full_name = first_name + \" \" + last_name\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Ada Lovelace!\n"
     ]
    }
   ],
   "source": [
    "first_name = \"ada\"\n",
    "last_name = \"lovelace\"\n",
    "full_name = first_name + \" \" + last_name  ## ada Lovelace\n",
    "message = \"Hello, \" + full_name.title() + \"!\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**转义字符**<br />\n",
    "如果字符串内部既包含'又包含\"可以用转义字符**\\**来标识，比如："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转义字符\\可以转义很多字符，比如\\n表示换行，\\t表示制表符，字符\\本身也要转义，所以\\\\表示的字符就是\\，\n",
    "可以在Python的交互式命令行用print()打印字符串看看："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Py\\nhon'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Py\\nhon\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Py\n",
      "hon\n"
     ]
    }
   ],
   "source": [
    "print(\"Py\\nhon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n",
      "\tPython\n"
     ]
    }
   ],
   "source": [
    "# 使用\\t添加制表符\n",
    "print(\"Python\")\n",
    "print(\"\\tPython\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Languages:\n",
      "Python\n",
      "C\n",
      "JavaScript\n"
     ]
    }
   ],
   "source": [
    "# 使用\\n换行\n",
    "print(\"Languages:\\nPython\\nC\\nJavaScript\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm ok.\n"
     ]
    }
   ],
   "source": [
    "# 转义字符输出'\n",
    "print('I\\'m ok.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-19-880a813be8c4>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-19-880a813be8c4>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    'I'm ok.'\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'I'm ok.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm learning\n",
      "Python.\n",
      "\\\n",
      "\\\n"
     ]
    }
   ],
   "source": [
    "# 打印\\符号本身\n",
    "print('I\\'m learning\\nPython.')\n",
    "print('\\\\\\n\\\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果字符串里面有很多字符都需要转义，就需要加很多**\\\\**，为了简化，Python还允许用**r' '**表示**' '**内部的字符串默认不转义："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\\t\\\n"
     ]
    }
   ],
   "source": [
    "print('\\\\\\t\\\\')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\\\\\t\\\\\n"
     ]
    }
   ],
   "source": [
    "print(r'\\\\\\t\\\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果**字符串内部有很多换行**，用\\n写在一行里不好阅读，为了简化，Python允许用'''...'''的格式表示多行内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line1\n",
      " line2\n",
      " line3\n"
     ]
    }
   ],
   "source": [
    "print('line1\\n line2\\n line3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'line1\\n line2\\n line3'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''line1\n",
    " line2\n",
    " line3'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line1\n",
      " line2\n",
      " line3\n"
     ]
    }
   ],
   "source": [
    "print('''line1\n",
    " line2\n",
    " line3''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**删除空白**<br/>\n",
    "在程序中，额外的空白可能令人迷惑。对程序员来说， 'python'和'python '看起来几乎没\n",
    "什么两样，但对程序来说，它们却是两个不同的字符串。 <br/>\n",
    "Python能够找出字符串开头和末尾多余的空白。要确保字符串末尾没有空白，可使用方法\n",
    "**rstrip()**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python \n"
     ]
    }
   ],
   "source": [
    "favorite_language1 = 'python '\n",
    "print(favorite_language1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python\n"
     ]
    }
   ],
   "source": [
    "favorite_language2 = favorite_language1.rstrip()\n",
    "print(favorite_language2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 6\n"
     ]
    }
   ],
   "source": [
    "print(len(favorite_language1),len(favorite_language2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你还可以剔除字符串开头的空白，或同时剔除字符串两端的空白。为此，可分别使用方法\n",
    "**lstrip()**和**strip()：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "8\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "favorite_language3 = '  python '\n",
    "print(len(favorite_language3))\n",
    "# 删除字符串末尾空白\n",
    "print(len(favorite_language3.rstrip()))\n",
    "#删除字符串前面空白\n",
    "print(len(favorite_language3.rstrip().lstrip()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "favorite_language3 = favorite_language3.rstrip().lstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(favorite_language3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "常见问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"One of Python's strengths is its diverse community.\""
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 双引号中出现单引号\n",
    "\"One of Python's strengths is its diverse community.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-21-77e8700358fe>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-21-77e8700358fe>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    'One of Python's strengths is its diverse community.'\u001b[0m\n\u001b[1;37m                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 使用单引号会出现问题\n",
    "'One of Python's strengths is its diverse community.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**请问输出是什么？**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# s1 = 'Hello, world'\n",
    "# s2 = 'Hello, \\'Adam\\''\n",
    "# s3 = r'Hello, \"Bart\"'\n",
    "s4 = r'''Hello,\n",
    " Lisa!'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello,\\n Lisa!'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,\n",
      " Lisa!\n"
     ]
    }
   ],
   "source": [
    "print('''Hello,\n",
    " Lisa!''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Hello, 'Adam'\""
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hello, \\'Adam\\''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**字符编码**<br/>\n",
    "因为计算机只能处理数字，如果要处理文本，就必须先把文本转换为数字才能处理。\n",
    "最早的计算机在设计时采用8个比特（bit）作为一个字节（byte），所以，一个字节能表示的最大的整数就是255（二进制11111111=十进制255），如果要表示更大的整数，就必须用更多的字节。比如两个字节可以表示的最大整数是65535，4个字节可以表示的最大整数是4294967295。<br />\n",
    "\n",
    "**ASCII编码：**最早只有127个字符被编码到计算机里，也就是大小写英文字母、数字和一些符号，这个编码表被称为ASCII编码，比如大写字母A的编码是65，小写字母z的编码是122。\n",
    "\n",
    "处理中文显然一个字节是不够的，至少需要两个字节，而且还不能和ASCII编码冲突，所以，中国制定了GB2312编码，用来把中文编进去。\n",
    "\n",
    "**问题：**全世界有上百种语言，日本把日文编到Shift_JIS里，韩国把韩文编到Euc-kr里，各国有各国的标准，就会不可避免地出现冲突，结果就是，在多语言混合的文本中，显示出来会有乱码。\n",
    "\n",
    "Unicode是一种把所有语言都统一到一套编码里，可以避免乱码问题。Unicode标准也在不断发展，但最常用的是用两个字节表示一个字符（如果要用到非常偏僻的字符，就需要4个字节）。现代操作系统和大多数编程语言都直接支持Unicode。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**Python的字符串**</font><br />\n",
    "在最新的**Python 3版本中，字符串是以Unicode编码的，**也就是说，Python的字符串支持多语言，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "包含中222文的str\n"
     ]
    }
   ],
   "source": [
    "print('包含中222文的str')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于**单个字符的编码**，Python提供了<br />\n",
    "1. ord()函数获取字符的整数表示<br />\n",
    "2. chr()函数把编码转换为对应的字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(65, 97)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord('A'),ord('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20013"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord('中')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'B'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('文', '┷')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(25991),chr(9527)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中文'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 果知道字符的整数编码，还可以用十六进制这么写str：\n",
    "'\\u4e2d\\u6587'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Python的字符串类型是str，在内存中以Unicode表示，**<font color=\"#dd0000\">一个字符对应若干个字节。</font><br />\n",
    "如果要在网络上传输，或者保存到磁盘上，就需要把str变为以字节为单位的bytes。\n",
    "**Python对bytes**类型的数据用**b**前缀的单引号或双引号表示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = b'ABC'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'ABC'\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要注意区分'ABC'和b'ABC'，前者是str，后者虽然内容显示得和前者一样，但bytes的每个字符都只占用一个字节。\n",
    "以Unicode表示的str通过encode()方法可以编码为指定的bytes，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'ABC'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'ABC'.encode('ascii')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'中文'.encode('utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<br />纯英文的str可以用ASCII编码为bytes，内容是一样的，含有中文的str可以用UTF-8编码为bytes。含有中文的str无法用ASCII编码，因为中文编码的范围超过了ASCII编码的范围，Python会报错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeEncodeError",
     "evalue": "'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeEncodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-76f41cd8dafa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m'中文'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ascii'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mUnicodeEncodeError\u001b[0m: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)"
     ]
    }
   ],
   "source": [
    "'中文'.encode('ascii')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在bytes中，无法显示为ASCII字符的字节，用\\x##显示。\n",
    "\n",
    "反过来，如果我们从网络或磁盘上读取了字节流，那么读到的数据就是bytes。要把bytes变为str，就需要用decode()方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABC'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b'ABC'.decode('ascii')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中文'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'.decode('utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果bytes中包含无法解码的字节，decode()方法会报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'utf-8' codec can't decode byte 0xff in position 3: invalid start byte",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-cd8de1b11dcd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34mb'\\xe4\\xb8\\xad\\xff'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'utf-8' codec can't decode byte 0xff in position 3: invalid start byte"
     ]
    }
   ],
   "source": [
    "b'\\xe4\\xb8\\xad\\xff'.decode('utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果bytes中只有一小部分无效的字节，可以传入errors='ignore'忽略错误的字节："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b'\\xe4\\xb8\\xad\\xff'.decode('utf-8', errors='ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 要计算str包含多少个字符，可以用len()函数：\n",
    "print(len('ABC'))\n",
    "print(len('中文'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# len()函数计算的是str的字符数，如果换成bytes，len()函数就计算字节数：\n",
    "print(len(b'ABC'))\n",
    "print(len(b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'))\n",
    "print(len('中文'.encode('utf-8')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见，1个中文字符经过UTF-8编码后通常会占用3个字节，而1个英文字符只占用1个字节。\n",
    "\n",
    "在操作字符串时，我们经常遇到str和bytes的互相转换。为了避免乱码问题，应当始终坚持使用UTF-8编码对str和bytes进行转换。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br/>\n",
    "#!/usr/bin/env python3<br />\n",
    " -*- coding: utf-8 -*-\n",
    "\n",
    "第一行注释是为了告诉Linux/OS X系统，这是一个Python可执行程序，Windows系统会忽略这个注释；\n",
    "\n",
    "第二行注释是为了告诉Python解释器，按照UTF-8编码读取源代码，否则，你在源代码中写的中文输出可能会有乱码。\n",
    "\n",
    "申明了UTF-8编码并不意味着你的.py文件就是UTF-8编码的，必须并且要确保文本编辑器正在使用UTF-8 without BOM编码："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.4 数值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**整数**<br/>\n",
    "在Python中，可对整数执行加（ +）减（ -）乘（ *）除（ /）运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 1 6 1.5\n"
     ]
    }
   ],
   "source": [
    "print(2 + 3, 3 - 2, 2 * 3, 3 / 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python使用两个乘号表示乘方运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " 3 ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 ** 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 + 3) * 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**浮点数**<br/>\n",
    "Python将带小数点的数字都称为浮点数。大多数编程语言都使用了这个术语，它指出了这样\n",
    "一个事实：小数点可出现在数字的任何位置。每种编程语言都须细心设计，以妥善地处理浮点数，\n",
    "确保不管小数点出现在什么位置，数字的行为都是正常的。\n",
    "从很大程度上说，使用浮点数时都无需考虑其行为。你只需输入要使用的数字， Python通常\n",
    "都会按你期望的方式处理它们："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " 0.2 + 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " 2 * 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.30000000000000004"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 浮点数的运算结果是不确定的\n",
    "0.2 + 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意拼接字符串和数值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Happy 23rd Birthday!\n"
     ]
    }
   ],
   "source": [
    "age = 23\n",
    "# message = \"Happy \" + age + \"rd Birthday!\"\n",
    "message = \"Happy \" + str(age) + \"rd Birthday!\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "**python3中的除法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.3333333333333335"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "9/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10//3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 % 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**布尔值**<br/>\n",
    " 布尔值和布尔代数的表示完全一致，一个布尔值只有True、False两种值，要么是True， 要么是False，在Python中，可以直接用True、False表示布尔值（请注意大小写），也可以通过布尔运算计算出来："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 > 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**布尔运算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 > 3 and 3 > 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**or**运算是或运算，只要其中有一个为True，or运算结果就是True："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or True\n",
    "\n",
    "True or False\n",
    "\n",
    "False or False\n",
    "\n",
    "5 > 3 or 1 > 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**not**运算是非运算，它是一个单目运算符，把True变成False，False变成True："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not True\n",
    "\n",
    "not False\n",
    "\n",
    "not 1 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**布尔值经常用在条件判断中**，比如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = int(input())\n",
    "if age >= 18:\n",
    "    print('adult')\n",
    "else:\n",
    "    print('teenager')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"#dd0000\">**空值**</font><br />\n",
    "空值是Python里一个特殊的值，用None表示。None不能理解为0，因为0是有意义的，而None是一个特殊的空值。\n",
    "\n",
    "Python还提供了列表、字典等多种数据类型，还允许创建自定义数据类型，我们后面会继续讲到。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输入\n",
    "现在，你已经可以用print()输出你想要的结果了。但是，如果要让用户从电脑输入一些字符怎么办？\n",
    "Python提供了一个input()，可以让用户输入字符串，并存放到一个变量里。比如输入用户的名字：\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sss\n"
     ]
    }
   ],
   "source": [
    "#输入为字符串形式\n",
    "name = input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'sss'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please enter your name: tom\n",
      "hello, tom\n"
     ]
    }
   ],
   "source": [
    "name = input('please enter your name: ')\n",
    "print('hello,', name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输出\n",
    "用print()在括号中加上字符串，就可以向屏幕上输出指定的文字。比如输出'hello, world'，用代码实现如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好，张三，你这个月的工资是8500元！'"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"你好，{1}，你这个月的工资是{0}元！\".format(8500,\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The quick brown fox jumps over the lazy dog\n",
      "cat\n"
     ]
    }
   ],
   "source": [
    "#print()函数也可以接受多个字符串，用逗号“,”隔开，就可以连成一串输出：\n",
    "print('The quick brown fox', 'jumps over', 'the lazy dog')\n",
    "print('cat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print()也可以打印整数，或者计算结果：\n",
    "print(300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 + 200 = 300\n"
     ]
    }
   ],
   "source": [
    "#把计算100 + 200的结果打印得更漂亮一点：\n",
    "#注意，对于100 + 200，Python解释器自动计算出结果300，但是，'100 + 200 ='是字符串而非数学公式，\n",
    "#Python把它视为字符串，请自行解释上述打印结果。\n",
    "print('100 + 200 =',100 + 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输出格式化字符串：\n",
    "经常会输出类似'亲爱的xxx你好！你xx月的话费是xx，余额是xx'之类的字符串，而xxx的内容都是根据变量变化的，所以，需要一种简便的格式化字符串的方式。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](001输出格式化字符串.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, 狗狗'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hello, %s' %'狗狗'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi, Michael, you have $1000000.'"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hi, %s, you have $%d.' %('Michael', 1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaa\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Hello, aaa'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hello, %s' % input()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "常见的占位符有：\n",
    "%d 整数;\n",
    "%f 浮点数; \n",
    "%s 字符串; \n",
    "%x 十六进制整数; \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      " 3-01\n",
      " 3.14\n"
     ]
    }
   ],
   "source": [
    "print(4)\n",
    "print('%2d-%02d' % (3, 1))\n",
    "print('%5.2f' % 3.1415926)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Age: 25. Gender: True'"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# %s永远起作用，它会把任何数据类型转换为字符串：\n",
    "'Age: %s. Gender: %s' % (25, True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'growth rate: 7 %'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串里面的%是一个普通字符怎么办？这个时候就需要转义，用%%来表示一个%：\n",
    "'growth rate: %d %%' % 7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**format()**<br/>\n",
    "另一种格式化字符串的方法是使用字符串的format()方法，它会用传入的参数依次替换字符串内的占位符{0}、{1}……，这种方式写起来比%要麻烦得多："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, 小明, 成绩提升了 6.4%'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hello, {0}, 成绩提升了 {2:.1f}%'.format('小明', 17.125, 6.445)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=1\n",
    "y=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = y,x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "z=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = z+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-47-623f213d8d2f>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-47-623f213d8d2f>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    x=(y = z+1)\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "x=(y = z+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6, 5)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y,z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=y=z+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 6)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "z=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'c', 'r', 'd', 'a'}\n",
      "{'m', 'c', 'z', 'l', 'a'}\n",
      "--------------\n",
      "{'b', 'd', 'r'}\n",
      "{'b', 'm', 'c', 'r', 'd', 'z', 'l', 'a'}\n",
      "{'a', 'c'}\n",
      "{'z', 'b', 'r', 'm', 'd', 'l'}\n"
     ]
    }
   ],
   "source": [
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)\n",
    "print(b)\n",
    "print('--------------------------')\n",
    "print(a - b)     # a 和 b 的差集\n",
    "print(a | b)     # a 和 b 的并集\n",
    "print(a & b)     # a 和 b 的交集\n",
    "print(a ^ b)     # a 和 b 中不同时存在的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'大海':'蓝色','天空':'灰色','大地':'黑色'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = ['Google', 'Runoob', 1997, 2000]\n",
    "list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Google', 'Runoob', 1997]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printme( str ):\n",
    "   # 打印任何传入的字符串\n",
    "    print (str)\n",
    "    return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaa\n"
     ]
    }
   ],
   "source": [
    "a = printme('aaa')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_1 = [1,2,3]   #创建列表list_1并赋值\n",
    "list_2 = [4,5,6]   #创建列表list_2并赋值\n",
    "list_3 = [7,8,9]   #创建列表list_3并赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_1+list_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6]]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_1.append(list_2)\t\t#将list_2看作一个对象，整体打包添加到list_1对象中\n",
    "list_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "list_2.extend(list_3)\t\t\t#将list_3看作一个序列，将这个序列和list_2序列合并\n",
    "print(list_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 3, 2, 1]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [1,2,3,4]                #创建列表并赋值\n",
    "x.reverse()\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 3, 2, 1]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [1,2,3,4]                #创建列表并赋值\n",
    "x[len(x)-1::-1]"
   ]
  },
  {
   "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": "301.1px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
