600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 暑假爆肝整理这篇python基础教程 全是干货 学完基础就过关(收藏加好评吧)

暑假爆肝整理这篇python基础教程 全是干货 学完基础就过关(收藏加好评吧)

时间:2023-11-08 05:40:39

相关推荐

暑假爆肝整理这篇python基础教程 全是干货 学完基础就过关(收藏加好评吧)

内容全干货,不说废话。代码讲解都在注释哦。希望挨个看着敲一遍,不要复制粘贴哦。

本基础分为十个部分:

1-数字

2-字符串

3-列表

4-控制流

5-函数

6-数据结构

7-模块

8-深层输入与输出

9-异常和错误

10-类

11-标准库简介

12-协程

为懒人准备,如果需要下载这些代码,本项目github链接下载即可:

/89461561511656/Introduction-to-python3.9.git

为小白准备的交流群:

970353786

为python爱好者准备的项目链接:qq机器人

/aisisi/p/15055168.html

mysql数据库入门到精通学习链接:

/weixin_46211269/article/details/119086242?spm=1001..3001.5501

第一部分:数字

"""作者:川川时间:/7/27"""#加减# a=2# b=2# c=a-b#d=a-b# print(c,d)#乘除# a=(50-5*6)/4# print(a)# a=8/5# print(a)'''/返回为float,//返回整数,%返回余数'''# a=17/3# print(a)# a=17//3# print(a)# a=5*3+2# print(a)'''幂运算 **'''# a=4**2# b=2**3# print(a,b)

第二部分:字符串

"""作者:川川时间:/7/27"""'''单引号和双引号效果相同 反斜杠 \ 用于转义'''# a='spam eggs'# b='doesn\'t'# c="\"Yes,\" they said."# print(a)# print(b)# print(c)'''如果不希望前置 \ 的字符转义成特殊字符,可以使用 原始字符串,在引号前添加 r '''#比如说我们路径问题# c=r'C:\some\name'# print(c)# print(r'C:\some\name')'''三引号使用'''# print("""\# Usage: thingy [OPTIONS]#-h Display this usage message#-H hostnameHostname to connect to# """)'''字符串可以用 + 合并(粘到一起),也可以用 * 重复'''# a='how are '+'you'# b='zhu'# c=b*3# print(a)# print(c)'''相邻的两个或多个 字符串字面值 (引号标注的字符)会自动合并'''# a= 'Py''thon'# print(a)'''拆分长字符串'''# text = ('Put several strings within parentheses '# 'to have them joined together.')# print(text)######### 切片#########'''字符串支持 索引 (下标访问),第一个字符的索引是 0。'''# word = 'Python'# print(word[0],word[2])'''索引还支持负数,用负数索引时,从右边开始计数'''# print(word[-1],word[-2])# text='s'# print(text[0])'''除了索引,字符串还支持 切片。输出结果包含切片开始,但不包含切片结束'''# print(word[0:2])# print(word[0:5])'''省略开始索引时,默认值为 0,省略结束索引时,默认为到字符串的结尾'''# print(word[:2])# print(word[4:])# print(word[-2:])'''切片会自动处理越界索引'''# print(word[4:42])# print(word[42:])''' 字符串不能修改'''# word[0] = 'J'#错误'''要生成不同的字符串,应新建一个字符串'''# z='J' + word[1:]# print(z)'''内置函数 len() 返回字符串的长度,一个空格也算一个长度'''# s = 'supercalifragilisticexpia lidocious'# print(len(s))

第三部分:列表

"""作者:川川时间:/7/27"""# squares = [1, 4, 9, 16, 25]# print(squares)'''支持索引和切片'''# print(squares[1])# print(squares[-1])# print(squares[-3:])'''列表还支持合并操作'''# c=squares + [36, 49, 64, 81, 100]# print(c)'''列表内容可修改'''# cubes = [1, 8, 27, 65, 125]# cubes[3] = 64# print(cubes)'''append() 方法 可以在列表结尾添加新元素'''# cubes.append(216)# cubes.append(7 ** 3)# print(cubes)'''切片赋值可以改变列表大小,甚至清空整个列表'''# letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']# print(letters)# letters[2:5] = ['C', 'D', 'E']# print(letters)# letters[2:5] = []# print(letters)# letters[:] = []# print(letters)'''内置函数 len() 也支持列表'''# letters = ['a', 'b', 'c', 'd']# print(len(letters))'''还可以嵌套列表(创建包含其他列表的列表)'''# a = ['a', 'b', 'c']# n = [1, 2, 3]# x = [a, n]# print(x)# print(x[0])#索取# print(x[0][1])#while'''斐波纳契数列,使得每个数字是两个前述者的总和,选自0和1。'''# a, b = 0, 1# while a < 10:#print(a)#a, b = b, a + b'''print'''# i = 256*256# print('The value of i is', i)'''end 可以取消输出后面的换行, 或用另一个字符串结尾'''# a, b = 0, 1# while a < 1000:#print(a, end=',')#a, b = b, a + b

