首页后端开发PythonPython知识书阅读笔记

Python知识书阅读笔记

时间2023-07-06 06:22:02发布访客分类Python浏览808
导读:前言阅读《Python从入门到精通》时产生的笔记,部分以及掌握的知识点或者是不常用知识点会被省略Python格言>>> import this The Zen of Python, by Tim Peters Beaut...

前言

阅读《Python从入门到精通》时产生的笔记,部分以及掌握的知识点或者是不常用知识点会被省略

Python格言

>
    >
    >
     import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

索引

>
    >
    >
     a='python'
>
    >
    >
     a[-1] #从右往左数,-1为开始
'n'
>
    >
    >
     a[-2]
'o'
>
    >
    >
     a[:] #切片
'python'
>
    >
    >
     a[0:]
'python'
>
    >
    >
     a[3:]
'hon'
>
    >
    >
     a[1:2]
'y'

赋值操作符

运算符

说 明

用法举例

等价形式

=

最基本的赋值运算

x = y

x = y

+=

加赋值

x += y

x = x + y

-=

减赋值

x -= y

x = x - y

*=

乘赋值

x *= y

x = x * y

/=

除赋值

x /= y

x = x / y

%=

取余数赋值

x %= y

x = x % y

**=

幂赋值

x **= y

x = x ** y

//=

取整数赋值

x //= y

x = x // y

& =

按位与赋值

x & = y

x = x & y

|=

按位或赋值

x |= y

x = x | y

^=

按位异或赋值

x ^= y

x = x ^ y

=

左移赋值

x = y

x = x y,这里的 y 指的是左移的位数

> > =

右移赋值

x > > = y

x = x > > y,这里的 y 指的是右移的位数

>
    >
    >
     num=0
>
    >
    >
     num-=10
>
    >
    >
     num
-10

关键字in和is

>
    >
    >
     1 in (1,2,3) #'in'相当于集合中的属于符号,其用于判断是否包含在指定的序列中
True
>
    >
    >
     0 in (1,2,3)
False
>
    >
    >
     (1,2) in (1,2,3) #'in'不想当于集合中的包含符号
False
>
    >
    >
     1 is 1 #is用于判断两个变量是否为同一个,"=="是判断变量是否相等
stdin>
    :1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True
>
    >
    >
     a=1
>
    >
    >
     b=1
>
    >
    >
     a is b 
True
>
    >
    >
     b=2
>
    >
    >
     a is b
False

长度,最小值,最大值和求和

>
    >
    >
     a=[1,2,3,4,5]
>
    >
    >
     len(a) #元素个数
5
>
    >
    >
     min(a) #最小值
1
>
    >
    >
     max(a) #最大值
5
>
    >
    >
     sum(a) #求和
15
>
    >
    >
     a='123' #也适用于字符串
>
    >
    >
     len(a)
3

列表的一些操作

添加& 删除

>
    >
    >
     a=[1,2,3] 
>
    >
    >
     a.append(4) #添加单个元素
>
    >
    >
     a
[1, 2, 3, 4]
>
    >
    >
     a.extend([5,6]) #添加多个元素,要用列表形式
>
    >
    >
     a
[1, 2, 3, 4, 5, 6]
>
    >
    >
     a.insert(0,0) #在指定位置插入元素,第一个参数为位置,第二个参数为插入内容
>
    >
    >
     a
[0, 1, 2, 3, 4, 5, 6]
>
    >
    >
     a.pop() #删除元素,默认是最后一个元素
6 #返回删除元素的内容
>
    >
    >
     a
[0, 1, 2, 3, 4, 5]
>
    >
    >
     a.pop(0) #删除指定位置的元素
0
>
    >
    >
     a
[1, 2, 3, 4, 5]
>
    >
    >
     a.remove(5) #按元素内容删除元素
>
    >
    >
     a
[1, 2, 3, 4]
>
    >
    >
     del a #直接删除整个变量
>
    >
    >
     a
Traceback (most recent call last):
  File "stdin>
    ", line 1, in module>
    
NameError: name 'a' is not defined

其他

>
    >
    >
     a=[1,2,3]
>
    >
    >
     a.index(3) #查找指定元素的位置
2
>
    >
    >
     b=[1,1,2,3]
>
    >
    >
     b.index(1) #查找时仅寻找到第一项
0
>
    >
    >
     a.reverse() #反转元素位置
