Skip to content

Commit 5b9bfcd

Browse files
committed
python3code
1 parent d73fb7a commit 5b9bfcd

22 files changed

+228
-123
lines changed

README.md

+34-123
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,36 @@
1-
#This is for everyone.
2-
31
>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)
42
5-
#《跟老齐学Python》(入门教程)
6-
7-
From beginner to master.
8-
9-
针对零基础的学习者,试图实现从基础到精通,还要看自己的造化。
10-
11-
原名叫做《零基础学Python》,后来由于图书出版,更名为《跟老齐学Python》。
12-
13-
《跟老齐学Python》一书已经由电子工业出版社出版,于2016年3月1日起,陆续在亚马逊、京东、当当、淘宝等网站发售,请关注,并感谢支持。
14-
15-
此处的在线版与上述印刷的版本有所不同。在印刷出来之后,又发觉有一些地方需要修改,于是就对一部分进行了重写,并且在和朋友们交流后,也会吸收很多意见和建议,用之于修改在线版。其中改动最大的在于,本教程不再包括“实战”部分,而是专门叫做面向零基础的“入门教程”。对于实战部分,我会在适当时候专门撰写。
16-
17-
##注意:以下仅仅是部分目录,原文已经删除。原因见:[https://qiwsir.github.io](https://qiwsir.github.io)中的说明。
18-
19-
#第壹季 基础
20-
21-
##第零章 预备
22-
23-
1. [关于python的故事](./01.md)
24-
2. [从小工到专家](./02.md)
25-
3. [安装python的开发环境](./03.md)
26-
4. [集成开发环境](./101.md)==>集成开发环境;python的IDE
27-
28-
##第壹章 基本数据类型
29-
30-
1. [数和四则运算](./102.md)==>整数和浮点数;变量;整数溢出问题;
31-
2. [除法](./103.md)==>整数、浮点数相除;`from __future__ import division`;余数;四舍五入;
32-
3. [常用数学函数和运算优先级](./104.md)==>math模块,求绝对值,运算优先级
33-
4. [写一个简单程序](./105.md)==>程序和语句,注释
34-
5. [字符串(1)](./106.md)==>字符串定义,转义符,字符串拼接,str()与repr()区别
35-
6. [字符串(2)](./107.md)==>raw_input,print,内建函数,原始字符串,再做一个小程序
36-
7. [字符串(3)](./108.md)==>字符串和序列,索引,切片,基本操作
37-
8. [字符串(4)](./109.md)==>字符串格式化,常用的字符串方法
38-
9. [字符编码](./110.md)==>编码的基础知识,python中避免汉字乱码
39-
10. [列表(1)](./111.md)==>列表定义,索引和切片,列表反转,元素追加,基本操作
40-
11. [列表(2)](./112.md)==>列表append/extend/index/count方法,可迭代的和判断方法,列表原地修改
41-
12. [列表(3)](./113.md)==>列表pop/remove/reverse/sort方法
42-
13. [回顾列表和字符串](./114.md)==>比较列表和字符串的相同点和不同点
43-
14. [元组](./115.md)==>元组定义和基本操作,使用意义
44-
15. [字典(1)](./116.md)==>字典创建方法、基本操作(长度、读取值、删除值、判断键是否存在)
45-
16. [字典(2)](./117.md)==>字典方法:copy/deepcopy/clear/get/setdefault/items/iteritems/keys/iterkeys/values/itervalues/pop/popitem/update/has_key
46-
17. [集合(1)](./118.md)==>创建集合,集合方法:add/update,pop/remove/discard/clear,可哈希与不可哈希
47-
18. [集合(2)](./119.md)==>不可变集合,集合关系
48-
49-
##第贰章 语句和文件
50-
51-
1. [运算符](./120.md)==>算数运算符,比较运算符,逻辑运算符/布尔类型
52-
2. [语句(1)](./121.md)==>print, import, 赋值语句、增量赋值
53-
3. [语句(2)](./122.md)==>if...elif...else语句,三元操作
54-
4. [语句(3)](./123.md)==>for循环,range(),循环字典
55-
5. [语句(4)](./124.md)==>并行迭代:zip(),enumerate(),list解析
56-
6. [语句(5)](./125.md)==>while循环,while...else,for...else
57-
7. [文件(1)](./126.md)==>文件打开,读取,写入
58-
8. [文件(2)](./127.md)==>文件状态,read/readline/readlines,大文件读取,seek
59-
9. [迭代](./128.md)==>迭代含义,iter()
60-
10. [练习](./129.md)==>通过四个练习,综合运用以前所学
61-
11. [自省](./130.md)==>自省概念,联机帮助,dir(),文档字符串,检查对象,文档
62-
63-
##第叁章 函数
64-
65-
1. [函数(1)](./201.md)==>定义函数方法,调用函数方法,命名方法,使用函数注意事项
66-
2. [函数(2)](./202.md)==>函数返回值,函数文档,形参和实参
67-
3. [函数(3)](./203.md)==>收集参数:`*``**`,及其逆过程,复习参数知识
68-
4. [函数(4)](./204.md)==>递归、传递函数、嵌套函数和装饰器
69-
5. [函数(5)](./242.md)==>闭包
70-
6. [函数(5)](./237.md)==>filter、map、reduce、lambda、yield
71-
7. [函数练习](./205.md)==>解一元二次方程,统计考试成绩,找素数
72-
8. [zip()补充](./236.md)==>对zip()函数的用法进行补充说明
73-
9. [命名空间](./241.md)==>全局变量和局部变量,作用域,命名空间
74-
75-
#第贰季 进阶
76-
77-
##第肆章 类
78-
79-
1. [类(1)](./206.md)==>类的初步认识和基本概念理解:问题空间、对象、面向对象、类和实例化类
80-
2. [类(2)](./207.md)==>新式类和旧式类,初步创建类和实例化
81-
3. [类(3)](./208.md)==>类属性、创建实例、实例属性、self作用、类内外数据流转
82-
4. [类(4)](./238.md)==>绑定方法和非绑定方法,类方法和静态方法
83-
5. [类(5)](./209.md)==>继承,super,多重继承
84-
6. [多态和封装](./211.md)==>多态,封装和私有化
85-
7. [定制类](./239.md)==>类和类型,定制类
86-
8. [黑魔法](./240.md)==>优化内存的`__slots__`,属性拦截
87-
9. [迭代器](./214.md)==>迭代器方法`__iter__`,`netx()`
88-
11. [生成器](./215.md)==>生成器定义,yield,生成器方法
89-
12. [上下文管理器](./235.md)==>上下文管理器的基本概念,使用方法和contextlib模块
90-
91-
##第伍章 错误和异常
92-
93-
1. [错误和异常(1)](./216.md)==>什么是错误和异常,常见异常类型,处理异常(try...except...)
94-
2. [错误和异常(2)](./217.md)==>处理多个异常,else子句,finally子句
95-
3. [错误和异常(3)](./218.md)==>assert断言,异常小结
96-
97-
##第陆章 模块
98-
99-
1. [编写模块](./219.md)==>模块是程序,模块的位置
100-
2. [标准库(1)](./220.md)==>引用模块的方式,dir()查看属性和方法,模块文档和帮助
101-
3. [标准库(2)](./221.md)==>sys,copy
102-
4. [标准库(3)](./222.md)==>os模块:操作文件、目录,查看修改属性,执行系统命令,打开网页
103-
5. [标准库(4)](./223.md)==>堆的基本知识,heapq模块,deque模块
104-
6. [标准库(5)](./224.md)==>calendar模块、time模块、datetime模块
105-
7. [标准库(6)](./225.md)==>urllib模块、urllib2模块
106-
8. [标准库(7)](./226.md)==>xml.etree.ElementTree模块:遍历查询、增删改查xml,应用实例
107-
9. [标准库(8)](./227.md)==>json模块:dumps(),loads(),dump(),load(),自定义类型数据的json编码和解码
108-
10. [第三方库](./228.md)==>第三方库的模块安装方法,以requests模块为例说明
109-
110-
##第柒章 保存数据
111-
112-
1. [将数据存入文件](./229.md)==>pickle模块,shelve模块
113-
2. [MySQL数据库(1)](./230.md)==>MySQL概况,安装,python连接MySQL模块和方法
114-
3. [MySQL数据库(2)](./231.md)==>连接对象方法,游标对象方法:数据库的增删改查基本操作
115-
4. [MongoDB数据库](./232.md)==>mongodb的安装启动,pymongo模块:连接客户端,数据库的增删改查操作
116-
5. [SQLite数据库](./233.md)==>通过sqlite3模块操作SQLite数据库:连接对象方法,游标对象方法,数据库增删改查
117-
6. [电子表格](./234.md)==>python操作Excel文件的第三方库openpyxl使用方法,以及其它与Excel相关的第三方库
118-
119-
##附:网络文摘
120-
121-
1. [如何成为python高手](./n001.md)
122-
2. [ASCII、Unicode、GBK和UTF-8字符编码的区别联系](./n002.md)
123-
3. [大数据全栈式开发语言 – Python](./n003.md)
124-
4. [机器学习编程语言之争,Python夺魁](./n004.md)
125-
5. [Python 2.7.x 和 3.x 版本的重要区别](./n005.md)
3+
#《跟老齐学Python:轻松入门》
4+
5+
本项目为《跟老齐学Python:轻松入门》一书的相关代码。此书是为初学Python的朋友而作,在各大网店有售。
6+
7+
![](./python-book1.png)
8+
9+
# 相关资源
10+
11+
## 个人网站
12+
13+
[itdiffer.com](http://www.itdiffer.com)
14+
15+
## QQ群:26913719
16+
17+
说明:此QQ群是读者交流,而非作者答疑区,请特别注意。如果读者有问题,可以在群里面跟其它读者交流。作者没有答疑的义务。恕不接待任何形式的答疑。
18+
19+
关于本QQ群的更多说明:
20+
21+
加入本群,收费1元,目的是免群主审核,并且拦住广告发布人。如果对此有异议,请不要加入。
22+
因为设置了上述功能,群主无法单独邀请用户加入了(这是QQ方面的规定),所以,不要让群主邀请。
23+
更不要通过其它途径给群主一块钱,然后让群主想办法把你加到群里,群主表示做不到。
24+
25+
再次提醒:对入群收费1元,有异议者请不要加入。
26+
27+
# 本书配套小程序
28+
29+
小程序名称:跟老齐学
30+
31+
![](https://raw.githubusercontent.com/qiwsir/DjangoPracticeProject/master/smallprogramming.jpg)
32+
33+
# Python学习资源推荐:
34+
35+
系列图书《跟老齐学Python:Django实战》、《跟老齐学Python:数据分析》,各大网店和书店有售
36+
在线课程:[Python3入门和能力提升](https://www.cctalk.com/m/course/111302)

python-book1.png

262 KB
Loading

python3code/chapter05/account.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# coding:utf-8
2+
'''
3+
filename: account.py
4+
'''
5+
class Account(object):
6+
def __init__(self, number):
7+
self.number = number
8+
self.balance = 0
9+
10+
def deposit(self, amount):
11+
try:
12+
assert amount > 0
13+
self.balance += amount
14+
except:
15+
print("The money should be bigger than zero.")
16+
17+
def withdraw(self, amount):
18+
assert amount > 0
19+
if amount <= self.balance:
20+
self.balance -= amount
21+
else:
22+
print("balance is not enough.")
23+
24+
if __name__ == "__main__":
25+
a = Account(1000)
26+
a.deposit(-10)

python3code/chapter05/calculator.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding=utf-8
2+
3+
class Calculator(object):
4+
is_raise = False
5+
def calc(self, express):
6+
try:
7+
return eval(express)
8+
except ZeroDivisionError:
9+
if self.is_raise:
10+
return "zero can not be division."
11+
else:
12+
raise
13+
14+
if __name__ == "__main__":
15+
c = Calculator()
16+
c.is_raise = True
17+
print(c.calc("8/0"))
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# coding:utf-8
2+
'''
3+
filename: customexception.py
4+
'''
5+
6+
class NegativeAgeException(RuntimeError):
7+
def __init__(self, age):
8+
super().__init__()
9+
self.age = age
10+
11+
def enterage(age):
12+
if age < 0:
13+
raise NegativeAgeException("Only positive integers are allowed")
14+
15+
if age % 2 == 0:
16+
print("age is even")
17+
else:
18+
print("age is odd")
19+
20+
try:
21+
num = int(input("Enter your age: "))
22+
enterage(num)
23+
except NegativeAgeException:
24+
print("Only integers are allowed")
25+
except:
26+
print("something is wrong")

python3code/chapter05/try_except.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# coding:utf-8
2+
'''
3+
filename: try_except.py
4+
'''
5+
6+
# while 1:
7+
# print("this is a division program.")
8+
# c = input("input 'c' continue, otherwise logout:")
9+
# if c == 'c':
10+
# a = input("first number:")
11+
# b = input("second number:")
12+
# try:
13+
# print(float(a)/float(b))
14+
# print("*************************")
15+
# except ZeroDivisionError:
16+
# print("The second number can't be zero!")
17+
# print("*************************")
18+
# except ValueError:
19+
# print("please input number.")
20+
# print("************************")
21+
# else:
22+
# break
23+
24+
while 1:
25+
print("this is a division program.")
26+
c = input("input 'c' continue, otherwise logout:")
27+
if c == 'c':
28+
a = input("first number:")
29+
b = input("second number:")
30+
try:
31+
print(float(a)/float(b))
32+
print("*************************")
33+
except (ZeroDivisionError, ValueError) as e:
34+
print(e)
35+
print("********************")
36+
else:
37+
break

python3code/chapter05/tryelse.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding:utf-8
2+
'''
3+
filename: tryelse.py
4+
'''
5+
6+
while 1:
7+
try:
8+
x = input("the first number:")
9+
y = input("the second number:")
10+
11+
r = float(x)/float(y)
12+
print(r)
13+
except Exception as e:
14+
print(e)
15+
print("try again.")
16+
else:
17+
break

python3code/chapter05/tryexceptccb.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# coding:utf-8
2+
'''
3+
filename: tryexceptccb.py
4+
'''
5+
6+
while 1:
7+
print("this is a division program.")
8+
c = input("input 'c' continue, otherwise logout:")
9+
if c == 'c':
10+
a = input("first number:")
11+
b = input("second number:")
12+
try:
13+
print(float(a)/float(b))
14+
print("*************************")
15+
except ZeroDivisionError:
16+
print("The second number can't be zero!")
17+
print("*************************")
18+
else:
19+
break
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#coding:utf-8
2+
'''
3+
filename: exampleimport.py
4+
'''
5+
6+
from mypackage.A import abasic
7+
8+
if __name__ == "__main__":
9+
r = abasic.basic
10+
print(r)

python3code/chapter06/mypackage/A/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#coding:utf-8
2+
'''
3+
path: ./mypackage/A/abasic.py
4+
filename: abasic.py
5+
'''
6+
7+
from . import apython
8+
from ..B import brust
9+
10+
basic = "BASIC-" + apython.python() + "-" + brust.rust
11+
12+
#it will be Error!
13+
if __name__ == "__main__":
14+
print(basic)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#coding:utf-8
2+
'''
3+
path: ./mypackage/A/apython.py
4+
firlename: apython.py
5+
'''
6+
7+
def python():
8+
return "PYTHON"

python3code/chapter06/mypackage/B/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#coding:utf-8
2+
'''
3+
path: ./mypackage/B/brust.py
4+
filename: brust.py
5+
'''
6+
rust = 'RUST'

python3code/chapter06/mypackage/__init__.py

Whitespace-only changes.
Binary file not shown.

python3code/chapter06/pp.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# coding:utf-8
2+
'''
3+
filename: pp.py
4+
'''
5+
__all__ = ['_private_variable', 'public_teacher']
6+
7+
public_variable = "Hello, I am a public variable."
8+
_private_variable = "Hi, I am a private variable."
9+
10+
def public_teacher():
11+
print("I am a public teacher, I am from JP.")
12+
13+
def _private_teacher():
14+
print("I am a private teacher, I am from CN.")

0 commit comments

Comments
 (0)