{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e3282e94",
   "metadata": {},
   "source": [
    "# Python 2和3的核心区别"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "89e7e006",
   "metadata": {},
   "source": [
    "不少企业的代码或实际项目升级Python版本的成本很高,所以保留了相当多的Python 2程序。为了阅读与运维这些程序,了解Python 2和3的一些核心区别十分重要"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "738480a2",
   "metadata": {},
   "source": [
    "## 整数除法\n",
    "\n",
    "- Python 2中,两个整数的运算结果只能是整数,对于除不尽的情况,Python 2会将结果向下取整,返回小于该结果的最大整数,如12/5的值为2。\n",
    "- Python 3中,除法返回浮点数,12/5的结果为2.4。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e1b08020",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12 / 5"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bbb55f6d",
   "metadata": {},
   "source": [
    "## 整形与长整型\n",
    "\n",
    "- Python 2中,当整数大于一定范围时,Python 2会自动将整数由整型转换为长整型(long),长整型以数字+L表示,如12345678765434567876543L。\n",
    "- Python 3中,长整型被取消,整数都是整型(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "42fedda1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345678765434567876543"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12345678765434567876543"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ada243c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(12345678765434567876543)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2541aa66",
   "metadata": {},
   "source": [
    "## f字符串\n",
    "\n",
    "- Python 3中,引入了f字符串进行格式化,可以在占位符中直接使用已有变量的表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51229ab6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "40844106",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a=100, a is 100'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"{a=}, a is {a}\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8779621e",
   "metadata": {},
   "source": [
    "## 字符串类型\n",
    "\n",
    "- Python 2中,str类型默认使用ASCII编码,unicode类型使用Unicode编码\n",
    "- Python 3中,str类型默认使用Unicode编码,bytes类型使用ASCII编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a33fef52",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"中国\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "983bf4f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a7353aff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8ae4eddc",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = b'abc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8917106e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bytes"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3d5be59f",
   "metadata": {},
   "source": [
    "## 不同类型的比较\n",
    "\n",
    "- Python 2中,支持,如字符串与数字可以比较大小\n",
    "- Python 3中,不支持,字符串与数字不可以比较大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5c61dbc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3 > \"abc\" #会报错"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "958e4ebe",
   "metadata": {},
   "source": [
    "## 字典键值对方法的返回类型\n",
    "\n",
    "- Python 2中,.keys()、.values()、.items()方法返回列表\n",
    "- Python 3中,.keys()、.values()、.items()方法分别返回dict_keys、dict_values、dict_items类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f44621c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {1: 1, 2: 4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8bbfe732",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(dict_keys([1, 2]), dict_values([1, 4]), dict_items([(1, 1), (2, 4)]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.keys(), a.values(), a.items()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0a7bc6c2",
   "metadata": {},
   "source": [
    "## range(), map(), filter(), zip()函数返回值\n",
    "\n",
    "- Python 2中,返回列表\n",
    "- Python 3中,返回一个迭代器对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "501b8879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "708a7b44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x10b9a0340>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip([1,2,3], [4,5,6])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e6b7930b",
   "metadata": {},
   "source": [
    "## ord(), chr()函数\n",
    "\n",
    "- Python 2中,只支持ASCII字符码即0-255\n",
    "- Python 3中,支持所有Unicode码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b9f8a2aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25105"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord(\"我\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d4f829b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(25105)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3de02656",
   "metadata": {},
   "source": [
    "## round()函数\n",
    "\n",
    "- Python 2中,数字5四舍五入到离0较远的一边\t\n",
    "- Python 3中,数字5近似到偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1ec96a68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62abe480",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.5)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b6c52125",
   "metadata": {},
   "source": [
    "## 迭代器对象的下一个值\n",
    "\n",
    "- Python 2中,`.next()`方法\n",
    "- Python 3中,`.__next__()`方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1e00cf64",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "04bbd79a",
   "metadata": {},
   "outputs": [],
   "source": [
    "i = a.__iter__()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8e2001ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i.__next__()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7efacbf0",
   "metadata": {},
   "source": [
    "## pathlib模块\n",
    "\n",
    "- Python 3中,新增pathlib模块,处理路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "78ab9791",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "94d8f514",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = Path(\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4ad5b46f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('123.txt')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p / \"123.txt\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "359c7dc0",
   "metadata": {},
   "source": [
    "## urllib模块\n",
    "\n",
    "- Python 2中,urllib和urllib2两个模块\n",
    "- Python 3中,功能移到urllib.request和urllib.parse两个子模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab63e19a",
   "metadata": {},
   "source": [
    "## 运算符@\n",
    "\n",
    "- Python 3中,矩阵乘法可以以运算符`@`实现:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a51c1f99",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4cda0dd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2], [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e3c23473",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a @ a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "75ebfc2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dot(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1736850d",
   "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.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}