diff --git a/README.md b/README.md index b7e5620..2d15805 100755 --- a/README.md +++ b/README.md @@ -1,125 +1,36 @@ -#This is for everyone. - >In the begning when God created the heavens and the earth. the earth was a formless void and darkness covered the face of the deep, while a wind from God swept over the face of the waters. Then God said,"Let there be light"; and there was light. And God saw that the light was good; and God separated the light from the darkness. (GENESIS 1:1-4) -#《跟老齐学Python》(入门教程) - -From beginner to master. - -针对零基础的学习者,试图实现从基础到精通,还要看自己的造化。 - -原名叫做《零基础学Python》,后来由于图书出版,更名为《跟老齐学Python》。 - -《跟老齐学Python》一书已经由电子工业出版社出版,于2016年3月1日起,陆续在亚马逊、京东、当当、淘宝等网站发售,请关注,并感谢支持。 - -此处的在线版与上述印刷的版本有所不同。在印刷出来之后,又发觉有一些地方需要修改,于是就对一部分进行了重写,并且在和朋友们交流后,也会吸收很多意见和建议,用之于修改在线版。其中改动最大的在于,本教程不再包括“实战”部分,而是专门叫做面向零基础的“入门教程”。对于实战部分,我会在适当时候专门撰写。 - -##注意:以下仅仅是部分目录,原文已经删除。原因见:[https://qiwsir.github.io](https://qiwsir.github.io)中的说明。 - -#第壹季 基础 - -##第零章 预备 - -1. [关于python的故事](./01.md) -2. [从小工到专家](./02.md) -3. [安装python的开发环境](./03.md) -4. [集成开发环境](./101.md)==>集成开发环境;python的IDE - -##第壹章 基本数据类型 - -1. [数和四则运算](./102.md)==>整数和浮点数;变量;整数溢出问题; -2. [除法](./103.md)==>整数、浮点数相除;`from __future__ import division`;余数;四舍五入; -3. [常用数学函数和运算优先级](./104.md)==>math模块,求绝对值,运算优先级 -4. [写一个简单程序](./105.md)==>程序和语句,注释 -5. [字符串(1)](./106.md)==>字符串定义,转义符,字符串拼接,str()与repr()区别 -6. [字符串(2)](./107.md)==>raw_input,print,内建函数,原始字符串,再做一个小程序 -7. [字符串(3)](./108.md)==>字符串和序列,索引,切片,基本操作 -8. [字符串(4)](./109.md)==>字符串格式化,常用的字符串方法 -9. [字符编码](./110.md)==>编码的基础知识,python中避免汉字乱码 -10. [列表(1)](./111.md)==>列表定义,索引和切片,列表反转,元素追加,基本操作 -11. [列表(2)](./112.md)==>列表append/extend/index/count方法,可迭代的和判断方法,列表原地修改 -12. [列表(3)](./113.md)==>列表pop/remove/reverse/sort方法 -13. [回顾列表和字符串](./114.md)==>比较列表和字符串的相同点和不同点 -14. [元组](./115.md)==>元组定义和基本操作,使用意义 -15. [字典(1)](./116.md)==>字典创建方法、基本操作(长度、读取值、删除值、判断键是否存在) -16. [字典(2)](./117.md)==>字典方法:copy/deepcopy/clear/get/setdefault/items/iteritems/keys/iterkeys/values/itervalues/pop/popitem/update/has_key -17. [集合(1)](./118.md)==>创建集合,集合方法:add/update,pop/remove/discard/clear,可哈希与不可哈希 -18. [集合(2)](./119.md)==>不可变集合,集合关系 - -##第贰章 语句和文件 - -1. [运算符](./120.md)==>算数运算符,比较运算符,逻辑运算符/布尔类型 -2. [语句(1)](./121.md)==>print, import, 赋值语句、增量赋值 -3. [语句(2)](./122.md)==>if...elif...else语句,三元操作 -4. [语句(3)](./123.md)==>for循环,range(),循环字典 -5. [语句(4)](./124.md)==>并行迭代:zip(),enumerate(),list解析 -6. [语句(5)](./125.md)==>while循环,while...else,for...else -7. [文件(1)](./126.md)==>文件打开,读取,写入 -8. [文件(2)](./127.md)==>文件状态,read/readline/readlines,大文件读取,seek -9. [迭代](./128.md)==>迭代含义,iter() -10. [练习](./129.md)==>通过四个练习,综合运用以前所学 -11. [自省](./130.md)==>自省概念,联机帮助,dir(),文档字符串,检查对象,文档 - -##第叁章 函数 - -1. [函数(1)](./201.md)==>定义函数方法,调用函数方法,命名方法,使用函数注意事项 -2. [函数(2)](./202.md)==>函数返回值,函数文档,形参和实参 -3. [函数(3)](./203.md)==>收集参数:`*`和`**`,及其逆过程,复习参数知识 -4. [函数(4)](./204.md)==>递归、传递函数、嵌套函数和装饰器 -5. [函数(5)](./242.md)==>闭包 -6. [函数(5)](./237.md)==>filter、map、reduce、lambda、yield -7. [函数练习](./205.md)==>解一元二次方程,统计考试成绩,找素数 -8. [zip()补充](./236.md)==>对zip()函数的用法进行补充说明 -9. [命名空间](./241.md)==>全局变量和局部变量,作用域,命名空间 - -#第贰季 进阶 - -##第肆章 类 - -1. [类(1)](./206.md)==>类的初步认识和基本概念理解:问题空间、对象、面向对象、类和实例化类 -2. [类(2)](./207.md)==>新式类和旧式类,初步创建类和实例化 -3. [类(3)](./208.md)==>类属性、创建实例、实例属性、self作用、类内外数据流转 -4. [类(4)](./238.md)==>绑定方法和非绑定方法,类方法和静态方法 -5. [类(5)](./209.md)==>继承,super,多重继承 -6. [多态和封装](./211.md)==>多态,封装和私有化 -7. [定制类](./239.md)==>类和类型,定制类 -8. [黑魔法](./240.md)==>优化内存的`__slots__`,属性拦截 -9. [迭代器](./214.md)==>迭代器方法`__iter__`,`netx()` -11. [生成器](./215.md)==>生成器定义,yield,生成器方法 -12. [上下文管理器](./235.md)==>上下文管理器的基本概念,使用方法和contextlib模块 - -##第伍章 错误和异常 - -1. [错误和异常(1)](./216.md)==>什么是错误和异常,常见异常类型,处理异常(try...except...) -2. [错误和异常(2)](./217.md)==>处理多个异常,else子句,finally子句 -3. [错误和异常(3)](./218.md)==>assert断言,异常小结 - -##第陆章 模块 - -1. [编写模块](./219.md)==>模块是程序,模块的位置 -2. [标准库(1)](./220.md)==>引用模块的方式,dir()查看属性和方法,模块文档和帮助 -3. [标准库(2)](./221.md)==>sys,copy -4. [标准库(3)](./222.md)==>os模块:操作文件、目录,查看修改属性,执行系统命令,打开网页 -5. [标准库(4)](./223.md)==>堆的基本知识,heapq模块,deque模块 -6. [标准库(5)](./224.md)==>calendar模块、time模块、datetime模块 -7. [标准库(6)](./225.md)==>urllib模块、urllib2模块 -8. [标准库(7)](./226.md)==>xml.etree.ElementTree模块:遍历查询、增删改查xml,应用实例 -9. [标准库(8)](./227.md)==>json模块:dumps(),loads(),dump(),load(),自定义类型数据的json编码和解码 -10. [第三方库](./228.md)==>第三方库的模块安装方法,以requests模块为例说明 - -##第柒章 保存数据 - -1. [将数据存入文件](./229.md)==>pickle模块,shelve模块 -2. [MySQL数据库(1)](./230.md)==>MySQL概况,安装,python连接MySQL模块和方法 -3. [MySQL数据库(2)](./231.md)==>连接对象方法,游标对象方法:数据库的增删改查基本操作 -4. [MongoDB数据库](./232.md)==>mongodb的安装启动,pymongo模块:连接客户端,数据库的增删改查操作 -5. [SQLite数据库](./233.md)==>通过sqlite3模块操作SQLite数据库:连接对象方法,游标对象方法,数据库增删改查 -6. [电子表格](./234.md)==>python操作Excel文件的第三方库openpyxl使用方法,以及其它与Excel相关的第三方库 - -##附:网络文摘 - -1. [如何成为python高手](./n001.md) -2. [ASCII、Unicode、GBK和UTF-8字符编码的区别联系](./n002.md) -3. [大数据全栈式开发语言 – Python](./n003.md) -4. [机器学习编程语言之争,Python夺魁](./n004.md) -5. [Python 2.7.x 和 3.x 版本的重要区别](./n005.md) +#《跟老齐学Python:轻松入门》 + +本项目为《跟老齐学Python:轻松入门》一书的相关代码。此书是为初学Python的朋友而作,在各大网店有售。 + +![](./python-book1.png) + +# 相关资源 + +## 个人网站 + +[itdiffer.com](http://www.itdiffer.com) + +## QQ群:26913719 + +说明:此QQ群是读者交流,而非作者答疑区,请特别注意。如果读者有问题,可以在群里面跟其它读者交流。作者没有答疑的义务。恕不接待任何形式的答疑。 + +关于本QQ群的更多说明: + + 加入本群,收费1元,目的是免群主审核,并且拦住广告发布人。如果对此有异议,请不要加入。 + 因为设置了上述功能,群主无法单独邀请用户加入了(这是QQ方面的规定),所以,不要让群主邀请。 + 更不要通过其它途径给群主一块钱,然后让群主想办法把你加到群里,群主表示做不到。 + +再次提醒:对入群收费1元,有异议者请不要加入。 + +# 本书配套小程序 + +小程序名称:跟老齐学 + +![](https://raw.githubusercontent.com/qiwsir/DjangoPracticeProject/master/smallprogramming.jpg) + +# Python学习资源推荐: + + 系列图书《跟老齐学Python:Django实战》、《跟老齐学Python:数据分析》,各大网店和书店有售 + 在线课程:[Python3入门和能力提升](https://www.cctalk.com/m/course/111302) diff --git a/python-book1.png b/python-book1.png new file mode 100644 index 0000000..5ec8ad1 Binary files /dev/null and b/python-book1.png differ diff --git a/python3code/chapter05/account.py b/python3code/chapter05/account.py new file mode 100644 index 0000000..8b65239 --- /dev/null +++ b/python3code/chapter05/account.py @@ -0,0 +1,26 @@ +# coding:utf-8 +''' +filename: account.py +''' +class Account(object): + def __init__(self, number): + self.number = number + self.balance = 0 + + def deposit(self, amount): + try: + assert amount > 0 + self.balance += amount + except: + print("The money should be bigger than zero.") + + def withdraw(self, amount): + assert amount > 0 + if amount <= self.balance: + self.balance -= amount + else: + print("balance is not enough.") + +if __name__ == "__main__": + a = Account(1000) + a.deposit(-10) diff --git a/python3code/chapter05/calculator.py b/python3code/chapter05/calculator.py new file mode 100644 index 0000000..a5a6683 --- /dev/null +++ b/python3code/chapter05/calculator.py @@ -0,0 +1,17 @@ +# coding=utf-8 + +class Calculator(object): + is_raise = False + def calc(self, express): + try: + return eval(express) + except ZeroDivisionError: + if self.is_raise: + return "zero can not be division." + else: + raise + +if __name__ == "__main__": + c = Calculator() + c.is_raise = True + print(c.calc("8/0")) diff --git a/python3code/chapter05/customexception.py b/python3code/chapter05/customexception.py new file mode 100644 index 0000000..bc49d3d --- /dev/null +++ b/python3code/chapter05/customexception.py @@ -0,0 +1,26 @@ +# coding:utf-8 +''' +filename: customexception.py +''' + +class NegativeAgeException(RuntimeError): + def __init__(self, age): + super().__init__() + self.age = age + +def enterage(age): + if age < 0: + raise NegativeAgeException("Only positive integers are allowed") + + if age % 2 == 0: + print("age is even") + else: + print("age is odd") + +try: + num = int(input("Enter your age: ")) + enterage(num) +except NegativeAgeException: + print("Only integers are allowed") +except: + print("something is wrong") diff --git a/python3code/chapter05/try_except.py b/python3code/chapter05/try_except.py new file mode 100644 index 0000000..759f496 --- /dev/null +++ b/python3code/chapter05/try_except.py @@ -0,0 +1,37 @@ +# coding:utf-8 +''' +filename: try_except.py +''' + +# while 1: +# print("this is a division program.") +# c = input("input 'c' continue, otherwise logout:") +# if c == 'c': +# a = input("first number:") +# b = input("second number:") +# try: +# print(float(a)/float(b)) +# print("*************************") +# except ZeroDivisionError: +# print("The second number can't be zero!") +# print("*************************") +# except ValueError: +# print("please input number.") +# print("************************") +# else: +# break + +while 1: + print("this is a division program.") + c = input("input 'c' continue, otherwise logout:") + if c == 'c': + a = input("first number:") + b = input("second number:") + try: + print(float(a)/float(b)) + print("*************************") + except (ZeroDivisionError, ValueError) as e: + print(e) + print("********************") + else: + break diff --git a/python3code/chapter05/tryelse.py b/python3code/chapter05/tryelse.py new file mode 100644 index 0000000..d61fd78 --- /dev/null +++ b/python3code/chapter05/tryelse.py @@ -0,0 +1,17 @@ +# coding:utf-8 +''' +filename: tryelse.py +''' + +while 1: + try: + x = input("the first number:") + y = input("the second number:") + + r = float(x)/float(y) + print(r) + except Exception as e: + print(e) + print("try again.") + else: + break diff --git a/python3code/chapter05/tryexceptccb.py b/python3code/chapter05/tryexceptccb.py new file mode 100644 index 0000000..75b2df8 --- /dev/null +++ b/python3code/chapter05/tryexceptccb.py @@ -0,0 +1,19 @@ +# coding:utf-8 +''' +filename: tryexceptccb.py +''' + +while 1: + print("this is a division program.") + c = input("input 'c' continue, otherwise logout:") + if c == 'c': + a = input("first number:") + b = input("second number:") + try: + print(float(a)/float(b)) + print("*************************") + except ZeroDivisionError: + print("The second number can't be zero!") + print("*************************") + else: + break diff --git a/python3code/chapter06/exampleimport.py b/python3code/chapter06/exampleimport.py new file mode 100644 index 0000000..974992f --- /dev/null +++ b/python3code/chapter06/exampleimport.py @@ -0,0 +1,10 @@ +#coding:utf-8 +''' +filename: exampleimport.py +''' + +from mypackage.A import abasic + +if __name__ == "__main__": + r = abasic.basic + print(r) diff --git a/python3code/chapter06/mypackage/A/__init__.py b/python3code/chapter06/mypackage/A/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python3code/chapter06/mypackage/A/__pycache__/__init__.cpython-36.pyc b/python3code/chapter06/mypackage/A/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..6d948b5 Binary files /dev/null and b/python3code/chapter06/mypackage/A/__pycache__/__init__.cpython-36.pyc differ diff --git a/python3code/chapter06/mypackage/A/__pycache__/abasic.cpython-36.pyc b/python3code/chapter06/mypackage/A/__pycache__/abasic.cpython-36.pyc new file mode 100644 index 0000000..d7a9015 Binary files /dev/null and b/python3code/chapter06/mypackage/A/__pycache__/abasic.cpython-36.pyc differ diff --git a/python3code/chapter06/mypackage/A/__pycache__/apython.cpython-36.pyc b/python3code/chapter06/mypackage/A/__pycache__/apython.cpython-36.pyc new file mode 100644 index 0000000..6bd9e2c Binary files /dev/null and b/python3code/chapter06/mypackage/A/__pycache__/apython.cpython-36.pyc differ diff --git a/python3code/chapter06/mypackage/A/abasic.py b/python3code/chapter06/mypackage/A/abasic.py new file mode 100644 index 0000000..5a049b2 --- /dev/null +++ b/python3code/chapter06/mypackage/A/abasic.py @@ -0,0 +1,14 @@ +#coding:utf-8 +''' + path: ./mypackage/A/abasic.py + filename: abasic.py +''' + +from . import apython +from ..B import brust + +basic = "BASIC-" + apython.python() + "-" + brust.rust + +#it will be Error! +if __name__ == "__main__": + print(basic) diff --git a/python3code/chapter06/mypackage/A/apython.py b/python3code/chapter06/mypackage/A/apython.py new file mode 100644 index 0000000..a545deb --- /dev/null +++ b/python3code/chapter06/mypackage/A/apython.py @@ -0,0 +1,8 @@ +#coding:utf-8 +''' + path: ./mypackage/A/apython.py + firlename: apython.py +''' + +def python(): + return "PYTHON" diff --git a/python3code/chapter06/mypackage/B/__init__.py b/python3code/chapter06/mypackage/B/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python3code/chapter06/mypackage/B/__pycache__/__init__.cpython-36.pyc b/python3code/chapter06/mypackage/B/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..2217899 Binary files /dev/null and b/python3code/chapter06/mypackage/B/__pycache__/__init__.cpython-36.pyc differ diff --git a/python3code/chapter06/mypackage/B/__pycache__/brust.cpython-36.pyc b/python3code/chapter06/mypackage/B/__pycache__/brust.cpython-36.pyc new file mode 100644 index 0000000..50bdb58 Binary files /dev/null and b/python3code/chapter06/mypackage/B/__pycache__/brust.cpython-36.pyc differ diff --git a/python3code/chapter06/mypackage/B/brust.py b/python3code/chapter06/mypackage/B/brust.py new file mode 100644 index 0000000..22e7ccd --- /dev/null +++ b/python3code/chapter06/mypackage/B/brust.py @@ -0,0 +1,6 @@ +#coding:utf-8 +''' + path: ./mypackage/B/brust.py + filename: brust.py +''' +rust = 'RUST' \ No newline at end of file diff --git a/python3code/chapter06/mypackage/__init__.py b/python3code/chapter06/mypackage/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python3code/chapter06/mypackage/__pycache__/__init__.cpython-36.pyc b/python3code/chapter06/mypackage/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..bf9e02c Binary files /dev/null and b/python3code/chapter06/mypackage/__pycache__/__init__.cpython-36.pyc differ diff --git a/python3code/chapter06/pp.py b/python3code/chapter06/pp.py new file mode 100644 index 0000000..a94deb4 --- /dev/null +++ b/python3code/chapter06/pp.py @@ -0,0 +1,14 @@ +# coding:utf-8 +''' +filename: pp.py +''' +__all__ = ['_private_variable', 'public_teacher'] + +public_variable = "Hello, I am a public variable." +_private_variable = "Hi, I am a private variable." + +def public_teacher(): + print("I am a public teacher, I am from JP.") + +def _private_teacher(): + print("I am a private teacher, I am from CN.")