第四部分:控制流

"""作者:川川时间:/7/27"""'''if语句固定搭配'''# x = int(input("Please enter an integer: "))# if x < 0:#x = 0#print('Negative changed to zero')# elif x == 0:#print('Zero')# elif x == 1:#print('Single')# else:#print('More')'''if ... elif ... elif ... 序列看作是其他语言中 switch 或 case 语句的替代品。''''''for语句'''# words = ['cat', 'window', 'defenestrate']# for w in words:## print(w, len(w))''' range() 函数用于遍历数字序列'''# for i in range(5):#print(i)'''range包左不包右'''# for i in range(5, 10):#print(i)'''range 可以不从 0 开始,还可以按指定幅度range(起点,终点,步长)'''# for i in range(0, 10, 3):#print(i)# for j in range(-10, -100, -30):#print(j)'''range() 和 len() 组合在一起,可以按索引迭代序列'''# a = ['Mary', 'had', 'a', 'little', 'lamb']# for i in range(len(a)):#print(i, a[i])'''使用sum和range求连加和'''# print(sum(range(5))) #0 + 1 + 2 + 3+4'''range 生成列表解决'''# print(list(range(4)))'''质数判断'''#仔细看:else 子句属于 for 循环,不属于 if 语句。# for n in range(2, 10):#for x in range(2, n):# if n % x == 0:# print(n, 'equals', x, '*', n // x)# break#else:# print(n, 'is a prime number')'''continue 语句也借鉴自 C 语言,表示继续执行循环的下一次迭代:'''# for num in range(2, 10):#if num % 2 == 0:# print("Found an even number", num)# continue#print("Found an odd number", num)#pass'''pass 语句不执行任何操作'''# while True:#pass'''下面这段代码创建了一个最小的类'''# class MyEmptyClass:#pass'''pass 还可以用作函数或条件子句的占位符'''# def initlog(*args):#pass

第五部分:函数

"""作者:川川时间:/7/27"""'''def 定义函数 斐波那契数列函数'''# def fib(n):#a, b = 0, 1#while a < n:# print(a, end=' ')# a, b = b, a + b#print()# fib(100) #传参#返回列表# def fib2(n):#result = []#a, b = 0, 1#while a < n:# result.append(a)# a, b = b, a + b#return result# f100 = fib2(100)# print(f100)'''默认值在 定义 作用域里的函数定义中求值'''# i = 5# def f(arg=i):#print(arg)# i = 6# f()#输出5而不是6'''默认值为列表、字典或类实例等可变对象时,会产生与该规则不同的结果。'''# def f(a, L=[]):#L.append(a)#return L# print(f(1))# print(f(2))# print(f(3))'''不想在后续调用之间共享默认值时,应以如下方式编写函数'''# def f(a, L=None):#if L is None:# L = []#L.append(a)#return L# print(f(10))# print(f(11))"""关键字参数"""# def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):#print("-- This parrot wouldn't", action, end=' ')#print("if you put", voltage, "volts through it.")#print("-- Lovely plumage, the", type)#print("-- It's", state, "!")# # parrot(5)# '''该函数接受一个必选参数(voltage)和三个可选参数(state, action 和 type)'''# parrot(1000) # 1 positional argument# parrot(voltage=1000) # 1 keyword argument# parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments# parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments# parrot('a million', 'bereft of life', 'jump') # 3 positional arguments# parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword'''*name 必须在 **name 前面'''# def cheeseshop(kind, *arguments, **keywords):#print("-- Do you have any", kind, "?")#print("-- I'm sorry, we're all out of", kind)#for arg in arguments:# print(arg)#print("-" * 40)#for kw in keywords:# print(kw, ":", keywords[kw])# cheeseshop("Limburger", "It's very runny, sir.",# "It's really very, VERY runny, sir.",# shopkeeper="Michael Palin",# client="John Cleese",# sketch="Cheese Shop Sketch")'''some example'''# def standard_arg(arg):#print(arg)# def pos_only_arg(arg, /):#print(arg)# def kwd_only_arg(*, arg):#print(arg)# def combined_example(pos_only, /, standard, *, kwd_only):#print(pos_only, standard, kwd_only)#来看看区别#第一个对调用方式没有任何限制,可以按位置也可以按关键字传递参数# print(standard_arg(2))# print(standard_arg(arg=2))#第二个 函数定义中有 /,仅限使用位置形参# print(pos_only_arg(1))# print(pos_only_arg(arg=1))#就会报错#第三个 函数定义通过 * 表明仅限关键字参数# print(kwd_only_arg(3))#就会报错# print(kwd_only_arg(arg=3))#第四个kwd_only_arg(arg=3)# print(combined_example(1, 2, 3))#报错# print(combined_example(1, 2, kwd_only=3))# print(combined_example(1, standard=2, kwd_only=3))# print(combined_example(pos_only=1, standard=2, kwd_only=3))#报错'''加上 / (仅限位置参数)后,就可以了。此时,函数定义把 name 当作位置参数,'name' 也可以作为关键字参数的键'''# def foo(name, /, **kwds):#return 'name' in kwds# print(foo(1, **{'name': 2}))#返回tuue# def foo(name, **kwds):#return 'name' in kwds# print(foo(name=5))#返回false,实际这name没用到,所以哟弄个上面那个方法'''解包实参列表'''# print(list(range(3, 6)))# args = [3, 6]# print(list(range(*args)))#用 * 操作符把实参从列表或元组解包出来'''字典可以用 ** 操作符传递关键字参数'''# def parrot(voltage, state='a stiff', action='voom'):#print("-- This parrot wouldn't", action, end=' ')#print("if you put", voltage, "volts through it.", end=' ')#print("E's", state, "!")# d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}# parrot(**d)"""Lambda 表达式"""'''lambda a, b: a+b 函数返回两个参数的和'''# def make_incrementor(n):#return lambda x: x + n# f = make_incrementor(42)# print(f(0))# print(f(1))