>
    >
    >
     a
[3, 2, 1]
>
    >
    >
     a.count(1) #统计元素出现次数
1
>
    >
    >
     a=[1,1,2,3]
>
    >
    >
     a.count(1)
2
>
    >
    >
     a=[3,1,2,0]
>
    >
    >
     a.sort() #对元素进行排列
>
    >
    >
     a
[0, 1, 2, 3]

字典的一些操作

>
    >
    >
 a={

... '123':'abc',
... '456':'def'}
    
>
    >
    >
 a
{
'123': 'abc', '456': 'def'}
    
>
    >
    >
     b=a.copy() #复制a的全部数据
>
    >
    >
 b
{
'123': 'abc', '456': 'def'}
    
>
    >
    >
     b.clear() #清空b的全部数据
>
    >
    >
 b
{
}
    
>
    >
    >
     c=['1.','2.','3.']
>
    >
    >
     c1=dict.fromkeys(c) #从c列表中提取信息以生成一个字典
>
    >
    >
 c1
{
'1.': None, '2.': None, '3.': None}
    
>
    >
    >
     c1=dict.fromkeys(c,'Emtry') #指定字典的默认值
>
    >
    >
 c1
{
'1.': 'Emtry', '2.': 'Emtry', '3.': 'Emtry'}
    
>
    >
    >
     a.get('123') #获取'1,2,3'对应的值
'abc'
>
    >
    >
     a.get('789','Emtry') #若获取对应值失败,返回默认值
'Emtry'
>
    >
    >
     a.keys() #从a字典中提取信息(:前)以生成一个列表
dict_keys(['123', '456'])
>
    >
    >
     '789' in a.keys() #判断某个值是否在字典中
False
>
    >
    >
     a.values() #从a字典中提取信息(:后)以生成一个列表
dict_values(['abc', 'def']) 
>
    >
    >
     a.items()
dict_items([('123', 'abc'), ('456', 'def')]) #从a字典中提取信息(全)以生成一个items
>
    >
    >
     for i in a: #用for遍历时返回:前值
...     print(i)
...
123
456
>
    >
    >
     for i,c in a: #无法一次性遍历全部信息
