{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python3 基本数据类型\n",
    "\n",
    "Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。<br/>\n",
    "在 Python 中，变量就是变量，它没有类型，我们所说的\"类型\"是变量所指的内存中对象的类型。<br/>\n",
    "等号（=）用来给变量赋值。等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter = 100          # 整型变量\n",
    "miles   = 1000.0       # 浮点型变量\n",
    "name    = \"runoob\"     # 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**多个变量赋值**\n",
    "\n",
    "Python允许你同时为多个变量赋值。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = b = c = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c = 1, 2, \"runoob\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 标准数据类型\n",
    "\n",
    "Python3 中有六个标准的数据类型： \n",
    "* Number（数字）\n",
    "* String（字符串）\n",
    "* List（列表）\n",
    "* Tuple（元组）\n",
    "* Set（集合）\n",
    "* Dictionary（字典）<br/>\n",
    "\n",
    "**Python3 的六个标准数据类型中：**<br/>\n",
    "不可变数据（3 个）：Number（数字）、String（字符串）、Tuple（元组）；<br/>\n",
    "可变数据（3 个）：List（列表）、Dictionary（字典）、Set（集合）。 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Number（数字）**<br/>\n",
    "Python3 支持 int、float、bool、complex（复数）。 <br/>\n",
    "在Python 3里，只有一种整数类型 int，表示为长整型，没有 python2 中的 Long。<br/>\n",
    "像大多数语言一样，数值类型的赋值和计算都是很直观的。<br/>\n",
    "内置的 type() 函数可以用来查询变量所指的对象类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "a, b, c, d = 20, 5.5, True, 4+3j\n",
    "print(type(a), type(b), type(c), type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**以用 isinstance 来判断类型**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 111\n",
    "isinstance(a, int)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**isinstance 和 type 的区别在于：**<br/>\n",
    "type()不会认为子类是一种父类类型。<br/>\n",
    "isinstance()会认为子类是一种父类类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class 熊:\n",
    "    pass\n",
    "class 熊猫(熊):\n",
    "    pass\n",
    "isinstance(熊(), 熊)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(熊()) == 熊 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(熊猫(), 熊)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(熊猫()) == 熊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#当你指定一个值时，Number 对象就会被创建：\n",
    "var1 = 1\n",
    "var2 = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#您可以通过使用del语句删除单个或多个对象。例如：\n",
    "del var1, var2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数值运算\n",
    "实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 + 4  # 加法\n",
    "4.3 - 2 # 减法\n",
    "3 * 7  # 乘法\n",
    "2 / 4  # 除法，得到一个浮点数\n",
    "2 // 4 # 除法，得到一个整数\n",
    "17 % 3 # 取余 \n",
    "2 ** 5 # 乘方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (<ipython-input-14-c55d72b7faf3>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-14-c55d72b7faf3>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    Python中的字符串用单引号 ' 或双引号 \" 括起来，同时使用反斜杠 \\ 转义特殊字符。\u001b[0m\n\u001b[1;37m                                                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "# String（字符串）\n",
    "\n",
    "Python中的字符串用单引号 ' 或双引号 \" 括起来，同时使用反斜杠 \\ 转义特殊字符。\n",
    "字符串的截取的语法格式如下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob\n",
      "Runoo\n",
      "R\n",
      "noo\n",
      "noob\n",
      "RunoobRunoob\n",
      "RunoobTEST\n"
     ]
    }
   ],
   "source": [
    "str = 'Runoob'\n",
    " \n",
    "print (str)          # 输出字符串\n",
    "print (str[0:-1])    # 输出第一个到倒数第二个的所有字符\n",
    "print (str[0])       # 输出字符串第一个字符\n",
    "print (str[2:5])     # 输出从第三个开始到第五个的字符\n",
    "print (str[2:])      # 输出从第三个开始的后的所有字符\n",
    "print (str * 2)      # 输出字符串两次\n",
    "print (str + \"TEST\") # 连接字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2483041726640\n",
      "RunoobTEST\n",
      "2483041743088\n"
     ]
    }
   ],
   "source": [
    "str = 'Runoob'\n",
    "print(id(str))\n",
    "# 输出字符串两次\n",
    "print (str + \"TEST\") \n",
    "print (id(str + \"TEST\")) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "str = 'Runing'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "uning\n"
     ]
    }
   ],
   "source": [
    "print (str[1:]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1!=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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):\t\t#循环变量x从1循环到9\n",
    "    for y in range(1,x+1):\t#循环变量y从1循环到x+1\n",
    "        print(y,\"*\",x,\"=\",x*y,\"\",end=\" \")\t#输出乘法表达式\n",
    "    print(\"\")\t\t\t#输出空字符串，作用是为了换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in range(1,5,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "116.5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "8\n",
      "3\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(3 - 3 and 3 < 6)\t\t#输出逻辑表达式的值\n",
    "print(3 < 6 and 3 + 5)\n",
    "print(1 + 2 or 3 < 6)\n",
    "print(3 < 6 or 3 + 5)\n",
    "print(not 3>6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 or True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 or True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or 3"
   ]
  },
  {
   "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
}