第六部分:数据结构

"""作者:川川时间:/7/27""""""5.1 列表详解"""# fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']'''list.count(x)返回列表中元素 x 出现的次数'''# print(fruits.count('apple'))'''list.index(x[, start[, end]])返回列表中第一个值为 x 的元素的零基索引位置,第一个!'''# print(fruits.index('banana'))# print(fruits.index('banana', 4,7))'''list.reverse()反转列表中的元素。'''# fruits.reverse()# print(fruits)'''list.append(x)在列表末尾添加一个元素'''# fruits.append('grape')# print(fruits)'''list.sort(*, key=None, reverse=False)就地排序列表中的元素'''# fruits.sort()# print(fruits)'''list.pop([i])删除列表中指定位置的元素,并返回被删除的元素。未指定位置时,a.pop() 删除并返回列表的最后一个元素。'''# fruits.pop()#默认删除最后一个# print(fruits)# fruits.pop(0)#删除第一个# print(fruits)'''list.copy()返回列表的浅拷贝,复制'''# fruits.copy()# print(fruits)'''list.clear()删除列表里的所有元素,相当于 del a[:] 。'''# fruits.clear()# print(fruits)'''list.remove(x)从列表中删除第一个值为 x 的元素。未找到指定元素时,触发 ValueError 异常。'''# fruits.remove('apple')# print(fruits)'''list.insert(i, x)在指定位置插入元素。第一个参数是插入元素的索引,因此,a.insert(0, x) 在列表开头插入元素'''# fruits.insert(1, 'nana')# print(fruits)'''用列表实现堆栈''''''特点:“后进先出” 把元素添加到堆栈的顶端,使用 append() 。从堆栈顶部取出元素,使用 pop() ,不用指定索引。'''# stack = [3, 4, 5]# stack.append(6)# stack.append(7)# print(stack)# stack.pop()# print(stack)# stack.pop()# print(stack)'''用列表实现队列''''''特点:“先进先出” 实现队列最好用 collections.deque,可以快速从两端添加或删除元素'''# from collections import deque# queue = deque(["Eric", "John", "Michael"])# queue.append("Terry")# queue.append("Graham")# print(queue)# print(queue.popleft())#第一个# print(queue.popleft())#第二个# print(queue)''''列表推导式''''''创建平方值的列表'''# squares = []# for x in range(10):#squares.append(x ** 2)# print(squares)# b=squares = [x**2 for x in range(10)]# print(b)# a=squares = list(map(lambda x: x**2, range(10)))# print(a)#同上'''列表推导式将两个列表中不相等的元素组合起来'''# a=[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]# print(a)'''等价如下'''# combs = []# for x in [1,2,3]:#for y in [3, 1, 4]:# if x != y:# combs.append((x, y))# print(combs)'''some example'''# vec = [-4, -2, 0, 2, 4]# print([x*2 for x in vec])# print([x for x in vec if x >= 0])# print([abs(x) for x in vec])#abs求绝对值# freshfruit = [' banana', ' loganberry ', 'passion fruit ']# a=[weapon.strip() for weapon in freshfruit]# print(a)'''!!!!表达式是元组(例如上例的 (x, y))时,必须加上括号'''# b=[(x, x**2) for x in range(6)]# print(b)'''列表推导式可以使用复杂的表达式和嵌套函数'''# from math import pi# a=[str(round(pi, i)) for i in range(1, 6)]# print(a)'''嵌套的列表推导式'''# matrix = [# [1, 2, 3, 4],# [5, 6, 7, 8],# [9, 10, 11, 12],#]# a=[[row[i] for row in matrix] for i in range(4)]# print(a)#等价如下# transposed = []# for i in range(4):#transposed.append([row[i] for row in matrix])# print(transposed)'''5.2del 语句 移除元素'''# a = [-1, 1, 66.25, 333, 333, 1234.5]# del a[1]# print(a)# del a[2:4]# print(a)# del a[:]# print(a)'''del 也可以用来删除整个变量'''# del a[:]# print(a)'''5.3元组和序列''''''元组由多个用逗号隔开的值组成'''# t = 12345, 54321, 'hello!'#输入时,圆括号可有可无,不过经常是必须的# print(t[0])# print(t) #输出时,元组都要由圆括号标注,这样才能正确地解释嵌套元组# empty = ()# singleton = 'hello', #注意不加逗号就成了字符串了# print(len(empty))# print(len(singleton))# print(singleton)'''5.4集合''''''创建空集合只能用 set(),不能用 {},{} 创建的是空字典'''# basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}# print(basket)# print('orange' in basket)# a = set('abracadabra')# b = set('alacazam')# print(a)# print(a-b)# print(a|b)#并集 letters in a or b or both# print(a & b)#交集 etters in both a and b# print( a ^ b)# # letters in a or b but not both'''集合也支持推导式'''# a = {x for x in 'abracadabra' if x not in 'abc'}# print(a)'''5.5字典'''# tel = {'jack': 4098, 'sape': 4139}# tel['guido'] = 4127 #添加# print(tel)# print(tel['jack'])#索取# del tel['sape']# tel['irv'] = 4127# print(tel)# print(list(tel))#转换为列表# print('guido' in tel)'''dict() 构造函数可以直接用键值对序列创建字典'''# a=dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])# print(a)# print(a['sape'])'''字典推导式可以用任意键值表达式创建字典'''# a={x: x**2 for x in (2, 4, 6)}#lamda# print(a)'''关键字是比较简单的字符串时,直接用关键字参数指定键值对更便捷'''# c=dict(sape=4139, guido=4127, jack=4098)# print(c)# print(c['jack'])'''5.6循环的技巧''''''在字典中循环时,用 items() 方法可同时取出键和对应的值'''# knights = {'gallahad': 'the pure', 'robin': 'the brave'}# for k, v in knights.items():#print(k,v)'''在序列中循环时,用 enumerate() 函数可以同时取出位置索引和对应的值'''# for i, v in enumerate(['tic', 'tac', 'toe']):#print(i,v)'''同时循环两个或多个序列时,用 zip() 函数可以将其内的元素一一匹配'''# questions = ['name', 'quest', 'favorite color']# answers = ['lancelot', 'the holy grail', 'blue']# for q, a in zip(questions, answers):#print('What is your {0}? It is {1}.'.format(q, a))#0和1只是占位,不起作用,可以空着'''逆向循环序列时,先正向定位序列,然后调用 reversed() 函数'''# for i in reversed(range(1, 10, 2)):#print(i)'''按指定顺序循环序列,可以用 sorted() 函数,在不改动原序列的基础上,返回一个重新的序列'''# basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']# for i in sorted(basket):#print(i)'''使用 set() 去除序列中的重复元素。使用 sorted() 加 set() 则按排序后的顺序,循环遍历序列中的唯一元素'''# basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']# for f in sorted(set(basket)):#print(f)'''一般来说,在循环中修改列表的内容时,创建新列表比较简单,且安全'''# import math# raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]# filtered_data = []# for value in raw_data:#if not math.isnan(value):# filtered_data.append(value)# print(filtered_data)#筛选出数字'''5.7. 深入条件控制¶''''''布尔运算符 and 和 or 也称为 短路 运算符:其参数从左至右解析,一旦可以确定结果,解析就会停止。'''# string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'# non_null = string1 or string2 or string3# print(non_null)#返回一个值