...     print(i,'->
    ',c)
...
Traceback (most recent call last):
  File "stdin>
    ", line 1, in module>
    
ValueError: too many values to unpack (expected 2)
>
    >
    >
     for i,c in a.items(): #用items方法试试
...     print(i,'->
    ',c)
...
123 ->
     abc #可以一次性遍历全部信息
456 ->
     def

集合

>
    >
    >
 a={
1,2,3}
    
>
    >
    >
 a
{
1, 2, 3}
    
>
    >
    >
 b=set({
1,2,3,'a','b','c'}
    ) #生成混合集合或空集合时常用set()方法
>
    >
    >
     b.add('d') #添加元素
>
    >
    >
 b
{
1, 2, 3, 'a', 'b', 'c'}
    
>
    >
    >
     b.add(1) #添加元素,但是不会存在多个相同的元素
>
    >
    >
 b
{
1, 2, 3, 'a', 'b', 'd', 'c'}
    
>
    >
    >
     b.remove('d') #删除元素
>
    >
    >
 b
{
1, 2, 3, 'a', 'b', 'c'}
    
>
    >
    >
 a={
1,2,3}
    
#常见集合运算
>
    >
    >
     print('交集',a&
b)
交集 {
1, 2, 3}
    
>
    >
    >
 print('并集',a|b)
并集 {
1, 2, 3, 'a', 'b', 'c'}
    
>
    >
    >
     print('差集',a-b)
差集 set()
>
    >
    >
 print('差集',b-a)
差集 {
'a', 'b', 'c'}
    
>
    >
    >
 print('对称差集',b^a)
对称差集 {
'a', 'b', 'c'}
    >
    >
    >
     b.add(1)

列表推导式

>
    >
    >
     a=[a for a in range(3)]
>
    >
    >
     a
[0, 1, 2]
>
    >
    >
     c=[b for b in range(5) if b!=2] #添加一个条件
>
    >
    >
     c
[0, 1, 3, 4]

列表生成式

>
    >
    >
     a=[i**2 for i in range(4)]
>
    >
    >
     a
[0, 1, 4, 9]
>
    >
    >
     [m+n for m in range(5) for n in range(2)]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
>
    >
    >
     [x+y for x in ['A','B','C'] for y in ['a','b','c']]
['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Bc', 'Ca', 'Cb', 'Cc']

函数

默认参数

>
    >
    >
     def main(a,b=123):
...     print('a=',a)
...     print('b=',b)
...
>
    >
    >
     main(123)
a= 123
b= 123
>
    >
    >
     main(456,000)
a= 456
b= 0

可变参数

>
    >
    >
     def main(abc,*args,**kwargs):
...     print('abc=',abc)
...     print('args=',args)
...     print('kwargs=',kwargs)
...
>
    >
    >
 main(abc=123,name='Whitemu')
abc= 123
args= ()
kwargs= {
'name': 'Whitemu'}
    
>
    >
    >
     def main(*args):
...     print(args)
...
>
    >
    >
     main(456)
(456,)
>
    >
    >
     main(456,789)
(456, 789)

文档字符串

>
    >
    >
     def main():
...     '''
...     It is a main
...     '''
...     print('main')
...
>
    >
    >
     main.__doc__
'\n\tIt is a main\n\t'
>
    >
    >
     help(main)
Help on function main in module __main__:

main()
    It is a main

函数注释

main.py

def main(time:str='Input your time',Mode:str="Y or N?") ->
     str:
    '''
    main函数帮助文档:
    time - 你的时间,字符串
    Mode - 模式,仅支持Y或N
    '''
    print('The time is',time)

匿名函数

>
    >
    >
     a = lambda x,y:x+y
>
    >
    >
     a(1,2)
3

私有属性

class Stu:
    def __init__(self,name):
        self.name=name
        self.__name='WhitemuTeam' #定义私有属性:__name,类似的,还有__abc之类,只需要在前面加上__即可
    def main(self):
        print('Your name is:',self.name)
        print('My name is',self.__name)

stu=Stu('White_mu')
stu.main()
print('The name in Stu is',stu.name)
print('The __name in Stu is',stu.__name)

输出:

Your name is: White_mu
My name is WhitemuTeam
The name in Stu is White_mu
Traceback (most recent call last):
  File "C:\Users\28734\Desktop\main.py", line 12, in module>
    
    print('The __name in Stu is',stu.__name)
AttributeError: 'Stu' object has no attribute '__name'

Python内建异常

BaseExceptiona 所有异常的基类 SystemExitb python 解释器请求退出 KeyboardInterruptc 用户中断执行(通常是输入^C) Exceptiond 常规错误的基类 StopIteratione 迭代器没有更多的值 GeneratorExita 生成器(generator)发生异常来通知退出 SystemExith Python 解释器请求退出 StandardErrorg 所有的内建标准异常的基类 ArithmeticErrord 所有数值计算错误的基类 FloatingPointErrord 浮点计算错误 OverflowError 数值运算超出最大限制 ZeroDivisionError 除(或取模)零 (所有数据类型) AssertionErrord 断言语句失败 AttributeError 对象没有这个属性 EOFError 没有内建输入,到达 EOF 标记 EnvironmentErrord 操作系统错误的基类 IOError 输入/输出操作失败 OSErrord 操作系统错误 WindowsErrorh Windows 系统调用失败 ImportError 导入模块/对象失败 KeyboardInterruptf 用户中断执行(通常是输入^C) LookupErrord 无效数据查询的基类 IndexError 序列中没有没有此索引(index) KeyError 映射中没有这个键 MemoryError 内存溢出错误(对于 Python 解释器不是致命的) NameError 未声明/初始化对象 (没有属性) UnboundLocalErrorh 访问未初始化的本地变量 ReferenceErrore 弱引用(Weak reference)试图访问已经垃圾回收了的对象 RuntimeError 一般的运行时错误 NotImplementedErrord 尚未实现的方法 SyntaxError Python 语法错误 IndentationErrorg 缩进错误 TabErrorg Tab 和空格混用 SystemError 一般的解释器系统错误 TypeError 对类型无效的操作 ValueError 传入无效的参数 UnicodeErrorh Unicode 相关的错误 UnicodeDecodeErrori Unicode 解码时的错误 UnicodeEncodeErrori Unicode 编码时错误 UnicodeTranslateErrorf Unicode 转换时错误 Warningj 警告的基类 DeprecationWarningj 关于被弃用的特征的警告 FutureWarningi 关于构造将来语义会有改变的警告 OverflowWarningk 旧的关于自动提升为长整型(long)的警告 PendingDeprecationWarningi 关于特性将会被废弃的警告 RuntimeWarningj 可疑的运行时行为(runtime behavior)的警告 SyntaxWarningj 可疑的语法的警告 UserWarningj 用户代码生成的警告

finally子句

#a=1
try:
    print(a)
except:
    print('Error')
finally: #无论是否有异常都会执行下列语句
    print('exit...')

运行结果:

C:\Users\28734\Desktop>
    py main.py
Error
exit...

C:\Users\28734\Desktop>
    py main.py
1
exit...

再来一次,这次我们不设置a变量

try:
    print(a)
except KeyboardInterrupt:
    print('Error')
finally:
    print('exit...')

输出

C:\Users\28734\Desktop>
    py main.py
exit... #可以看到即使报错也可以执行finally子句下的内容
Traceback (most recent call last):
  File "C:\Users\28734\Desktop\main.py", line 2, in module>
    
    print(a)
NameError: name 'a' is not defined

标准库

Python中自带的一些库

sys

识别操作系统

我们会用到sys.platform来识别操作系统

>
    >
    >
     import sys
>
    >
    >
     print(sys.platform)
win32

win32代表的是Windows系统(包括但不限于x32和x64体系版本)

Linux-> linux

Mac OS X-> darwin

处理命令行参数

我们会用到sys.argv

learn.py:

import sys

a=sys.argv
print(a)

cmd:

C:\Users\28734\Desktop>
    py learn.py abc 123
['learn.py', 'abc', '123']

你可以看到列表的第一个元素是我们的py文件名,第二个开始才是我们运行时指定的参数的内容

如果用Vscode直接运行learn.py呢?

C:\Users\28734>
    C:/Users/28734/AppData/Local/Programs/Python/Python39/python.exe c:/Users/28734/Desktop/learn.py
['c:/Users/28734/Desktop/learn.py']

您可以看到它返回了文件路径,说明了列表中的元素和命令行中输入的明亮有关

退出程序

我们会用到sys.exit 但是我们更常用的是exit()

>
    >
    >
     sys.exit()

C:\Users\28734\Desktop>
    

os

获取当前文件所在目录

import os

print("__file__:",__file__)
print("os.path.dirname(__file__):",os.path.dirname(__file__))

运行结果:

__file__: c:\Users\28734\Desktop\learn.py
os.path.dirname(__file__): c:\Users\28734\Desktop

可以看到,Python中自带的变量__file__是Py文件的路径(包括文件名),而os.path.dirname(__file__)返回的是不带文件名的版本

获取/切换当前路径

import os

print(os.getcwd()) #获取当前路径,输出:C:\Users\28734\Desktop(即源cmd当前路径,否则就是文件路径)
os.chdir("c:\\") #切换路径
print(os.getcwd())

重命名文件

import os

os.rename("a.text", "b.txt")

a.text被重命名为b.txt

查看指定的路径是否存在

import os

folder = os.path.exists("c:\windows") #查看指定的路径是否存在
print(folder) #若存在,返回True,若不存在,则False

判断给出的路径是否是一个文件

import os

folder = os.path.isfile("c:\\windows\\system32")
print(folder) #若是一个文件,则True,若不是或不存在,则False

反之,我们可以使用

import os

folder = os.path.isdir("c:\\windows\\system32")
print(folder) #若是一个文件夹,则True,若不是或不存在,则False

创建一个文件夹

import os

os.mkdir("d:\\mu")

创建多个文件夹

import os

os.makedirs("d:\\mu1\\mu2\\mu3")

random

import random

print(random.random()) #用于生成随机0~1的浮点数,不能指定参数
print(random.randint(1,100)) #用于生成随机整数
print(random.uniform(1,100)) #用于生成随机浮点数
import random

seq1 = (1, 15, 8, 97, 22)
seq2 = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
print(random.choice(seq1)) #用于随机抽取序列中的元素
print(random.choice(seq2))

文件

文件模式

用open打开文件时的mode:

文件格式

t:以文本格式打开文件(默认)。一般用于文本文件,如:txt。 b:以二进制格式打开文件。一般用于非文本文件,如:图片。 这一类参数可以与其它的模式参数组合使用,用于指定打开文件的格式。

读写模式

r:以只读方式打开文件(默认模式)。文件指针定位在文件头的位置。如果文件不存在会报错。 w:以只写方式打开文件。如果文件存在,则打开文件,清空文件内容,从文件头开始编辑;如果文件不存在,则创建新文件,打开编辑。 a:以追加方式打开文件,同样是只写,不允许进行读操作。如果文件存在,则打开文件,将文件指针定位到文件尾。因此,新的内容是追加在已有内容之后。如果文件不存在,则创建新文件进行写入。 +:打开一个文件进行更新(可读写)。注意:该模式不能单独使用,需要与r/w/a组合使用。文件指针的位置取决于另一个组合参数。

组合模式

r+:打开一个文件用于读写。如果文件存在,则打开文件,将文件指针定位在文件头,新写入的内容在原有内容的前面;如果文件不存在会报错。 w+:打开一个文件用于读写。如果文件存在,则打开文件,清空原有内容,进入编辑模式;如果文件不存在,则创建一个新文件进行读写操作。 a+:以追加模式打开一个文件用于读写。如果文件存在,则打开文件,将文件指针定位在文件尾,新写入的内容在原有内容的后面;如果文件不存在,则创建一个新文件用于读写。 所有上面这些模式默认都是t——文本模式,如果要以二进制模式打开,需要加上参数b,如:rb、rb+、wb、wb+、ab、ab+。

在了解了各种模式参数的具体用法后,根据您要打开的文件类型,以及打开文件后的操作类型来选用正确的mode参数即可。

按行读文件

我们会用到x.readline或者是x.readlines

>
    >
    >
     f=open('abc.txt','r')
>
    >
    >
     f.readline()
'123\n'
>
    >
    >
     f.readline()
'abc\n'
>
    >
    >
     f.readline()
'efg'

可以看到,单次只能读取一行

而用到x.readlines时会返回一个列表

>
    >
    >
     f=open('abc.txt','r')
>
    >
    >
     a=f.readlines()
>
    >
    >
     a
['123\n', 'abc\n', 'efg']

上述的方法都会保留换行符,可以用for遍历列表中的元素再用i.split方法去掉字符串后再生成一个新的列表

按行写文件

我们会用到x.writelines方法

>
    >
    >
     open('abc.txt','w')
>
    >
    >
     f.writelines(['hello','world'])

你也可以使用字符串按行写文件

时间和日期

时间日期格式化符号

用于格式化时间日期:

  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身
  • %f 毫秒

获取当前时间

>
    >
    >
     import time
>
    >
    >
     print('当前时间:',time.localtime())
当前时间: time.struct_time(tm_year=2021, tm_mon=10, tm_mday=1, tm_hour=8, tm_min=3, tm_sec=30, tm_wday=4, tm_yday=274, tm_isdst=0)
>
    >
    >
     print('当前时间:',time.asctime(time.localtime()))
当前时间: Fri Oct  1 08:04:41 2021
>
    >
    >
     import datetime
>
    >
    >
     print('当前日期',datetime.date.today())
当前日期 2021-10-01
>
    >
    >
     print('当前是星期几',datetime.date.today().weekday())
当前是星期几 4

计算程序运行时间

import time

times=time.time()

for i in range(10):
	time.sleep(1)

print('代码运行时间',time.time() -times)

输出结果:

代码运行时间 11.105396270751953

加密与解密

MD5加密

>
    >
    >
     import hashlib
>
    >
    >
     text='Hello world'
>
    >
    >
     hashlib.md5(text.encode()).hexdigest()
'3e25960a79dbc69b674cd4ec67a72c62'

Base64加解密

>
    >
    >
     import base64
>
    >
    >
     text='Hello world'
>
    >
    >
     base64text=base64.b64encode(text.encode())
>
    >
    >
     print('base64加密后:',base64text)
base64加密后: b'SGVsbG8gd29ybGQ='
>
    >
    >
     newtext=base64.b64decode(base64text)
>
    >
    >
     print('base64解密后',newtext)
base64解密后 b'Hello world'

书本后面还有弄Mysql和网站搭建的,因为实用性不高,这里不再提及

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

python笔记变量集合异常

若转载请注明出处: Python知识书阅读笔记
本文地址: https://pptw.com/jishu/291394.html
学习Pythonの笔记 Python的常见数据结构

游客 回复需填写必要信息