|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Python 基础"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. 导入库与Python之禅\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['0-ipython_notebook.ipynb',\n",
- " '0-ipython_notebook_EN.ipynb',\n",
- " '1_Basics.ipynb',\n",
- " '1_Basics_EN.ipynb',\n",
- " '2_Print_Statement.ipynb',\n",
- " '2_Print_Statement_EN.ipynb',\n",
- " '3_Data_Structure_1.ipynb',\n",
- " '3_Data_Structure_1_EN.ipynb',\n",
- " '4_Data_Structure_2.ipynb',\n",
- " '4_Data_Structure_2_EN.ipynb',\n",
- " '5_Control_Flow.ipynb',\n",
- " '5_Control_Flow_EN.ipynb',\n",
- " '6_Function.ipynb',\n",
- " '6_Function_EN.ipynb',\n",
- " '7_Class.ipynb',\n",
- " '7_Class_EN.ipynb',\n",
- " 'Python.pdf',\n",
- " 'README.md',\n",
- " 'README_ENG.md',\n",
- " 'images',\n",
- " 'test.txt',\n",
- " '.ipynb_checkpoints']"
- ]
- },
- "execution_count": 1,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 导入库\n",
- "import os\n",
- "os.listdir('.')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "The Zen of Python, by Tim Peters\n",
- "\n",
- "Beautiful is better than ugly.\n",
- "Explicit is better than implicit.\n",
- "Simple is better than complex.\n",
- "Complex is better than complicated.\n",
- "Flat is better than nested.\n",
- "Sparse is better than dense.\n",
- "Readability counts.\n",
- "Special cases aren't special enough to break the rules.\n",
- "Although practicality beats purity.\n",
- "Errors should never pass silently.\n",
- "Unless explicitly silenced.\n",
- "In the face of ambiguity, refuse the temptation to guess.\n",
- "There should be one-- and preferably only one --obvious way to do it.\n",
- "Although that way may not be obvious at first unless you're Dutch.\n",
- "Now is better than never.\n",
- "Although never is often better than *right* now.\n",
- "If the implementation is hard to explain, it's a bad idea.\n",
- "If the implementation is easy to explain, it may be a good idea.\n",
- "Namespaces are one honking great idea -- let's do more of those!\n"
- ]
- }
- ],
- "source": [
- "import this"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**Python 之禅, by Tim Peters**\n",
- "```\n",
- "优美胜于丑陋(Python 以编写优美的代码为目标)\n",
- "明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)\n",
- "简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)\n",
- "复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)\n",
- "扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)\n",
- "间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)\n",
- "可读性很重要(优美的代码是可读的)\n",
- "即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)\n",
- "不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)\n",
- "当存在多种可能,不要尝试去猜测\n",
- "而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)\n",
- "虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido)\n",
- "做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)\n",
- "如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)\n",
- "命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)\n",
- "```"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. 变量"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "用于表示某物或值的名称称为**变量**。在Python中,可以声明变量并为其赋值,如下所示:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "x = 2\n",
- "y = 5\n",
- "xy = 'Hey'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "7 Hey\n"
- ]
- }
- ],
- "source": [
- "print(x+y, xy)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "多变量可以被赋予相同的名字。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "x = y = 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 1\n"
- ]
- }
- ],
- "source": [
- "print(x,y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. 运算符"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.1 算数运算符"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| + | Addition |\n",
- "| - | Subtraction |\n",
- "| / | division |\n",
- "| % | mod |\n",
- "| * | multiplication |\n",
- "| // | floor division |\n",
- "| ** | to the power of |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1+2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2-1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1*2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1/2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Python3版本之后,自动将整数的除法转换成浮点数,这样和人类的直觉比较一致。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1.0/2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1/2.0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "5"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "15%10"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "地板除法(floor divide)就是将这样得到的结果转换成最接近的整数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1.0"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2.8//2.0"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.2 关系运算符"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| == | True, if it is equal |\n",
- "| != | True, if not equal to |\n",
- "| < | less than |\n",
- "| > | greater than |\n",
- "| <= | less than or equal to |\n",
- "| >= | greater than or equal to |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "z = 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z == 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z > 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.3 位运算符"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| & | Logical And |\n",
- "| l | Logical OR |\n",
- "| ^ | XOR |\n",
- "| ~ | Negate |\n",
- "| >> | Right shift |\n",
- "| << | Left shift |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "a = 2 #10\n",
- "b = 3 #11"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n",
- "0b10\n"
- ]
- }
- ],
- "source": [
- "print(a & b)\n",
- "print(bin(a&b))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "5 >> 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "0000 0101 -> 5 \n",
- "\n",
- "将数字向右移1并填充0。\n",
- "\n",
- "0000 0010 -> 2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "5 << 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "0000 0101 -> 5 \n",
- "\n",
- "将数字做移1并填充0。\n",
- "\n",
- "0000 1010 -> 10"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 4. 内置函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Python带有预先构建的函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.1 从一个进制到另一个进制的转换"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "从十六进制到十进制的转换是通过向十六进制值添加前缀**0x**来完成的,反之亦然,使用内置的**hex()**,通过在八进制值中添加前缀**0**,从八进制到十进制,或者通过使用内建函数**oct()** 从八进制到十进制。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'0xaa'"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "hex(170)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "170"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "0xAA"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'0o10'"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "oct(8)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**int( )** 在用于转换时接受两个值,一个是不同数字系统中的值,另一个是它的基数。注意,不同数字系统中的输入数字应该是字符串类型。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "8\n",
- "170\n",
- "10\n"
- ]
- }
- ],
- "source": [
- "print(int('010',8))\n",
- "print(int('0xaa',16))\n",
- "print(int('1010',2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**int( )** 也可用于仅获取浮点数的整数值,或用于将字符串类型的数字转换为整数格式。相似地,函数 **str( )** 可以被用来将整形转化为字符串类型。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "7\n",
- "7\n"
- ]
- }
- ],
- "source": [
- "print(int(7.7))\n",
- "print(int('7'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "同样需要注意的是 **bin( )** 被用作二进制而 **float( )** 被用作十进制、浮点值。**chr( )** 用于将ASCII码转换为等效的字母表,**ord( )** 被反过来使用。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'b'"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "chr(98)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "98"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "ord('b')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.2 数值函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**round( )** 函数将输入值四舍五入到指定的位数或最接近的整数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "6\n",
- "4.56\n"
- ]
- }
- ],
- "source": [
- "print(round(5.6231))\n",
- "print(round(4.55892, 2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**complex( )** 被用来定义一个复数 **abs( )** 输出相同的绝对值。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5.385164807134504\n"
- ]
- }
- ],
- "source": [
- "c =complex('5+2j')\n",
- "print(abs(c))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**divmod(x,y)** 输出商和余数在元组的格式(商,余数)。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(4, 1)"
- ]
- },
- "execution_count": 31,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "divmod(9,2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果第一个参数是该类的实例,**isinstance( )** 则返回True。还可以同时检查多个类。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n",
- "True\n"
- ]
- }
- ],
- "source": [
- "print(isinstance(1, int))\n",
- "print(isinstance(1.0,int))\n",
- "print(isinstance(1.0,(int,float)))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pow(x,y,z)** 可以被用来找到幂函数$x^y$也可以找到第三个指定数字的mod值,即:($x^y$ % z)。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "27\n",
- "2\n"
- ]
- }
- ],
- "source": [
- "print(pow(3,3))\n",
- "print(pow(3,3,5))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**range( )** 函数输出指定范围的整数。它还可以用于通过指定特定范围内的两个数字之间的差来生成一个序列,元素以列表的形式返回。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 1, 2]\n",
- "[2, 3, 4, 5, 6, 7, 8]\n",
- "[2, 10, 18, 26]\n"
- ]
- }
- ],
- "source": [
- "print(list(range(3)))\n",
- "print(list(range(2,9)))\n",
- "print(list(range(2,27,8)))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.3 接受用户输入"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**input( )** 接受输入并以字符串的形式保存。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Type something here and it will be stored in variable abc \thello\n"
- ]
- }
- ],
- "source": [
- "abc = input(\"Type something here and it will be stored in variable abc \\t\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "str"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(abc)"
- ]
- }
- ],
- "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.7.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|