第七部分:模块

"""作者:川川时间:/7/27"""'''用文本编辑器在当前目录下创建 fibo.py 文件,输入以下内容'''#这部分单独创建文件!在这展示fibo内容只是便于好看# def fib(n): # write Fibonacci series up to n#a, b = 0, 1#while a < n:# print(a, end=' ')# a, b = b, a+b#print()## def fib2(n): # return Fibonacci series up to n#result = []#a, b = 0, 1#while a < n:# result.append(a)# a, b = b, a+b#return result'''这项操作不直接把 fibo 函数定义的名称导入到当前符号表,只导入模块名 fibo 。要使用模块名访问函数'''# import fibo# print(fibo.fib(1000))'''如果经常使用某个函数,可以把它赋值给局部变量'''# a = fibo.fib# print(a(400))'''6.1. 模块详解''''''import 语句有一个变体,可以直接把模块里的名称导入到另一个模块的符号表'''# from fibo import fib, fib2# print(fib(500))'''还有一种变体可以导入模块内定义的所有名称 不建议从模块或包内导入 *, 因为,这项操作经常让代码变得难以理解。'''# from fibo import *# print(fib(500))'''模块名后使用 as 时,直接把 as 后的名称与导入模块绑定。意思就是改变名称使用,'''# import fibo as fib# print(fib.fib(500))'''from 中也可以使用这种方式'''# from fibo import fib as fibonacci# print(fibonacci(500))

第八部分:深层输入输出

"""作者:川川时间:/7/27"""'''在字符串开头的引号/三引号前添加 f 或 F 。在这种字符串中,可以在 { 和 } 字符之间输入引用的变量'''# year = # event = 'Referendum'# a=f'Results of the {year} {event}'# print(a)'''str.format() 该方法也用 { 和 } 标记替换变量的位置a 这种方法支持详细的格式化指令'''# yes_votes = 42_572_654# no_votes = 43_132_495# percentage = yes_votes / (yes_votes + no_votes)# a='{:-5} YES votes {:1.1%}'.format(yes_votes, percentage)#调整{}内部感受下# print(a)'''只想快速显示变量进行调试,可以用 repr() 或 str() 函数把值转化为字符串。'''# s = 'Hello, world.'# print(str(s))#str() 函数返回供人阅读的值# print(repr(s))#repr() 则生成适于解释器读取的值# print(str(1/7))# hellos = repr('hello')# print(hellos)'''7.1.1. 格式化字符串字面值''''''格式化字符串字面值 (简称为 f-字符串)在字符串前加前缀 f 或 F,通过 {expression} 表达式,把 Python 表达式的值添加到字符串内''''''下例将 pi 舍入到小数点后三位'''# import math# print(f'The value of pi is approximately {math.pi:.3f}.')'''在 ':' 后传递整数,为该字段设置最小字符宽度,常用于列对齐'''# table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}# for name, phone in table.items():#print(f'{name:10} ==> {phone:10d}')'''7.1.2. 字符串 format() 方法'''# print('We are the {} who say "{}!"'.format('knights', 'Ni'))'''花括号及之内的字符(称为格式字段)被替换为传递给 str.format() 方法的对象。花括号中的数字表示传递给 str.format() 方法的对象所在的位置。'''# print('{0} and {1}'.format('spam', 'eggs'))# print('{1} and {0}'.format('spam', 'eggs'))'''使用关键字参数名引用值。'''# print('This {food} is {adjective}.'.format(food='spam', adjective='absolutely horrible'))'''位置参数和关键字参数可以任意组合'''# print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',#other='Georg'))'''用方括号 '[]' 访问键来完成'''# table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}# print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))'''也可以用 '**' 符号,把 table 当作传递的关键字参数。'''# print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))'''生成一组整齐的列,包含给定整数及其平方与立方'''# for x in range(1, 11):#print('{0:2d} {1:3d} {2:4d}'.format(x, x * x, x * x * x))'''7.1.3. 手动格式化字符串'''# for x in range(1, 11):#print(repr(x).rjust(2), repr(x * x).rjust(3), end=' ')#print(repr(x * x * x).rjust(4))'''7.1.4. 旧式字符串格式化方法'''# import math# print('The value of pi is approximately %5.3f.' % math.pi)'''7.2. 读写文件¶''''''最常用的参数有两个: open(filename, mode)'''# f = open('workfile', 'w')'''第一个实参是文件名字符串第二个实参是包含描述文件使用方式字符的字符串。mode 的值包括 'r' ,表示文件只能读取;'w' 表示只能写入(现有同名文件会被覆盖);'a' 表示打开文件并追加内容,任何写入的数据会自动添加到文件末尾。'r+' 表示打开文件进行读写。mode 实参是可选的,省略时的默认值为 'r'。'''# with open('workfile') as f:#read_data = f.read()#print(read_data)# f.close()#如果没有使用 with 关键字,则应调用 f.close() 关闭文件,即可释放文件占用的系统资源。# with open('workfile') as f:#a=f.read()#print(a)# f.close()'''f.readline() 从文件中读取单行数据'''# with open('workfile') as f:# a=f.readline()# b=f.readline()# c=f.readline()# print(a,b,c)# for i in f:#print(i)# f.close()'''从文件中读取多行时,可以用循环遍历整个文件对象'''# with open('workfile') as f:#for line in f:# print(line, end='')# f.close()'''f.write(string) 把 string 的内容写入文件,并返回写入的字符数。'''# with open('workfile','w') as f:#f.write('This is a test\n')# f.close()'''写入其他类型的对象前,要先把它们转化为字符串(文本模式)或字节对象(二进制模式)'''# with open('workfile','a') as f:#value = ('the answer', 42)#s = str(value)#f.write(s)# f.close()# f = open('workfile', 'rb+')# f.write(b'0123456789abcdef')# print(f.read())# print(f.seek(5))# print(f.read(1))'''7.2.2. 使用 json 保存结构化数据'''# import json# a=json.dumps([1, 'simple', 'list'])# print(a)'''dumps() 函数还有一个变体, dump() ,它只将对象序列化为 text file '''#如果 f 是 text file 对象# json.dump(x, f)#要再次解码对象,如果 f 是已打开、供读取的 text file 对象# x = json.load(f)

第九部分:异常和错误

"""作者:川川时间:/7/27"""'''处理异常搭配:try except''''''如果没有异常发生,则跳过 except 子句 并完成 try 语句的执行。'''# while True:#try:# x = int(input("Please enter a number: "))# break#except ValueError:# print("Oops! That was no valid number. Try again...")'''一个 try 语句可能有多个 except 子句,以指定不同异常的处理程序。'''# class B(Exception):#pass## class C(B):#pass## class D(C):#pass## for cls in [B, C, D]:#try:# raise cls()#except D:# print("D")#except C:# print("C")#except B:# print("B")'''最后的 except 子句可以省略异常名,以用作通配符'''# try:#f = open('workfile')#s = f.readline()#i = int(s.strip())# except OSError as err:#print("OS error: {0}".format(err))# except ValueError:#print("Could not convert data to an integer.")# except:#print("Unexpected error:", sys.exc_info()[0])#raise'''try ... except 语句有一个可选的 else 子句,在使用时必须放在所有的 except 子句后面'''# import sys# for arg in sys.argv[1:]:#try:# f = open(arg, 'r')#except OSError:# print('cannot open', arg)#else:# print(arg, 'has', len(f.readlines()), 'lines')# f.close()'''处理 try 子句中调用(即使是间接地)的函数内部发生的异常。'''# def this_fails():#x = 1 / 0# try:#this_fails()# except ZeroDivisionError as err:#print('Handling run-time error:', err)'''raise 语句支持强制触发指定的异常'''# try:#raise NameError('HiThere')# except NameError:#print('An exception flew by!')#raise'''异常链'''# def func():#raise IOError# try:#func()# except IOError as exc:#raise RuntimeError('Failed to open database') from exc'''异常链在 except 或 finally 子句触发异常时自动生成'''# try:#open('database.sqlite')# except IOError:#raise RuntimeError from None'''定义清理操作'''# try:#raise KeyboardInterrupt# finally:#print('Goodbye, world!')'''一个更为复杂的例子'''def divide(x, y):try:result = x / yexcept ZeroDivisionError:print("division by zero!")else:print("result is", result)finally:print("executing finally clause")print(divide(2,1))

第十部分:类

"""作者:川川时间:/7/27"""# class Complex:#def __init__(self, realpart, imagpart):# self.r = realpart# self.i = imagpart# x = Complex(3.0, -4.5)# print(x.r,x.i)'''实例对象¶'''# x.counter = 1# while x.counter < 10:#x.counter = x.counter * 2# print(x.counter)# del x.counter'''9.3.4. 方法对象'''''''通常,方法在绑定后立即被调用'''# class MyClass:#"""A simple example class"""#i = 12345##def f(self):# return 'hello world'# x=MyClass()# xf = x.f# while True:#print(xf())'''9.3.5. 类和实例变量'''# class Dog:#kind = 'canine'#def __init__(self, name):# self.name = name# d = Dog('Fido')# e = Dog('Buddy')# print(d.name)# print(e.name)# print(d.kind)''''正确的类设计应该使用实例变量:'''# class Dog:#def __init__(self, name):# self.name = name# self.tricks = [] # creates a new empty list for each dog##def add_trick(self, trick):# self.tricks.append(trick)# d = Dog('Fido')# e = Dog('Buddy')# d.add_trick('roll over')# e.add_trick('play dead')# e.add_trick('play dead')# # print(d.tricks)# print(e.tricks)''''迭代器'''# for element in [1, 2, 3]:#print(element)# for element in (1, 2, 3):#print(element)# for key in {'one':1, 'two':2}:#print(key)# for char in "123":#print(char)# for line in open("workfile"):#print(line, end='')'''生成器'''# def reverse(data):#for index in range(len(data)-1, -1, -1):# yield data[index]## for char in reverse('golf'):#print(char)'''生成器表达式'''# a=sum(i*i for i in range(10))# print(a)# xvec = [10, 20, 30]# yvec = [7, 5, 3]# c=sum(x*y for x,y in zip(xvec, yvec))# print(c)

第十一部分:标准库简介

"""作者:川川时间:/7/27"""'''操作系统接口(个人感觉没啥用)'''# import os# print(os.getcwd())#打印出当前文件位置# os.chdir('/server/accesslogs')#改变运行位置# os.system('mkdir today') #运行这个再系统shell'''文件通配符''''''glob 模块提供了一个在目录中使用通配符搜索创建文件列表的函数:'''# import glob# print(glob.glob('*.py'))'''命令行参数'''# import sys# print(sys.argv)#打印本文件位置''' 字符串模式匹配'''# import re# b=re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')# print(b)# a='tea for too'.replace('too', 'two')# print(a)'''数学'''# import math# a=math.cos(math.pi / 4)# print(a)# import random# b=random.choice(['apple', 'pear', 'banana'])# print(b)'''互联网访问'''# from urllib.request import urlopen# with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:#for line in response:# line = line.decode('utf-8') # Decoding the binary data to text.# if 'EST' in line or 'EDT' in line:# print(line)# smtplib 用于发送邮件# import smtplib# server = smtplib.SMTP('localhost')# server.sendmail('soothsayer@', 'jcaesar@')# server.quit()''' 日期和时间'''# from datetime import date# now = date.today()# print(now)# a=now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")# print(a)# birthday = date(2000, 9, 20)# age = now - birthday# c=age.days# print(c)'''数据压缩'''# import zlib# s = b'witch which has which witches wrist watch'# print(len(s))# t = press(s)# print(len(t))# print( zlib.decompress(t))# print(zlib.crc32(s))'''性能测量'''# from timeit import Timer# a= Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()# print(a)

第十二部分:协程

"""作者:川川时间:/7/27"""# import asyncio# async def main():#print('Hello ...')#await asyncio.sleep(1)#print('... World!')# asyncio.run(main())'''等待 1 秒后打印 "hello",然后 再次 等待 2 秒后打印 "world"'''# import asyncio# import time## async def say_after(delay, what):#await asyncio.sleep(delay)#print(what)# async def main():#print(f"started at {time.strftime('%X')}")#await say_after(1, 'hello')#await say_after(2, 'world')#print(f"finished at {time.strftime('%X')}")# asyncio.run(main())'''asyncio.create_task() 函数用来并发运行作为 asyncio 任务 的多个协程。'''# async def main():#task1 = asyncio.create_task(# say_after(1, 'hello'))#task2 = asyncio.create_task(# say_after(2, 'world'))#print(f"started at {time.strftime('%X')}")## Wait until both tasks are completed (should take## around 2 seconds.)#await task1#await task2##print(f"finished at {time.strftime('%X')}")# asyncio.run(main())'''Python 协程属于 可等待 对象,因此可以在其他协程中被等待'''# import asyncio## async def nested():#return 42## async def main():## Nothing happens if we just call "nested()".## A coroutine object is created but not awaited,## so it *won't run at all*.## Let's do it differently now and await it:#print(await nested()) # will print "42".## asyncio.run(main())'''协程函数: 定义形式为 async def 的函数;协程对象: 调用 协程函数 所返回的对象。''''''当一个协程通过 asyncio.create_task() 等函数被封装为一个 任务,该协程会被自动调度执行'''# import asyncio## async def nested():#return 42## async def main():## Schedule nested() to run soon concurrently## with "main()".#task = asyncio.create_task(nested())### "task" can now be used to cancel "nested()", or## can simply be awaited to wait until it is complete:#await task# asyncio.run(main())'''运行 asyncio 程序''''''asyncio.run(coro, *, debug=False)¶'''# import asyncio# async def main():#await asyncio.sleep(1)#print('hello')## asyncio.run(main())'''创建任务''''''asyncio.create_task(coro, *, name=None)¶'''# import asyncio# async def coro():#return # task = asyncio.create_task(coro())#python3.7+# This works in all Python versions but is less readable# task = asyncio.ensure_future(coro())#before python3.7'''休眠'''''' asyncio.sleep(delay, result=None, *, loop=None)¶''''''以下协程示例运行 5 秒,每秒显示一次当前日期'''# import asyncio# import datetime## async def display_date():#loop = asyncio.get_running_loop()#end_time = loop.time() + 5.0#while True:# print(datetime.datetime.now())# if (loop.time() + 1.0) >= end_time:# break# await asyncio.sleep(1)## asyncio.run(display_date())'''并发运行任务'''''' asyncio.gather(*aws, loop=None, return_exceptions=False)¶'''# import asyncio## async def factorial(name, number):#f = 1#for i in range(2, number + 1):# print(f"Task {name}: Compute factorial({i})...")# await asyncio.sleep(1)# f *= i#print(f"Task {name}: factorial({number}) = {f}")## async def main():## Schedule three calls *concurrently*:#await asyncio.gather(# factorial("A", 2),# factorial("B", 3),# factorial("C", 4),#)## asyncio.run(main())'''屏蔽取消操作''''''asyncio.shield(aw, *, loop=None)保护一个 可等待对象 防止其被 取消'''# res = await shield(something())#demo'''如果希望完全忽略取消操作 (不推荐) 则 shield() 函数需要配合一个 try/except 代码段'''# try:#res = await shield(something())# except CancelledError:#res = None'''超时''''''asyncio.wait_for(aw, timeout, *, loop=None)¶'''# import asyncio# async def eternity():## Sleep for one hour#await asyncio.sleep(3600)#print('yay!')## async def main():## Wait for at most 1 second#try:# await asyncio.wait_for(eternity(), timeout=1.0)#except asyncio.TimeoutError:# print('timeout!')## asyncio.run(main())'''简单等待''''''syncio.wait(aws, *, loop=None, timeout=None, return_when=ALL_COMPLETED)'''# 用法:# import asyncio# done, pending = await asyncio.wait(aws)# async def foo():#return 42## task = asyncio.create_task(foo())# done, pending = await asyncio.wait({task})## if task in done:#asyncio.run(task)## Everything will work as expected now.'''在线程中运行''''''asyncio.to_thread(func, /, *args, **kwargs)在不同的线程中异步地运行函数 func。''''''这个协程函数主要是用于执行在其他情况下会阻塞事件循环的 IO 密集型函数/方法'''# import asyncio,time# def blocking_io():#print(f"start blocking_io at {time.strftime('%X')}")## Note that time.sleep() can be replaced with any blocking## IO-bound operation, such as file operations.#time.sleep(1)#print(f"blocking_io complete at {time.strftime('%X')}")## async def main():#print(f"started main at {time.strftime('%X')}")##await asyncio.gather(# asyncio.to_thread(blocking_io),# asyncio.sleep(1))#print(f"finished main at {time.strftime('%X')}")# asyncio.run(main())''':要取消一个正在运行的 Task 对象可使用 cancel() 方法。调用此方法将使该 Task 对象抛出一个 CancelledError 异常给打包的协程''''''以下示例演示了协程是如何侦听取消请求的'''# import asyncio# async def cancel_me():#print('cancel_me(): before sleep')##try:# # Wait for 1 hour# await asyncio.sleep(3600)#except asyncio.CancelledError:# print('cancel_me(): cancel sleep')# raise#finally:# print('cancel_me(): after sleep')## async def main():## Create a "cancel_me" Task#task = asyncio.create_task(cancel_me())### Wait for 1 second#await asyncio.sleep(1)##task.cancel()#try:# await task#except asyncio.CancelledError:# print("main(): cancel_me is cancelled now")## asyncio.run(main())'''基于生成器的协程''''''@asyncio.coroutine用来标记基于生成器的协程的装饰器。此装饰器使得旧式的基于生成器的协程能与 async/await 代码相兼容'''# import asyncio# @asyncio.coroutine# def old_style_coroutine():#yield from asyncio.sleep(1)## async def main():#await old_style_coroutine()'''队列能被用于多个的并发任务的工作量分配:'''import asyncioimport randomimport timeasync def worker(name, queue):while True:# Get a "work item" out of the queue.sleep_for = await queue.get()# Sleep for the "sleep_for" seconds.await asyncio.sleep(sleep_for)# Notify the queue that the "work item" has been processed.queue.task_done()print(f'{name} has slept for {sleep_for:.2f} seconds')async def main():# Create a queue that we will use to store our "workload".queue = asyncio.Queue()# Generate random timings and put them into the queue.total_sleep_time = 0for _ in range(20):sleep_for = random.uniform(0.05, 1.0)total_sleep_time += sleep_forqueue.put_nowait(sleep_for)# Create three worker tasks to process the queue concurrently.tasks = []for i in range(3):task = asyncio.create_task(worker(f'worker-{i}', queue))tasks.append(task)# Wait until the queue is fully processed.started_at = time.monotonic()await queue.join()total_slept_for = time.monotonic() - started_at# Cancel our worker tasks.for task in tasks:task.cancel()# Wait until all worker tasks are cancelled.await asyncio.gather(*tasks, return_exceptions=True)print('====')print(f'3 workers slept in parallel for {total_slept_for:.2f} seconds')print(f'total expected sleep time: {total_sleep_time:.2f} seconds')asyncio.run(main())

如果你有一定的编程基础,那么你一定会通过本文章查漏补缺,同时领略python基础。由于内容实在过多,本篇文章偏语法介绍,并没有举很多例子,如有不好之处多对见谅。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。