diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/.DS_Store" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/.DS_Store" new file mode 100644 index 0000000000000000000000000000000000000000..c49333fc72c8cbc8a19678511132c78ebc9625cc Binary files /dev/null and "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/.DS_Store" differ diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/01_\346\263\250\351\207\212.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/01_\346\263\250\351\207\212.py" new file mode 100644 index 0000000000000000000000000000000000000000..097a91c6d3f4ef97d2750b3e1a5fe6f82c029ff5 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/01_\346\263\250\351\207\212.py" @@ -0,0 +1,13 @@ +# 单行注释 + +''' + 第一行注释 + 第二行注释 + 第三行注释 +''' + +""" + 第一行注释 + 第二行注释 + 第三行注释 +""" \ No newline at end of file diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/02_\345\217\230\351\207\217.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/02_\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..138257c3e3a32eb8b56a0e94bd7739e6c0aa99ca --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/02_\345\217\230\351\207\217.py" @@ -0,0 +1,37 @@ +####变量#### +## 1、定义变量————python定义变量 不需要声明类型 +num1 = 2 +str1 = "python" +#python可以多重赋值 +num2, num3, str2 = 10 , 10.3 , "number" + +''' +标识符命名规则: + 由数字、字母、下划线组成 + 不能数字开头 + 不能使用内置关键字 + 严格区分大小写 + +命名习惯: + 见名知义 + 大驼峰:每个单词首字母大写 MyName + 小驼峰:第二个单词及以后的单词首字母大写 myName + 下划线:my_name + +变量的命名 不要和函数名一样————不然会导致函数无法调用 +''' + +## 2、使用变量 +name='马丽亚' +print(name) +print('标识',id(name)) # 内存地址 +print('类型',type(name)) +print('值',name) + + +## 3、Python中的保留字 +import keyword +print(keyword.kwlist) + + + diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/03_\350\256\244\350\257\206\346\225\260\346\215\256\347\261\273\345\236\213.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/03_\350\256\244\350\257\206\346\225\260\346\215\256\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..3a8fabea254fd48d450c5e74a4cff45d55330242 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/03_\350\256\244\350\257\206\346\225\260\346\215\256\347\261\273\345\236\213.py" @@ -0,0 +1,68 @@ +""" +1、按经验将不同的变量存储不同的类型的数据 + +2、验证这些数据到底是什么类型 -- 检测数据类型 -- type(数据) +""" + +# int -- 整型 +num1 = 1 + +# float -- 浮点型,就是小数 +num2 = 1.1 +print(type(num1)) +print(type(num2)) +print(0.1 + 0.2 == 0.3) # False 由于精度问题 + +# decimal库 +# float 最多精确到 15位————要想让小数更加精确————利用decimal库 +import decimal +decimal1 = decimal.Decimal('3.141592653') +decimal2 = decimal.Decimal('1.234567891') # 里面的字符串 只能用单引号‘’ +decimal.getcontext().prec = 20 # 设置数据类型的 精确度 +print(decimal1 + decimal2) + +# e/E 的使用 +# e == a * 10**b +print(4.3e-3) # 0.0043 + +# 复数类型————虚数部分用j +real1 = (1.23e-4+5.67e+89j).real #取复数的 实数部分 +imag1 = (1.23e-4+5.67e+89j).imag #取复数的 虚数部分 + +# str -- 字符串,特点:数据都要带引号 +str1='人生苦短,我用Python' +str2="人生苦短,我用Python" +str3="""人生苦短, +我用Python""" + +str4='''人生苦短, +我用Python''' +print(str1,type(str1)) +print(str2,type(str2)) +print(str3,type(str3)) # 包括了换行 +print(str4,type(str4)) # 包括了换行 + + +# bool -- 布尔型,通常判断使用,布尔型有两个取值 True 和 False +b = True +print(type(b)) + +# 布尔值可以转成整数计算 +print(True + 1) #2 1+1的结果为2 True表示1 +print(False + 1) #1 0+1的结果为1 False表示0 + +# list -- 列表 +c = [10, 20, 30] +print(type(c)) + +# tuple -- 元组 +d = (10, 20, 30) +print(type(d)) + +# set -- 集合 +e = {10, 20, 30} +print(type(e)) + +# dict -- 字典 -- 键值对 +f = {'name': 'TOM', 'age': 18} +print(type(f)) diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/04_\350\276\223\345\207\272_print\350\257\255\345\217\245.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/04_\350\276\223\345\207\272_print\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..fec2282a492c1dfd53c1e860e25ea95d51c5e3a0 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/04_\350\276\223\345\207\272_print\350\257\255\345\217\245.py" @@ -0,0 +1,28 @@ +## 1、认识print()语句 ## +# print函数的格式: print(value , …… , sep=' ' , end='\n' , file=sys.stdout , flush= False) +# value:要输出的值,可以是多个 +# sep:多个值之间的分隔符, 默认为‘ ’ +# end:结束符, 默认为‘\n’ +# file: 输出流,默认为sys.stdout +# flush: 是否刷新到输出流——默认 否 + +print("python") # 换行打印 +print('hello','world','Python') + +print("python", end='\t') # 不换行打印 +print(" 同一行的python") + +name = '小明' +score = 89 +print(3 * ("我的名字叫" + name + ",我考了" + str(score) + "分")) # 字符串拼接 +# 注意拼接时,都要变成字符串 +print('=================================================') +''' +print可将数据输出文件中 +注意点: + 1.所指定的盘符在存在, + 2.使用 file= fp +''' +fp = open('text.txt', 'a+') # a+如果文件不存在就创建,存在就在文件内容的后面继续追加 +print('helloworld', file=fp) +fp.close() diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/05_\350\276\223\345\207\272_\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/05_\350\276\223\345\207\272_\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000000000000000000000000000000000000..90203fb25d5984d2594b594d9ebfc2bdc81d56f4 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/05_\350\276\223\345\207\272_\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,68 @@ +## 1、格式化输出的一般方法 ## +''' + %d 填充整数 + %06d 表示输出的整数显示6位数,不足以0补全,超出则原样输出 + + %s 填充字符串,如果填充不是字符串,则转为字符串 + %f 填充浮点数,默认保留6位小数 %.2f保留2位小数 %10.2f保留2位小数,总宽度为10 + + 也可以使用 f'我的名字是{表达式A},小名是{表达式B}' +''' +age = 18 +name = 'TOM' +weight = 75.5 +stu_id = 1 +stu_id2 = 9999 + +# (1)今年我的年龄是x岁 -- 整数 %d %06d 表示输出的整数显示6位数,不足以0补全,超出则原样输出 +print('今年我的年龄是%06d岁' % age) +print('今年我的年龄是%s岁' % age) +print(f'今年我的年龄是{age}岁') +print('=================================================') + +# (2)我的名字是x -- 字符串 %s +print('我的名字是%s' % name) +print('=================================================') + +# (3)我的体重是x公斤 -- 浮点数 %6.2f 保留2位小数,总长度为6 +print('我的体重是%6.2f公斤' % weight) +print('=================================================') + +# (4)我的学号是x +print('我的学号是%d' % stu_id) +print('我的学号是%03d' % stu_id) +print('我的学号是%03d' % stu_id2) +print('=================================================') + +# (5)我的名字是x,今年x岁了 +print('我的名字是%s,今年%d岁了' % (name, age)) +print('我的名字是%s,明年%d岁了' % (name, age + 1)) +print('=================================================') + +# (6)我的名字是x,今年x岁了,体重x公斤,学号是x +print('我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%03d' % (name, age, weight, stu_id)) +print('我的名字是%s,今年%s岁了,体重%s公斤,学号是%s' % (name, age, weight, stu_id)) +print(f'我的名字是{name},今年{age}岁了,体重{weight}公斤,学号是{stu_id}') +print('=================================================') + + +## 2、利用format()方法,对字符串类型的格式化处理 ## +# 利用format()方法————格式化:随时改变 字符串中 某部分的内容————将要修改的内容,用{}隔开 +"{}:计算机{}的CPU占用率为{}%。".format('2016-12-31', '外星人', 10) # format()里的参数,按顺序填入{}内 +"{1}:计算机{0}的CPU占用率为{2}%。".format('2016-12-31', '外星人', 10) # format()里的参数,按标记好的顺序 填入{}内 + +# 若调用format()方法时,希望{}本身是 字符串的一部分 ,则需要如下形式: +print('圆周率{{{1}{2}}}是{0}'.format('无理数', 3.1415926, '……')) + +""" +{}内部,可以填入参数序号,还可以填入 格式控制信息————{<参数序号> : <格式控制标记>} +以下来介绍 格式控制标记: + <填充> <对齐> <宽度> <,> <.精度> <类型> + 默认空格 <左对齐 先设置为3 千位分隔符 10.2保留2为小数,长度为10 整数类型 浮点数类型 + 设置* >右对齐 参数为4位,则输出4位 .2 字符串输出2位 b 二进制 e 对应小写字母e的指数形式 + ^中对齐 参数为2位,则填充字符占1位 c 对应的字符 E 对应大写字母E的指数形式 + d 十进制 f 标准浮点形式 + o 八进制 % 浮点数的百分形式 + x 小写十六进制 + X 大写十六进制 +""" diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/06_\350\276\223\345\207\272_\350\275\254\344\271\211\345\255\227\347\254\246.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/06_\350\276\223\345\207\272_\350\275\254\344\271\211\345\255\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..539e297d9b520e68747f99cb0d99d50a79413cf3 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/06_\350\276\223\345\207\272_\350\275\254\344\271\211\345\255\227\347\254\246.py" @@ -0,0 +1,40 @@ +""" + \n: 换行 + \t: 制表符 -- 利用空格,将一个制表位(4个单元格)占满 + \r: 光标回到行首,如果继续输入,则覆盖该行原本内容 + \b: 退一个 -- 删除前一个字符 + \: 消除特殊字符的作用,变成普通字符输出 + R/r: 原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r,或R +""" + +print('hello') +print('world') +print('===========') +# \n: 换行 +print('hello\nworld') + +print('===========') +# \t: 制表符 -- 利用空格,将一个制表位(4个单元格)占满 +print('hello\tworld') +print('helloooo\tworld') + +print('===========') +# \r: 光标回到行首,如果继续输入,则覆盖该行原本内容 +print('hello\rworld') + +print('===========') +# \b:退一个格,将o退没了 +print('hello\bworld') + +print('===========') +# \: 消除特殊字符的作用,变成普通字符输出 +print('http:\\\\www.baidu.com') +print('老师说:\'大家好\'') + +print("===========") +# R/r: 原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r,或R +print(r'hello\nworld') + +# 注意事项, 最后一个字符不能是反斜杠 +# error: print(r'hello\nworld\') +print(r'hello\nworld\\') \ No newline at end of file diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/07_\350\276\223\345\205\245.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/07_\350\276\223\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..b567fb9104ce54a34da1eab3d8820b4730799c72 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/07_\350\276\223\345\205\245.py" @@ -0,0 +1,21 @@ +## 使用input()函数 ## +""" +格式: + input('提示符')————返回 用户输入的 字符串————若要输入其他数据类型 则要转换 + +特点: + 1、当程序执行到input,等待用户输入,输入完成之后才继续向下执行。 + 2、input接收用户输入后,一般存储到变量,方便使用 + 3、input会把接收到的任意用户输入的数据都当做 字符串 处理 +""" + +age = input('请输入您的年龄:\n') +print(age) +print(type(age)) +print('==================') +# eval() // 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回 +print(eval(" 16*54 - 64 +128//2 ")) + +score = eval(input("请输入你的分数:\n")) +print(type(score)) +print('您的分数是%d' % (score + 10)) diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/08_\346\225\260\346\215\256\347\261\273\345\236\213\347\232\204\350\275\254\346\215\242.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/08_\346\225\260\346\215\256\347\261\273\345\236\213\347\232\204\350\275\254\346\215\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..918a8d08fe7a50b83225cfd931f601010d0f3bab --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/08_\346\225\260\346\215\256\347\261\273\345\236\213\347\232\204\350\275\254\346\215\242.py" @@ -0,0 +1,101 @@ +""" +体验数据类型转换: + 1、input + + 2、检测input数据类型str + + 3、int() 转换数据类型 + + 4、检测是否转换成功 + +""" +num = input('请输入数字:\n') +print(num) +print(type(num)) + +num = int(num) +print(type(num)) +print('================') + +## 数据类型转换函数 ## +# 1、float() -- 将数据转换成浮点型 +# 显式转换 -- 使用float函数 +s1 = '128.98' +s2 = '76' +ff = True +s3 = 'hello' +i = 98 +print(type(s1), type(s2), type(ff), type(s3), type(i)) +print(float(s1), type(float(s1))) +print(float(s2), type(float(s2))) +print(float(ff), type(float(ff))) +# error: print(float(s3),type(float(s3))) # 字符串中的数据如果是非数字串,则不允许转换 +print(float(i), type(float(i))) + +# 隐式转换 +print(type(10 + 0.1)) +print('================') + +# 2、str() -- 将数据转换成字符串 +name = '张三' +age = 20 +print(type(name), type(age)) # 说明name与age的数据类型不相同 +# error: print('我叫'+name+'今年,'+age+'岁') #当将str类型与int类型进行连接时,报错,解决方案,类型转换 +print('我叫' + name + '今年,' + str(age) + '岁') + +print("================") + +# 3、int() -- 将数据转换成整型 +s1 = '128' +f1 = 98.7 +s2 = '76.77' +ff = True +s3 = 'hello' +print(type(s1), type(f1), type(s2), type(ff), type(s3)) + +print(int(s1), type(int(s1))) # 将str转成int类型 ,字符串为 数字串 +print(int(f1), type(int(f1))) # float转成int类型,截取整数部分,舍掉小数部分 +# error: print(int(s2),type(int(s2))) #将str转成int类型,报错,因为字符串为小数串 +print(int(ff), type(int(ff))) +# error: print(int(s3),type(int(s3))) #将str转成int类型时,字符串必须为数字串(整数),非数字串是不允许转换 +print("================") + +# 4、tuple() -- 将一个序列转换成元组 +list1 = [10, 20, 30] +print(tuple(list1)) +print("================") + +# 5、list() -- 将一个序列转换成列表 +t1 = (10, 20, 30) +print(list(t1)) +print("================") + +# 6、eval() -- 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回 +str2 = '1' +str3 = '1.1' +str4 = '(1000, 2000, 3000)' +str5 = '[1000, 2000, 3000]' +print(type(eval(str2))) +print(type(eval(str3))) +print(type(eval(str4))) +print(type(eval(str5))) +print("================") + +## 字符 ————> Unicode +ord('c') + +## Unicode ————> 字符 +chr(3) + +## round() -- 四舍六入规则——如果是5,则取最接近的偶数 +round(7.7) # 结果: 8 +round(7.4) # 结果: 7 +round(-9.5) # 结果: -10 取最接近的偶数 +round(3.1415926, 3) # 对3.1415926 四舍六入————并保留3位小数 + +## 其他类型————>复数 +""" +complex(real,imag) # real为实部————可以是 整数、浮点数、字符串 + # imag为虚部————可以省略————可以是 整数、浮点数, 不能是字符串 +""" +complex(3, 4) diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/09_\350\277\233\345\210\266\344\271\213\351\227\264\347\232\204\350\275\254\346\215\242.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/09_\350\277\233\345\210\266\344\271\213\351\227\264\347\232\204\350\275\254\346\215\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..6a64fd8ca68e891361665e8da2e35b18b012d8cc --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/09_\350\277\233\345\210\266\344\271\213\351\227\264\347\232\204\350\275\254\346\215\242.py" @@ -0,0 +1,34 @@ +### 进制之间的转换 ### + +# 十进制 ——> 二进制 +a = 10 +print(bin(a)) + +# 二进制 ——> 十进制 +b = '10111001' +print(int(b,2)) + +b = 0b10111001 +print(b) + +# 十进制 ——> 八进制 +a = 32 +print(oct(a)) + +# 八进制 ——> 十进制 +b = '756' +print(int(b,8)) + +b = '0o756' +print(eval(b)) + +# 十进制 ——> 十六进制 +a = 87 +print(hex(a)) + +# 十六进制 ——> 十进制 +b = 'A34' +print(int(b,16)) + +b = '0xA34' +print(eval(b)) \ No newline at end of file diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/10_\344\272\214\350\277\233\345\210\266\344\270\216\345\255\227\347\254\246\347\274\226\347\240\201.py" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/10_\344\272\214\350\277\233\345\210\266\344\270\216\345\255\227\347\254\246\347\274\226\347\240\201.py" new file mode 100644 index 0000000000000000000000000000000000000000..5a0616c10818226dd4da4a26717a65a827121cbb --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/10_\344\272\214\350\277\233\345\210\266\344\270\216\345\255\227\347\254\246\347\274\226\347\240\201.py" @@ -0,0 +1,5 @@ +# chr: 数字 -> 字符 +print(chr(0b100111001011000)) + +# ord: 字符 -> 数字 +print(ord('乘')) \ No newline at end of file diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/Python\345\237\272\347\241\200\347\237\245\350\257\206.ipynb" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/Python\345\237\272\347\241\200\347\237\245\350\257\206.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..8fb03df1cdcef977cd5a5265257334a90e364196 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/Python\345\237\272\347\241\200\347\237\245\350\257\206.ipynb" @@ -0,0 +1,1034 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ca355dd3", + "metadata": {}, + "source": [ + "## 注释" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5aa160dc", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.026378Z", + "start_time": "2023-09-13T04:17:25.021736Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n 第一行注释\\n 第二行注释\\n 第三行注释\\n'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 单行注释\n", + "\n", + "'''\n", + " 第一行注释\n", + " 第二行注释\n", + " 第三行注释\n", + "'''\n", + "\n", + "\"\"\"\n", + " 第一行注释\n", + " 第二行注释\n", + " 第三行注释\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "id": "9ede5dbd", + "metadata": {}, + "source": [ + "## 变量" + ] + }, + { + "cell_type": "markdown", + "id": "70a56430", + "metadata": {}, + "source": [ + "### 定义变量" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f87b3246", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.029749Z", + "start_time": "2023-09-13T04:17:25.027244Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n标识符命名规则:\\n 由数字、字母、下划线组成\\n 不能数字开头\\n 不能使用内置关键字\\n 严格区分大小写\\n \\n命名习惯:\\n 见名知义\\n 大驼峰:每个单词首字母大写 MyName\\n 小驼峰:第二个单词及以后的单词首字母大写 myName\\n 下划线:my_name\\n \\n变量的命名 不要和函数名一样————不然会导致函数无法调用\\n'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "## 1、定义变量————python定义变量 不需要声明类型\n", + "num1 = 2\n", + "str1 = \"python\"\n", + "\n", + "# python可以多重赋值\n", + "num2, num3, str2 = 10 , 10.3 , \"number\"\n", + "\n", + "'''\n", + "标识符命名规则:\n", + " 由数字、字母、下划线组成\n", + " 不能数字开头\n", + " 不能使用内置关键字\n", + " 严格区分大小写\n", + " \n", + "命名习惯:\n", + " 见名知义\n", + " 大驼峰:每个单词首字母大写 MyName\n", + " 小驼峰:第二个单词及以后的单词首字母大写 myName\n", + " 下划线:my_name\n", + " \n", + "变量的命名 不要和函数名一样————不然会导致函数无法调用\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "id": "40a8ebcc", + "metadata": {}, + "source": [ + "### 使用变量" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "444edfb3", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.033612Z", + "start_time": "2023-09-13T04:17:25.030980Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "马丽亚\n", + "标识 4350940912\n", + "类型 \n", + "值 马丽亚\n" + ] + } + ], + "source": [ + "## 2、使用变量\n", + "name='马丽亚'\n", + "print(name)\n", + "print('标识',id(name)) # 内存地址\n", + "print('类型',type(name))\n", + "print('值',name)" + ] + }, + { + "cell_type": "markdown", + "id": "15ee0340", + "metadata": {}, + "source": [ + "### Python中的保留字" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2fd11aec", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.036486Z", + "start_time": "2023-09-13T04:17:25.034483Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" + ] + } + ], + "source": [ + "## 3、Python中的保留字\n", + "import keyword\n", + "print(keyword.kwlist)" + ] + }, + { + "cell_type": "markdown", + "id": "1e2ed99d", + "metadata": {}, + "source": [ + "## 认识数据类型" + ] + }, + { + "cell_type": "markdown", + "id": "3edc6ca2", + "metadata": {}, + "source": [ + "### int" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c0c138d6", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.038774Z", + "start_time": "2023-09-13T04:17:25.037374Z" + } + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "1、按经验将不同的变量存储不同的类型的数据\n", + "\n", + "2、验证这些数据到底是什么类型 -- 检测数据类型 -- type(数据)\n", + "\"\"\"\n", + "\n", + "# int -- 整型\n", + "num1 = 1" + ] + }, + { + "cell_type": "markdown", + "id": "39306034", + "metadata": {}, + "source": [ + "### float" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "55c00ab8", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.041679Z", + "start_time": "2023-09-13T04:17:25.039662Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "False\n" + ] + } + ], + "source": [ + "# float -- 浮点型,就是小数\n", + "num2 = 1.1\n", + "print(type(num1))\n", + "print(type(num2))\n", + "print(0.1 + 0.2 == 0.3) # False 由于精度问题" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ff52bc69", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.045985Z", + "start_time": "2023-09-13T04:17:25.042599Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.376160544\n" + ] + } + ], + "source": [ + "# decimal库\n", + "# float 最多精确到 15位————要想让小数更加精确————利用decimal库\n", + "import decimal\n", + "decimal1 = decimal.Decimal('3.141592653')\n", + "decimal2 = decimal.Decimal('1.234567891') # 里面的字符串 只能用单引号‘’\n", + "decimal.getcontext().prec = 20 # 设置数据类型的 精确度\n", + "print(decimal1 + decimal2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "142d4bbf", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.048355Z", + "start_time": "2023-09-13T04:17:25.046723Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0043\n" + ] + } + ], + "source": [ + "# e/E 的使用\n", + "# e == a * 10**b\n", + "print(4.3e-3) # 0.0043" + ] + }, + { + "cell_type": "markdown", + "id": "55cd34ab", + "metadata": {}, + "source": [ + "### 复数" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "cd02400b", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.052299Z", + "start_time": "2023-09-13T04:17:25.050558Z" + } + }, + "outputs": [], + "source": [ + "# 复数类型————虚数部分用j\n", + "real1 = (1.23e-4+5.67e+89j).real #取复数的 实数部分\n", + "imag1 = (1.23e-4+5.67e+89j).imag #取复数的 虚数部分" + ] + }, + { + "cell_type": "markdown", + "id": "7d6f6253", + "metadata": {}, + "source": [ + "### str" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "dfaa9d89", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.055208Z", + "start_time": "2023-09-13T04:17:25.053047Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "人生苦短,我用Python \n", + "人生苦短,我用Python \n", + "人生苦短,\n", + "我用Python \n", + "人生苦短,\n", + "我用Python \n" + ] + } + ], + "source": [ + "# str -- 字符串,特点:数据都要带引号\n", + "str1='人生苦短,我用Python'\n", + "str2=\"人生苦短,我用Python\"\n", + "str3=\"\"\"人生苦短,\n", + "我用Python\"\"\"\n", + "\n", + "str4='''人生苦短,\n", + "我用Python'''\n", + "print(str1,type(str1))\n", + "print(str2,type(str2))\n", + "print(str3,type(str3)) # 包括了换行\n", + "print(str4,type(str4)) # 包括了换行" + ] + }, + { + "cell_type": "markdown", + "id": "4680e3ec", + "metadata": {}, + "source": [ + "### bool" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bdd9cb29", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.057891Z", + "start_time": "2023-09-13T04:17:25.056025Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "# bool -- 布尔型,通常判断使用,布尔型有两个取值 True 和 False\n", + "b = True\n", + "print(type(b))\n", + "\n", + "# 布尔值可以转成整数计算\n", + "print(True + 1) #2 1+1的结果为2 True表示1\n", + "print(False + 1) #1 0+1的结果为1 False表示0" + ] + }, + { + "cell_type": "markdown", + "id": "fab2ce48", + "metadata": {}, + "source": [ + "### list" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "77259c2f", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.060344Z", + "start_time": "2023-09-13T04:17:25.058602Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# list -- 列表\n", + "c = [10, 20, 30]\n", + "print(type(c))" + ] + }, + { + "cell_type": "markdown", + "id": "71019848", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:16:36.294119Z", + "start_time": "2023-09-13T04:16:36.291585Z" + } + }, + "source": [ + "### tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4bf9ee05", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.063170Z", + "start_time": "2023-09-13T04:17:25.061407Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# tuple -- 元组\n", + "d = (10, 20, 30)\n", + "print(type(d))" + ] + }, + { + "cell_type": "markdown", + "id": "3062aed9", + "metadata": {}, + "source": [ + "### set" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "98363bf3", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.066376Z", + "start_time": "2023-09-13T04:17:25.064227Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# set -- 集合\n", + "e = {10, 20, 30}\n", + "print(type(e))" + ] + }, + { + "cell_type": "markdown", + "id": "fe19f510", + "metadata": {}, + "source": [ + "### dict" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "f840eb7b", + "metadata": { + "ExecuteTime": { + "end_time": "2023-09-13T04:17:25.069071Z", + "start_time": "2023-09-13T04:17:25.067220Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# dict -- 字典 -- 键值对\n", + "f = {'name': 'TOM', 'age': 18}\n", + "print(type(f))\n" + ] + }, + { + "cell_type": "markdown", + "id": "16e3fdb2", + "metadata": {}, + "source": [ + "## 输出" + ] + }, + { + "cell_type": "markdown", + "id": "64ac778a", + "metadata": {}, + "source": [ + "### print()语句" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d228a04b", + "metadata": {}, + "outputs": [], + "source": [ + "## 1、认识print()语句 ##\n", + "# print函数的格式: print(value , …… , sep=' ' , end='\\n' , file=sys.stdout , flush= False)\n", + "# value:要输出的值,可以是多个\n", + "# sep:多个值之间的分隔符, 默认为‘ ’\n", + "# end:结束符, 默认为‘\\n’\n", + "# file: 输出流,默认为sys.stdout\n", + "# flush: 是否刷新到输出流——默认 否\n", + "\n", + "print(\"python\") # 换行打印\n", + "print('hello','world','Python')\n", + "\n", + "print(\"python\", end='\\t') # 不换行打印\n", + "print(\" 同一行的python\")\n", + "\n", + "name = '小明'\n", + "score = 89\n", + "print(3 * (\"我的名字叫\" + name + \",我考了\" + str(score) + \"分\")) # 字符串拼接\n", + "# 注意拼接时,都要变成字符串\n", + "print('=================================================')\n", + "'''\n", + "print可将数据输出文件中\n", + "注意点:\n", + " 1.所指定的盘符在存在,\n", + " 2.使用 file= fp\n", + "'''\n", + "fp = open('text.txt', 'a+') # a+如果文件不存在就创建,存在就在文件内容的后面继续追加\n", + "print('helloworld', file=fp)\n", + "fp.close()\n" + ] + }, + { + "cell_type": "markdown", + "id": "a8bfd758", + "metadata": {}, + "source": [ + "### 格式化字符串" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c9ed9f8", + "metadata": {}, + "outputs": [], + "source": [ + "## 1、格式化输出的一般方法 ##\n", + "'''\n", + " %d 填充整数\n", + " %06d 表示输出的整数显示6位数,不足以0补全,超出则原样输出\n", + "\n", + " %s 填充字符串,如果填充不是字符串,则转为字符串\n", + " %f 填充浮点数,默认保留6位小数 %.2f保留2位小数 %10.2f保留2位小数,总宽度为10\n", + "\n", + " 也可以使用 f'我的名字是{表达式A},小名是{表达式B}'\n", + "'''\n", + "age = 18\n", + "name = 'TOM'\n", + "weight = 75.5\n", + "stu_id = 1\n", + "stu_id2 = 9999\n", + "\n", + "# (1)今年我的年龄是x岁 -- 整数 %d %06d 表示输出的整数显示6位数,不足以0补全,超出则原样输出\n", + "print('今年我的年龄是%06d岁' % age)\n", + "print('今年我的年龄是%s岁' % age)\n", + "print(f'今年我的年龄是{age}岁')\n", + "print('=================================================')\n", + "\n", + "# (2)我的名字是x -- 字符串 %s\n", + "print('我的名字是%s' % name)\n", + "print('=================================================')\n", + "\n", + "# (3)我的体重是x公斤 -- 浮点数 %6.2f 保留2位小数,总长度为6\n", + "print('我的体重是%6.2f公斤' % weight)\n", + "print('=================================================')\n", + "\n", + "# (4)我的学号是x\n", + "print('我的学号是%d' % stu_id)\n", + "print('我的学号是%03d' % stu_id)\n", + "print('我的学号是%03d' % stu_id2)\n", + "print('=================================================')\n", + "\n", + "# (5)我的名字是x,今年x岁了\n", + "print('我的名字是%s,今年%d岁了' % (name, age))\n", + "print('我的名字是%s,明年%d岁了' % (name, age + 1))\n", + "print('=================================================')\n", + "\n", + "# (6)我的名字是x,今年x岁了,体重x公斤,学号是x\n", + "print('我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%03d' % (name, age, weight, stu_id))\n", + "print('我的名字是%s,今年%s岁了,体重%s公斤,学号是%s' % (name, age, weight, stu_id))\n", + "print(f'我的名字是{name},今年{age}岁了,体重{weight}公斤,学号是{stu_id}')\n", + "print('=================================================')\n", + "\n", + "\n", + "## 2、利用format()方法,对字符串类型的格式化处理 ##\n", + "# 利用format()方法————格式化:随时改变 字符串中 某部分的内容————将要修改的内容,用{}隔开\n", + "\"{}:计算机{}的CPU占用率为{}%。\".format('2016-12-31', '外星人', 10) # format()里的参数,按顺序填入{}内\n", + "\"{1}:计算机{0}的CPU占用率为{2}%。\".format('2016-12-31', '外星人', 10) # format()里的参数,按标记好的顺序 填入{}内\n", + "\n", + "# 若调用format()方法时,希望{}本身是 字符串的一部分 ,则需要如下形式:\n", + "print('圆周率{{{1}{2}}}是{0}'.format('无理数', 3.1415926, '……'))\n", + "\n", + "\"\"\"\n", + "{}内部,可以填入参数序号,还可以填入 格式控制信息————{<参数序号> : <格式控制标记>}\n", + "以下来介绍 格式控制标记:\n", + " <填充> <对齐> \t\t <宽度> <,> \t\t <.精度> \t\t\t <类型>\n", + " 默认空格\t <左对齐\t\t 先设置为3\t\t\t\t 千位分隔符\t 10.2保留2为小数,长度为10\t\t 整数类型\t\t\t\t\t浮点数类型\n", + " 设置*\t\t >右对齐\t 参数为4位,则输出4位\t\t\t\t\t\t.2 字符串输出2位\t\t\t b 二进制\t\t\t\t e 对应小写字母e的指数形式\n", + "\t\t\t ^中对齐\t 参数为2位,则填充字符占1位\t\t\t\t\t\t\t\t\t\t\t c 对应的字符 \t E 对应大写字母E的指数形式\n", + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t d 十进制\t\t\t\t f 标准浮点形式\n", + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t o 八进制\t\t\t\t % 浮点数的百分形式\n", + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t x 小写十六进制\n", + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t X 大写十六进制\n", + "\"\"\"\n" + ] + }, + { + "cell_type": "markdown", + "id": "91206b83", + "metadata": {}, + "source": [ + "### 转义字符" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "01c7665c", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + " \\n: 换行\n", + " \\t: 制表符 -- 利用空格,将一个制表位(4个单元格)占满\n", + " \\r: 光标回到行首,如果继续输入,则覆盖该行原本内容\n", + " \\b: 退一个 -- 删除前一个字符\n", + " \\: 消除特殊字符的作用,变成普通字符输出\n", + " R/r: 原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r,或R\n", + "\"\"\"\n", + "\n", + "print('hello')\n", + "print('world')\n", + "print('===========')\n", + "# \\n: 换行\n", + "print('hello\\nworld')\n", + "\n", + "print('===========')\n", + "# \\t: 制表符 -- 利用空格,将一个制表位(4个单元格)占满\n", + "print('hello\\tworld')\n", + "print('helloooo\\tworld')\n", + "\n", + "print('===========')\n", + "# \\r: 光标回到行首,如果继续输入,则覆盖该行原本内容\n", + "print('hello\\rworld')\n", + "\n", + "print('===========')\n", + "# \\b:退一个格,将o退没了\n", + "print('hello\\bworld')\n", + "\n", + "print('===========')\n", + "# \\: 消除特殊字符的作用,变成普通字符输出\n", + "print('http:\\\\\\\\www.baidu.com')\n", + "print('老师说:\\'大家好\\'')\n", + "\n", + "print(\"===========\")\n", + "# R/r: 原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r,或R\n", + "print(r'hello\\nworld')\n", + "\n", + "# 注意事项, 最后一个字符不能是反斜杠\n", + "# error: print(r'hello\\nworld\\')\n", + "print(r'hello\\nworld\\\\')" + ] + }, + { + "cell_type": "markdown", + "id": "ea833a57", + "metadata": {}, + "source": [ + "## 输入" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e20668f", + "metadata": {}, + "outputs": [], + "source": [ + "## 使用input()函数 ##\n", + "\"\"\"\n", + "格式:\n", + "\tinput('提示符')————返回 用户输入的 字符串————若要输入其他数据类型 则要转换\n", + "\n", + "特点:\n", + " 1、当程序执行到input,等待用户输入,输入完成之后才继续向下执行。\n", + " 2、input接收用户输入后,一般存储到变量,方便使用\n", + " 3、input会把接收到的任意用户输入的数据都当做 字符串 处理\n", + "\"\"\"\n", + "\n", + "age = input('请输入您的年龄:\\n')\n", + "print(age)\n", + "print(type(age))\n", + "print('==================')\n", + "# eval() // 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回\n", + "print(eval(\" 16*54 - 64 +128//2 \"))\n", + "\n", + "score = eval(input(\"请输入你的分数:\\n\"))\n", + "print(type(score))\n", + "print('您的分数是%d' % (score + 10))\n" + ] + }, + { + "cell_type": "markdown", + "id": "782fb987", + "metadata": {}, + "source": [ + "## 数据类型的转换" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00478afb", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "体验数据类型转换:\n", + " 1、input\n", + "\n", + " 2、检测input数据类型str\n", + "\n", + " 3、int() 转换数据类型\n", + "\n", + " 4、检测是否转换成功\n", + "\n", + "\"\"\"\n", + "num = input('请输入数字:\\n')\n", + "print(num)\n", + "print(type(num))\n", + "\n", + "num = int(num)\n", + "print(type(num))\n", + "print('================')\n", + "\n", + "## 数据类型转换函数 ##\n", + "# 1、float() -- 将数据转换成浮点型\n", + "# 显式转换 -- 使用float函数\n", + "s1 = '128.98'\n", + "s2 = '76'\n", + "ff = True\n", + "s3 = 'hello'\n", + "i = 98\n", + "print(type(s1), type(s2), type(ff), type(s3), type(i))\n", + "print(float(s1), type(float(s1)))\n", + "print(float(s2), type(float(s2)))\n", + "print(float(ff), type(float(ff)))\n", + "# error: print(float(s3),type(float(s3))) # 字符串中的数据如果是非数字串,则不允许转换\n", + "print(float(i), type(float(i)))\n", + "\n", + "# 隐式转换\n", + "print(type(10 + 0.1))\n", + "print('================')\n", + "\n", + "# 2、str() -- 将数据转换成字符串\n", + "name = '张三'\n", + "age = 20\n", + "print(type(name), type(age)) # 说明name与age的数据类型不相同\n", + "# error: print('我叫'+name+'今年,'+age+'岁') #当将str类型与int类型进行连接时,报错,解决方案,类型转换\n", + "print('我叫' + name + '今年,' + str(age) + '岁')\n", + "\n", + "print(\"================\")\n", + "\n", + "# 3、int() -- 将数据转换成整型\n", + "s1 = '128'\n", + "f1 = 98.7\n", + "s2 = '76.77'\n", + "ff = True\n", + "s3 = 'hello'\n", + "print(type(s1), type(f1), type(s2), type(ff), type(s3))\n", + "\n", + "print(int(s1), type(int(s1))) # 将str转成int类型 ,字符串为 数字串\n", + "print(int(f1), type(int(f1))) # float转成int类型,截取整数部分,舍掉小数部分\n", + "# error: print(int(s2),type(int(s2))) #将str转成int类型,报错,因为字符串为小数串\n", + "print(int(ff), type(int(ff)))\n", + "# error: print(int(s3),type(int(s3))) #将str转成int类型时,字符串必须为数字串(整数),非数字串是不允许转换\n", + "print(\"================\")\n", + "\n", + "# 4、tuple() -- 将一个序列转换成元组\n", + "list1 = [10, 20, 30]\n", + "print(tuple(list1))\n", + "print(\"================\")\n", + "\n", + "# 5、list() -- 将一个序列转换成列表\n", + "t1 = (10, 20, 30)\n", + "print(list(t1))\n", + "print(\"================\")\n", + "\n", + "# 6、eval() -- 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回\n", + "str2 = '1'\n", + "str3 = '1.1'\n", + "str4 = '(1000, 2000, 3000)'\n", + "str5 = '[1000, 2000, 3000]'\n", + "print(type(eval(str2)))\n", + "print(type(eval(str3)))\n", + "print(type(eval(str4)))\n", + "print(type(eval(str5)))\n", + "print(\"================\")\n", + "\n", + "## 字符 ————> Unicode\n", + "ord('c')\n", + "\n", + "## Unicode ————> 字符\n", + "chr(3)\n", + "\n", + "## round() -- 四舍六入规则——如果是5,则取最接近的偶数\n", + "round(7.7) # 结果: 8\n", + "round(7.4) # 结果: 7\n", + "round(-9.5) # 结果: -10 取最接近的偶数\n", + "round(3.1415926, 3) # 对3.1415926 四舍六入————并保留3位小数\n", + "\n", + "## 其他类型————>复数\n", + "\"\"\"\n", + "complex(real,imag) # real为实部————可以是 整数、浮点数、字符串\n", + "\t\t\t\t # imag为虚部————可以省略————可以是 整数、浮点数, 不能是字符串\n", + "\"\"\"\n", + "complex(3, 4)\n" + ] + }, + { + "cell_type": "markdown", + "id": "c69288fc", + "metadata": {}, + "source": [ + "## 进制之间的转换" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88b685d9", + "metadata": {}, + "outputs": [], + "source": [ + "### 进制之间的转换 ###\n", + "\n", + "# 十进制 ——> 二进制\n", + "a = 10\n", + "print(bin(a))\n", + "\n", + "# 二进制 ——> 十进制\n", + "b = '10111001'\n", + "print(int(b,2))\n", + "\n", + "b = 0b10111001\n", + "print(b)\n", + "\n", + "# 十进制 ——> 八进制\n", + "a = 32\n", + "print(oct(a))\n", + "\n", + "# 八进制 ——> 十进制\n", + "b = '756'\n", + "print(int(b,8))\n", + "\n", + "b = '0o756'\n", + "print(eval(b))\n", + "\n", + "# 十进制 ——> 十六进制\n", + "a = 87\n", + "print(hex(a))\n", + "\n", + "# 十六进制 ——> 十进制\n", + "b = 'A34'\n", + "print(int(b,16))\n", + "\n", + "b = '0xA34'\n", + "print(eval(b))" + ] + }, + { + "cell_type": "markdown", + "id": "ded089da", + "metadata": {}, + "source": [ + "## 二进制与字符串编码" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9635ad3e", + "metadata": {}, + "outputs": [], + "source": [ + "# chr: 数字 -> 字符\n", + "print(chr(0b100111001011000))\n", + "\n", + "# ord: 字符 -> 数字\n", + "print(ord('乘'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0285d179", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "288px" + }, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/text.txt" "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/text.txt" new file mode 100644 index 0000000000000000000000000000000000000000..31e0fce560e96c8b357f5b8630c7d8fbeb0a3ec8 --- /dev/null +++ "b/01_Python\345\237\272\347\241\200\347\237\245\350\257\206/text.txt" @@ -0,0 +1 @@ +helloworld diff --git "a/02_\350\277\220\347\256\227\347\254\246/01_\347\256\227\346\234\257\350\277\220\347\256\227\347\254\246.py" "b/02_\350\277\220\347\256\227\347\254\246/01_\347\256\227\346\234\257\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..9c6e9f540f0adebdc4132724e538114ba01f12a1 --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/01_\347\256\227\346\234\257\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,33 @@ +## 算术运算符 + +a = 20 ** 3 # 幂运算 +print(a) + +print("=============") +b = 20 / 7 # 除法运算,结果是小数 +print(b) + +print("=============") +print(9.0 // 4) # 整除运算,结果只保留整数部分 -- 类型仍是float +print(9 // 4) # 类型是int +print(9 // -4) # -3 +print(-9 // 4) # -3 一正一负的整数公式,向下取整 + +print("=============") +print(20 % 7) # 取余运算 -- 类型是int +print(20.0 % 7) # 取余运算 -- 类型是float + +# 一正一负的公式:余数 = 被除数 - 除数 * 商(整除的结果) +print(9 % -4) # 9 - (-4) * (-3) --> 9 - 12 --> -3 +print(-9 % 4) # -9 - 4 * (-3) --> -9 + 12 --> 3 + +print("=============") +print(-a) # 取a的 负数 +print(+a) # 取a 本身 + +""" +运算符优先级:() + ** + *, /, //, % + +, - +""" diff --git "a/02_\350\277\220\347\256\227\347\254\246/02_\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" "b/02_\350\277\220\347\256\227\347\254\246/02_\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..c0d6edf7bf5aea663b92d772d711f78af5115a62 --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/02_\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,21 @@ +# 单个变量赋值 = +num = 1 +print(num) + +# 多个变量赋值 = +num1, float1, str1 = 1, 0.5, 'hello world' +print(f"num1 = {num1}, float1 = {float1}, str1 = {str1}") +# error: a, b = 20, 30, 40 报错,因为左右变量的个数和值的个数不对应 + +# 多变量赋相同值 = +a = b = 10 +print(f'a = {a}, b = {b}') +print(a, id(a)) +print(b, id(b)) # 内存地址是一样的 + +# 交换两个变量的值 +a, b = 10, 20 +print('交换之前:', a, b) +# 交换 +a, b = b, a +print('交换之后:', a, b) diff --git "a/02_\350\277\220\347\256\227\347\254\246/03_\345\244\215\345\220\210\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" "b/02_\350\277\220\347\256\227\347\254\246/03_\345\244\215\345\220\210\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..1ccdb78a4a184613fe19f2a06a3e6bedfe262265 --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/03_\345\244\215\345\220\210\350\265\213\345\200\274\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,23 @@ +""" + += + -= + *= + /= + //= + %= + **= +""" +# 例如: +a = 10 +a += 1 # a = a + 1 +print(a) + +b = 10 +b -= 1 # b = b - 1 +print(b) + +# 注意:先算复合赋值运算符右边的表达式,再算赋值运算 +c = 10 +c *= 1 + 2 * 2 # c = c * (1 + 2 * 2) +print(c) + diff --git "a/02_\350\277\220\347\256\227\347\254\246/04_\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" "b/02_\350\277\220\347\256\227\347\254\246/04_\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..5622c5c21921509d7eb63ccbc5a5e82cb1e52ccf --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/04_\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,35 @@ +""" + == + != + > + < + >= + <= +""" +# 比较运算符返回的结果为bool,True和False +print(1 == 1) +print(1 != 1) +print("==================") + +''' + 一个 = 称为赋值运算符 , == 称为比较运算符 + 一个变量由三部分组成,标识,类型,值 + == 比较的是值还是标识呢? 比较的是值 + 比较对象的标识使用 is +''' +a = 10 +b = 10 +print(a == b) # True 说明,a与b的value 相等 +print(a is b) # True 说明,a与b的id标识id ,相等 +print("==================") + +# 以下代码没学过,后面会给大家讲解 +lst1 = [11, 22, 33, 44] +lst2 = [11, 22, 33, 44] +print(lst1 == lst2) # value -->True +print(lst1 is lst2) # id -->False +print(id(lst1)) +print(id(lst2)) +print(a is not b) # False a的id与b的id是相等的 +print(lst1 is not lst2) # True +print("==================") diff --git "a/02_\350\277\220\347\256\227\347\254\246/05_\351\200\273\350\276\221\350\277\220\347\256\227\347\254\246.py" "b/02_\350\277\220\347\256\227\347\254\246/05_\351\200\273\350\276\221\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..db10ea5661120b4fdfebcd618da1aeb3caaeb213 --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/05_\351\200\273\350\276\221\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,41 @@ +""" + and 都真才真 + or 一真即真 + not 取反 +""" +a = 0 +b = 2 +c = 3 + +# 1、and 与 +print((a < b) and (c > b)) +print((a > b) and (c > b)) +print("=============") + +# 2、or 或 +print((a < b) or (c > b)) +print((a > b) or (c > b)) +print((a > b) or (c < b)) +print("=============") + +# 3、not +print(not False) +print(not (c > b)) +print("=============") + +# 4、in +s = 'helloworld' +print('w' in s) +print('k' in s) +print('w' not in s) # False +print('k' not in s) # True +print("=============") + +# 5、数字之间的逻辑运算符 +# and 只要有一个值为0,则结果为0,否则结果为最后一个非0数字 +print(a and b) # 0 +print(b and c and 9) # 9 + +# or 只有所有值为0,其结果为0,否则结果为第一个非0数字 +print(a or 0) +print(a or b or c) # 2 diff --git "a/02_\350\277\220\347\256\227\347\254\246/06_\344\275\215\350\277\220\347\256\227.py" "b/02_\350\277\220\347\256\227\347\254\246/06_\344\275\215\350\277\220\347\256\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..696ef75c7dc0065381a81d60a28b046d1310c71b --- /dev/null +++ "b/02_\350\277\220\347\256\227\347\254\246/06_\344\275\215\350\277\220\347\256\227.py" @@ -0,0 +1,7 @@ +print(4 & 8) # 按位与& ,同为1时结果为1 +print(4 | 8) # 按位或| ,同为0时结果为0 +print(4 << 1) # 向左移动1位(移动一个位置) 相当于乘以2 +print(4 << 2) # 向左移动2位(移动2个位置) + +print(4 >> 1) # 向右移动1位,相当于除以2 +print(4 >> 2) # 向右移动2位,相当于除以4 diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/01_if\350\257\255\345\217\245.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/01_if\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..cdd4bf4c548d5ce58c6110ad4e0713031bef0200 --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/01_if\350\257\255\345\217\245.py" @@ -0,0 +1,42 @@ +### if语句 ### +""" +if语句的格式: + if 条件1 and 条件2: + 代码块 + elif 条件1 or 条件2: + 代码块 + else: + 代码块 + +if语句的紧凑式: + 表达式1 if 条件 else 表达式2 +""" +if False: + print("条件成立执行的代码1") + print("条件成立执行的代码2") +print("这个代码执行了吗?") +# 注意:在这个上方没有加缩进的代码,不属于if语句块,即与条件成立与否无关 + +print('Python' > 'Java') # 字符串也可以比较,比较各个字符的编码 + +print("空气{}污染".format('存在' if 3 > 2 else '没有')) + +print('-----------------以下对象的布尔值为False--------------------------------') +print(bool(False)) # False +print(bool(0)) # False +print(bool(0.0)) # False +print(bool(None)) # False +print(bool('')) # False +print(bool("")) # False +print(bool([])) # 空列表 +print(bool(list())) # 空列表 +print(bool(())) # 空元组 +print(bool(tuple())) # 空元组 +print(bool({})) # 空字典 +print(bool(dict())) # 空字典 +print(bool(set())) # 空集合 + +print('---------------------其它对象的布尔值均为True------------------------------------') +print(bool(18)) +print(bool(True)) +print(bool('helloworld')) diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/02_\346\241\210\344\276\213_\347\275\221\345\220\247\344\270\212\347\275\221.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/02_\346\241\210\344\276\213_\347\275\221\345\220\247\344\270\212\347\275\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..8f36462145e68c010a1b54573ac7afaa03bb95cd --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/02_\346\241\210\344\276\213_\347\275\221\345\220\247\344\270\212\347\275\221.py" @@ -0,0 +1,15 @@ +""" +分析: + 年龄大于等于18,输出:已经成年,可以上网 + 新增需求:用户可以输入自己的年龄 +""" + +# 注意:input接收到的数据是str,不能和18做判断 -- 转换成int +age = int(input('请输入您的年龄:\n')) + +if age >= 18: + print(f'您的年龄是{age}岁,已经成年,可以上网。') +else: + print(f'您的年龄是{age}岁,小朋友,回家写作业。') + +print('系统关闭') \ No newline at end of file diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/03_\346\241\210\344\276\213_\345\267\245\344\275\234\345\271\264\351\276\204.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/03_\346\241\210\344\276\213_\345\267\245\344\275\234\345\271\264\351\276\204.py" new file mode 100644 index 0000000000000000000000000000000000000000..ed9ddd92d95ba69397b6f5274ca6b47a2a0ca99c --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/03_\346\241\210\344\276\213_\345\267\245\344\275\234\345\271\264\351\276\204.py" @@ -0,0 +1,21 @@ +""" +需求: + 如果年龄小于18,为童工,不合法 + 如果年龄18-60岁,为合法工作年龄 + 如果年龄大于60岁,为退休年龄 +""" + +""" +步骤: + 1、用户输入自己的年龄,保存变量 -- str + 2、if做判断 elif + 3、输出提示信息 +""" +age = int(input('请输入您的年龄:\n')) + +if age < 18: + print(f'您的年龄为{age}岁,童工') +elif 18 <= age <= 60: + print(f'您的年龄为{age}岁,合法工人') +elif age > 60: + print(f'您的年龄为{age}岁,可以退休了') diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/04_\346\241\210\344\276\213_\345\205\254\344\272\244\350\275\246\351\227\256\351\242\230.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/04_\346\241\210\344\276\213_\345\205\254\344\272\244\350\275\246\351\227\256\351\242\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..f37abac49ce3960f7c840b9cc42d725f6b94e043 --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/04_\346\241\210\344\276\213_\345\205\254\344\272\244\350\275\246\351\227\256\351\242\230.py" @@ -0,0 +1,25 @@ +""" +坐公交: + 如果有钱可以上车,没有钱,不能上车 + 如果上车了,判断能否坐下 -- 是否有空座位 +""" + +""" +分析: + 1、准备要用到的数据:钱和空座位 + 2、判断是否有钱:上车 和 不能上车 + 3、上车了,判断是否能坐下:有无空座位 +""" +money = eval(input('请输入您的存款:\n')) +seat = eval(input('请输入车上的空座位数量:\n')) + +# 判断是否能上车 +if money: + print('土豪,请上车') + # 判断是否能坐下 + if seat: + print('有空座位,坐下了') + else: + print('没有空座位,站着') +else: + print('朋友,您忘带钱了,不能上车') \ No newline at end of file diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\272\224\347\224\250_\347\214\234\346\213\263\346\270\270\346\210\217.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\272\224\347\224\250_\347\214\234\346\213\263\346\270\270\346\210\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..3e4ebafa8ccbefd7cbdc4173bac8c5c7d084da59 --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\272\224\347\224\250_\347\214\234\346\213\263\346\270\270\346\210\217.py" @@ -0,0 +1,29 @@ +""" +1、出拳 + 玩家:手动输入 + 电脑:随机 + 1.1 导入模块 + import random + 1.2 使用这个模块中的功能 + random.randint() +2、判断输赢 +""" + +# 1、出拳 +# 玩家 +player = int(input('请出拳:0--石头;1--剪刀;2--布\n')) +# 电脑 +import random +computer = random.randint(0,2) + +# 2、判断输赢 +if (((player == 0) and (computer == 1)) or ((player == 1) and (computer == 2)) or ((player == 2) and (computer == 0))): + print('玩家胜利') +elif player == computer: + print('平局') +else: + print('电脑胜利') + + + + diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/06_\344\270\211\347\233\256\350\277\220\347\256\227\347\254\246.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/06_\344\270\211\347\233\256\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..3439da34718f8a449029767e8bce37c2e2f38b11 --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/06_\344\270\211\347\233\256\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,24 @@ +""" +语法: + 条件成立执行的表达式 if 条件 else 条件不成立执行的表达式 +""" +a = 1 +b = 2 + +min = a if a < b else b +print(min) + +""" +需求:有两个变量,比较大小 + 如果变量1 大于 变量2:执行 变量1 - 变量2 + 否则 变量2 - 变量1 +""" +num1 = 66 +num2 = 88 +num = num1 - num2 if num1 > num2 else num2 - num1 +print(num) + + + + + diff --git "a/03_\345\210\244\346\226\255\350\257\255\345\217\245/07_pass\350\257\255\345\217\245.py" "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/07_pass\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..a58553e9c93165a3e5ebff17ab39ce8a4b3fb96a --- /dev/null +++ "b/03_\345\210\244\346\226\255\350\257\255\345\217\245/07_pass\350\257\255\345\217\245.py" @@ -0,0 +1,8 @@ +# pass语句,什么都不做,只是一个占位符,用到需要写语句的地方 +answer = input('您是会员吗?y/n') + +# 判断是否是会员 +if answer == 'y': + pass +else: + pass diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/20_while\345\276\252\347\216\257.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/20_while\345\276\252\347\216\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..8684f5d2447ae706c7599d84d7ce4b7356fe1713 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/20_while\345\276\252\347\216\257.py" @@ -0,0 +1,18 @@ +## while循环 ## +""" +格式: + while 条件: + 代码块1 + else :#当循环正常运行完毕,执行else + 代码块2 +""" + +# 需求:重复打印5次 我真牛逼 +i = 0 +while i < 5: + print('我真牛逼!') + i += 1 + + + + diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/21_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2141.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/21_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2141.py" new file mode 100644 index 0000000000000000000000000000000000000000..3906a89bec09a48653a3c54dcffda89006384dd4 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/21_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2141.py" @@ -0,0 +1,15 @@ +## 需求:求1-100数字累加和 + +# 准备数据 +i = 1 + +# 结果变量 +result = 0 + +# 循环 +while i <= 100: + result += i + i += 1 + +# 打印最终结果 +print(result) \ No newline at end of file diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/22_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2142.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/22_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2142.py" new file mode 100644 index 0000000000000000000000000000000000000000..c0ac7943e6ddf392ac750692e3a87dc77df5821a --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/22_\345\272\224\347\224\250_\347\264\257\345\212\240\345\222\2142.py" @@ -0,0 +1,19 @@ +## 需求:1-100偶数累加和 + +# 方法一:条件判断 +i = 1 +result = 0 +while i <= 100: + if i % 2 == 0: + result += i + i += 1 + +print(result) + +# 方法二:计数器控制 +i = 0 +result = 0 +while i <= 100: + result += i + i += 2 +print(result) \ No newline at end of file diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/23_break\345\205\263\351\224\256\345\255\227.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/23_break\345\205\263\351\224\256\345\255\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..2825ccb2243db04bd7ff682405430dedaf445e27 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/23_break\345\205\263\351\224\256\345\255\227.py" @@ -0,0 +1,11 @@ +# break: 当某些条件成立,退出整个循环 +# 循环吃5个苹果,吃完第3个吃饱了,之后的不吃了 + +i = 1 +while i <= 5: + print(f'吃第{i}个苹果') + i += 1 + # 吃到第4个时,不吃了 + if i == 4: + print('吃饱了,不吃了') + break diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/24_continue\345\205\263\351\224\256\345\255\227.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/24_continue\345\205\263\351\224\256\345\255\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..c1445ae1135c447ee7dbd40775a77e1a1b225962 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/24_continue\345\205\263\351\224\256\345\255\227.py" @@ -0,0 +1,10 @@ +# continue: 当条件成立,跳过当前的一次循环,继续执行下一次循环 +# 吃5个苹果 -- 循环:第3个苹果是坏的,不吃 + +i = 0 +while i < 5: + i = i + 1 + if i == 3: + print(f'第{i}个苹果不吃') + continue + print(f'吃第{i}个苹果') diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/25_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/25_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..b0766bad8d3d1a316e71d166b0972e959a8a1552 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/25_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227.py" @@ -0,0 +1,14 @@ +""" +1、循环打印5次:媳妇儿,我错了 +2、今天晚上的碗,我来刷 +3、以上为一套惩罚,要重复执行3次 +""" +i = 0 +while i < 3: + j = 0 + while j < 5: + print('媳妇儿,我错了') + j += 1 + print('今天晚上的碗,我来刷') + i += 1 + print('一套惩罚结束==============') \ No newline at end of file diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/26_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2671.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/26_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2671.py" new file mode 100644 index 0000000000000000000000000000000000000000..d49f305c6381543f0fbc3e6565f11d21c31363b1 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/26_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2671.py" @@ -0,0 +1,15 @@ +""" +打印星号: + 1、一行5颗星星 + 2、一共打印5行 +""" +i = 0 +while i < 5: + # 一行星星的开始 + j = 0 + while j < 5: + print('*', end=' ') + j += 1 + # 一行星星的结束 + print() + i += 1 diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/27_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2672.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/27_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2672.py" new file mode 100644 index 0000000000000000000000000000000000000000..37a03602b498695269553a55548648c95d7c2886 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/27_\346\241\210\344\276\213_\346\211\223\345\215\260\346\230\237\345\217\2672.py" @@ -0,0 +1,11 @@ +# 打印星星三角形:每行星星的个数与行号数相同,打印5行 +i = 0 +while i < 5: + # 一行星星的开始 + j = 0 + while j <= i: + print('*', end=' ') + j += 1 + # 一行星星的结束 + i += 1 + print() diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/28_\345\272\224\347\224\250_\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/28_\345\272\224\347\224\250_\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..f6ecb28fa0a2fff887a0f888487c0cbba9686e42 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/28_\345\272\224\347\224\250_\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" @@ -0,0 +1,10 @@ +# 打印九九乘法表 -- 一行表达式的个数和行数相等 + +i = 1 +while i <= 9: + j = 1 + while j <= i: + print(f'{j} * {i} = {i * j}', end='\t') + j += 1 + print() + i += 1 diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/29_for\345\276\252\347\216\257.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/29_for\345\276\252\347\216\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..1a4fa395c2eb413c4aa7834cac63aa23e75da8a7 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/29_for\345\276\252\347\216\257.py" @@ -0,0 +1,41 @@ +## for循环 ## +""" + 格式: + for 循环变量 in 迭代对象(列表、字符串……): + 代码块1 + else : #当循环正常运行完毕,执行else————没有因为break、return而退出————continue不影响else的执行 + 代码块2 +""" +for i in range(10): + print(i) +print('==========') + +for i in range(0,6): #== for(int i =0;i<6;i++) + print("循环") +print('==========') +#0 1 2 3 4 5 不是传统的i++,而是组合的数据类型 +#i i i i i i + + +# 不打印e之后的内容 -- 使用break,退出循环 +str1 = "BIT-itheima" +for i in str1: + print(i) + if i == 'e': + break +print('==========') + + +# 不打印e -- 使用continue,跳过此次循环 +str1 = "BIT-itheima" +for i in str1: + if i == 'e': + print('遇到e不打印') + continue + print(i) + + +''' +for i in file: #file为一个文件,则i一行一行遍历 + print(i) #一行一行输出 +''' diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/30_while...else\350\257\255\345\217\245.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/30_while...else\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..6ec200534d50a04a2798888f8eaf55d554caa2a9 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/30_while...else\350\257\255\345\217\245.py" @@ -0,0 +1,28 @@ +""" +格式: + while 条件: + 代码块1 + else :#当循环正常运行完毕,执行else + 代码块2 +""" +i = 0 +while i < 5: + if i == 3: + print('这遍说的没底气') + break + print('我真牛逼!') + i += 1 +else: + print('你真自信') +print('===================') + +i = 0 +while i < 5: + if i == 3: + print('这遍说的不够大声') + i += 1 + continue + print('我真牛逼!') + i += 1 +else: + print('你真自信') \ No newline at end of file diff --git "a/04_\345\276\252\347\216\257\350\257\255\345\217\245/31_for..else\350\257\255\345\217\245.py" "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/31_for..else\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..983484be393fd9c455bd79a2bc640778a3e492a1 --- /dev/null +++ "b/04_\345\276\252\347\216\257\350\257\255\345\217\245/31_for..else\350\257\255\345\217\245.py" @@ -0,0 +1,15 @@ +""" +格式: + for 循环变量 in 迭代对象(列表、字符串……): + 代码块1 + else : #当循环正常运行完毕,执行else————没有因为break、return而退出————continue不影响else的执行 + 代码块2 +""" +str1 = 'itheima' +for i in str1: + if i == 'e': + break + # continue + print(i) +else: + print('循环正常结束执行的else代码') \ No newline at end of file diff --git "a/05_\345\255\227\347\254\246\344\270\262/01_\345\255\227\347\254\246\344\270\262\347\232\204\351\251\273\347\225\231\346\234\272\345\210\266.py" "b/05_\345\255\227\347\254\246\344\270\262/01_\345\255\227\347\254\246\344\270\262\347\232\204\351\251\273\347\225\231\346\234\272\345\210\266.py" new file mode 100644 index 0000000000000000000000000000000000000000..ccc66debe86fd175052439cb0ccbe4925b8a1939 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/01_\345\255\227\347\254\246\344\270\262\347\232\204\351\251\273\347\225\231\346\234\272\345\210\266.py" @@ -0,0 +1,62 @@ +""" +字符串的驻留机制: + 仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串的驻留池之中。 + +驻留机制的几种情况: + 1、字符串的长度为0或1时。 + 2、符合标识符的字符串(只含有数字、字母、下划线,不能以数字开头,不能以关键字重名) + 3、字符串只在编译时进行驻留,而非运行时 + 4、[-5,256]之间的整数数字 +""" + +# 符合标识符,发生驻留机制 +a = 'Python' +b = "Python" +c = '''Python''' +print(a, id(a)) +print(b, id(b)) +print(c, id(c)) # 发现 a,b,c三个字符串共占一个空间 +print("================") + +# 字符串只在编译时进行驻留,而非运行时 +str1 = "abc" +str2 = "ab" + "c" +str3 = "".join(["ab", "c"]) +print(str1, id(str1)) +print(str2, id(str2)) +print(str3, id(str3)) # 内存地址和上面的不一样 +print("================") + +# 强制驻留 +import sys + +s1 = "abc%" +s2 = "abc%" +print(s1 is s2) # 本应该是False,Pycharm内做了优化 +s1 = sys.intern(s2) +print(s1 is s2) + +# 字符串的比较 +print('apple' > 'app') # True +print('apple' > 'banana') # False,相当于97 > 98 > False +print(ord('a'), ord('b')) +print(ord('杨')) + +print(chr(97), chr(98)) +print(chr(26472)) + +''' +== 与 is 的区别: + == 比较的是 value + is 比较的是id是否相等 +''' +a = b = 'Python' +c = 'Python' +print(a == b) # True +print(b == c) # True + +print(a is b) # True +print(a is c) # True +print(id(a)) # 2204259933168 +print(id(b)) # 2204259933168 +print(id(c)) # 2204259933168 \ No newline at end of file diff --git "a/05_\345\255\227\347\254\246\344\270\262/02_\347\273\204\345\220\210\346\225\260\346\215\256\347\261\273\345\236\213\346\246\202\350\277\260.py" "b/05_\345\255\227\347\254\246\344\270\262/02_\347\273\204\345\220\210\346\225\260\346\215\256\347\261\273\345\236\213\346\246\202\350\277\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..74f426e77cf7c3f9f6dc4850e46927259a5a12e9 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/02_\347\273\204\345\220\210\346\225\260\346\215\256\347\261\273\345\236\213\346\246\202\350\277\260.py" @@ -0,0 +1,20 @@ +## 组合数据类型 ## +""" +组合数据类型: + 序列类型————字符串、元组、列表————均支持操作符 in len() [:]等 ———— 均可利用序号索引(正向、反向) + 集合类型————集合 + 映射类型————字典 +""" + +""" +序列类型的 通用操作符: + x not in y + x + y : 连接x,y + x[1:4:2] 切片 + x[::-1] 倒序 + len(x) + max(x) \ min(x) + s.index(x,i,j) ———— x从i到j,第一次出现的序号 + s.index(x) ————x第一次出现时的序号 + s.count(x) ———— x在s中,出现了几次 +""" diff --git "a/05_\345\255\227\347\254\246\344\270\262/03_\350\256\244\350\257\206\345\255\227\347\254\246\344\270\262.py" "b/05_\345\255\227\347\254\246\344\270\262/03_\350\256\244\350\257\206\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000000000000000000000000000000000000..c060d17fbb3cc87a21605c072915dd61bb8821bd --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/03_\350\256\244\350\257\206\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,52 @@ +## 字符串 ## +# 可用单引号、双引号、三引号 来表示字符串 +a = 'hello '\ + 'world' # 单引号 +print(a) +print(type(a)) + +b = "TOM" # 双引号 +print(type(b)) + +# 三引号 +c = '''I am TOM''' +print(type(c)) + +d = """I + am Jack""" +print(type(d)) + +# 单引号 表示字符串 -- 若字符串中有‘’,如let's go -- 则会报错 -- 此时需要用“” -- 使用双引号时,同理 +go = "let's go" + +# 或者使用 \ +go2 = 'let\'s go' +print(type(go2)) + + + +## 若字符串中 出现\时 要特别留意 +"\n"# 回车 +'\b'# 退一个 +'\r'# 到一行之首 +# 想要输入“\”这个特殊字符,则需要:“\\” +path = '\\' +print(path) +# \t表示制表符 \n表示换行 +print('Python\n语言\t') + + +## 字符串的长度 -- 利用len() +str1 = "I Like Python" +print(len(str1))# 13 -- 空格也算一个字符 + +## 拼接字符串 -- 利用‘+’、‘*’ +str2 = "hello" + " " + "world" +str3 = 3 * 'OK!' # 多次拼接 +result1 = str2 in str3 # 若str1 是 str2的子串,则返回True + + + + + + diff --git "a/05_\345\255\227\347\254\246\344\270\262/04_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\207\272.py" "b/05_\345\255\227\347\254\246\344\270\262/04_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\207\272.py" new file mode 100644 index 0000000000000000000000000000000000000000..d316b72f996d24e2ceb0aeb1ed26b7b3c5470aa7 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/04_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\207\272.py" @@ -0,0 +1,5 @@ +print("hello world") + +name = "ROSE" +print('我的名字是%s' % name) +print(f'我的名字是{name}') \ No newline at end of file diff --git "a/05_\345\255\227\347\254\246\344\270\262/05_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\205\245.py" "b/05_\345\255\227\347\254\246\344\270\262/05_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..77abf7f6e3c7077d16c1613f547fb6a971b428bb --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/05_\345\255\227\347\254\246\344\270\262\347\232\204\350\276\223\345\205\245.py" @@ -0,0 +1,3 @@ +# 输入密码 +password = input('请输入您的密码:\n') +print(f'您输入的密码是:{password}') \ No newline at end of file diff --git "a/05_\345\255\227\347\254\246\344\270\262/06_\345\255\227\347\254\246\344\270\262\347\232\204\344\270\213\346\240\207.py" "b/05_\345\255\227\347\254\246\344\270\262/06_\345\255\227\347\254\246\344\270\262\347\232\204\344\270\213\346\240\207.py" new file mode 100644 index 0000000000000000000000000000000000000000..7c432dfcee4d38466a29c7222f873e26138c7669 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/06_\345\255\227\347\254\246\344\270\262\347\232\204\344\270\213\346\240\207.py" @@ -0,0 +1,12 @@ +go = "why don't we go?" +print(go) + +# 0……3……15 索引 +str1 = go[6] +str2 = go[4] +print(str1) +print(str2) + +# -16…………-1 索引 +str3 = go[-3] +print(str3) diff --git "a/05_\345\255\227\347\254\246\344\270\262/07_\345\210\207\347\211\207.py" "b/05_\345\255\227\347\254\246\344\270\262/07_\345\210\207\347\211\207.py" new file mode 100644 index 0000000000000000000000000000000000000000..7d943a4ceedcf4770c227123c8ca41736de33a6e --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/07_\345\210\207\347\211\207.py" @@ -0,0 +1,39 @@ +""" +切片:截取其中的一部分 + 字符串、列表、元素都支持切片操作 + +语法: + 序列[开始位置下标:结束位置下标:步长] # 左闭右开 +""" + +go = "why don't we go?" + +# 得到整个字符串数据 +print(go) + +# 切片 +str1 = go[3:7] +str2 = go[:3] # 如果不写开始,默认从0开始 +str3 = go[3:] # 如果不写结束,默认选取到最后 + +print(str1) +print(str2) +print(str3) + +# 考虑步长 +str4 = '0123456789' +print(str4[2:5:1]) # 234 +print(str4[2:5:2]) # 24 +print(str4[2:5]) # 234 如果不写步长,默认为1 +print(str4[-4:-1]) # 678 -1表示最后一个数,其余依次类推 +print(str4[-4:-1:1]) # 678 + +print(str4[::-1]) # 如果步长为负数,表示倒叙选取 + + +print(str4[2:5:-1]) # 无法选取数据 +print(str4[-4:-1:-1]) # 无法选取数据 +# 选取方向:从左到右。 -1步长:从右到左选取 +# 如果选取方向和步长方向冲突,则无法选取数据 + +print(str4[-1:-4:-1]) # 987 diff --git "a/05_\345\255\227\347\254\246\344\270\262/08_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\346\237\245\346\211\276.py" "b/05_\345\255\227\347\254\246\344\270\262/08_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\346\237\245\346\211\276.py" new file mode 100644 index 0000000000000000000000000000000000000000..8f3c3d8595904e142e1d7a2e85b926a573ec0edd --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/08_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\346\237\245\346\211\276.py" @@ -0,0 +1,50 @@ +mystr = "hello world and itcast and itheima and Python" + +""" +1.find(): + 检测某个子串是否包含在这个字符串中,如果存在,则返回这个子串的开始下标,如果不存在,则返回-1 + +语法: + 字符串序列.find(子串, 开始位置下标, 结束位置下标) +""" + +# find() +print(mystr.find('and')) # 12 +print(mystr.find('and', 15, 30)) # 23 +print(mystr.find('ands')) # -1,ands子串不存在 +print("===============") + + +""" +2.index(): + 检测某个子串是否包含在这个字符串中,如果存在,则返回这个子串的开始下标,如果不存在,则报异常 + +语法: + 字符串序列.index(子串,开始位置下标,结束位置下标) +""" +# 2.index() +print(mystr.index('and')) # 12 +print(mystr.index('and', 15, 30)) # 23 +# print(mystr.index('ands')) # ands子串不存在,报异常 +print("===============") + + +# 3.count(): 返回某子串在字符串中出现的个数 +print(mystr.count('and')) # 3 +print(mystr.count('and', 15, 30)) # 1 +print(mystr.count('ands')) # 0 +print("===============") + + +# 4.rfind(): 和find()功能相同,但是查找方向为右侧开始 +print(mystr.rfind('ands')) # -1 +print(mystr.rfind('and')) # 35 +print("===============") + + +# 5.rindex(): 和index()功能相同,但查找方向为右侧开始 +# print(mystr.rindex('ands')) # 异常 +print(mystr.rindex('and')) # 35 +print("===============") + + diff --git "a/05_\345\255\227\347\254\246\344\270\262/09_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\344\277\256\346\224\271.py" "b/05_\345\255\227\347\254\246\344\270\262/09_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\344\277\256\346\224\271.py" new file mode 100644 index 0000000000000000000000000000000000000000..4214dbb2eb93b8016c9b912a34be65201212fd97 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/09_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\344\277\256\346\224\271.py" @@ -0,0 +1,51 @@ +mystr = "hello world and itcast and itheima and Python" + +""" +1、replace(): 替换 + 字符串序列.replace(旧子串,新子串,替换次数) + 注意:替换次数如果查出子串出现次数,则替换次数为该子串出现次数。 +""" +# replace() 把and换成he +# replace() 函数有返回值,返回值是修改后的字符串,原来的数据则没有修改 -- 字符串是不可变数据类型 +new_str1 = mystr.replace('and', 'he') # 默认替换所有and +new_str2 = mystr.replace('and', 'he', 1) # 替换一个and +new_str3 = mystr.replace('and', 'he', 10) # 替换次数如果超过子串出现的次数,则替换所有 +print(mystr) +print(new_str1) +print(new_str2) +print(new_str3) + +""" +2、split(): 按照指定字符分割字符串,返回一个列表 + 字符串序列.split(sep='分割字符',maxsplit=num) + 注意:num表示的是分割字符出现的次数,即将来返回数据个数为 num + 1 个 + 默认是将分割字符全部丢失,num可以做到其只丢失部分 +""" +list1 = mystr.split('and') # 返回一个列表,丢失分割字符 +list2 = mystr.split('and', 2) # 分割2次,其余分割字符保留 +print(list1) +print(list2) + +str1 = 'python|java|keras' +list3 = str1.split('|') # 以 ‘|’ 分割字符串,生产列表 ['python' , 'java' , 'keras'] +print(list3) + +''' +3、rsplit(分割字符, num): + 从右侧开始劈分 +''' +str1 = 'python|java|keras' +list3 = str1.split('|', 1) +print(list3) # ['python', 'java|keras'] + +""" +4、join(): 用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串 + 子串.join(多个字符串组成的序列) +""" +mylist = ['aa', 'bb', 'cc'] +new_str = '...'.join(mylist) # 合并列表里面的字符串为一个大字符串 +print(new_str) + +str2 = "pythonJava" +new_str = ','.join(str2) # 效果:用‘,‘分割 字符串里的所有元素 +print(new_str) diff --git "a/05_\345\255\227\347\254\246\344\270\262/10_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\244\247\345\260\217\345\206\231\350\275\254\346\215\242.py" "b/05_\345\255\227\347\254\246\344\270\262/10_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\244\247\345\260\217\345\206\231\350\275\254\346\215\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..189d56149f889ad4228d6d935f1ad7bfb003b4e4 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/10_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\244\247\345\260\217\345\206\231\350\275\254\346\215\242.py" @@ -0,0 +1,25 @@ +mystr = "hello world and itcast and itheima and Python" + +# 1、capitalize(): 字符串首字母大写 +new_str = mystr.capitalize() +print(new_str) + +# 2、title(): 字符串中每个单词首字母大写 +new_str = mystr.title() +print(new_str) + +# 3、upper(): 小写全部转大写 +new_str = mystr.upper() +print(new_str) + +# 4、lower(): 大写全部转小写 +new_str = mystr.lower() +print(new_str) + +print(new_str.islower()) # new_str中,全是小写时,返回True + +# 5、swapcase(): 字符串的大小写字母转换 +new_str = mystr.swapcase() +print(new_str) + + diff --git "a/05_\345\255\227\347\254\246\344\270\262/11_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\240\351\231\244\347\251\272\346\240\274.py" "b/05_\345\255\227\347\254\246\344\270\262/11_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\240\351\231\244\347\251\272\346\240\274.py" new file mode 100644 index 0000000000000000000000000000000000000000..4aa965162d94bad51e3d5c5cc1d6f3de671a0342 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/11_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\240\351\231\244\347\251\272\346\240\274.py" @@ -0,0 +1,19 @@ +mystr = ' hello world and itcast and itheima and Python ' +print(mystr) + +# 1、lstrip(): 删除左侧空白字符 +new_str = mystr.lstrip() # 默认删除空白字符 +print(new_str) + +# 2、rstrip(): 删除右侧空白字符 +new_str = mystr.rstrip() +print(new_str) + +# 3、strip(): 删除两侧空白字符 +new_str = mystr.strip() +print(new_str) + +str1 = 'pppppiiii my name is zhao iiiiipppppipi' +new_str = str1.strip('pi') # 删除str两侧的 字符p或i +print(new_str) + diff --git "a/05_\345\255\227\347\254\246\344\270\262/12_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\255\227\347\254\246\344\270\262\345\257\271\351\275\220.py" "b/05_\345\255\227\347\254\246\344\270\262/12_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\255\227\347\254\246\344\270\262\345\257\271\351\275\220.py" new file mode 100644 index 0000000000000000000000000000000000000000..0d7c8d182351f47d45375f0bcb39e8e2db63b923 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/12_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\255\227\347\254\246\344\270\262\345\257\271\351\275\220.py" @@ -0,0 +1,38 @@ +## 填充字符串 + +""" +1、ljust(): + 返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串 +语法: + 字符串序列.ljust(长度,填充字符) +""" +mystr = 'Hello' +new_str1 = mystr.ljust(10, '.') +new_str2 = mystr.ljust(10) +print(new_str1) +print(new_str2) + + +""" +2、rjust(): + 返回一个原字符串右对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串 +""" +new_str = mystr.rjust(10, '.') +print(new_str) + + +""" +3、center(): + 返回一个原字符串居中对齐,并使用指定字符(默认空格)填充至对应长度 的新字符串 +""" +new_str = mystr.center(10, '!') # 定义长度,填充物 +print(new_str) + + +''' +4、zfill(长度): + 右对齐,使用0进行填充 +''' +print(mystr.zfill(20)) +print(mystr.zfill(10)) +print('-8910'.zfill(8)) # 注意0填充的位置 \ No newline at end of file diff --git "a/05_\345\255\227\347\254\246\344\270\262/13_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255\345\274\200\345\244\264\345\222\214\347\273\223\345\260\276.py" "b/05_\345\255\227\347\254\246\344\270\262/13_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255\345\274\200\345\244\264\345\222\214\347\273\223\345\260\276.py" new file mode 100644 index 0000000000000000000000000000000000000000..d5102b67f5bcc52e20c98ec185ab411421b0e298 --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/13_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255\345\274\200\345\244\264\345\222\214\347\273\223\345\260\276.py" @@ -0,0 +1,22 @@ +mystr = 'hello world and itcast and itheima and Python' + +""" +1、startswith(): + 检查字符串是否是以指定子串开头,是则返回True,否则返回False。 + 如果设置开始和结束位置下标,则在指定范围内检查 + +语法: + 字符串序列.startswith(子串,开始位置下标,结束位置下标) +""" + +# 1、startswith(): 判断字符串是否以某个子串开头 +print(mystr.startswith('hello')) +print(mystr.startswith('hel')) +print(mystr.startswith('hellos')) + + +# 2、endswith(): 判断字符串是否以某个子串结尾,语法和startswith()类似 +print(mystr.endswith('Python')) +print(mystr.endswith('Pythons')) +print(mystr.endswith('on')) + diff --git "a/05_\345\255\227\347\254\246\344\270\262/14_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255.py" "b/05_\345\255\227\347\254\246\344\270\262/14_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255.py" new file mode 100644 index 0000000000000000000000000000000000000000..0d14ce555541e6411b9a7e6d30f1b13e9e5e72af --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/14_\345\255\227\347\254\246\344\270\262\345\270\270\347\224\250\346\226\271\346\263\225_\345\210\244\346\226\255.py" @@ -0,0 +1,48 @@ +mystr = 'hello world and itcast and itheima and Python' + +# 1、isalpha(): 字符串不为空而且全部是字母,则返回True,否则返回False +mystr1 = 'hello' +mystr2 = 'hello1234' +print(mystr1.isalpha()) # True +print(mystr2.isalpha()) # False +print('张三a'.isalpha()) # True,汉字也可 +print('==========') + +# 2、isdigit(): 字符串全部是数字,则返回True,否则返回False +mystr1 = "aaa12345" +mystr2 = '12345' +print(mystr1.isdigit()) # False +print(mystr2.isdigit()) # True +print('==========') + +# 3、isalnum: 字符串不为空,并且全部是数字或者字母,则返回True,否则返回False +mystr1 = 'aaa12345' +mystr2 = '12345-' +print(mystr1.isalnum()) # True +print(mystr2.isalnum()) # False +print('==========') + +# 4、isspace(): 如果字符串只包含空白,则返回True,否则返回False +mystr1 = '1 2 3 4 5' +mystr2 = ' ' +print(mystr1.isspace()) # False +print(mystr2.isspace()) # True +print('\t'.isspace()) # True +print('==========') + +# 5、isidentifier(): 判断字符串是不是有效标识符,是返回True,否则返回False +mystr1 = 'abc%' +mystr2 = 'hellopython' +print(mystr1.isidentifier()) # False +print(mystr2.isidentifier()) # True +print('==========') + +# 6、isdecimal():检查字符串是否只包含十进制字符 +print('123'.isdecimal()) # True +print('123四'.isdecimal()) # False +print('==========') + +# 7、isnumeric():如果字符串中只包括数字,就返回Ture,否则返回False。 +print('123'.isnumeric()) # True +print('123六'.isnumeric()) # True,中文数字也算 +print('IIIIIIIV'.isnumeric()) # False diff --git "a/05_\345\255\227\347\254\246\344\270\262/15_\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" "b/05_\345\255\227\347\254\246\344\270\262/15_\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" new file mode 100644 index 0000000000000000000000000000000000000000..332d11fe896bbf03e79aa294c5c8f711d7962f1e --- /dev/null +++ "b/05_\345\255\227\347\254\246\344\270\262/15_\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" @@ -0,0 +1,12 @@ +s = '天涯共此时' +# 编码 +print(s.encode(encoding='GBK')) # 在GBK这种编码格中 一个中文占两个字节 +print(s.encode(encoding='UTF-8')) # 在UTF-8这种编辑格式中,一个中文占三个字节 + +# 解码 +# byte代表就是一个二进制数据(字节类型的数据) +byte = s.encode(encoding='GBK') # 编码 +print(byte.decode(encoding='GBK')) # 解码 + +byte = s.encode(encoding='UTF-8') +print(byte.decode(encoding='UTF-8')) diff --git "a/06_\345\210\227\350\241\250/00_\347\224\237\346\210\220\345\210\227\350\241\250.py" "b/06_\345\210\227\350\241\250/00_\347\224\237\346\210\220\345\210\227\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..d5e89abfb51738592f035da1885bda46121232f2 --- /dev/null +++ "b/06_\345\210\227\350\241\250/00_\347\224\237\346\210\220\345\210\227\350\241\250.py" @@ -0,0 +1,26 @@ +""" +1、运用range()函数————自动生成 数据 +格式: + range([start ,] stop [, step]) + #start: 列表 开始值,默认为 0 + #stop: 列表 结束值,不入列表中 + #即 [start,stop) + + #step: 列表步长 默认为 1 +""" +for i in range(2, 15, 3): + print(i, end=',') +print() + +''' +2、创建列表的第二种方式,使用[] +''' +lst = ['hello', 'world', 98, 'hello'] +print(lst) +print(lst[0], lst[-4]) + +''' +3、创建列表的第三种方式,使用内置函数list() +''' + +lst2 = list(['hello', 'world', 98]) diff --git "a/06_\345\210\227\350\241\250/01_\344\270\213\346\240\207.py" "b/06_\345\210\227\350\241\250/01_\344\270\213\346\240\207.py" new file mode 100644 index 0000000000000000000000000000000000000000..a9a2a5714d0e10198165904b9e9c85fc081d26d4 --- /dev/null +++ "b/06_\345\210\227\350\241\250/01_\344\270\213\346\240\207.py" @@ -0,0 +1,63 @@ +## 列表概述 ## +# 列表————有序的 数据所构成的 集合————列表中,可以存放 不同类型 的数据 + +# 访问列表中的元素————列表中,每一个元素都有 唯一标号————通过标号索引元素 +# 0 1 2 3 4 …… +# …… -3 -2 -1 + +name_list = ['TOM', 'Lily', 'ROSE'] +print(name_list) + +print(name_list[1]) +print(name_list[0]) +# error: print(name_list[3]) # list index out of range +print("======================") + +##获取一个元素## +# 创建列表a +a = [2, 3, 4, 5, 6] +a.append("混入了一个字符串") + +print(a[0]) # 获取第一个元素 +# 获取最后一个元素的三个方法: +print(a[4]) +print(a[-1]) +print(a[len(a) - 1]) +print("======================") + +##获取多个连续的元素## +# 从第一个元素开始,连取三个元素 +print(a[:3]) # 输出索引为[0,3)的元素 +print(a[1:3]) # 输出索引为[1,3)的元素 +print(a[1:]) # 输出索引为1及之后的所有元素 +print("======================") + +## 关于列表切片的细节 切片默认情况为 [start=0 : stop=len(list)-1 : step=1] +lst = [10, 20, 30, 40, 50, 60, 70, 80] + +print('原列表', id(lst)) +lst2 = lst[1:6:1] +print('切的片段:', id(lst2)) # 不再是同一个列表 + +print(lst[1:6]) # 默认步长为1 +print(lst[1:6:]) + +# start=1,stop=6,step=2 +print(lst[1:6:2]) + +# stop=6,step=2,start采用默认 +print(lst[:6:2]) + +# start=1,step=2,stop采用默认 +print(lst[1::2]) + +print('------------ step步长为负数的情况-倒序 ---------------') + +print('原列表:', lst) +print(lst[::-1]) + +# start=7,stop省略 step=-1 +print(lst[7::-1]) + +# start=6,stop=0,step=-2 +print(lst[6:0:-2]) diff --git "a/06_\345\210\227\350\241\250/02_\346\237\245\346\211\276\345\207\275\346\225\260.py" "b/06_\345\210\227\350\241\250/02_\346\237\245\346\211\276\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..98e57ab833ff93d12d8f54351d7f1c7444500a62 --- /dev/null +++ "b/06_\345\210\227\350\241\250/02_\346\237\245\346\211\276\345\207\275\346\225\260.py" @@ -0,0 +1,34 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +""" +1、index(): + 返回指定数据所在位置的下标 +语法: + 列表序列.index(数据,开始位置下标,结束位置下标) + 注意:如果查找的数据不存在则报错 +""" +print(name_list.index('ROSE')) # 2 +# print(name_list.index('tom')) # 报错 + +lst = ['hello', 'world', 98, 'hello'] +print(lst.index('hello')) # 如果列表中有相同元素只返回列表中相同元素的第一个元素的索引 + +# 2、count(): 统计指定数据在当前列表中出现的次数 +print(name_list.count('TOM')) # 1 +print(name_list.count('TOMS')) # 0 + +# 3、len(): 返回列表长度 +print(len(name_list)) + +list2 = [[98, 78, 69, 60], 54, [93, 87, 87]] # 创造一个列表 +print(len(list2)) # 3 +print(len(list2[0])) # 4 + +# 4、统计函数 +list1 = [98, 78, 69, 54, 93, 87, 87] # 创造一个列表 +##求列表中的最大(小)值————利用max()、min()函数 +print(max(list1), min(list1)) # 98 54 + +##求列表数值总和————利用sum()函数 +print(sum(list1)) +print(sum(list1) / len(list1)) # 求平均值 diff --git "a/06_\345\210\227\350\241\250/03_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" "b/06_\345\210\227\350\241\250/03_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..bdc40261ff6ffadbe8b0b60b6902c905fcf992fc --- /dev/null +++ "b/06_\345\210\227\350\241\250/03_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" @@ -0,0 +1,10 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +# 1、in +print('TOM' in name_list) +print('TOMS' in name_list) +print("===========") + +# 2、not in +print('TOM' not in name_list) +print('TOMS' not in name_list) diff --git "a/06_\345\210\227\350\241\250/04_\346\241\210\344\276\213_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" "b/06_\345\210\227\350\241\250/04_\346\241\210\344\276\213_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..853f08dae82dc04b20186fbf3d9b264aba489d26 --- /dev/null +++ "b/06_\345\210\227\350\241\250/04_\346\241\210\344\276\213_\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" @@ -0,0 +1,14 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +# 需求:注册邮箱,用户输入一个账号名,判断这个账号名是否存在,如果存在,提示用户,否则提示可以注册 +""" +1、用户输入账号 +2、判断if...else +""" +name = input('请输入您的邮箱账号名:\n') +if name in name_list: + # 提示用户名已经存在 + print(f'您输入的名字是{name},此用户名已经存在') +else: + # 提示可以注册 + print(f'您输入的名字是{name},可以注册') diff --git "a/06_\345\210\227\350\241\250/05_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_append.py" "b/06_\345\210\227\350\241\250/05_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_append.py" new file mode 100644 index 0000000000000000000000000000000000000000..e4002484ebcc6142ca6253768eae527f358b70f5 --- /dev/null +++ "b/06_\345\210\227\350\241\250/05_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_append.py" @@ -0,0 +1,17 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +""" +append(): + 列表结尾追加数据 +语句: + 列表序列.append(数据) +""" +# 1、列表数据可改的 -- 列表可变类型 +name_list.append('xiaoming') +print(name_list) + +# 2、append函数追加数据的时候,如果数据是一个序列,追加整个序列到列表结尾 +name_list.append(['zxy', 13]) +print(name_list) + + diff --git "a/06_\345\210\227\350\241\250/06_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_extend.py" "b/06_\345\210\227\350\241\250/06_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_extend.py" new file mode 100644 index 0000000000000000000000000000000000000000..2bc83a27f19eef01502c55bc0430155779fa6a86 --- /dev/null +++ "b/06_\345\210\227\350\241\250/06_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_extend.py" @@ -0,0 +1,13 @@ +name_list = ['TOM', 'Lily', 'Rose'] + +""" +extend(): + 列表结尾追加数据,如果数据是一个序列,则将这个序列的数据逐一添加到列表 +语法: + 列表序列.extend(数据) +""" +name_list.extend('xiaoming') +print(name_list) + +name_list.extend(['zxy', 'smy', 'lyb']) +print(name_list) diff --git "a/06_\345\210\227\350\241\250/07_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_insert.py" "b/06_\345\210\227\350\241\250/07_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_insert.py" new file mode 100644 index 0000000000000000000000000000000000000000..7ce0c9fedb95cdcf4b5accefb3ce5615fc4af00f --- /dev/null +++ "b/06_\345\210\227\350\241\250/07_\345\210\227\350\241\250\345\242\236\345\212\240\346\225\260\346\215\256_insert.py" @@ -0,0 +1,11 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +""" +insert(): + 指定位置新增数据 +语法: + 列表序列.insert(位置下标,数据) +""" +name_list.insert(1, 'zxy') +print(name_list) + diff --git "a/06_\345\210\227\350\241\250/08_\345\210\227\350\241\250\345\210\240\351\231\244\346\225\260\346\215\256.py" "b/06_\345\210\227\350\241\250/08_\345\210\227\350\241\250\345\210\240\351\231\244\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..42f44d344cd9454d9e803b7a9b5786228039f9b3 --- /dev/null +++ "b/06_\345\210\227\350\241\250/08_\345\210\227\350\241\250\345\210\240\351\231\244\346\225\260\346\215\256.py" @@ -0,0 +1,65 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +""" +1、del +语法: + del 目标 +""" +# 清空列表 +del name_list[:] +print(name_list) + +# 删除列表: 删除的是整个列表结构 +del name_list +# error: print(name_list) + +# del 可以删除指定下标的数据 +name_list = ['TOM', 'Lily', 'ROSE'] +del name_list[0] +print(name_list) + +list = ['Python', 'java', 'C++', 'coffee', 'milk'] ##创建一个列表 +del list[1:4:2] ##直接删除索引为 [1,4)的元素,步长为 2————无返回值 + +""" +2、pop(): + 删除指定下标的数据(默认为最后一个),并返回该数据 -- 若下标越界则报错 +语法: + 列表序列.pop(下标) +""" +del_name1 = name_list.pop(1) +del_name2 = name_list.pop() +print(del_name1) +print(del_name2) +print(name_list) + +""" +3、remove(): + 移除列表中某个数据的第一个匹配项 -- 无匹配项则报错 +语法: + 列表序列.remove(数据) +""" +name_list = ['TOM', 'Lily', 'ROSE'] +name_list.remove('ROSE') +print(name_list) + +""" +4、clear(): + 清空列表 +""" +name_list.clear() +print(name_list) + +''' +5、利用切片操作 + 删除数据 +''' +lst = [10, 20, 30, 40, 50, 60, 30] +print('----------切片操作-删除至少一个元素 ,将产生一个新的列表对象-------------------') +new_list = lst[1:3] +print('原列表', lst) +print('切片后的列表', new_list) + +'''不产生新的列表对象,而是删除原列表中的内容''' +lst[1:3] = [] +print(lst) diff --git "a/06_\345\210\227\350\241\250/09_\345\210\227\350\241\250\344\277\256\346\224\271\346\225\260\346\215\256.py" "b/06_\345\210\227\350\241\250/09_\345\210\227\350\241\250\344\277\256\346\224\271\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..55ec22641b0f427484c3f207f634165d08258d4e --- /dev/null +++ "b/06_\345\210\227\350\241\250/09_\345\210\227\350\241\250\344\277\256\346\224\271\346\225\260\346\215\256.py" @@ -0,0 +1,66 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +# 1、修改指定下标的数据 +name_list[0] = 'aaa' +print(name_list) + +# 2、逆序 reverse() +list1 = [1, 9, 3, 8, 5, 10, 7] +list1.reverse() +print(list1) +print(list1[::-1]) + +# 3、sort() 排序:升序 和 降序 +# reverse = True 降序;reverse = False 升序(默认) +list1.sort() +print(list1) +list1.sort(reverse=True) +print(list1) + +# sort()————永久改变 原列表次序————操作后,没有返回值 +list = [98, 78, 69, 54, 93, 87, 87] # 创造一个数字列表 +print(list.sort(reverse=False)) # 改变了次序,但sort()函数没有返回值,结果为:None +print(list) ##按照数字 升序 排列 +list.sort(reverse=True) ##按数字 降序 排列 +print(list) + +list = ['python', 'c++', 'java'] # 创造一个 字符串列表 +print(list.sort(reverse=False)) # 改变了次序,但sort()函数没有返回值,结果为:None +print(list) ##按照字母 升序 排列 +list.sort(reverse=True) ##按字母 降序 排列 +print(list) + +# 4、sorted()————不改变 原列表次序————操作后,返回 改变的 序列 +list = [98, 78, 69, 54, 93, 87, 87] # 创造一个数字列表 +print(sorted(list, reverse=False)) # 返回 改变的序列 升序————使用方法也不同 +print(list) # 原来列表不变 +list = ['python', 'c++', 'java'] # 创造一个 字符串列表 +print(sorted(list, reverse=False)) + +# 5、一次性改变列表中多个数据 +list1 = ["北京", "上海", "广州", "深圳"] +# 一次性,改变列表中的 多个数————无步长,不要求元素个数一致 +list1[0:3] = ["通过list2改变了list1中的元素"] +print(list1) + +list1[1:] = [True, False, 'hello'] +print(list1) + +list3 = ["北京", "上海", "广州", "深圳", ';;', 'm'] +list4 = [1, 3, 2] +# list3的子列表 变成 list4————有步长,要求元素个数一致 +list3[::2] = list4 +print(list3) + +print("==========================") + +# 6、列表的运算 +# 列表的加法 +a = [1, 2, 3, 4] +b = [5, 6, 7, 8] +a += b +print(a) + +# 列表自身相乘 +a *= 2 +print(a) diff --git "a/06_\345\210\227\350\241\250/10_\345\210\227\350\241\250\345\244\215\345\210\266\346\225\260\346\215\256.py" "b/06_\345\210\227\350\241\250/10_\345\210\227\350\241\250\345\244\215\345\210\266\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..1e0243940a77d75ab1199f59b687e11333d8b6e4 --- /dev/null +++ "b/06_\345\210\227\350\241\250/10_\345\210\227\350\241\250\345\244\215\345\210\266\346\225\260\346\215\256.py" @@ -0,0 +1,10 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +list1 = name_list.copy() # 开辟一个新空间 +list2 = name_list # 传入的是另一个列表的地址 + +name_list[0] = 'smy' + +print(f'name_list = {name_list}') +print(f'list1 = {list1}') +print(f'list2 = {list2}') diff --git "a/06_\345\210\227\350\241\250/11_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_while.py" "b/06_\345\210\227\350\241\250/11_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_while.py" new file mode 100644 index 0000000000000000000000000000000000000000..13ed04e3c33c061fc12f450bd4a259e3dafd37dd --- /dev/null +++ "b/06_\345\210\227\350\241\250/11_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_while.py" @@ -0,0 +1,6 @@ +name_list = ['TOM', 'Lily', 'Rose'] + +i = 0 +while i < len(name_list): + print(name_list[i]) + i += 1 diff --git "a/06_\345\210\227\350\241\250/12_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_for.py" "b/06_\345\210\227\350\241\250/12_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_for.py" new file mode 100644 index 0000000000000000000000000000000000000000..e6618df9fc792c79e648ba191759d7defa0f1112 --- /dev/null +++ "b/06_\345\210\227\350\241\250/12_\345\210\227\350\241\250\347\232\204\351\201\215\345\216\206_for.py" @@ -0,0 +1,12 @@ +name_list = ['TOM', 'Lily', 'ROSE'] + +for i in name_list: + # 遍历序列中的数据 + print(i) + + + + + + + diff --git "a/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227.py" "b/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..2717f604ef4d15b97cb026481125f8a8c5c898e0 --- /dev/null +++ "b/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227.py" @@ -0,0 +1,9 @@ +name_list = [['TOM', 'Lily', 'Rose'], + ['张三', '李四', '王五'], + ['xiaohong', 'xiaoming', 'xiaoqiang']] + +print(name_list) + +# 数据查询 +print(name_list[0]) +print(name_list[0][1]) diff --git "a/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227\346\241\210\344\276\213_\347\224\265\345\275\261\346\216\222\345\272\217.py" "b/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227\346\241\210\344\276\213_\347\224\265\345\275\261\346\216\222\345\272\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..92f78fb24082b65a5c949ed0cb6cda704686ed0a --- /dev/null +++ "b/06_\345\210\227\350\241\250/13_\345\210\227\350\241\250\345\265\214\345\245\227\346\241\210\344\276\213_\347\224\265\345\275\261\346\216\222\345\272\217.py" @@ -0,0 +1,78 @@ +myList = [ + ['饥饿站台', '2019-09-06', 7.8, 94712], + ['隐形人', '2020-02-28', 7.3, 61917], + ['绅士们', '2020-01-24', 8.4, 28374], + ['大赢家', '2020-03-20', 6.8, 112079], + ['狩猎', '2020-03-13', 7.2, 28414] +] + +# 1、按照电影评分排序输出 +result = [] # 储存结果的列表 + + +def fun(x): # x为列表中的元素 + return x[2] + +myList.sort(key=fun, reverse=True) # 按评分降序排列 +print(myList) +""" +语法: + list.sort(key=排序规则,reverse=排序方式) +""" + +# 只输出电影名 +for l in myList: + name = l[0] + result.append(name) +print(result) + + + +# 2、按照评论量排序输出 +result = [] # 储存结果的列表 + +myList.sort(key=lambda x: x[3], reverse=True) # 按评论量降序排列 +print(myList) + +""" +lambda x: x[3] +等价于: +def fun(x): # x为列表中的元素 + return x[2] +""" + +# 只输出电影名 +for l in myList: + name = l[0] + result.append(name) +print(result) + + +# 3、按照电影上映日期排序后输出 +result = [] # 储存结果的列表 + +myList.sort(key=lambda x: x[1], reverse=False) # 按日期升序排列 +print(myList) + +# 只输出电影名 +for l in myList: + name = l[0] + result.append(name) +print(result) + + +# 4、输出一个新列表:只含评分和评论数 +myNewList = [] # 储存结果的列表 + +# 只输出电影名 +for l in myList: + temp = [] # 临时存储一部电影的评分与评论量 + score = l[2] + comment = l[3] + + temp.append(score) + temp.append(comment) + + myNewList.append(temp) + +print(myNewList) \ No newline at end of file diff --git "a/06_\345\210\227\350\241\250/14_\346\241\210\344\276\213_\351\232\217\346\234\272\345\210\206\351\205\215\345\212\236\345\205\254\345\256\244.py" "b/06_\345\210\227\350\241\250/14_\346\241\210\344\276\213_\351\232\217\346\234\272\345\210\206\351\205\215\345\212\236\345\205\254\345\256\244.py" new file mode 100644 index 0000000000000000000000000000000000000000..b48ac83cfc7c32c92f863595cbfacef56fcb9475 --- /dev/null +++ "b/06_\345\210\227\350\241\250/14_\346\241\210\344\276\213_\351\232\217\346\234\272\345\210\206\351\205\215\345\212\236\345\205\254\345\256\244.py" @@ -0,0 +1,21 @@ +# 需求:8位老师,3个办公室,将8位老师随机分配到3个办公室 + +# 1、准备数据 +teachers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] +offices = [[], [], []] + +# 2、随机分配 +import random + +for name in teachers: + num = random.randint(0, 2) + offices[num].append(name) + +# 3、检验分配成果 +i = 1 +print(offices) +for office in offices: + print(f'办公室{i}的人数是{len(office)},老师分别是:') + i += 1 + for name in office: + print(name) diff --git "a/07_\345\205\203\347\273\204/01_\344\275\223\351\252\214\345\205\203\347\273\204.py" "b/07_\345\205\203\347\273\204/01_\344\275\223\351\252\214\345\205\203\347\273\204.py" new file mode 100644 index 0000000000000000000000000000000000000000..05eb0e5db8f52522f03e0fb7406e41887a1797a6 --- /dev/null +++ "b/07_\345\205\203\347\273\204/01_\344\275\223\351\252\214\345\205\203\347\273\204.py" @@ -0,0 +1,30 @@ +# 一个元组可以存储多个数据,元组内的数据是不能修改的 +t1 = (10, 20, 30) + +print(t1) +print(type(t1)) + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/07_\345\205\203\347\273\204/02_\345\256\232\344\271\211\345\205\203\347\273\204.py" "b/07_\345\205\203\347\273\204/02_\345\256\232\344\271\211\345\205\203\347\273\204.py" new file mode 100644 index 0000000000000000000000000000000000000000..d19c8a340af5f54eab9836851fbe95ef2f67a4d5 --- /dev/null +++ "b/07_\345\205\203\347\273\204/02_\345\256\232\344\271\211\345\205\203\347\273\204.py" @@ -0,0 +1,30 @@ +# 1、多个数据的元组 +t1 = (10, 20, 30) +print(type(t1)) + +# 2、单个数据的元组 +t2 = (10,) +print(type(t2)) + +# 3、如果单个数据的元组不加逗号 -- 不是元组类型 +t3 = (30) +print(type(t3)) # int + +t4 = ('aaa') +print(type(t4)) + +# 4、空元组 +t5 = () # 此时元组中没有元素————元组定义成功 +print(type(t5)) # 查看其类型 :tuple + +# 5、定义一个元组 +creature = "cat", "dog", "tiger", "human" # 自动生成元组 +color = ("red", "green", 0x1100, creature) +print(color[-1][1]) # dog +print(color[2]) # 4352——自动变成十进制 + +# 6、生成元组————用tuple()————强制类型转化 +tu1 = tuple(range(5)) +print(tu1) +list1 = [1, 3, 5, 7, 9] +print(tuple(list1)) diff --git "a/07_\345\205\203\347\273\204/03_\345\205\203\347\273\204\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276.py" "b/07_\345\205\203\347\273\204/03_\345\205\203\347\273\204\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276.py" new file mode 100644 index 0000000000000000000000000000000000000000..eda0257f6e76472934c66150f8af38b0274f1ec8 --- /dev/null +++ "b/07_\345\205\203\347\273\204/03_\345\205\203\347\273\204\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276.py" @@ -0,0 +1,22 @@ +t1 = ('aa', 'bb', 'cc', 'dd') + +# 1、下标查找 +print(t1[0]) +print(t1[2:]) + +""" +2、index(): + 返回指定数据所在位置的下标 +语法: + 元组序列.index(数据,开始位置下标,结束位置下标) + 注意:如果查找的数据不存在则报错 +""" +print(t1.index('bb')) +#error: print(t1.index('bbb')) + +# 3、count(): 统计指定数据在当前元组中出现的次数 +print(t1.count('aa')) +print(t1.count('aaa')) + +# 4、len(): 返回元组长度 +print(len(t1)) diff --git "a/07_\345\205\203\347\273\204/04_\345\205\203\347\273\204\346\225\260\346\215\256\347\232\204\344\277\256\346\224\271\346\223\215\344\275\234.py" "b/07_\345\205\203\347\273\204/04_\345\205\203\347\273\204\346\225\260\346\215\256\347\232\204\344\277\256\346\224\271\346\223\215\344\275\234.py" new file mode 100644 index 0000000000000000000000000000000000000000..5fcad85b701105db4bbb7a6330020b553fdaad8d --- /dev/null +++ "b/07_\345\205\203\347\273\204/04_\345\205\203\347\273\204\346\225\260\346\215\256\347\232\204\344\277\256\346\224\271\346\223\215\344\275\234.py" @@ -0,0 +1,10 @@ +t1 = ('aa', 'bb', 'cc', 'dd') +print(t1) + +# 元组内的直接数据,如果修改则立即报错 +# error: t1[0] = 'aaa' + +# 如果元组里面有列表,修改列表里面的数据则是支持的 +t2 = ('aa', 'bb', ['cc', 'dd']) +t2[2][0] = 'TOM' +print(t2) diff --git "a/07_\345\205\203\347\273\204/05_\345\205\203\347\273\204\347\232\204\350\247\243\345\214\205.py" "b/07_\345\205\203\347\273\204/05_\345\205\203\347\273\204\347\232\204\350\247\243\345\214\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..318738339a0c356b41fc37906491caa19b090f1f --- /dev/null +++ "b/07_\345\205\203\347\273\204/05_\345\205\203\347\273\204\347\232\204\350\247\243\345\214\205.py" @@ -0,0 +1,10 @@ +# 元组解包 ———— 将元组中的各个元素,分别赋值给 各个变量 +a, b, c = (2, 5, 10) +print(a, b, c) + +# 函数的多返回值 +def returnMany(x, y): + return x, y, x ** y + +newTupple = returnMany(10, 3) # 自动生成一个 元组 +print(newTupple[1]) diff --git "a/08_\345\255\227\345\205\270/01_\345\210\233\345\273\272\345\255\227\345\205\270.py" "b/08_\345\255\227\345\205\270/01_\345\210\233\345\273\272\345\255\227\345\205\270.py" new file mode 100644 index 0000000000000000000000000000000000000000..82c59eb360605a33e09eb0b931a64ddca1e8bef9 --- /dev/null +++ "b/08_\345\255\227\345\205\270/01_\345\210\233\345\273\272\345\255\227\345\205\270.py" @@ -0,0 +1,37 @@ +# {} 键值对 各个键值对用逗号隔开 +""" + +""" +# 1、有数据的字典: +# 第一种创建字典:name的值TOM,age的值是20,gender的值是男 +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} +print(dict1) +print(type(dict1)) + +length = len(dict1) # 有几个键值对 +print(length) + +lst = [10, 20, 30] +# error: d = {lst: 100} # 字典的键key必须是不可变对象 + +# 第二种创建字典:dict() +student = dict(name='jack', age=20) +print(student) + +# 2、创建空字典 +dict2 = {} +print(type(dict2)) + +dict3 = dict() +print(type(dict3)) + +# 3、字典生成式 +""" + zip(可迭代对象1,可迭代对象2...): + 将对象中对应的元素打包成一个元组,然后返回由这些元组组成的列表 +""" +items = ['Fruits', 'Books', 'Others'] +prices = [96, 78, 85, 100, 120] + +d = {item.upper(): price for item, price in zip(items, prices)} +print(d) diff --git "a/08_\345\255\227\345\205\270/02_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\226\260\345\242\236.py" "b/08_\345\255\227\345\205\270/02_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\226\260\345\242\236.py" new file mode 100644 index 0000000000000000000000000000000000000000..0d079fb5b0f4c930d2755c8061fe11cd7652ede9 --- /dev/null +++ "b/08_\345\255\227\345\205\270/02_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\226\260\345\242\236.py" @@ -0,0 +1,12 @@ +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} + +# 字典序列[key] = 值 +# 如果key存在则修改这个key对应的值;如果key不存在则新增此键值对 -- 字典为可变数据类型 +dict1['id'] = 110 +print(dict1) + +dict1['name'] = 'ROSE' +print(dict1) + +dict1['number'] = [5, 6, 7] # 值为列表 +print(dict1) diff --git "a/08_\345\255\227\345\205\270/03_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\345\210\240\351\231\244.py" "b/08_\345\255\227\345\205\270/03_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\345\210\240\351\231\244.py" new file mode 100644 index 0000000000000000000000000000000000000000..6ffed681c49955f42d6520a710b6a7f189b21db7 --- /dev/null +++ "b/08_\345\255\227\345\205\270/03_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\345\210\240\351\231\244.py" @@ -0,0 +1,23 @@ +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} + +# 1、del 删除字典或者指定键值对 +del dict1 # 删除整个字典 +# error: print(dict1) + +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} +del dict1['name'] +print(dict1) + +# error: del dict1['name3'] 要删除的数据不存在 + +# 2、popitem() +item = dict1.popitem() # 随机弹出 一个键值对,并返回一个 元组 类型 +print(item) + +# 3、pop(key, 默认值) +# 若有 key 键,则返回对应的值,并且删除该键对 -- 否则,返回后面的默认值 +print(dict1.pop('小羊', 2)) + +# 4、clear() +dict1.clear() +print(dict1) diff --git "a/08_\345\255\227\345\205\270/04_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\344\277\256\346\224\271.py" "b/08_\345\255\227\345\205\270/04_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\344\277\256\346\224\271.py" new file mode 100644 index 0000000000000000000000000000000000000000..98d5797fd5e16c74c937bc75143278662c123af8 --- /dev/null +++ "b/08_\345\255\227\345\205\270/04_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\344\277\256\346\224\271.py" @@ -0,0 +1,9 @@ +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} + +# 字典序列[key] = 值 +# 如果key存在则修改这个key对应的值;如果key不存在则新增此键值对 -- 字典为可变数据类型 +dict1['name'] = 'Lily' +print(dict1) + +dict1['id'] = 110 +print(dict1) \ No newline at end of file diff --git "a/08_\345\255\227\345\205\270/05_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\237\245\346\211\276.py" "b/08_\345\255\227\345\205\270/05_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\237\245\346\211\276.py" new file mode 100644 index 0000000000000000000000000000000000000000..cccfb4cc21718bcf98faa995f0e31f7a8ce6a802 --- /dev/null +++ "b/08_\345\255\227\345\205\270/05_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\237\245\346\211\276.py" @@ -0,0 +1,45 @@ +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} + +# 判断是否是dict中的键 +print('name' in dict1) +print('TOM' in dict1) +print('===========') + +# 1、key值查找 +print(dict1['name']) #返回对应的值 +# print(dict1['id']) 报错 +print('===========') + +d = {'a': 1, 'b': 2, 'b': '3'} +print(d) # 对于重复的key,只保留最后一个键值对 +print(d['b']) # '3' -- 以最后一个为准 +print('===========') + +# 2、函数 +""" +2.1 get(): + 字典序列.get(key, 默认值) +注意: + 如果当前查找的key不存在,则返回第二个参数(默认值),如果省略第二个参数,则返回None +""" +print(dict1.get('name')) +print(dict1.get('id',110)) +print(dict1.get('id')) +print('===========') + +""" +2.2 keys(): 查找字典中所有的key,返回可迭代的对象 +""" +print(dict1.keys()) + +""" +2.3 values(): 查找字典中的所有value,返回可迭代对象 +""" +print(dict1.values()) + +""" +2.4 items(): 查找字典中所有的键值对,返回可迭代对象,里面的数据是元组 +""" +print(dict1.items()) + + diff --git "a/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\216\222\345\272\217.py" "b/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\216\222\345\272\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..eb43ae0ef883b85c27afc9c05b7214d6d4f4beab --- /dev/null +++ "b/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\345\270\270\347\224\250\346\223\215\344\275\234_\346\216\222\345\272\217.py" @@ -0,0 +1,21 @@ +dict1 = {1: 3, 4: 2, 2: 6} + +# 返回来的是 键组成的 列表,默认正序 +key_list = sorted(dict1) +print(key_list) + +# 返回来的是 值组成的 列表,默认正序 +value_list = sorted(dict1.values()) +print(value_list) + +# 返回来的是 元组组成的 列表, 以键 逆序排列 +item_list1 = sorted(dict1.items(), reverse=True) +print(item_list1) + +# 返回来的是 元组组成的 列表, 以值 逆序排列 +item_list2 = sorted(dict1.items(), key=lambda x: x[1], reverse=True) +print(item_list2) + +# 返回来的是 元组组成的 列表, 以值 逆序排列 +item_list3 = sorted(dict1.items(), key=lambda x: -x[1]) +print(item_list3) diff --git "a/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\347\232\204\345\276\252\347\216\257\351\201\215\345\216\206.py" "b/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\347\232\204\345\276\252\347\216\257\351\201\215\345\216\206.py" new file mode 100644 index 0000000000000000000000000000000000000000..3401fde2220010ff94d346f829efdf7766e944bd --- /dev/null +++ "b/08_\345\255\227\345\205\270/06_\345\255\227\345\205\270\347\232\204\345\276\252\347\216\257\351\201\215\345\216\206.py" @@ -0,0 +1,27 @@ +dict1 = {'name': 'TOM', 'age': 20, 'gender': '男'} + +# key的遍历 +for key in dict1.keys(): + print(key) +print('===================') + +for i in dict1: # 此时,默认返回所有的 键,列表形式 + print(i) +print('===================') + +# value的遍历 +print(type(dict1.values())) # +for value in dict1.values(): + print(value) +print('===================') + +# 键值对的遍历 +for item in dict1.items(): + print(item) + +print(list(dict.items())) # 转换之后的列表元素是由元组组成 +print('===================') + +# 键值对的拆包 +for key, value in dict1.items(): + print(f'{key} = {value}') diff --git "a/09_\351\233\206\345\220\210/01_\345\210\233\345\273\272\351\233\206\345\220\210.py" "b/09_\351\233\206\345\220\210/01_\345\210\233\345\273\272\351\233\206\345\220\210.py" new file mode 100644 index 0000000000000000000000000000000000000000..42158757b8498a0f8fbe51299b7aa5961d7921b8 --- /dev/null +++ "b/09_\351\233\206\345\220\210/01_\345\210\233\345\273\272\351\233\206\345\220\210.py" @@ -0,0 +1,31 @@ +""" +## 集合 ## +# 集合 -- 许多 不重复 的元素 -- 集合中的元素可以是: int,float,string,tupple(元组) -- 固定数据类型 +# 不可以是:列表、字典、集合 -- 可变数据类型 +""" + +# 1、创建有数据的集合 {} 或 set() +s1 = {10, 20, 30, 40, 50} +print(s1) # 集合无序 + +s2 = {10, 30, 20, 10, 30, 40, 30, 50} +print(s2) # 集合元素各异 + +s3 = set('abcdefg') +print(s3) + +set1 = {1, 2, 4, 3, 8} - {4} # 集合可 相减,不可相加 +print(set1) + +# 2、创建空集合: set() +s4 = set() +print(s4) +print(type(s4)) + +s5 = {} +print(type(s5)) # 空字典 + +# 利用set()函数 -- 其他数据类型 --> 集合 -- 自动去重 +list1 = [1, 4, 5, 5, 7, 8] +set2 = set(list1) +print(set2) diff --git "a/09_\351\233\206\345\220\210/02_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\242\236\345\212\240\346\225\260\346\215\256.py" "b/09_\351\233\206\345\220\210/02_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\242\236\345\212\240\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..57f15bb5fb916e8c7a654699568450d132719de4 --- /dev/null +++ "b/09_\351\233\206\345\220\210/02_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\242\236\345\212\240\346\225\260\346\215\256.py" @@ -0,0 +1,18 @@ +s1 = {10, 20} + +# 集合是可变类型 +# 1、add() +s1.add(100) +print(s1) + +# 集合有去重功能,如果追加的数据是集合已有数据,则什么事情都不做 +s1.add(100) +print(s1) +# s1.add([12,13]) 报错 + + +# 2、update(): 增加的数据是序列 +s1.update([10, 20, 30, 40]) +print(s1) + +# s1.update(200) 报错 diff --git "a/09_\351\233\206\345\220\210/03_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\210\240\351\231\244\346\225\260\346\215\256.py" "b/09_\351\233\206\345\220\210/03_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\210\240\351\231\244\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..12aafc750ca428a4537e63ee8a4dcb85cb585d67 --- /dev/null +++ "b/09_\351\233\206\345\220\210/03_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\345\210\240\351\231\244\346\225\260\346\215\256.py" @@ -0,0 +1,23 @@ +s1 = {10, 20, 30, 40, 50, 70, 80} + +# 1、remove(): 删除集合中的指定数据,如果数据不存在则报错 +s1.remove(10) +print(s1) + +# error: s1.remove(99) + +# 2、discard(): 删除集合中的指定数据,如果数据不存在也不会报错 +s1.discard(20) +print(s1) + +s1.discard(99) +print(s1) + +# 3、pop(): 随机删除某个数据,并返回这个数据,若集合为空,则报错 +del_num = s1.pop() +print(del_num) +print(s1) + +# 4、clear() 删除所有元素 +s1.clear() +print(s1) diff --git "a/09_\351\233\206\345\220\210/04_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276\346\225\260\346\215\256.py" "b/09_\351\233\206\345\220\210/04_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276\346\225\260\346\215\256.py" new file mode 100644 index 0000000000000000000000000000000000000000..37c383ae73b9a04ed0c97d7a0d50158877aa5f02 --- /dev/null +++ "b/09_\351\233\206\345\220\210/04_\351\233\206\345\220\210\345\270\270\350\247\201\346\223\215\344\275\234_\346\237\245\346\211\276\346\225\260\346\215\256.py" @@ -0,0 +1,7 @@ +s1 = {10, 20, 30, 40, 50} + +# in: 判断数据在集合序列 +print(10 in s1) + +# not in: 判断数据不在集合序列 +print(10 not in s1) diff --git "a/09_\351\233\206\345\220\210/05_\351\233\206\345\220\210\347\232\204\350\277\220\347\256\227.py" "b/09_\351\233\206\345\220\210/05_\351\233\206\345\220\210\347\232\204\350\277\220\347\256\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..f07cac65d8fa48b5cb1512cf877ed79b289eda54 --- /dev/null +++ "b/09_\351\233\206\345\220\210/05_\351\233\206\345\220\210\347\232\204\350\277\220\347\256\227.py" @@ -0,0 +1,22 @@ +## 两个集合的操作 +set1 = {1, 2, 0, 2, 2, 6, 8, 8} +set2 = {2, 2, 3, 0, 9, 7, 7} + +# 两集合的 并集 +print(set1 | set2) +print(set1.union(set2)) + +# 在set1中,并且不在set2中 +print(set1 - set2) +print(set1.difference(set2)) + +# 两集合的 交集 +print(set1 & set2) +print(set1.intersection(set2)) + +# 去除两集合相同的部分 +print(set1 ^ set2) +print(set1.symmetric_difference(set2)) + +# 赋值运算 +set1 &= set2 \ No newline at end of file diff --git "a/09_\351\233\206\345\220\210/06_\351\233\206\345\220\210\347\232\204\345\205\263\347\263\273.py" "b/09_\351\233\206\345\220\210/06_\351\233\206\345\220\210\347\232\204\345\205\263\347\263\273.py" new file mode 100644 index 0000000000000000000000000000000000000000..f9445639bbb12d4dd62f81ec77a14c18c61f027e --- /dev/null +++ "b/09_\351\233\206\345\220\210/06_\351\233\206\345\220\210\347\232\204\345\205\263\347\263\273.py" @@ -0,0 +1,23 @@ +'''两个集合是否相等(元素相同,就相等)''' +s = {10, 20, 30, 40} +s2 = {30, 40, 20, 10} +print(s == s2) # True +print(s != s2) # False + +'''一个集合是否是另一个集合的子集''' +s1 = {10, 20, 30, 40, 50, 60} +s2 = {10, 20, 30, 40} +s3 = {10, 20, 90} +print(s2.issubset(s1)) # True +print(s3.issubset(s1)) # False +print(s1 < s2) # False + +'''一个集合是否是另一个集合的超集''' +print(s1.issuperset(s2)) # True +print(s1.issuperset(s3)) # False + +'''两个集合是否含有交集''' +print(s2.isdisjoint(s3)) # False 有交集为False +s4 = {100, 200, 300} + +print(s2.isdisjoint(s4)) # True 没有交集为True diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/00_\345\217\257\345\217\230\345\272\217\345\210\227\344\270\216\344\270\215\345\217\257\345\217\230\345\272\217\345\210\227.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/00_\345\217\257\345\217\230\345\272\217\345\210\227\344\270\216\344\270\215\345\217\257\345\217\230\345\272\217\345\210\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..7cb9d7544ea91959f782a501748e7bf6bf6238ef --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/00_\345\217\257\345\217\230\345\272\217\345\210\227\344\270\216\344\270\215\345\217\257\345\217\230\345\272\217\345\210\227.py" @@ -0,0 +1,14 @@ +'''不可变序列,可变序列''' + +'''可变序列 列表,字典''' +lst = [10, 20, 45] +print(id(lst)) +lst.append(300) +print(id(lst)) + +'''不可变序列 字符串,元组,集合''' +s = 'hello' +print(id(s)) +s = s + ' world' +print(id(s)) +print(s) \ No newline at end of file diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/01_\350\277\220\347\256\227\347\254\246\345\212\240\345\217\267.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/01_\350\277\220\347\256\227\347\254\246\345\212\240\345\217\267.py" new file mode 100644 index 0000000000000000000000000000000000000000..5182a69c9afdec46ef998c3791726e97d6df6cf5 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/01_\350\277\220\347\256\227\347\254\246\345\212\240\345\217\267.py" @@ -0,0 +1,18 @@ +str1 = 'aa' +str2 = 'bb' + +list1 = [1, 2] +list2 = [10, 20] + +t1 = (1, 2) +t2 = (10, 20) + +dict1 = {'name': 'Python'} +dict2 = {'age': 30} + +# 运算符+ 合并 支持类型:字符串、列表、元组 +print(str1 + str2) +print(list1 + list2) +print(t1 + t2) + +# error: print(dict1 + dict2) 字典不支持合并运算 diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/02_\350\277\220\347\256\227\347\254\246\344\271\230\345\217\267.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/02_\350\277\220\347\256\227\347\254\246\344\271\230\345\217\267.py" new file mode 100644 index 0000000000000000000000000000000000000000..2ca5bc7a1f175341b313b0c1df4492f8c62c925f --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/02_\350\277\220\347\256\227\347\254\246\344\271\230\345\217\267.py" @@ -0,0 +1,24 @@ +str1 = 'a' +list1 = ['hello'] +t1 = ('world',) + +# *: 复制 支持类型:字符串、列表、元组 +print(str1 * 5) + +# 打印10个-: +print('-' * 10) + +print(list1 * 5) + +print(t1 * 5) + + + + + + + + + + + diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/03_\350\277\220\347\256\227\347\254\246\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/03_\350\277\220\347\256\227\347\254\246\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..6fcd09fad2c9953fd72343f353f6e23a22bd2ab6 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/03_\350\277\220\347\256\227\347\254\246\345\210\244\346\226\255\346\230\257\345\220\246\345\255\230\345\234\250.py" @@ -0,0 +1,23 @@ +str1 = 'abcd' +list1 = [10, 20, 30, 40] +t1 = (100, 200, 300, 400) +dict1 = {'name': 'Python', 'age': 30} + +# in 和 not in : 字符串、列表、元组、字典 +# 1、字符a是否存在 +print('a' in str1) +print('a' not in str1) + +# 2、判断10是否存在 +print(10 in list1) +print(10 not in list1) + +# 3、100是否存在 +print(100 in t1) +print(100 not in t1) + +# 4、name是否存在 +print('name' in dict1) +print('name' not in dict1) +print('name' in dict1.keys()) +print('name' in dict1.values()) diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/04_len\346\226\271\346\263\225.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/04_len\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..d374a98b8f4e0f6217190f52158cedea4a107cbe --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/04_len\346\226\271\346\263\225.py" @@ -0,0 +1,12 @@ +str1 = 'abcd' +list1 = [10, 20, 30, 40] +t1 = (100, 200, 300, 400) +s1 = {10, 20, 30, 40, 50} +dict1 = {'name': 'Python', 'age': 30} + +# len(): 计算容器中元素个数 +print(len(str1)) +print(len(list1)) +print(len(t1)) +print(len(s1)) +print(len(dict1)) \ No newline at end of file diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/05_del\346\226\271\346\263\225.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/05_del\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..68dca2ef14bda13508c8213454d6abd8a5f27c23 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/05_del\346\226\271\346\263\225.py" @@ -0,0 +1,22 @@ +str1 = 'abcd' +list1 = [10, 20, 30, 40] +t1 = (100, 200, 300, 400) +s1 = {10, 20, 30, 40, 50} +dict1 = {'name': 'Python', 'age': 30} + +# del 目标 或 del(目标) +del str1 +# error: print(str1) + +del list1[0] +print(list1) +del list1 +# error: print(list1) + +del s1 +# error: print(s1) + +del dict1['name'] +print(dict1) +del dict1 +# error: print(dict1) \ No newline at end of file diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/06_max\345\222\214min.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/06_max\345\222\214min.py" new file mode 100644 index 0000000000000000000000000000000000000000..63cdd36dbb050b59af8ce3da71ae466e2fbc1a97 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/06_max\345\222\214min.py" @@ -0,0 +1,10 @@ +str1 = 'abcdefg' +list1 = [10, 20, 30, 40, 50] + +# max(): 最大值 +print(max(str1)) +print(max(list1)) + +# min(): 最小值 +print(min(str1)) +print(min(list1)) diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/07_range\346\226\271\346\263\225.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/07_range\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..879ebecce84c884fcbb7981d231d16a36d06be70 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/07_range\346\226\271\346\263\225.py" @@ -0,0 +1,24 @@ +""" +range(start, end, step): + 生成从start到end的数字(左闭右开),步长为step,供for循环使用 +""" +print(range(1, 10, 1)) + +for i in range(1, 10, 1): + print(i, end=' ') +print() + +for i in range(1, 10): + print(i, end=' ') +print() + +for i in range(1, 10, 2): + print(i, end=' ') +print() + +for i in range(10): + print(i,end=' ') +print() +# 1、如果不写开始,默认从0开始 +# 2、如果不写步长,默认为1 + diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/08_enumerate\346\226\271\346\263\225.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/08_enumerate\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..542c5ae6678cdba866fe8c56a5c866e9a83cb7ec --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/08_enumerate\346\226\271\346\263\225.py" @@ -0,0 +1,13 @@ +""" +enumerate(可遍历对象,start=0): + 返回结果是元组,第一个数据是对应的下标,第二个数据是值本身 + start为下标起始值 +""" +list1 = ['a', 'b', 'c', 'd', 'e'] + +for i in enumerate(list1): + print(i) +print('=============') + +for i in enumerate(list1, 3): + print(i) diff --git "a/10_\345\205\254\345\205\261\346\223\215\344\275\234/09_\345\256\271\345\231\250\347\261\273\345\236\213\350\275\254\346\215\242.py" "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/09_\345\256\271\345\231\250\347\261\273\345\236\213\350\275\254\346\215\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..63f912fa6e8c63b8c5d72b7784793dadaa2419b8 --- /dev/null +++ "b/10_\345\205\254\345\205\261\346\223\215\344\275\234/09_\345\256\271\345\231\250\347\261\273\345\236\213\350\275\254\346\215\242.py" @@ -0,0 +1,15 @@ +list1 = [10, 20, 30, 20, 40, 50] +s1 = {100, 300, 200, 500} +t1 = ('a', 'b', 'c', 'd', 'e') + +# tuple(): 转换成元组 +print(tuple(list1)) +print(tuple(s1)) + +# list(): 转换成列表 +print(list(s1)) +print(list(t1)) + +# set(): 转换成集合 +print(set(list1)) +print(set(t1)) diff --git "a/11_\346\216\250\345\257\274\345\274\217/01_\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" "b/11_\346\216\250\345\257\274\345\274\217/01_\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..4357d3f040211e5357f4eaf38a8e453d3fe960d0 --- /dev/null +++ "b/11_\346\216\250\345\257\274\345\274\217/01_\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" @@ -0,0 +1,39 @@ +# 创建0-9的列表 + +# 1、循环实现 +# while 实现 +list1 = [] + +i = 0 +while i < 10: + list1.append(i) + i += 1 + +print(list1) + +# for 实现 +list2 = [] + +i = 0 +for i in range(10): + list2.append(i) + +print(list2) + +# 2、列表推导式: 简化代码,创建或控制有规律的列表 +list3 = [i for i in range(10)] +print(list3) + + +# 案例:返回1-100之间的偶数 +# [i*0.5 for i in list if i >30] +# 将i*0.5存于列表中 #i在list中 #i要大于30 + +# 普通方法 +list = [] +for i in range(1, 101): + if (i % 2 == 0): + list.append(i) + +# 列表推导式 +list = [i for i in range(1, 101) if i % 2 == 0] \ No newline at end of file diff --git "a/11_\346\216\250\345\257\274\345\274\217/02_\345\270\246if\347\232\204\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" "b/11_\346\216\250\345\257\274\345\274\217/02_\345\270\246if\347\232\204\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..d2df017557a9083be5eea9c96e345ba41b2e19cc --- /dev/null +++ "b/11_\346\216\250\345\257\274\345\274\217/02_\345\270\246if\347\232\204\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" @@ -0,0 +1,17 @@ +# 创建0-10的偶数列表 + +# 1、简单列表推导式,range步长 +list1 = [i for i in range(0, 10, 2)] +print(list1) + +# 2、for循环加if 创建有规律的列表 +list2 = [] +for i in range(10): + if i % 2 == 0: + list2.append(i) + +print(list2) + +# 3、把for循环配合if的代码 改写成带if的列表推导式 +list3 = [i for i in range(10) if i % 2 == 0] +print(list3) diff --git "a/11_\346\216\250\345\257\274\345\274\217/03_\345\244\232for\345\276\252\347\216\257\345\256\236\347\216\260\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" "b/11_\346\216\250\345\257\274\345\274\217/03_\345\244\232for\345\276\252\347\216\257\345\256\236\347\216\260\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..92fcc9bb527ee939a4ab755c68d5fada882a9e44 --- /dev/null +++ "b/11_\346\216\250\345\257\274\345\274\217/03_\345\244\232for\345\276\252\347\216\257\345\256\236\347\216\260\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217.py" @@ -0,0 +1,14 @@ +""" +创建列表如下: + [(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] +""" +list1 = [] +for i in range(1, 3): + for j in range(3): + # 往列表里添加元组 + list1.append((i, j)) +print(list1) + +# 多个for实现列表推导式 +list2 = [(i, j) for i in range(1, 3) for j in range(3)] +print(list2) diff --git "a/11_\346\216\250\345\257\274\345\274\217/04_\345\255\227\345\205\270\346\216\250\345\257\274\345\274\217.py" "b/11_\346\216\250\345\257\274\345\274\217/04_\345\255\227\345\205\270\346\216\250\345\257\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..3d1dfdd14693fe005af859366abe31201bc05523 --- /dev/null +++ "b/11_\346\216\250\345\257\274\345\274\217/04_\345\255\227\345\205\270\346\216\250\345\257\274\345\274\217.py" @@ -0,0 +1,16 @@ +# 创建字典 key是1-5的数字 value是这个数字的平方 +dict1 = {i: i ** 2 for i in range(1, 6)} +print(dict1) + +# 将两个列表合并为一个字典 +list1 = ['name', 'age', 'gender'] +list2 = ['Tom', '20', 'man'] +dict2 = {list1[i]: list2[i] for i in range(len(list1))} +print(dict2) + +# 提取字典中的目标数据 +counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99} + +# 需求:提取上述电脑数量大于等于200的字典数据 +count1 = {key: value for key, value in counts.items() if value >= 200} +print(count1) diff --git "a/11_\346\216\250\345\257\274\345\274\217/05_\351\233\206\345\220\210\346\216\250\345\257\274\345\274\217.py" "b/11_\346\216\250\345\257\274\345\274\217/05_\351\233\206\345\220\210\346\216\250\345\257\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..a5642a377991a3c5ffc5fe9fafd70328d616ce5f --- /dev/null +++ "b/11_\346\216\250\345\257\274\345\274\217/05_\351\233\206\345\220\210\346\216\250\345\257\274\345\274\217.py" @@ -0,0 +1,4 @@ +# 需求:创建一个集合,数据为下方列表的2次方 +list1 = [1, 1, 2] +set1 = {i ** 2 for i in list1} +print(set1) diff --git "a/12_\345\207\275\346\225\260/01_\344\275\223\351\252\214\345\207\275\346\225\260.py" "b/12_\345\207\275\346\225\260/01_\344\275\223\351\252\214\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..b53b0bb920ac9679a157703bb12559a7e66f2cbf --- /dev/null +++ "b/12_\345\207\275\346\225\260/01_\344\275\223\351\252\214\345\207\275\346\225\260.py" @@ -0,0 +1,25 @@ +# 因为函数要先定义再调用,所以步骤2和3要在步骤1的上面书写 + +# 2、确定选择功能界面:# 3、封装函数 +def sel_func(): + print('显示余额') + print('存款') + print('取款') + + + +# 1、搭建整体框架 + +print('恭喜您登录成功') +# 显示功能界面 +sel_func() + +print('您的余额是1000000') +# 显示功能界面 +sel_func() + +print('取了100元钱') +# 显示功能界面 +sel_func() + +# 4、在需要的位置调用函数 \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/02_\345\207\275\346\225\260\347\232\204\346\263\250\346\204\217\344\272\213\351\241\271.py" "b/12_\345\207\275\346\225\260/02_\345\207\275\346\225\260\347\232\204\346\263\250\346\204\217\344\272\213\351\241\271.py" new file mode 100644 index 0000000000000000000000000000000000000000..63be5008e96df4088d12a1ad5d65ea23b97149a9 --- /dev/null +++ "b/12_\345\207\275\346\225\260/02_\345\207\275\346\225\260\347\232\204\346\263\250\346\204\217\344\272\213\351\241\271.py" @@ -0,0 +1,17 @@ +# 1、使用一个函数 2、测试注意事项 +# 需求: 一个函数:打印hello world + +# error: info_print() + +# 定义函数 +def info_print(): + print('Hello World') + +# 调用函数 +info_print() + +""" +结论: + 1、函数先定义后调用,如果先调用会报错 + 2、如果没有调用函数,函数里面的代码不会执行 +""" \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/03_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\344\275\234\347\224\250.py" "b/12_\345\207\275\346\225\260/03_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\344\275\234\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..1836e092ef8546bd1735eb0a2e3d8efa4a7881ce --- /dev/null +++ "b/12_\345\207\275\346\225\260/03_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\344\275\234\347\224\250.py" @@ -0,0 +1,41 @@ +# 1、函数:固定数据1 和 2 加法 +def add_num1(): + result = 1 + 2 + print(result) + + +add_num1() + + +# 2、参数形式传入真实数据 做加法运算 +# 定义函数时,同时定义了接收用户数据的参数a和b,a和b是形参 +def add_num2(a, b): + result = a + b + print(result) + + +# 调用函数时传入了真实的数据10 和 20,真实数据为实参 +add_num2(10, 20) + +add_num2(100, 200) + + +''' +3、形参与实参: + 在函数调用过程中,进行参数的传递 + 如果是不可变对象, 在函数体的修改不会影响实参的值 arg1的修改为100,不会影响n1的值 + 如果是可变对象,在函数体的的修改会影响到实参的值 arg2的修改,append(10),会影响到n2的值 +''' + +def fun(arg1, arg2): + arg1 = 100 + arg2.append(10) + +n1 = 11 +n2 = [22, 33, 44] +print('n1', n1) +print('n2', n2) +fun(n1, n2) + +print('n1', n1) +print('n2', n2) diff --git "a/12_\345\207\275\346\225\260/04_\344\275\223\351\252\214\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" "b/12_\345\207\275\346\225\260/04_\344\275\223\351\252\214\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" new file mode 100644 index 0000000000000000000000000000000000000000..b9bba6cff0319b4538de9cc2dc3112f00e017811 --- /dev/null +++ "b/12_\345\207\275\346\225\260/04_\344\275\223\351\252\214\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" @@ -0,0 +1,15 @@ +# 定义一个函数,返回 烟 +def buy(): + return '烟' + print('ok') + +goods = buy() +print(goods) + +# return返回结果给函数调用的地方 + +""" +return 作用: + 1、负责函数返回值 + 2、退出当前函数:导致return下方的所有代码(函数体内部)不执行 +""" \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/05_\345\207\275\346\225\260\350\277\224\345\233\236\345\200\274\347\232\204\345\272\224\347\224\250.py" "b/12_\345\207\275\346\225\260/05_\345\207\275\346\225\260\350\277\224\345\233\236\345\200\274\347\232\204\345\272\224\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..b4fb8b41453004bcf84ea7c10cf65e0ba5379026 --- /dev/null +++ "b/12_\345\207\275\346\225\260/05_\345\207\275\346\225\260\350\277\224\345\233\236\345\200\274\347\232\204\345\272\224\347\224\250.py" @@ -0,0 +1,14 @@ +# 需求:制作计算器,计算任意两个数字相加的结果,并返回结果 + +def sum_num(a, b): + return a + b + +result = sum_num(200, 199) +print(result) + +''' +函数的返回值: + (1) 如果函数没有返回值【函数执行完毕之后,不需要给调用处提供数据】 return可以省略不写 + (2) 函数的返回值,如果是1个,直接返回类型 + (3) 函数的返回值,如果是多个,返回的结果为元组 +''' \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\257\264\346\230\216\346\226\207\346\241\243.py" "b/12_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\257\264\346\230\216\346\226\207\346\241\243.py" new file mode 100644 index 0000000000000000000000000000000000000000..3b9312347dae50d7a782875f54ed0bc62bf81601 --- /dev/null +++ "b/12_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\257\264\346\230\216\346\226\207\346\241\243.py" @@ -0,0 +1,22 @@ +help(len) # help函数的作用:查看函数的说明文档(函数解释说明的信息) + + +# 定义函数的说明文档 +def sum_num(a, b): + """ 在此定义说明文档:求和函数 """ + return a + b + +help(sum_num) + + +# 函数说明文档的高级使用 +def sum_num1(a, b): + """ + 求和函数sum_num1 + :param a: 参数1 + :param b: 参数2 + :return: 返回值 + """ + return a + b + +help(sum_num1) \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/07_\345\207\275\346\225\260\347\232\204\345\265\214\345\245\227\350\260\203\347\224\250.py" "b/12_\345\207\275\346\225\260/07_\345\207\275\346\225\260\347\232\204\345\265\214\345\245\227\350\260\203\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..63536f448066fc768950c92b15415208b4fbe6b1 --- /dev/null +++ "b/12_\345\207\275\346\225\260/07_\345\207\275\346\225\260\347\232\204\345\265\214\345\245\227\350\260\203\347\224\250.py" @@ -0,0 +1,15 @@ +# 两个函数 testA 和 testB -- 在A里面嵌套调用B +# B函数 +def testB(): + print('B函数开始-----') + print('这是B函数') + print('B函数结束-----') + +# A函数 +def testA(): + print('A函数开始-----') + # 嵌套调用B + testB() + print('A函数结束-----') + +testA() \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/08_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\346\211\223\345\215\260\346\250\252\347\272\277\345\233\276\345\275\242.py" "b/12_\345\207\275\346\225\260/08_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\346\211\223\345\215\260\346\250\252\347\272\277\345\233\276\345\275\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..b4ea58c9dd4a5e74c840a8c877a5873aedc9491b --- /dev/null +++ "b/12_\345\207\275\346\225\260/08_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\346\211\223\345\215\260\346\250\252\347\272\277\345\233\276\345\275\242.py" @@ -0,0 +1,14 @@ +# 1、打印一条横线 +def print_line(): + print('-' * 20) + +print_line() + +# 2、函数嵌套调用 实现多条横线 +def print_lines(num): + i = 0 + while i < num: + print_line() + i += 1 + +print_lines(5) diff --git "a/12_\345\207\275\346\225\260/09_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\345\207\275\346\225\260\350\256\241\347\256\227.py" "b/12_\345\207\275\346\225\260/09_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\345\207\275\346\225\260\350\256\241\347\256\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..c7b923b84eaeeaee90a8c3ce0af48e903bcb2efa --- /dev/null +++ "b/12_\345\207\275\346\225\260/09_\345\207\275\346\225\260\345\265\214\345\245\227\350\260\203\347\224\250_\345\207\275\346\225\260\350\256\241\347\256\227.py" @@ -0,0 +1,18 @@ +# 1、任意三个数之和 +def sum_num(a, b, c): + return a + b + c + + +result = sum_num(1, 2, 3) +print(result) + + +# 2、任意三个数求平均值 +def average_num(a, b, c): + # 先求和 再除以3 + sumResult = sum_num(a, b, c) + return sumResult / 3 + + +averageResult = average_num(1, 2, 3) +print(averageResult) diff --git "a/12_\345\207\275\346\225\260/10_\345\261\200\351\203\250\345\217\230\351\207\217.py" "b/12_\345\207\275\346\225\260/10_\345\261\200\351\203\250\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..88fdb8b15680f01b2ccfe353da428fc66e66b500 --- /dev/null +++ "b/12_\345\207\275\346\225\260/10_\345\261\200\351\203\250\345\217\230\351\207\217.py" @@ -0,0 +1,7 @@ +# 定义一个函数,声明一个变量:函数体内部访问、函数体外部访问 +def testA(): + a = 100 + print(a) # 函数体内部访问,能访问到a变量 + +testA() +# print(a) 报错:a变量是函数内部的变量,函数外部无法访问 -- a是一个局部变量 \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/11_\345\205\250\345\261\200\345\217\230\351\207\217.py" "b/12_\345\207\275\346\225\260/11_\345\205\250\345\261\200\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..c9b189871e017c5f2de9b3578cd6571ac042e17a --- /dev/null +++ "b/12_\345\207\275\346\225\260/11_\345\205\250\345\261\200\345\217\230\351\207\217.py" @@ -0,0 +1,26 @@ +# 声明全局变量:函数体内外部都能访问 +a = 100 + +print(a) + +def testA(): + print(a) + +def testB(): + # a = 200 # 如果直接修改a=200,此时的a是全局a还是局部a?-- 得到结论:这个a是局部变量 + # print(a) + + # 想要修改全局变量a,值是200 + global a # 声明a是全局变量 + a = 200 + print(a) + +testA() +testB() +print(f'全局变量a = {a}') + +""" +总结: + 1、如果在函数里面直接把变量a=200赋值,此时的a不是全局变量的修改,而是相当于在函数内部声明了一个新的局部变量 + 2、函数体内部修改全局变量:先global声明a为全局变量,然后再变量重新赋值 +""" \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/12_\345\244\232\345\207\275\346\225\260\346\211\247\350\241\214\346\265\201\347\250\213.py" "b/12_\345\207\275\346\225\260/12_\345\244\232\345\207\275\346\225\260\346\211\247\350\241\214\346\265\201\347\250\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..824f56218525cf93caf1017d4d77563d3e66c419 --- /dev/null +++ "b/12_\345\207\275\346\225\260/12_\345\244\232\345\207\275\346\225\260\346\211\247\350\241\214\346\265\201\347\250\213.py" @@ -0,0 +1,19 @@ +""" + 1、声明全局变量; + 2、定义两个函数; + 3、函数一:修改全局变量 +""" +glo_num = 0 + +def test1(): + global glo_num + glo_num = 100 + +def test2(): + print(glo_num) + +print(glo_num) # 0,因为修改的函数没有执行 +test2() # 0,因为修改的函数没有执行 +test1() +test2() # 100,先调用了test1 +print(glo_num) # 100,先调用了test1 diff --git "a/12_\345\207\275\346\225\260/13_\350\277\224\345\233\236\345\200\274\344\275\234\344\270\272\345\217\202\346\225\260\344\274\240\351\200\222.py" "b/12_\345\207\275\346\225\260/13_\350\277\224\345\233\236\345\200\274\344\275\234\344\270\272\345\217\202\346\225\260\344\274\240\351\200\222.py" new file mode 100644 index 0000000000000000000000000000000000000000..1ee65d933fed37cd48d7c1656aa0e5ecfd079da4 --- /dev/null +++ "b/12_\345\207\275\346\225\260/13_\350\277\224\345\233\236\345\200\274\344\275\234\344\270\272\345\217\202\346\225\260\344\274\240\351\200\222.py" @@ -0,0 +1,11 @@ +def test1(): + return 50 + +def test2(num): + print(num) + +# 1、保存函数test1的返回值 +result = test1() + +# 2、将函数返回值所在变量 作为参数传递到test2函数 +test2(result) \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/14_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" "b/12_\345\207\275\346\225\260/14_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" new file mode 100644 index 0000000000000000000000000000000000000000..4ef7d1fba63f68fca7516ec210c3a3d2fb9a376a --- /dev/null +++ "b/12_\345\207\275\346\225\260/14_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274.py" @@ -0,0 +1,24 @@ +# 需求:一个函数有两个返回值 1和2 + +# 一个函数如果有多个return不能都执行,只能执行第一个return,无法执行第二个return +def return_num(): + return 1 + return 2 + + +result = return_num() +print(result) # 1 + + +# 一个函数多个返回值的写法 +def return_num2(): + return 1, 2 # 默认返回的是元组 + + +result = return_num2() +print(result) + + +def return_num3(): + # return [1, 2] # 返回的可以是元组、列表、字典 + return {'name': 'Python', 'age': 30} diff --git "a/12_\345\207\275\346\225\260/15_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" "b/12_\345\207\275\346\225\260/15_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..cdd9ec8a5ae82ae0890c2d6d352e675094415678 --- /dev/null +++ "b/12_\345\207\275\346\225\260/15_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" @@ -0,0 +1,9 @@ +# 位置参数:调用函数时根据函数定义的参数位置来传递参数 + +def user_info(name, age, gender): + print(f'您的姓名是{name},年龄是{age},性别是{gender}') + + +user_info('TOM', 20, '男') +# user_info('TOM', 20) # 个数定义和传入不一致会报错 +user_info(20, 'TOM', '男') # 顺序也和定义必须是一致的,否则导致数据无意义 diff --git "a/12_\345\207\275\346\225\260/16_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" "b/12_\345\207\275\346\225\260/16_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..546f29aef1681fbac01699d0dda0d360d0fe1ba8 --- /dev/null +++ "b/12_\345\207\275\346\225\260/16_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" @@ -0,0 +1,10 @@ +def user_info(name, age, gender): + print(f'您的姓名是{name},年龄是{age},性别是{gender}') + + +# 调用函数传参 +user_info('ROSE', age=20, gender='女') +user_info('小明', gender='男', age=18) # 关键字参数之间不分先后顺序 + +# 位置参数必须写在关键字参数的前面 +# error: user_info(age=20, gender='男', 'TOM') diff --git "a/12_\345\207\275\346\225\260/17_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\347\274\272\347\234\201\345\217\202\346\225\260.py" "b/12_\345\207\275\346\225\260/17_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\347\274\272\347\234\201\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..aea34fffe207022db620210b09a76604831c84cb --- /dev/null +++ "b/12_\345\207\275\346\225\260/17_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260_\347\274\272\347\234\201\345\217\202\346\225\260.py" @@ -0,0 +1,6 @@ +def user_info(name, age, gender='男'): + print(f'您的姓名是{name},年龄是{age},性别是{gender}') + + +user_info('TOM', 18) # 没有为缺省参数传值,表示使用默认值 +user_info('TOM', 18, gender='女') # 为缺省参数传值,使用这个值,不用默认值 diff --git "a/12_\345\207\275\346\225\260/18_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" "b/12_\345\207\275\346\225\260/18_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..b040566236691f770e2bd88267b38ea3694ff80a --- /dev/null +++ "b/12_\345\207\275\346\225\260/18_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\344\275\215\347\275\256\345\217\202\346\225\260.py" @@ -0,0 +1,16 @@ +# 接收所有位置参数,返回一个元组 -- 位置参数,只能有一个 +# 包裹位置参数 +def user_info(*args): + print(args) + + +user_info('TOM') +user_info('TOM', 20) +user_info('TOM', 20, 'man') +user_info() + +''' +def fun2(*args,*a): + pass + 以上代码,程序会报错,个数可变的位置参数,只能是1个 +''' diff --git "a/12_\345\207\275\346\225\260/19_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" "b/12_\345\207\275\346\225\260/19_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..1f019d815d19b9ff9aff9b1f7716e0372539cdeb --- /dev/null +++ "b/12_\345\207\275\346\225\260/19_\344\270\215\345\256\232\351\225\277\345\217\202\346\225\260_\345\205\263\351\224\256\345\255\227\345\217\202\346\225\260.py" @@ -0,0 +1,24 @@ +# 收集所有关键字参数,返回一个字典 -- 关键字参数只能有一个 +def user_info(**kwargs): + print(kwargs) + + +user_info() +user_info(name='TOM') +user_info(name='TOM', age=20) + +''' +def fun2(**args,**args): + pass + 以上代码,程序会报错,个数可变的关键字参数,只能是1个 +''' + +def fun2(*args1,**args2): + pass + +''' +error: +def fun3(**args1,*arg2): + pass +在一个函数的定义过程中,既有个数可变的关键字形参,也有个数可变的位置形参,要求,个数可变的位置形参,放在个数可变的关键字形参之前 + ''' \ No newline at end of file diff --git "a/12_\345\207\275\346\225\260/19_\345\207\275\346\225\260\347\232\204\344\274\240\345\217\202.py" "b/12_\345\207\275\346\225\260/19_\345\207\275\346\225\260\347\232\204\344\274\240\345\217\202.py" new file mode 100644 index 0000000000000000000000000000000000000000..5b055df0af0ab8fcc5003e498fa458736e857132 --- /dev/null +++ "b/12_\345\207\275\346\225\260/19_\345\207\275\346\225\260\347\232\204\344\274\240\345\217\202.py" @@ -0,0 +1,27 @@ +# 函数的传参 +def fun(a, b, c): # a,b,c在函数的定义处,所以是形式参数 + print('a=', a) + print('b=', b) + print('c=', c) + +fun(10, 20, 30) # 函数调用时的参数传递,称为位置传参 +print('---------------------') + +lst = [11, 22, 33] +fun(*lst) # 在函数调用时,将列表中的每个元素都转换为位置实参传入 + +print('---------------------') + +fun(a=100, c=300, b=200) # 函数的调用,所以是关键字实参 +dic = {'a': 111, 'b': 222, 'c': 333} +fun(**dic) # 在函数调用时,将字典中的键值对都转换为关键字实参传入 +print('---------------------') + +def fun4(a, b, *, c, d): # 从*之后的参数,在函数调用时,只能采用关键字参数传递 + print('a=', a) + print('b=', b) + print('c=', c) + print('d=', d) + +fun4(a=10, b=20, c=30, d=40) # 关键字实参传递 +fun4(10, 20, c=30, d=40) # 前两个参数,采用的是位置实参传递,而c,d采用的是关键字实参传递 diff --git "a/12_\345\207\275\346\225\260/20_\345\205\203\347\273\204\346\213\206\345\214\205.py" "b/12_\345\207\275\346\225\260/20_\345\205\203\347\273\204\346\213\206\345\214\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..27a8870fb0eea422d9aac40ff4dced5330b01b8f --- /dev/null +++ "b/12_\345\207\275\346\225\260/20_\345\205\203\347\273\204\346\213\206\345\214\205.py" @@ -0,0 +1,24 @@ +# 1、拆包元组数据 +def return_num(): + return 100, 200 + + +result = return_num() +print(result) + +num1, num2 = return_num() +print(num1) # 100 +print(num2) # 200 + +# 2、字典数据拆包 +# 先准备字典,然后拆包 +dict1 = {'name': 'TOM', 'age': 18} + +# dict1中有两个键值对,拆包的时候用两个变量接收数据 +a, b = dict1 +print(a) +print(b) + +# value值 +print(dict1[a]) +print(dict1[b]) diff --git "a/12_\345\207\275\346\225\260/21_\344\272\244\346\215\242\345\217\230\351\207\217\347\232\204\345\200\274_\346\226\271\346\263\225\344\270\200.py" "b/12_\345\207\275\346\225\260/21_\344\272\244\346\215\242\345\217\230\351\207\217\347\232\204\345\200\274_\346\226\271\346\263\225\344\270\200.py" new file mode 100644 index 0000000000000000000000000000000000000000..d061da8e0079daba8da2db937d281d54b5934b0d --- /dev/null +++ "b/12_\345\207\275\346\225\260/21_\344\272\244\346\215\242\345\217\230\351\207\217\347\232\204\345\200\274_\346\226\271\346\263\225\344\270\200.py" @@ -0,0 +1,16 @@ +a = 10 +b = 20 + +# 1、方法一:利用中间变量 +c = a +a = b +b = c +print(f'a = {a}') +print(f'b = {b}') +print('============') + +# 2、方法二 +a, b = b, a +print(f'a = {a}') +print(f'b = {b}') +print('============') diff --git "a/13_\345\274\225\347\224\250/01_\344\272\206\350\247\243\345\274\225\347\224\250_\344\270\215\345\217\257\345\217\230\347\261\273\345\236\213.py" "b/13_\345\274\225\347\224\250/01_\344\272\206\350\247\243\345\274\225\347\224\250_\344\270\215\345\217\257\345\217\230\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..72a2ae27bffa3de2c4fcf5aa4804fc4ad12b8676 --- /dev/null +++ "b/13_\345\274\225\347\224\250/01_\344\272\206\350\247\243\345\274\225\347\224\250_\344\270\215\345\217\257\345\217\230\347\261\273\345\236\213.py" @@ -0,0 +1,24 @@ +# 可变和不可变 + +""" +1、不可变:int、float、tuple、str + 1.1 声明变量保存整型数据,把这个数据赋值到另一个变量 + 1.2 id()检测两个变量的id值(内存的十进制值) +""" +a = 1 +b = a + +print(b) + +# 发现a和b的id值是相同的 +print(id(a)) +print(id(b)) + +# 修改a的数据 测试id值 +a = 2 + +print(b) # 1 说明int类型为不可变类型 + +# 因为修改了a的数据,内存要开辟另外一份内存存储2,id检测a和b的地址不同 +print(id(a)) +print(id(b)) diff --git "a/13_\345\274\225\347\224\250/02_\344\272\206\350\247\243\345\274\225\347\224\250_\345\217\257\345\217\230\347\261\273\345\236\213.py" "b/13_\345\274\225\347\224\250/02_\344\272\206\350\247\243\345\274\225\347\224\250_\345\217\257\345\217\230\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..5ddbf0c23db190ef8673047385c8ed4213c72088 --- /dev/null +++ "b/13_\345\274\225\347\224\250/02_\344\272\206\350\247\243\345\274\225\347\224\250_\345\217\257\345\217\230\347\261\273\345\236\213.py" @@ -0,0 +1,15 @@ +# 2、可变类型:list、set、dict +a = [10, 20] +b = a + +print(b) + +print(id(a)) +print(id(b)) + +a.append(30) +print(a) +print(b) # 列表是可变类型 + +print(id(a)) +print(id(b)) diff --git "a/13_\345\274\225\347\224\250/03_\345\274\225\347\224\250\345\275\223\345\201\232\345\256\236\345\217\202.py" "b/13_\345\274\225\347\224\250/03_\345\274\225\347\224\250\345\275\223\345\201\232\345\256\236\345\217\202.py" new file mode 100644 index 0000000000000000000000000000000000000000..9a36bf9e5ed7a4da1de319e8849392578cd86e55 --- /dev/null +++ "b/13_\345\274\225\347\224\250/03_\345\274\225\347\224\250\345\275\223\345\201\232\345\256\236\345\217\202.py" @@ -0,0 +1,17 @@ +# 需求:引用是否可以当做实参 + +def test1(a): + print(a) + print(id(a)) + + a += a + print(a) + print(id(a)) + + +b = 100 +test1(b) + +print('==============') +c = [10, 20] +test1(c) diff --git "a/14_\345\272\224\347\224\250_\345\255\246\345\221\230\347\256\241\347\220\206\347\263\273\347\273\237/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.py" "b/14_\345\272\224\347\224\250_\345\255\246\345\221\230\347\256\241\347\220\206\347\263\273\347\273\237/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.py" new file mode 100644 index 0000000000000000000000000000000000000000..f0a45611287a7596bb8256a3d036822622d53e2b --- /dev/null +++ "b/14_\345\272\224\347\224\250_\345\255\246\345\221\230\347\256\241\347\220\206\347\263\273\347\273\237/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.py" @@ -0,0 +1,124 @@ +# 定义功能界面函数 +def info_print(): + print('请选择功能-----') + print('1、添加学员') + print('2、删除学员') + print('3、修改学员') + print('4、查询学员') + print('5、显示所有学员') + print('6、退出系统') + print('-------------') + + +# 学生信息的容器 +info = [] + + +def add_info(): + """ 添加学员信息 """ + # 1、用户输入学号、手机号、姓名 + new_id = input('请输入学号:') + new_name = input('请输入姓名:') + new_phone = input('请输入手机号:') + + # 2、判断是否添加这个学员:姓名存在则不添加,姓名不存在则添加 + # 2.1 姓名存在,不添加 + global info + for dict in info: + if new_name == dict['name']: + print('此学生姓名已经存在') + return # 退出当前函数,不再添加 + + # 2.2 姓名不存在,添加 + info_dict = {} # 要新增的字典 -- 存储新学员的信息 + info_dict['id'] = new_id + info_dict['name'] = new_name + info_dict['phone'] = new_phone + + # 列表中添加新学员 + info.append(info_dict) + + +def del_info(): + """ 删除学员信息 """ + # 1、用户输入要删除的学员姓名 + del_name = input('请输入要删除的学员姓名:') + # 2、判断学员姓名是否存在,存在则删除,不存在则提示 + global info + for dict in info: + if del_name == dict['name']: + info.remove(dict) + break + else: + print('该学员不存在,无法删除') + + +def modify_info(): + """ 修改学员信息 """ + # 1、用户输入想要修改的学员姓名 + modify_name = input('请输入要修改的学员姓名:') + + # 2、判断学员是否存在:存在则修改,不存在则提示 + global info + for dict in info: + if modify_name == dict['name']: + dict['id'] = input('请输入新的学号:') + dict['phone'] = input('请输入新的手机号:') + break + else: + print('该学员不存在') + + +def search_info(): + """ 查询学员信息 """ + # 1、请输入目标学员姓名 + search_name = input('请输入要查询的学员姓名:') + + # 2、检查学员是否存在:存在则打印这个学员的信息;不存在则提示 + global info + for dict in info: + if search_name == dict['name']: + # 查找到该学员 + print('查询到的学员信息如下-------') + print(f"该学生的学号为{dict['id']},姓名为{search_name},手机号码为{dict['phone']}") + break + else: + # 学员不存在则提示 + print('该学员不存在') + + +def print_all(): + """ 显示所有学员信息 """ + # 1、打印提示字 + print('学号\t姓名\t手机号') + + # 2、打印所有学员的数据 + for dict in info: + print(f"{dict['id']}\t{dict['name']}\t{dict['phone']}") + + + +while True: + # 1、显示功能界面 + info_print() + + # 2、用户输入功能序号 + user_number = eval(input('请输入您的选择:')) + + # 3、按照用户输入的功能序号,执行不同的功能 -- 多重判断 + if user_number == 1: + add_info() + elif user_number == 2: + del_info() + elif user_number == 3: + modify_info() + elif user_number == 4: + search_info() + elif user_number == 5: + print_all() + elif user_number == 6: + exit_flag = input('确定要退出吗?1、退出 2、取消\n') + if exit_flag == '1': + break + else: + print('您输入的功能序号有误') diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/01_\351\200\222\345\275\222\347\232\204\344\273\243\347\240\201\345\256\236\347\216\260.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/01_\351\200\222\345\275\222\347\232\204\344\273\243\347\240\201\345\256\236\347\216\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..aecf047868c236b759c354a584ebb4fe77ce724b --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/01_\351\200\222\345\275\222\347\232\204\344\273\243\347\240\201\345\256\236\347\216\260.py" @@ -0,0 +1,15 @@ +# 需求:3以内的数字累加和 +# 递归的特点:函数内部自己调用自己,必须要有出口 + +def sum_numbers(num): + # 递归出口 + if num == 1: + return 1 + + # 当前数字 + 当前数字-1的累加和 + return num + sum_numbers(num - 1) + +result = sum_numbers(3) +print(result) + +# 如果没有出口,报错:超出最大递归深度 \ No newline at end of file diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/02_\344\275\223\351\252\214lambda.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/02_\344\275\223\351\252\214lambda.py" new file mode 100644 index 0000000000000000000000000000000000000000..32ad3cef9a08f48a504c48fde3788c38eb3dace5 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/02_\344\275\223\351\252\214lambda.py" @@ -0,0 +1,20 @@ +# 需求:函数 返回值100 + +# 1、函数 +def fn1(): + return 100 + + +result = fn1() +print(result) + +# 2、lambda 匿名函数 +""" +lambda 参数列表:表达式 + 1、lambda表达式的参数可有可无 + 2、lambda表达式有且仅有 1个返回值 +""" +fn2 = lambda: 100 + +print(fn2) # lambda的内存地址 +print(fn2()) # 100 diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/03_lambda\346\241\210\344\276\213_\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/03_lambda\346\241\210\344\276\213_\344\270\244\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000000000000000000000000000000000000..b2b2f27e38c053445184f78ccea5c956044b0313 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/03_lambda\346\241\210\344\276\213_\344\270\244\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,12 @@ +# 需求:计算任意两个数字的累加和 + +# 1、函数 +def add1(a, b): + return a + b + +result = add1(1, 2) +print(result) + +# 2、lambda表达式 +add2 = lambda a, b: a + b +print(add2(1, 2)) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/04_lambda\345\217\202\346\225\260.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/04_lambda\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..9fcae1d187808cee3e81367e722461fa9eabfb3d --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/04_lambda\345\217\202\346\225\260.py" @@ -0,0 +1,21 @@ +# 1、无参数 +fn1 = lambda: 100 +print(fn1()) + +# 2、一个参数 +fn2 = lambda a: a +print(fn2('Hello World')) + +# 3、默认参数/缺省参数 +fn3 = lambda a, b, c=100: a + b + c +print(fn3(10, 20)) +print(fn3(10, 20, 200)) + +# 4、可变参数 *args -- 位置参数 +fn4 = lambda *args: args +print(fn4(10, 20, 30)) +print(fn4(10)) + +# 5、可变参数 **kwargs -- 关键字参数 +fn5 = lambda **kwargs: kwargs +print(fn5(work='Python', score=97)) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/05_\345\272\224\347\224\250_\345\270\246\345\210\244\346\226\255\347\232\204lambda.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/05_\345\272\224\347\224\250_\345\270\246\345\210\244\346\226\255\347\232\204lambda.py" new file mode 100644 index 0000000000000000000000000000000000000000..ccc0101dfee0fcca935f681c98f0307d12bd8932 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/05_\345\272\224\347\224\250_\345\270\246\345\210\244\346\226\255\347\232\204lambda.py" @@ -0,0 +1,3 @@ +# lambda 两个数字比较大小,返回较大数 +fn1 = lambda num1, num2: num1 if num1 > num2 else num2 +print(fn1(1000, 200)) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/06_\345\210\227\350\241\250\345\206\205\345\255\227\345\205\270\346\225\260\346\215\256\346\216\222\345\272\217.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/06_\345\210\227\350\241\250\345\206\205\345\255\227\345\205\270\346\225\260\346\215\256\346\216\222\345\272\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..0afe58a565652740fdaceb03e96c440011cf5f41 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/06_\345\210\227\350\241\250\345\206\205\345\255\227\345\205\270\346\225\260\346\215\256\346\216\222\345\272\217.py" @@ -0,0 +1,18 @@ +students = [ + {'name': 'TOM', 'age': 20}, + {'name': 'ROSE', 'age': 19}, + {'name': 'Jack', 'age': 22} +] + +# sort(key=lambda..., reverse=bool数据) +# 1、按照name进行升序排序 -- 参数x,依次表示students列表中的元素 +students.sort(key=lambda x: x['name']) +print(students) + +# 2、按照name进行降序排序 +students.sort(key=lambda x: x['name'], reverse=True) +print(students) + +# 3、按照age进行升序排序 +students.sort(key=lambda x: x['age']) +print(students) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/07_\344\275\223\351\252\214\351\253\230\351\230\266\345\207\275\346\225\260.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/07_\344\275\223\351\252\214\351\253\230\351\230\266\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..abbe88e782d01c708fa66059f4b2529a6d1926c1 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/07_\344\275\223\351\252\214\351\253\230\351\230\266\345\207\275\346\225\260.py" @@ -0,0 +1,49 @@ +# 高阶函数:把一个函数,当做另一个函数的参数 + +# 1、预备知识 -- 一些内置的运算函数 +# abs(a):求数字的绝对值 +print(abs(-10)) + +# round(a):对数字进行四舍五入 +print(round(1.3)) +print(round(1.9)) + +# divmod(x, y): 输出二元组(x//y , x%y) +print(divmod(10, 3)) + +# pow(x, y): 等价于 x**y +print(pow(2, 4)) + +# pow(x, y, z): 等价于 (x ** y) % z +print(pow(2, 4, 3)) + +# max(*args): 找最大值 +print(max(1, 4, 6, 2, 3)) + +# min(*args): 找最小值 +print(min(1, 30, 25, -3, 7)) + +print('================') + +# 2、高阶函数 +# 需求:任意两个数字,先进行数字处理(绝对值或四舍五入)再求和计算 +# 写法一: +def add_num(num1, num2): + # 数字处理 仅可取绝对值 + return abs(num1) + abs(num2) + + +result = add_num(-3, -1.6) +print(result) + + +# 写法二:高阶函数 +def sum_num(num1, num2, fun): # fun用来接收将来传入的函数 + # 数字可以进行任意处理 + return fun(num1) + fun(num2) + + +result = sum_num(-3, -1.6, abs) +print(result) +result = sum_num(-3, -1.4, round) +print(result) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/08_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_map.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/08_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_map.py" new file mode 100644 index 0000000000000000000000000000000000000000..bf7e4a185e8575eeed0cd991bedda3a44c56016f --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/08_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_map.py" @@ -0,0 +1,23 @@ +""" +map(func, list): + 参数func: 函数名 + 参数list:迭代器,可以是list、set、tuple、dict... + +作用: + 将函数func作用于list中的每一个元素,返回一个数据处理之后的迭代器 +""" + +# 需求:计算list1序列中各个数字的2次方 +# 1、准备列表数据 +list1 = [1, 2, 3, 4, 5] + +# 2、准备2次方计算的函数 +def func(x): + return x**2 + +# 3、调用map +result = map(func,list1) + +# 4、结果 +print(result) # 打印迭代器的地址 +print(list(result)) # 强转,显示迭代器的内容 diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/09_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_reduce.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/09_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_reduce.py" new file mode 100644 index 0000000000000000000000000000000000000000..881ffbff1f00bd3765bec6a7da666b814958b55e --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/09_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_reduce.py" @@ -0,0 +1,23 @@ +""" +reduce(func, list): + 参数func: 函数名, 必须要有两个参数 + 参数list:迭代器,可以是list、set、tuple、dict... + +作用: + 每次func计算的结果,继续和list中下一个元素 做累积计算 +""" + +# 需求:计算list1序列中各个数字的累加和 +# 1、导入模块 +import functools + +# 2、准备数据 +list1 = [1, 2, 3, 4, 5] + +# 3、定义功能函数 +def fun(num1, num2): + return num1 + num2 + +# 4、调用reduce,显示结果 +result = functools.reduce(fun, list1) +print(result) diff --git "a/15_\345\207\275\346\225\260\351\253\230\351\230\266/10_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_filter.py" "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/10_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_filter.py" new file mode 100644 index 0000000000000000000000000000000000000000..d65e1ed196dd8a078b0bbf6a04ca7d876fa152d4 --- /dev/null +++ "b/15_\345\207\275\346\225\260\351\253\230\351\230\266/10_\345\206\205\347\275\256\351\253\230\351\230\266\345\207\275\346\225\260_filter.py" @@ -0,0 +1,18 @@ +""" +filter(func, list): + 参数func: 函数名,返回bool类型 + 参数list:迭代器,可以是list、set、tuple、dict... + +作用: + 用于过滤符合func要求的数据,返回一个filter对象 -- 查看内容用强制类型转换 +""" + +list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +# 1、定义功能函数:过滤,找出序列中的偶数 +def func(num): + return num % 2 == 0 + +# 2、调用filter +result = filter(func, list1) +print(result) # filter对象的内存地址 +print(list(result)) # 强制转换成list,查看内容 diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\346\223\215\344\275\234\346\255\245\351\252\244.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\346\223\215\344\275\234\346\255\245\351\252\244.py" new file mode 100644 index 0000000000000000000000000000000000000000..2ec5d5d0632ed98f6ce6c537c8c408fa79cb8861 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\346\223\215\344\275\234\346\255\245\351\252\244.py" @@ -0,0 +1,14 @@ +""" +1、打开文件 +open(name, mode): + name: 文件名 或者 文件路径 + mode: 打开文件的模式/访问模式 -- 只读,写入,追加 + 返回值:文件对象 +""" +f = open('test.txt', 'w', encoding='utf-8') # 设置编码 + +# 2、读写文件 +f.write('aaa') + +# 3、关闭文件 +f.close() diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/02_\350\256\277\351\227\256\346\250\241\345\274\217.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/02_\350\256\277\351\227\256\346\250\241\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..ecd07eb0abac421ed1d5c468c822f08cf8f3a590 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/02_\350\256\277\351\227\256\346\250\241\345\274\217.py" @@ -0,0 +1,31 @@ +""" +r: 只读模式,默认值 + 如果文件不存在,报错 + 不支持写入操作,表示只读 +""" +f = open('test.txt', 'r') +# error: f.write('aaa') +f.close() + +with open('test.txt','r') as file: + print(file.read()) + +""" +w: 只写模式 + 如果文件不存在,则会新建一个文件 + 执行写入,会覆盖原有资源 +""" +f = open('test2.txt', 'w') +f.write('aaa') +f.close() + + +""" +a: 追加模式 + 如果文件不存在,则会新建一个文件 + 在原有内容的基础上,追加内容 +""" +f = open('test.txt','a') +f.write('xyz') + +f.close() diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\345\206\231\345\207\275\346\225\260.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\345\206\231\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..a1759fd6ca2a20790845cb52759f1b2193987b83 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\345\206\231\345\207\275\346\225\260.py" @@ -0,0 +1,13 @@ +file = open('test.txt', 'a') +file.write('hello') # write(str):把字符串写入文件,单行写入。 + +lst = ['java', 'go', 'python'] +file.writelines(lst) # writelines(str): 把字符串按行写入文件,多行写入. + +file.flush() # 立刻将缓冲区的数据存入磁盘 +file.close() + +# 拷贝文件 +with open('copylogo.png','rb') as src_file: + with open('copy2logo.png','wb') as target_file: + target_file.write(src_file.read()) \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\350\257\273\345\217\226\345\207\275\346\225\260.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\350\257\273\345\217\226\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..d65f50b888d80e1e182c37d34a4a4b34461bea73 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/03_\350\257\273\345\217\226\345\207\275\346\225\260.py" @@ -0,0 +1,53 @@ +""" +1、read(): + 文件对象.read(num) + + num表示从文件中读取的数据长度(单位为字节) + 如果没有传入num,表示读取文件中的所有数据 +""" +f = open('test.txt', 'r') + +# read() 不写参数,表示读取所有 +print(f.read()) +f.close() + +f = open('test.txt', 'r') +# read(num) 读取num个字节的内容,换行符也占一个字节 +print(f.read(10)) +f.close() + +""" +2、readlines(): + 按照行的方式把整个文件中的内容进行读取 + 返回一个列表,每个元素为每一行的内容 + + readlines(num): + 读取前num行,并形成元素个数为num 的列表 +""" +f = open('test.txt', 'r') +lines = f.readlines() +print(lines) +f.close() + +""" +3、readline(): + 一次读取一行内容 + + readline(num): + 读取一行的 前num个字符 +""" +f = open('test.txt', 'r') +line = f.readline() +print(line) + +line = f.readline() +print(line) +f.close() + +""" +4、循环读取 +""" +f = open('test.txt', 'r') +for line in f: + # 每次循环读取一行 + print(line, end='') diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/04_\350\256\277\351\227\256\346\250\241\345\274\217\350\277\233\351\230\266.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/04_\350\256\277\351\227\256\346\250\241\345\274\217\350\277\233\351\230\266.py" new file mode 100644 index 0000000000000000000000000000000000000000..b458d89929065951fc8bdc19f27eaba758998110 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/04_\350\256\277\351\227\256\346\250\241\345\274\217\350\277\233\351\230\266.py" @@ -0,0 +1,26 @@ +""" +带 b: + 以二进制的方式处理 +带 +: + 可读可写 +""" + +# r+:没有该文件则报错,文件指针在开头 +f = open('test.txt', 'r+') +content = f.read() +print(content) +f.close() +print('================') + +# w+:没有该文件会新建文件,文件指针在开头,用新内容覆盖原内容,执行read(没有写入内容),会清空文件 +f = open('test2.txt', 'w+') +content = f.read() +print(content) +f.close() +print('================') + +# a+: 没有该文件会新建文件,文件指针在结尾,所以用read无法读取数据 +f = open('test.txt', 'a+') +content = f.read() +print(content) +f.close() \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/05_seek\345\207\275\346\225\260.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/05_seek\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..dab486b42485ad90799bd2571778a1838b0674ae --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/05_seek\345\207\275\346\225\260.py" @@ -0,0 +1,25 @@ +""" +seek(): + 文件对象.seek(偏移量, 起始位置) 0开头 1当前 2结尾 +作用: + 改变文件指针的位置 +""" +# 测试 r+ +f = open('test.txt', 'r+') +f.seek(2, 0) # 不读取前两个a -- 指针在开头,再向后偏移两个单位 + +print(f.tell()) # 告知我们当前指针的位置 +content = f.read() +print(content) + +f.close() +print('===========') + +# 测试 a+ +f = open('test.txt', 'a+') +f.seek(0, 0) # 设置文件指针在开头 -- 可简写为 f.seek(0) + +content = f.read() +print(content) + +f.close() \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/06_\346\241\210\344\276\213_\346\226\207\344\273\266\345\244\207\344\273\275.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/06_\346\241\210\344\276\213_\346\226\207\344\273\266\345\244\207\344\273\275.py" new file mode 100644 index 0000000000000000000000000000000000000000..ebf12627e3a0445fc5cb7531f724a66628222e5c --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/06_\346\241\210\344\276\213_\346\226\207\344\273\266\345\244\207\344\273\275.py" @@ -0,0 +1,28 @@ +# 1、用户输入目标文件 +old_name = input('请输入您要备份的文件名:\n') + + +# 2、规划备份文件的名字 +# 提取后缀 -- 找到文件中最后一个点 -- 名字和后缀分离 +index = old_name.rfind('.') + +# 判断文件名是否有效 -- 无效则报错 +if index > 0: + # 组织新名字 + name = old_name[:index] + tail = old_name[index:] + new_name = name + '[备份]' + tail + + +# 3、备份文件中添加数据 +new_file = open(new_name, 'wb') +old_file = open(old_name, 'rb') + +while True: + content = old_file.read() + if len(content) == 0: + break + new_file.write(content) + +new_file.close() +old_file.close() \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/07_\346\226\207\344\273\266\346\223\215\344\275\234\345\207\275\346\225\260.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/07_\346\226\207\344\273\266\346\223\215\344\275\234\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..cfed4ca4c6913b7b27858140e153c1d8879ada7f --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/07_\346\226\207\344\273\266\346\223\215\344\275\234\345\207\275\346\225\260.py" @@ -0,0 +1,14 @@ +import os + +""" +1、rename(目标文件名, 新文件名): + 重命名 +""" +os.rename('test1.txt', 'test.txt') + + +""" +2、remove(): + 删除文件,如果文件不存在则删除 +""" +os.remove("test2.txt") \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/08_\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\345\207\275\346\225\260.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/08_\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..b849b6234ac85bb1893fe20021966afd518be4d5 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/08_\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\345\207\275\346\225\260.py" @@ -0,0 +1,43 @@ +import os + +""" +1、mkdir(文件夹名字): + 创建文件夹 -- 若文件夹已经存在,则会报错 +""" +os.mkdir('a') + + +""" +2、rmdir(文件夹名字): + 删除文件夹 +""" +os.rmdir('a') + + +""" +3、getcwd(): + 返回当前文件所在目录路径 +""" +path = os.getcwd() +print(path) + +""" +4、chdir(目录): + 改变默认目录 +""" +# 需求:在a文件夹里面创建b文件夹 +# 切换目录到a文件夹,再创建b +os.chdir('aa') +os.mkdir('bb') + +""" +5、listdir(目录): + 获取文件所在文件夹下的所有文件 -- 返回列表 +""" +print(os.listdir('aa')) + +""" +6、rename(旧名字,新名字): + 重命名文件夹 +""" +os.rename('aa','a') \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/09_\345\272\224\347\224\250_\346\211\271\351\207\217\351\207\215\345\221\275\345\220\215.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/09_\345\272\224\347\224\250_\346\211\271\351\207\217\351\207\215\345\221\275\345\220\215.py" new file mode 100644 index 0000000000000000000000000000000000000000..5002a203d1120da63ba15359f87f4e07829fce39 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/09_\345\272\224\347\224\250_\346\211\271\351\207\217\351\207\215\345\221\275\345\220\215.py" @@ -0,0 +1,29 @@ +# 需求1:把 aa 文件夹所有文件重命名 -- 文件名前增加 "Python_" +# 需求2:把 aa 文件夹所有文件重命名 -- 文件名前删除 "Python_" + +import os + +# 1、找到所有文件:获取aa文件夹的目录列表 -- listdir() +file_list = os.listdir('aa') + +os.chdir('aa') # 改变操作的目录 + +# 1表示给文件名加前缀,2表示删除文件名的前缀 +print('请输入您要对文件名执行的操作:') +print('1、给文件名加前缀"Python_"') +print('2、删除文件名的前缀"Python_"') +flag = input() + +# 2、构造名字 +for name in file_list: + if flag == '1': + new_name = 'Python_' + name + + elif flag == '2': + num = len('Python_') + new_name = name[num:] + + # 3、重命名 + os.rename(name, new_name) + + diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/10_\344\270\212\344\270\213\346\226\207\347\256\241\347\220\206\345\231\250.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/10_\344\270\212\344\270\213\346\226\207\347\256\241\347\220\206\345\231\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..514ddc68a55e793c8cb20a48e61f30444b80fd10 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/10_\344\270\212\344\270\213\346\226\207\347\256\241\347\220\206\345\231\250.py" @@ -0,0 +1,23 @@ +""" +MyContentMgr实现了特殊方法__enter__(),__exit__()称为该类对象遵守了上下文管理器协议 +该类对象的实例对象,称为上下文管理器 + +MyContentMgr() + +""" + + +class MyContentMgr(object): + def __enter__(self): + print('enter方法被调用执行了') + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + print('exit方法被调用执行了') + + def show(self): + print('show方法被调用执行了') + + +with MyContentMgr() as file: # 相当于file=MyContentMgr() + file.show() diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/11_os\346\250\241\345\235\227.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/11_os\346\250\241\345\235\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..25aa78346e7d8ec2dae01dcca8c92147f25d9794 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/11_os\346\250\241\345\235\227.py" @@ -0,0 +1,21 @@ +import os # os模块是与操作系统相关的一个模块 + +# os.system(要打开的小工具) +# +# os.startfile(可执行文件) + +print(os.getcwd()) # 返回当前的操作目录 + +lst = os.listdir("../15_函数高阶") # 返回文件夹中的所有文件信息 +print(lst) + +os.mkdir("new_dir") # 创建目录 + +os.makedirs('A/B/C') # 创建多级目录 + +os.rmdir("new_dir") # 删除目录 + +os.removedirs('A/B/C') # 移除多级目录 + +os.chdir('aa') # 设置当前工作目录 +print(os.getcwd()) # 获取当前工作目录 diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/12_os.path\346\250\241\345\235\227.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/12_os.path\346\250\241\345\235\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..8a821444db4233368c7e30b6bc9e34b3e275006b --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/12_os.path\346\250\241\345\235\227.py" @@ -0,0 +1,18 @@ +import os.path + +print(os.path.abspath('10_上下文管理器.py')) # 获取绝对路径 + +print(os.path.exists('10_上下文管理器.py'),os.path.exists('12.py')) # 判断文件是否存在 + +print(os.path.join('/Users/macbook/Documents','10_上下文管理器.py')) # 将目录与文件名进行拼接 + +print(os.path.split('/Users/macbook/Documents/Python/16_文件操作/10_上下文管理器.py')) # 将目录与文件名分离 + +print(os.path.splitext('10_上下文管理器.py')) # 把文件和后缀名进行拆分 + +print(os.path.basename('/Users/macbook/Documents/Python/16_文件操作/10_上下文管理器.py')) # 从路径中提取文件名字 + +print(os.path.dirname('/Users/macbook/Documents/Python/16_文件操作/10_上下文管理器.py')) # 从路径中提取目录 + +print(os.path.isdir('/Users/macbook/Documents/Python/16_文件操作')) # 判断是否是目录 + diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/13_\346\241\210\344\276\213_\345\210\227\345\207\272\346\211\200\346\234\211\346\226\207\344\273\266.py" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/13_\346\241\210\344\276\213_\345\210\227\345\207\272\346\211\200\346\234\211\346\226\207\344\273\266.py" new file mode 100644 index 0000000000000000000000000000000000000000..c5a5022341e4016840b0615094659342f399e31c --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/13_\346\241\210\344\276\213_\345\210\227\345\207\272\346\211\200\346\234\211\346\226\207\344\273\266.py" @@ -0,0 +1,24 @@ +# 列出指定目录下的所有py文件 +import os + +path = os.getcwd() # 获取当前目录 +lst = os.listdir(path) # 获取当前目录下的文件和文件夹 + +for filename in lst: + if filename.endswith('.py'): + print(filename) +print('===================') + +# 将当前目录下的所有文件(包括子目录)都列出来 +path = os.getcwd() +lst_files = os.walk(path) +for dirpath, dirname, filename in lst_files: + # print(dirpath) + # print(dirname) + # print(filename) + for dir in dirname: + print(os.path.join(dirpath,dir)) + + for file in filename: + print(os.path.join(dirpath,file)) + print('=====================') diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/copy2logo.png" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/copy2logo.png" new file mode 100644 index 0000000000000000000000000000000000000000..af2164f1325cc392256a9410ff223154b9d8ce72 Binary files /dev/null and "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/copy2logo.png" differ diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/copylogo.png" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/copylogo.png" new file mode 100644 index 0000000000000000000000000000000000000000..af2164f1325cc392256a9410ff223154b9d8ce72 Binary files /dev/null and "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/copylogo.png" differ diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/test.txt" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test.txt" new file mode 100644 index 0000000000000000000000000000000000000000..5a16389e4e12f77337cff6caca11b090ab93ebc8 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test.txt" @@ -0,0 +1,5 @@ +aaa +bbb +ccc +ddd +hellojavagopythonxyzhellojavagopythonhellojavagopython \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/test2.txt" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test2.txt" new file mode 100644 index 0000000000000000000000000000000000000000..7c4a013e52c76442ab80ee5572399a30373600a2 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test2.txt" @@ -0,0 +1 @@ +aaa \ No newline at end of file diff --git "a/16_\346\226\207\344\273\266\346\223\215\344\275\234/test[\345\244\207\344\273\275].txt" "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test[\345\244\207\344\273\275].txt" new file mode 100644 index 0000000000000000000000000000000000000000..35fbd83349cf5962cbef75d9f6340f48be890382 --- /dev/null +++ "b/16_\346\226\207\344\273\266\346\223\215\344\275\234/test[\345\244\207\344\273\275].txt" @@ -0,0 +1,4 @@ +aaa +bbb +ccc +ddd diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/01_\344\275\223\351\252\214\351\235\242\345\220\221\345\257\271\350\261\241.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/01_\344\275\223\351\252\214\351\235\242\345\220\221\345\257\271\350\261\241.py" new file mode 100644 index 0000000000000000000000000000000000000000..e1d6bfdb8db8b124c588dade3879f6180269b688 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/01_\344\275\223\351\252\214\351\235\242\345\220\221\345\257\271\350\261\241.py" @@ -0,0 +1,22 @@ +# 需求: 洗衣机, 功能:能洗衣服 + +# 1、定义洗衣机类 -- 大驼峰命名 +""" +class 类名(): + 代码 + ... +""" +class Washer(): + def wash(self): + print('洗衣机正在洗衣服...') + +# 2、创建对象 +# 对象名 = 类名() +haier = Washer() + +# 3、验证成果 +# 3.1 打印对象的内存地址 +print(haier) + +# 3.2 使用wash功能 -- 实例方法/对象方法 -- 对象名.wash() +haier.wash() diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/02_\347\261\273\351\207\214\351\235\242\347\232\204self.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/02_\347\261\273\351\207\214\351\235\242\347\232\204self.py" new file mode 100644 index 0000000000000000000000000000000000000000..2bdd67da317d1ed45e043976f3cd90768c27a233 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/02_\347\261\273\351\207\214\351\235\242\347\232\204self.py" @@ -0,0 +1,11 @@ +# 需求: 洗衣机, 功能:能洗衣服 + +class Washer(): + def wash(self): + print('洗衣机正在洗衣服...') + print(self) # 打印对象的内存地址 -- self指的是调用该函数的对象 + +haier = Washer() +print(haier) # 打印内存地址 + +haier.wash() diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/03_\344\270\200\344\270\252\347\261\273\345\210\233\345\273\272\345\244\232\344\270\252\345\257\271\350\261\241.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/03_\344\270\200\344\270\252\347\261\273\345\210\233\345\273\272\345\244\232\344\270\252\345\257\271\350\261\241.py" new file mode 100644 index 0000000000000000000000000000000000000000..ad83091ee290b8ba2d8b41884c25241951d463c4 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/03_\344\270\200\344\270\252\347\261\273\345\210\233\345\273\272\345\244\232\344\270\252\345\257\271\350\261\241.py" @@ -0,0 +1,28 @@ + +class Washer(): + def wash(self): + print('洗衣机正在洗衣服...') + print(self) + +# 1、一个类可以创建多个对象 +# 2、不同的对象,其内存地址也不同 +haier1 = Washer() +haier1.wash() + +haier2 = Washer() +haier2.wash() + +haier3 = Washer() +haier3.wash() + + + + + + + + + + + + diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/04_\347\261\273\345\244\226\351\235\242\346\267\273\345\212\240\345\222\214\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/04_\347\261\273\345\244\226\351\235\242\346\267\273\345\212\240\345\222\214\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" new file mode 100644 index 0000000000000000000000000000000000000000..e07992036f17b214d727ec2d843d9f8c4669ad5d --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/04_\347\261\273\345\244\226\351\235\242\346\267\273\345\212\240\345\222\214\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" @@ -0,0 +1,17 @@ + +class Washer(): + def wash(self): + print('洗衣机正在洗衣服...') + print(self) + +haier1 = Washer() + +# 添加属性 对象名.属性名 = 值 -- 类外动态添加属性 +haier1.width = 400 # 宽 +haier1.height = 500 # 高 + + +# 获取属性 对象名.属性名 +print(f'洗衣机的宽度是{haier1.width}cm,长度是{haier1.height}cm') + + diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/05_\347\261\273\351\207\214\351\235\242\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/05_\347\261\273\351\207\214\351\235\242\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" new file mode 100644 index 0000000000000000000000000000000000000000..a658e57ae611846724776d70c3b55e5d57202b59 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/05_\347\261\273\351\207\214\351\235\242\350\216\267\345\217\226\345\257\271\350\261\241\345\261\236\346\200\247.py" @@ -0,0 +1,18 @@ +# 定义类 +class Washer(): + def wash(self): + print('洗衣机正在洗衣服...') + + def print_info(self): + # 类里面获取属性的语法:self.属性名 + print(f'洗衣机的宽度是{self.width}cm,高度是{self.height}cm') + +# 创建对象 +haier1 = Washer() + +# 添加属性 对象名.属性名 = 值 +haier1.width = 400 # 宽 +haier1.height = 500 # 高 + +haier1.print_info() + diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/06_\351\255\224\346\263\225\346\226\271\346\263\225_\346\227\240\345\217\202\347\232\204init.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/06_\351\255\224\346\263\225\346\226\271\346\263\225_\346\227\240\345\217\202\347\232\204init.py" new file mode 100644 index 0000000000000000000000000000000000000000..4d4d5d7334f912e28c299179d62bed921dc82c0a --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/06_\351\255\224\346\263\225\346\226\271\346\263\225_\346\227\240\345\217\202\347\232\204init.py" @@ -0,0 +1,27 @@ +""" +初始化方法: +def __init__(self): + 定义类中属性的代码 + +注意: + 1、在创建一个对象时自动被调用,无需手动调用 + 2、self 无需我们手动输入,解释器会自动输入 + 3、不写init方法时,系统会自动给一个空的init方法 -- 写init方法之后,系统则将空的init方法收回 +""" +# 定义类 +class Washer(): + def __init__(self): + # 添加实例属性 + self.width = 400 + self.height = 500 + + def wash(self): + print('洗衣机正在洗衣服...') + + def print_info(self): + # 类里面获取属性的语法:self.属性名 + print(f'洗衣机的宽度是{self.width}cm,高度是{self.height}cm') + +haier1 = Washer() +haier1.print_info() + diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/07_\351\255\224\346\263\225\346\226\271\346\263\225_\345\270\246\345\217\202\347\232\204init.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/07_\351\255\224\346\263\225\346\226\271\346\263\225_\345\270\246\345\217\202\347\232\204init.py" new file mode 100644 index 0000000000000000000000000000000000000000..35e8563babf5512886c1bde385d82af668bfbe24 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/07_\351\255\224\346\263\225\346\226\271\346\263\225_\345\270\246\345\217\202\347\232\204init.py" @@ -0,0 +1,27 @@ +# 定义类 +class Washer(): + # 不写init方法时,系统会自动给一个空的init方法 - - 写init方法之后,系统则将空的init方法收回 + def __init__(self, width, height): + # 添加实例属性 + self.width = width + self.height = height + + def wash(self): + print('洗衣机正在洗衣服...') + + def print_info(self): + # 类里面获取属性的语法:self.属性名 + print(f'洗衣机的宽度是{self.width}cm,高度是{self.height}cm') + + +# 创建多个对象,属性值不同 +haier1 = Washer(400,500) +haier1.print_info() + +haier2 = Washer(300,400) +haier2.print_info() + +haier3 = Washer(200,300) +haier3.print_info() + +# error: haier4 = Washer() 空参init已经被系统收回 \ No newline at end of file diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/08_\351\255\224\346\263\225\346\226\271\346\263\225_str.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/08_\351\255\224\346\263\225\346\226\271\346\263\225_str.py" new file mode 100644 index 0000000000000000000000000000000000000000..3baee4f56b4b8f11ee3795f8876ec734762059b3 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/08_\351\255\224\346\263\225\346\226\271\346\263\225_str.py" @@ -0,0 +1,25 @@ +""" +问题:当使用print输出对象的时候,默认打印对象的地址 -- 想改变,使用str魔法方法 + +__str__(self): + return 结果 +作用: + 当使用str魔法方法之后,打印的是str方法return后的数据 +""" + +# 洗衣机类 +class Washer(): + def __init__(self, width, height): + # 添加实例属性 + self.width = width + self.height = height + + def wash(self): + print('洗衣机正在洗衣服...') + + def __str__(self): + return f'洗衣机的宽度是{self.width}cm,高度是{self.height}cm' + + +haier1 = Washer(400, 500) +print(haier1) # 打印的不再是对象的内存地址了 默会调用__str__()这样的方法 diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_add\345\222\214len.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_add\345\222\214len.py" new file mode 100644 index 0000000000000000000000000000000000000000..58a3391adc066ab9bfd116cbb98e350815999fa1 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_add\345\222\214len.py" @@ -0,0 +1,32 @@ +a = 20 +b = 100 +c = a + b # 两个整数类型的对象的相加操作 +d = a.__add__(b) + +print(c) +print(d) + + +class Student: + def __init__(self, name): + self.name = name + + def __add__(self, other): + return self.name + other.name + + def __len__(self): + return len(self.name) + + +stu1 = Student('Jack') +stu2 = Student('李四') + +s = stu1 + stu2 # 实现了两个对象的加法运算(因为在Student类中 编写__add__()特殊的方法) +print(s) +s = stu1.__add__(stu2) +print(s) +print('----------------------------------------') +lst = [11, 22, 33, 44] +print(len(lst)) # len是内容函数len +print(lst.__len__()) +print(len(stu1)) diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_del.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_del.py" new file mode 100644 index 0000000000000000000000000000000000000000..a6b57a708b52f7485e101b38abca4256cb09c55d --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_del.py" @@ -0,0 +1,25 @@ +""" +__del__(self): + 当删除对象时,默认调用del魔法方法 +""" + + +# 洗衣机类 +class Washer(): + def __init__(self, width, height): + # 添加实例属性 + self.width = width + self.height = height + + def wash(self): + print('洗衣机正在洗衣服...') + + def __str__(self): + return f'洗衣机的宽度是{self.width}cm,高度是{self.height}cm' + + def __del__(self): + print('洗衣机对象被删除') + + +haier = Washer(400, 500) +print(haier) diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_new.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_new.py" new file mode 100644 index 0000000000000000000000000000000000000000..97bba6fb042dcaad4f0fda32f6fc86801f4d4c12 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/09_\351\255\224\346\263\225\346\226\271\346\263\225_new.py" @@ -0,0 +1,35 @@ +""" +__new__(cls): + 1、使用 类名() 创建对象时,Python 的解释器 首先 会 调用 __new__ 方法为对象 分配空间 + + 2、__new__ 是一个 由 object 基类提供的 内置的静态方法,主要作用有两个: + 在内存中为对象 分配空间 + 返回 对象的引用 + Python 的解释器获得对象的 引用 后,将引用作为 第一个参数,传递给 __init__ 方法 + + 3、重写 __new__ 方法 的代码非常固定! + 重写 __new__ 方法 一定要 return super().__new__(cls) + 否则 Python 的解释器 得不到 分配了空间的 对象引用,就不会调用对象的初始化方法 + +""" + + +class Person(object): + def __new__(cls, *args, **kwargs): + print('__new__被调用执行了,cls的id值为{0}'.format(id(cls))) + obj = super().__new__(cls) + print('创建的对象的id为:{0}'.format(id(obj))) + return obj + + def __init__(self, name, age): + print('__init__被调用了,self的id值为:{0}'.format(id(self))) + self.name = name + self.age = age + + +print('object这个类对象的id为:{0}'.format(id(object))) +print('Person这个类对象的id为:{0}'.format(id(Person))) + +# 创建Person类的实例对象 +p1 = Person('张三', 20) +print('p1这个Person类的实例对象的id:{0}'.format(id(p1))) diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/10_\345\272\224\347\224\250_\347\203\244\345\234\260\347\223\234.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/10_\345\272\224\347\224\250_\347\203\244\345\234\260\347\223\234.py" new file mode 100644 index 0000000000000000000000000000000000000000..0e44b01332ab9b962f228a7bd6adb1601f13707c --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/10_\345\272\224\347\224\250_\347\203\244\345\234\260\347\223\234.py" @@ -0,0 +1,47 @@ +# 1、定义地瓜类: +class SweetPotato(): + def __init__(self): + """ 初始化方法 """ + # 被烤的时间 + self.cook_time = 0 + # 地瓜的状态 + self.cook_static = '生的' + # 调料列表 + self.condiments = [] + + def cook(self, time): + """ 烤地瓜的方法 """ + # 1、更新地瓜烤了的时间 + self.cook_time += time + + # 2、更新地瓜的状态 + if 0 <= self.cook_time < 3: + self.cook_static = '生的' + elif 3 <= self.cook_time < 5: + self.cook_static = '半生不熟的' + elif 5 <= self.cook_time < 8: + self.cook_static = '熟的' + elif 8 <= self.cook_time: + self.cook_static = '烤糊了的' + + def add_condiments(self, *args): + # 更新调料列表 + self.condiments += list(args) + # 去除重复调料 + self.condiments = list(set(self.condiments)) + + def __str__(self): + return f'这个地瓜烤了{self.cook_time}分钟,\n' \ + f'这个地瓜是{self.cook_static},\n' \ + f'这个地瓜的调料有{self.condiments}' + + +# 2、创建对象,并调用对应的实例方法 +digua1 = SweetPotato() +print(digua1) +print('====================') + +digua1.cook(5) +digua1.add_condiments('辣椒粉', '芝麻酱', '辣椒粉', '辣椒油') +digua1.cook(4) +print(digua1) diff --git "a/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/11_\345\272\224\347\224\250_\346\220\254\345\256\266\345\205\267.py" "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/11_\345\272\224\347\224\250_\346\220\254\345\256\266\345\205\267.py" new file mode 100644 index 0000000000000000000000000000000000000000..460ab74f8ca7fe4c85b2671aff3b5766d2b547f6 --- /dev/null +++ "b/17_\351\235\242\345\220\221\345\257\271\350\261\241\345\237\272\347\241\200/11_\345\272\224\347\224\250_\346\220\254\345\256\266\345\205\267.py" @@ -0,0 +1,46 @@ +# 需求:把小于房子剩余空间的家具摆放到房子中去 + +# 房子类 +class Home(): + def __init__(self, address, area): + # 地理位置 + self.address = address + # 房屋面积 + self.area = area + # 剩余面积 + self.free_area = area + # 家具列表 + self.furnitures = [] + + def add_furnitures(self, *args): + # 逐一添加家具 + for furniture in args: + if self.free_area >= furniture.area: + self.furnitures.append(furniture.name) + # 更新房屋剩余面积 + self.free_area -= furniture.area + else: + print('房屋的剩余面积不足,无法再添置家具。') + + def __str__(self): + return f'房子的地理位置在{self.address}.房屋面积是{self.area},' \ + f'其中的家具有{self.furnitures},剩余面积有{self.free_area}' + + +# 家具类 +class Furniture(): + def __init__(self, name, area): + # 家具名字 + self.name = name + # 家具占地面积 + self.area = area + + +# 家具对象 +bed = Furniture('双人床', 10) +sofa = Furniture('沙发', 5) + +# 房子对象 +home1 = Home('北京', 15) +home1.add_furnitures(bed, sofa) +print(home1) diff --git "a/18_\347\273\247\346\211\277/01_\347\273\217\345\205\270\347\261\273\345\222\214\346\226\260\345\274\217\347\261\273.py" "b/18_\347\273\247\346\211\277/01_\347\273\217\345\205\270\347\261\273\345\222\214\346\226\260\345\274\217\347\261\273.py" new file mode 100644 index 0000000000000000000000000000000000000000..ea8419290efcd94c93126b8da741f07e91c0d111 --- /dev/null +++ "b/18_\347\273\247\346\211\277/01_\347\273\217\345\205\270\347\261\273\345\222\214\346\226\260\345\274\217\347\261\273.py" @@ -0,0 +1,16 @@ +# 经典类 -- 无小括号 +class Student: + def study(self): + print('学生正在学习') + + +# 新式类 -- Python3.0以上 -- 有小括号,填入继承信息 -- 默认继承object +class Teacher(object): + def teach(self): + print('老师正在教书') + +stu = Student() +tea = Teacher() + +stu.study() +tea.teach() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/02_\344\275\223\351\252\214\347\273\247\346\211\277.py" "b/18_\347\273\247\346\211\277/02_\344\275\223\351\252\214\347\273\247\346\211\277.py" new file mode 100644 index 0000000000000000000000000000000000000000..0e4339646abe0a3778a8188fbd1e373152446c5a --- /dev/null +++ "b/18_\347\273\247\346\211\277/02_\344\275\223\351\252\214\347\273\247\346\211\277.py" @@ -0,0 +1,15 @@ +# 父类 Fu +class Fu(object): + def __init__(self): + self.num = 1 + + def info_print(self): + print(self.num) + +# 子类 Zi -- 默认继承父类的所有属性和方法 +class Zi(Fu): + pass + + +zi = Zi() +zi.info_print() # 1 diff --git "a/18_\347\273\247\346\211\277/03_\345\215\225\347\273\247\346\211\277.py" "b/18_\347\273\247\346\211\277/03_\345\215\225\347\273\247\346\211\277.py" new file mode 100644 index 0000000000000000000000000000000000000000..89ae7c52415b22ccb029b5a7fe9c384930e3c382 --- /dev/null +++ "b/18_\347\273\247\346\211\277/03_\345\215\225\347\273\247\346\211\277.py" @@ -0,0 +1,21 @@ +# 单继承 -- 一个子类,继承一个父类 + +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + +# 徒弟类 +class Prentice(Master): + pass + + +# 验证 +tudi = Prentice() + +print(tudi.kongfu) + +tudi.make_cake() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/04_\345\244\232\347\273\247\346\211\277.py" "b/18_\347\273\247\346\211\277/04_\345\244\232\347\273\247\346\211\277.py" new file mode 100644 index 0000000000000000000000000000000000000000..cdc6bdc711aa3238ebbb3f2e84f44912daf6ea2c --- /dev/null +++ "b/18_\347\273\247\346\211\277/04_\345\244\232\347\273\247\346\211\277.py" @@ -0,0 +1,28 @@ +# 多继承 -- 一个子类,继承多个父类 + +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + pass + +# 注意 -- 一个类继承多个父类时,默认使用第一个父类的同名属性和方法 +tudi = Prentice() +print(tudi.kongfu) # [古法煎饼果子配方] +tudi.make_cake() # 运用[古法煎饼果子配方]制作煎饼果子 \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/05_\345\255\220\347\261\273\351\207\215\345\206\231\347\210\266\347\261\273\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" "b/18_\347\273\247\346\211\277/05_\345\255\220\347\261\273\351\207\215\345\206\231\347\210\266\347\261\273\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" new file mode 100644 index 0000000000000000000000000000000000000000..d208a4bbfb507e24bb12dc76045529345de24411 --- /dev/null +++ "b/18_\347\273\247\346\211\277/05_\345\255\220\347\261\273\351\207\215\345\206\231\347\210\266\347\261\273\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" @@ -0,0 +1,28 @@ +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + +# 子类重写父类同名属性和方法 -- 使用子类自身的属性和方法 +tudi = Prentice() +print(tudi.kongfu) # [独创煎饼果子配方] +tudi.make_cake() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/06_\345\220\204\347\247\215\347\261\273\346\226\271\346\263\225.py" "b/18_\347\273\247\346\211\277/06_\345\220\204\347\247\215\347\261\273\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..6c594871dc32ad4fc45ef702309c16e76ca38e42 --- /dev/null +++ "b/18_\347\273\247\346\211\277/06_\345\220\204\347\247\215\347\261\273\346\226\271\346\263\225.py" @@ -0,0 +1,65 @@ +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + +tudi = Prentice() + +""" +1、__mro__: + 展示一个类的继承关系 +""" +print(Prentice.__mro__) + + +""" +2、__dict__: + 展示一个实例的属性字典 +""" +print(tudi.__dict__) + + +""" +3、__class__: + 输出一个对象所属的类 +""" +print(tudi.__class__) + + +""" +4、__bases__: + 展示一个类的父类 +""" +print(Prentice.__bases__) + + +""" +5、__base__: + 展示一个类的基类 -- 多继承时,看哪个类写在前面 +""" +print(Prentice.__base__) + + +""" +6、__subclasses__(): + 展示子类列表 +""" +print(Master.__subclasses__()) \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/07_\345\255\220\347\261\273\350\260\203\347\224\250\347\210\266\347\261\273\347\232\204\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" "b/18_\347\273\247\346\211\277/07_\345\255\220\347\261\273\350\260\203\347\224\250\347\210\266\347\261\273\347\232\204\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" new file mode 100644 index 0000000000000000000000000000000000000000..538fedda7d9ce524bb9f10dc3c6506b45fe26756 --- /dev/null +++ "b/18_\347\273\247\346\211\277/07_\345\255\220\347\261\273\350\260\203\347\224\250\347\210\266\347\261\273\347\232\204\345\220\214\345\220\215\346\226\271\346\263\225\345\222\214\345\261\236\346\200\247.py" @@ -0,0 +1,46 @@ +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + # 做独创煎饼果子 + def make_cake(self): + # 如果是先调用父类的属性和方法,会将子类的属性和方法覆盖 + # 所以在调用子类方法前,先调用子类的初始化 + self.__init__() + print(f'运用{self.kongfu}制作煎饼果子') + + # 做古法煎饼果子 + def make_master_cake(self): + # 调用父类方法,为了保证也调用到父类的属性,必须先调用父类的初始化方法 + Master.__init__(self) + Master.make_cake(self) + + # 做黑马煎饼果子 + def make_school_cake(self): + School.__init__(self) + School.make_cake(self) + +# 测试: +tudi = Prentice() + +tudi.make_cake() +tudi.make_school_cake() +tudi.make_master_cake() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/08_\345\244\232\345\261\202\347\273\247\346\211\277.py" "b/18_\347\273\247\346\211\277/08_\345\244\232\345\261\202\347\273\247\346\211\277.py" new file mode 100644 index 0000000000000000000000000000000000000000..5c7c0467d4c2d7c1b781cc70015def89df846b3b --- /dev/null +++ "b/18_\347\273\247\346\211\277/08_\345\244\232\345\261\202\347\273\247\346\211\277.py" @@ -0,0 +1,48 @@ +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + # 做独创煎饼果子 + def make_cake(self): + self.__init__() + print(f'运用{self.kongfu}制作煎饼果子') + + # 做古法煎饼果子 + def make_master_cake(self): + Master.__init__(self) + Master.make_cake(self) + + # 做黑马煎饼果子 + def make_school_cake(self): + School.__init__(self) + School.make_cake(self) + + +# 徒孙类 +class Tusun(Prentice): + pass + +# 多层继承 -- 默认继承父类所有的方法 +tusun = Tusun() +tusun.make_cake() +tusun.make_master_cake() +tusun.make_school_cake() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/09_super\346\226\271\346\263\225.py" "b/18_\347\273\247\346\211\277/09_super\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..3a6e5d080ed02932449cdab84307bec57bdcc1f1 --- /dev/null +++ "b/18_\347\273\247\346\211\277/09_super\346\226\271\346\263\225.py" @@ -0,0 +1,64 @@ +# 改变了继承关系:师傅 - 学校 - 徒弟 + +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(Master): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + # 做独创煎饼果子 + def make_cake(self): + self.__init__() + print(f'运用{self.kongfu}制作煎饼果子') + + # 做古法煎饼果子 + def make_master_cake(self): + Master.__init__(self) + Master.make_cake(self) + + # 做黑马煎饼果子 + def make_school_cake(self): + School.__init__(self) + School.make_cake(self) + + + # 调用父类的方法 -- 遵循__mro__顺序 + # 方法一:如果父类的类名修改,代码也要修改;代码量庞大 + def make_old_cake1(self): + Master.__init__(self) + Master.make_cake(self) + + School.__init__(self) + School.make_cake(self) + + # 方法二: + # 2.1 super(当前类名, self).函数() + def make_old_cake2(self): + super(Prentice,self).__init__() + super(Prentice,self).make_cake() + + # 2.2 super().函数() + def make_old_cake3(self): + super().__init__() + super().make_cake() + +# 测试 +tudi = Prentice() +tudi.make_old_cake2() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/10_\345\256\232\344\271\211\347\247\201\346\234\211\345\261\236\346\200\247\345\222\214\346\226\271\346\263\225.py" "b/18_\347\273\247\346\211\277/10_\345\256\232\344\271\211\347\247\201\346\234\211\345\261\236\346\200\247\345\222\214\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..9721236830b44577e043db7b073bb0a47ba7d6dd --- /dev/null +++ "b/18_\347\273\247\346\211\277/10_\345\256\232\344\271\211\347\247\201\346\234\211\345\261\236\346\200\247\345\222\214\346\226\271\346\263\225.py" @@ -0,0 +1,69 @@ +""" +设置私有权限的方法: + 在属性名和方法名的 前面 加上两个下划线__ +""" + +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + # 定义私有属性 + self.__money = 3000 + + # 定义私有方法 + def __info_print(self): + print(self.kongfu) + print(self.__money) + + # 做独创煎饼果子 + def make_cake(self): + self.__init__() + print(f'运用{self.kongfu}制作煎饼果子') + + # 做古法煎饼果子 + def make_master_cake(self): + Master.__init__(self) + Master.make_cake(self) + + # 做黑马煎饼果子 + def make_school_cake(self): + School.__init__(self) + School.make_cake(self) + + +# 徒孙类 +class Tusun(Prentice): + pass + + +tusun = Tusun() + +# error: print(tusun.__money) 私有属性无法继承 +print(tusun.kongfu) + +# error: tusun.__info_print() 私有方法无法继承 +tusun.make_cake() + +# 强制访问类的私有方法和私有属性 _Prentice__money 进行访问 +tudi = Prentice() +print(tudi._Prentice__money) +tudi._Prentice__info_print() \ No newline at end of file diff --git "a/18_\347\273\247\346\211\277/11_\350\216\267\345\217\226\345\222\214\344\277\256\346\224\271\347\247\201\346\234\211\345\261\236\346\200\247\345\200\274.py" "b/18_\347\273\247\346\211\277/11_\350\216\267\345\217\226\345\222\214\344\277\256\346\224\271\347\247\201\346\234\211\345\261\236\346\200\247\345\200\274.py" new file mode 100644 index 0000000000000000000000000000000000000000..9f0d5ba0f901db25dc6d8f68551d3f98917c3b2a --- /dev/null +++ "b/18_\347\273\247\346\211\277/11_\350\216\267\345\217\226\345\222\214\344\277\256\346\224\271\347\247\201\346\234\211\345\261\236\346\200\247\345\200\274.py" @@ -0,0 +1,70 @@ +# 私有属性 -- 可以在类里面被修改 + +# 师傅类 +class Master(object): + def __init__(self): + self.kongfu = '[古法煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 学校类 +class School(object): + def __init__(self): + self.kongfu = '[黑马煎饼果子配方]' + + def make_cake(self): + print(f'运用{self.kongfu}制作煎饼果子') + + +# 徒弟类 +class Prentice(Master, School): + def __init__(self): + self.kongfu = '[独创煎饼果子配方]' + + # 定义私有属性 + self.__money = 3000 + + # 获取私有属性 + def get_money(self): + return self.__money + + # 修改私有属性 + def set_money(self, money): + self.__money = money + + # 定义私有方法 + def __info_print(self): + print(self.kongfu) + print(self.__money) + + # 做独创煎饼果子 + def make_cake(self): + self.__init__() + print(f'运用{self.kongfu}制作煎饼果子') + + # 做古法煎饼果子 + def make_master_cake(self): + Master.__init__(self) + Master.make_cake(self) + + # 做黑马煎饼果子 + def make_school_cake(self): + School.__init__(self) + School.make_cake(self) + + +# 徒孙类 +class Tusun(Prentice): + pass + + +tusun = Tusun() + +print(tusun.get_money()) +# 修改私有属性 +tusun.set_money(9999) + +# 获取私有属性 +print(tusun.get_money()) \ No newline at end of file diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/01_\344\275\223\351\252\214\345\244\232\346\200\201.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/01_\344\275\223\351\252\214\345\244\232\346\200\201.py" new file mode 100644 index 0000000000000000000000000000000000000000..a9f5680d3ad5998daa275a8b8fe540f09a3a72db --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/01_\344\275\223\351\252\214\345\244\232\346\200\201.py" @@ -0,0 +1,31 @@ +# 狗 +class Dog(object): + def work(self): + pass + + +# 军犬 +class ArmyDog(Dog): + def work(self): + print('追击敌人...') + + +# 缉毒犬 +class DrugDog(Dog): + def work(self): + print('追查毒品...') + + +# 人 +class Person(object): + def work_with_dog(self, dog): # 传入的狗不同,执行不同的代码 + dog.work() + +# 测试代码: +armydog = ArmyDog() +drugdog = DrugDog() + +police = Person() + +police.work_with_dog(armydog) +police.work_with_dog(drugdog) \ No newline at end of file diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/02_\347\261\273\345\261\236\346\200\247.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/02_\347\261\273\345\261\236\346\200\247.py" new file mode 100644 index 0000000000000000000000000000000000000000..6fee6adc2e4193dee3148a387b0d9fc3cf8c2fc7 --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/02_\347\261\273\345\261\236\346\200\247.py" @@ -0,0 +1,38 @@ +""" +类属性: + 被该类的所有对象所共用 + 类属性,可以使用类名、实例对象来访问 + 类属性,只能通过类名来修改 -- 如果通过实例化对象修改,则表示创建一个实例属性 + +优点: + 实例属性 -- 每个对象单独开辟空间 + 类属性 -- 全部对象共用一个空间 -- 节省内存 +""" + +class Dog(object): + # 设置类属性 + tooth = 10 + +# 创建实例化对象 +dog1 = Dog() +dog2 = Dog() + +# 访问类属性 -- 类名、对象均可 +print(Dog.tooth) +print(dog1.tooth) +print(dog2.tooth) +print('===========') + +# 修改类属性 +Dog.tooth = 12 +print(Dog.tooth) +print(dog1.tooth) +print(dog2.tooth) +print('===========') + +# 如果通过实例化对象 -- 则不能修改 +dog1.tooth = 15 +print(Dog.tooth) +print(dog1.tooth) +print(dog2.tooth) +print('===========') diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/03_\347\261\273\346\226\271\346\263\225.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/03_\347\261\273\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..2b122d39bc31d41ba8cb108d8a33104651b55c61 --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/03_\347\261\273\346\226\271\346\263\225.py" @@ -0,0 +1,21 @@ +""" +类方法: + 1、用装饰器@classmethod -- 标识其为类方法 + 2、第一个参数必须为类对象 + +作用: + 访问私有类属性 +""" +class Dog(object): + __tooth = 10 + + @classmethod + def get_tooth(cls): + return cls.__tooth + + +num1 = Dog.get_tooth() +print(num1) # 10 + +dog1 = Dog() +print(dog1.get_tooth()) \ No newline at end of file diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/04_\351\235\231\346\200\201\346\226\271\346\263\225.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/04_\351\235\231\346\200\201\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..bd49bf18cf24530aaeccdfa05c925fc11ef8fde9 --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/04_\351\235\231\346\200\201\346\226\271\346\263\225.py" @@ -0,0 +1,15 @@ +""" +静态方法: + 1、通过装饰器@staticmethod 修饰 + 2、方法无需传递类对象、实例对象 -- 没有self、cls + 3、可以通过类名和实例对象访问 +""" +class Dog(object): + @staticmethod + def info_print(): + print('这是一个狗类...') + +dog1 = Dog() +# 类名、实例对象,均可访问静态方法 +dog1.info_print() +Dog.info_print() \ No newline at end of file diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/05_\347\261\273\345\244\226\346\267\273\345\212\240\346\226\271\346\263\225.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/05_\347\261\273\345\244\226\346\267\273\345\212\240\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..5cf6b8e239aa5711deb011cfa7507c7d6a6f510e --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/05_\347\261\273\345\244\226\346\267\273\345\212\240\346\226\271\346\263\225.py" @@ -0,0 +1,19 @@ +class Student: + def __init__(self, name, age): + self.name = name + self.age = age + + def eat(self): + print(self.name + '在吃饭') + +stu1 = Student('张三', 20) +stu2 = Student('李四', 30) + + +def show(): + print('定义在类之外的,称函数') + +# 类外动态添加成员方法 +stu1.show = show +stu1.show() +# error: stu2.show() # 因为stu2并没有绑定show方法 diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/06_dir\345\207\275\346\225\260.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/06_dir\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..6e8389dde16f0472b59015a04444fb6f620e4bb6 --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/06_dir\345\207\275\346\225\260.py" @@ -0,0 +1,28 @@ +""" +dir(对象): + 函数的参数是你传入的对象,它会返回对象的属性和方法。 +""" +print(dir("")) # 字符串的所有属性和方法 + +print(dir(())) # 查询元组的所有属性和方法 + +print(dir([])) # 查询列表的所有属性和方法 + +print(dir({})) # 查询字典的所有属性和方法 + + +class Student: + def __init__(self, name, age): + self.name = name + self.age = age + + def show(self): + print(f'{self.name}今年{self.age}岁了') + + +print(dir(Student)) # 查询Student的所有属性和方法 + +stu = Student("张三", 23) +stu.score = 100 # 动态添加成绩属性 +print(dir(stu)) + diff --git "a/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/07_\346\267\261\346\213\267\350\264\235\345\222\214\346\265\205\346\213\267\350\264\235.py" "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/07_\346\267\261\346\213\267\350\264\235\345\222\214\346\265\205\346\213\267\350\264\235.py" new file mode 100644 index 0000000000000000000000000000000000000000..334b52a43dcaaecc1a1e0e068b32d9e4120a2640 --- /dev/null +++ "b/19_\351\235\242\345\220\221\345\257\271\350\261\241_\345\205\266\344\273\226/07_\346\267\261\346\213\267\350\264\235\345\222\214\346\265\205\346\213\267\350\264\235.py" @@ -0,0 +1,41 @@ +# 教育机构 :马士兵教育 +# 讲 师:杨淑娟 + +class CPU: + pass + + +class Disk: + pass + + +class Computer: + def __init__(self, cpu, disk): + self.cpu = cpu + self.disk = disk + + +# 变量的赋值 +cpu1 = CPU() +cpu2 = cpu1 +print(cpu1, id(cpu1)) +print(cpu2, id(cpu2)) + +print('------------------------------') +disk = Disk() # 创建一个硬盘类的对象 +computer = Computer(cpu1, disk) # 创建一个计算机类的对象 + + +import copy + +# 浅拷贝 +print(disk) +computer2 = copy.copy(computer) +print(computer, computer.cpu, computer.disk) +print(computer2, computer2.cpu, computer2.disk) +print('----------------------------------------') + +# 深拷贝 +computer3 = copy.deepcopy(computer) +print(computer, computer.cpu, computer.disk) +print(computer3, computer3.cpu, computer3.disk) diff --git "a/20_\345\274\202\345\270\270/01_\344\275\223\351\252\214\345\274\202\345\270\270.py" "b/20_\345\274\202\345\270\270/01_\344\275\223\351\252\214\345\274\202\345\270\270.py" new file mode 100644 index 0000000000000000000000000000000000000000..b270ac5691b54dd3dc0133c337a07132654262d6 --- /dev/null +++ "b/20_\345\274\202\345\270\270/01_\344\275\223\351\252\214\345\274\202\345\270\270.py" @@ -0,0 +1,14 @@ +""" +try: + 可能发生错误的代码 +except: + 如果出现异常,要执行的代码 +""" + +# 需求:尝试以r模式打开文件,如果文件不存在,则以w方式打开 +try: + f = open('test.txt', 'r') +except: + f = open('test.txt', 'w') + +f.close() \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/02_\344\272\206\350\247\243\345\274\202\345\270\270\347\261\273\345\236\213.py" "b/20_\345\274\202\345\270\270/02_\344\272\206\350\247\243\345\274\202\345\270\270\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..88f876b3b0e754da323a62008359500d2d0507aa --- /dev/null +++ "b/20_\345\274\202\345\270\270/02_\344\272\206\350\247\243\345\274\202\345\270\270\347\261\273\345\236\213.py" @@ -0,0 +1,5 @@ +# NameError +print(num) + +# ZeroDivisionError +print(1 / 0) diff --git "a/20_\345\274\202\345\270\270/03_\346\215\225\350\216\267\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" "b/20_\345\274\202\345\270\270/03_\346\215\225\350\216\267\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..eb838bf785e5bffdd4296c1ecf3eb2ff12224635 --- /dev/null +++ "b/20_\345\274\202\345\270\270/03_\346\215\225\350\216\267\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" @@ -0,0 +1,17 @@ +""" +try: + 可能发生错误的代码 +except 异常类型: + 如果捕获到该异常类型,执行的代码 + +注意: + 1、如果尝试执行的代码的异常类型 和 要捕获的异常类型不一致,则无法捕获异常 + 2、一般try下方只放一行尝试执行的代码 +""" + +# 需求:尝试执行打印num,捕获异常类型NameError,如果捕获到这个类型,则提示有错误 +try: + # print(num) + print(1 / 0) # 此时无法捕获 +except NameError: + print('有错误') diff --git "a/20_\345\274\202\345\270\270/04_\346\215\225\350\216\267\345\244\232\344\270\252\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" "b/20_\345\274\202\345\270\270/04_\346\215\225\350\216\267\345\244\232\344\270\252\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..abebbd0bdd4b8405567e537d24f08ebc31f22c26 --- /dev/null +++ "b/20_\345\274\202\345\270\270/04_\346\215\225\350\216\267\345\244\232\344\270\252\346\214\207\345\256\232\345\274\202\345\270\270\347\261\273\345\236\213.py" @@ -0,0 +1,28 @@ +""" +try: + 可能发生错误的代码 +except (异常类型1,异常类型2,……): + 如果捕获到该异常类型,执行的代码 +""" +try: + print(1 / 0) +except (NameError, ZeroDivisionError): + print('有错误') + + +""" +try: + 语句块1 +except 异常处理类型1: + 语句块2 +except 异常处理类型2: + 语句块3 +except: #处理其他所有异常 + 语句块4 + +else: #当try正常运行时,执行else + 语句块5 +finally : #无论try是否异常,都执行finally + 语句块6 + +""" diff --git "a/20_\345\274\202\345\270\270/05_\346\215\225\350\216\267\345\274\202\345\270\270\346\217\217\350\277\260\344\277\241\346\201\257.py" "b/20_\345\274\202\345\270\270/05_\346\215\225\350\216\267\345\274\202\345\270\270\346\217\217\350\277\260\344\277\241\346\201\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..5eaec0c41a713fe896adab785ae30034024e8eb1 --- /dev/null +++ "b/20_\345\274\202\345\270\270/05_\346\215\225\350\216\267\345\274\202\345\270\270\346\217\217\350\277\260\344\277\241\346\201\257.py" @@ -0,0 +1,13 @@ +try: + print(num) +except (NameError, ZeroDivisionError, FileNotFoundError) as result: + print(result) # 捕获异常描述信息 +print("=================") + +import traceback + +try: + print('---------------------') + print(1 / 0) +except: + traceback.print_exc() # 可以更详细地知道报错信息 diff --git "a/20_\345\274\202\345\270\270/06_\346\215\225\350\216\267\346\211\200\346\234\211\345\274\202\345\270\270.py" "b/20_\345\274\202\345\270\270/06_\346\215\225\350\216\267\346\211\200\346\234\211\345\274\202\345\270\270.py" new file mode 100644 index 0000000000000000000000000000000000000000..45aecbc516bb395ddf5e4c7ca7b3635fcb704015 --- /dev/null +++ "b/20_\345\274\202\345\270\270/06_\346\215\225\350\216\267\346\211\200\346\234\211\345\274\202\345\270\270.py" @@ -0,0 +1,7 @@ +# Exception 是所有程序异常类的父类 + +# 需求:可以捕获所有异常 -- 不只是某些个别类型的异常 +try: + print(1/0) +except Exception as result: + print(result) \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/07_\345\274\202\345\270\270\347\232\204else.py" "b/20_\345\274\202\345\270\270/07_\345\274\202\345\270\270\347\232\204else.py" new file mode 100644 index 0000000000000000000000000000000000000000..6bb4944608fbc66b53286100c96593b912dfaf17 --- /dev/null +++ "b/20_\345\274\202\345\270\270/07_\345\274\202\345\270\270\347\232\204else.py" @@ -0,0 +1,7 @@ +# else 表示没有异常时,要执行的代码 +try: + print(1) +except Exception as e: + print(e) +else: + print('我是else,是没有异常时执行的代码') \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/08_\345\274\202\345\270\270\347\232\204finally.py" "b/20_\345\274\202\345\270\270/08_\345\274\202\345\270\270\347\232\204finally.py" new file mode 100644 index 0000000000000000000000000000000000000000..9796472c0066500d797bda46999ae30bfc69fad9 --- /dev/null +++ "b/20_\345\274\202\345\270\270/08_\345\274\202\345\270\270\347\232\204finally.py" @@ -0,0 +1,10 @@ +# finally: 表示无论是否异常都要执行的代码 -- 例如:关闭文件 +try: + f = open('test1.txt','r') +except Exception as e: + print(e) + f = open('test1.txt','w') +else: + print('没有异常,真开心') +finally: + f.close() \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/09_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" "b/20_\345\274\202\345\270\270/09_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" new file mode 100644 index 0000000000000000000000000000000000000000..9079fe2baff857b9b47a5dcd845d12150390c56e --- /dev/null +++ "b/20_\345\274\202\345\270\270/09_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" @@ -0,0 +1,25 @@ +""" +需求: + 1、尝试只读方式打开test.txt文件,如果文件存在,则读取文件内容,文件不存在则提示用户即可 + 2、读取内容的要求: + 尝试循环读取内容,如果读取过程中出现异常,则捕获并提醒用户 +""" +import time +try: + f = open('test.txt') + + # 开始读取数据 + try: + while True: + content = f.readline() + if len(content) == 0: + break + time.sleep(2) + print(content) + except: + # 读取过程中出现异常 + print('意外终止了读取数据') + finally: + f.close() +except: + print('没有该文件') \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/10_\350\207\252\345\256\232\344\271\211\345\274\202\345\270\270.py" "b/20_\345\274\202\345\270\270/10_\350\207\252\345\256\232\344\271\211\345\274\202\345\270\270.py" new file mode 100644 index 0000000000000000000000000000000000000000..cb99ad42411466edd67eec3f8d29a82301524509 --- /dev/null +++ "b/20_\345\274\202\345\270\270/10_\350\207\252\345\256\232\344\271\211\345\274\202\345\270\270.py" @@ -0,0 +1,28 @@ +# 需求:密码长度不足6位,则报异常 + +# 1、自定义异常类,继承Exception +class ShortInputError(Exception): + def __init__(self, length, min_len): + self.length = length + self.min_len = min_len + + # 设置抛出异常的描述信息 + def __str__(self): + return f'你输入的密码长度是{self.length},不能少于{self.min_len}个字符' + + +# 2、抛出异常 -- raise方法 +def main(): + try: + password = input('请输入密码:\n') + if len(password) < 6: + raise ShortInputError(len(password), 6) + + # 3、捕获该异常 + except Exception as e: + print(e) + + else: + print('该密码可用') + +main() diff --git "a/20_\345\274\202\345\270\270/test.txt" "b/20_\345\274\202\345\270\270/test.txt" new file mode 100644 index 0000000000000000000000000000000000000000..f70c2c05a9be25cc6a35bd2baa0f9eafe0ea8901 --- /dev/null +++ "b/20_\345\274\202\345\270\270/test.txt" @@ -0,0 +1,4 @@ +aaa +bbb +ccc +ddd \ No newline at end of file diff --git "a/20_\345\274\202\345\270\270/test1.txt" "b/20_\345\274\202\345\270\270/test1.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/21_\346\250\241\345\235\227/01_\345\257\274\345\205\245\346\250\241\345\235\227\347\232\204\346\226\271\345\274\217.py" "b/21_\346\250\241\345\235\227/01_\345\257\274\345\205\245\346\250\241\345\235\227\347\232\204\346\226\271\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..117824f77da2bc6764c7c0226455cdd0d2c5d0d2 --- /dev/null +++ "b/21_\346\250\241\345\235\227/01_\345\257\274\345\205\245\346\250\241\345\235\227\347\232\204\346\226\271\345\274\217.py" @@ -0,0 +1,34 @@ +""" +1、导入模块的方法一: + import 模块名 + +调用模块功能: + 模块名.功能名() +""" +import math +print(math.sqrt(9)) # 3 + + +""" +2、导入模块的方法二: + from 模块名 import 功能1,功能2,功能3... + +调用模块功能: + 功能名() +""" +from math import sqrt +print(sqrt(9)) + + +""" +3、导入模块的方法三: + from 模块名 import * + +调用模块功能: + 功能名() +""" +# 能导入模块中的所有功能 +from math import * +print(sqrt(9)) + + diff --git "a/21_\346\250\241\345\235\227/02_as\345\256\232\344\271\211\345\210\253\345\220\215.py" "b/21_\346\250\241\345\235\227/02_as\345\256\232\344\271\211\345\210\253\345\220\215.py" new file mode 100644 index 0000000000000000000000000000000000000000..ccf9858bdb4e60434abc14f2c8c3a1430c41063f --- /dev/null +++ "b/21_\346\250\241\345\235\227/02_as\345\256\232\344\271\211\345\210\253\345\220\215.py" @@ -0,0 +1,21 @@ +# 定义别名之后,原名就不能再使用 +""" +模块定义别名: + import 模块名 as 别名 + +功能定义别名: + from 模块名 import 功能名 as 别名 +""" + +# 模块别名 +import time as tt +tt.sleep(3) +# error: time.sleep(3) +print('Hello') + + +# 功能别名 +from time import sleep as s +s(3) +# error: sleep(3) +print('World') \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/03_\345\210\266\344\275\234\346\250\241\345\235\227.py" "b/21_\346\250\241\345\235\227/03_\345\210\266\344\275\234\346\250\241\345\235\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..5eaf68a4a51ae890b1803b96d397359dbcd671ff --- /dev/null +++ "b/21_\346\250\241\345\235\227/03_\345\210\266\344\275\234\346\250\241\345\235\227.py" @@ -0,0 +1,22 @@ +""" +1、定义模块: + my_module1.py + 模块名需要满足标识符命名规则 +""" + + +""" +2、测试模块 + 直接在my_module1.py里面测试 + 测试完成之后,测试信息需要进行处理 -- 否则导入模块的时候,会自动调用 +""" + + +""" +3、调用模块 + 导入模块 + 调用功能 +""" +import my_module1 + +my_module1.add(10, 30) diff --git "a/21_\346\250\241\345\235\227/04_\346\250\241\345\235\227\347\232\204\345\256\232\344\275\215\351\241\272\345\272\217.py" "b/21_\346\250\241\345\235\227/04_\346\250\241\345\235\227\347\232\204\345\256\232\344\275\215\351\241\272\345\272\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..30fde6b8b50d232c09ba890d7a1b3f8e98763b19 --- /dev/null +++ "b/21_\346\250\241\345\235\227/04_\346\250\241\345\235\227\347\232\204\345\256\232\344\275\215\351\241\272\345\272\217.py" @@ -0,0 +1,27 @@ +""" +导入一个模块 -- 系统查找该模块的位置 + 1、解析器先在当前目录找 + 2、再搜索PYTHONPATH下的每一个目录 + 3、查看默认路径 + +注意: + 1、文件名不要与已有模块名重复 -- 否则会使模块功能无法使用 + 2、from 模块名 import 功能名 -- 如果有多个同名的功能,则默认导入最后一个同名功能 +""" + +from time import sleep +def sleep(): + print('我先睡觉了...') + +# 无法调用time模块下的sleep功能 +sleep(3) + + +# 拓展:名字重复的严重性 +# 使用:import 模块名 -- 无需担心功能名重复 -- 但是... +import time +print(time) + +time = 1 +print(time) # 变量time 对 模块time 进行了覆盖 + diff --git "a/21_\346\250\241\345\235\227/05_all\345\210\227\350\241\250.py" "b/21_\346\250\241\345\235\227/05_all\345\210\227\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..23dba51d5460e1cefd04b9081619485cb5e2c10f --- /dev/null +++ "b/21_\346\250\241\345\235\227/05_all\345\210\227\350\241\250.py" @@ -0,0 +1,11 @@ +""" +__all__: + 如果模块文件中有__all__列表, + 当使用 from 模块名 import * 导入时, + 只能导入这个列表中的功能 +""" + +from my_module1 import * +testA() +# error: testB() +# error: testC() \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/06_\345\210\266\344\275\234\345\214\205.py" "b/21_\346\250\241\345\235\227/06_\345\210\266\344\275\234\345\214\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..5ec5c157b0dd279a49228c5a94f4da57912e9de0 --- /dev/null +++ "b/21_\346\250\241\345\235\227/06_\345\210\266\344\275\234\345\214\205.py" @@ -0,0 +1,41 @@ +""" +1、了解包: + 包是一个文件夹,里面将有联系的模块组织在一起。 + 包创建后,会自动生成__init__.py文件,控制包的导入行为 +""" + +""" +2、制作包: + my_package + 添加:my_module2, my_module3 +""" + +""" +3、导入包方法一: + import 包名.模块名 + +调用功能: + 包名.模块名.功能名 +""" +import my_package.my_module2 +my_package.my_module2.info_print2() + + +""" +4、导入包方法二: + from 包名 import * +调用功能: + 模块名.功能名 +注意: + 必须在__init__.py文件中添加__all__ = [], 控制允许导入的模块列表 + 否则无法导入模块 +""" +from my_package import * +my_module2.info_print2() +# error: my_module3.info_print2() + +""" +5、导入包方式三: + from 包名.模块名 import 方法名 + import 包名.模块名 as 自定义名 +""" \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/07_\345\220\204\347\247\215\346\250\241\345\235\227\347\232\204\344\275\277\347\224\250.py" "b/21_\346\250\241\345\235\227/07_\345\220\204\347\247\215\346\250\241\345\235\227\347\232\204\344\275\277\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..5a4a004dc90f7f5d38e7e807eedbbc8f533fb13f --- /dev/null +++ "b/21_\346\250\241\345\235\227/07_\345\220\204\347\247\215\346\250\241\345\235\227\347\232\204\344\275\277\347\224\250.py" @@ -0,0 +1,142 @@ +### 各种库的使用 ### + +### turtle库及其使用 ### +import turtle #引入turtle库 + +#海龟 默认 面向水平右方 处于画布最中央(0,0) +""" +turtle.setup(width, height, startx, starty) # 设置 画布的大小和位置 + width # 画布的宽度————整数 则为 像素值,小数 则为 窗口与画布的比例 + height # 画布的高度————整数 则为 像素值,小数 则为 窗口与画布的比例 + startx # 画布左侧 距离 左窗口的距离————可以不设,默认为在中央 + starty # 画布顶部 距离 窗口顶部的距离————可以不设,默认为在中央 +""" + + +turtle.penup() #抬起画笔,海龟移动不留痕迹————别名:turtle.pu()、turtle.up() +turtle.pendown() #落下画笔,海龟移动留痕————别名:turtle.pd()、turtle.down() + +turtle.pensize(25) #设置画笔的宽度 +a = turtle.width() #turtle.pensize()的别名,无参数时,返回当前画笔 宽度 + +turtle.pencolor("purple") #设置画笔的 颜色 +turtle.pencolor(51,204,140) #red\green\black 设置画笔颜色 + +turtle.begin_fill() #开始填充颜色 +turtle.fillcolor("purple") #给图形填充颜色 +turtle.color("white","black") +#同时设置pencolor、fillcolor +turtle.end_fill() #结束填充颜色 + +turtle.fd(-1) # 向前走-1步。别名:turtle.forward() +# turtle.goto(x,y) # 画笔移动到 (x,y) + +turtle.seth(-40) #调整龟头的朝向。别名:turtle.setheading() +turtle.right(30) #顺时针旋转 40度 +turtle.left(30) #逆时针旋转 30度 + +""" +turtle.circle(radius,extent,sides) #用来 画弧 + radius #表示半径。正数时,圆心在海龟左侧 ; 负数时,圆心在海龟右侧 + extent #表示度数 + sides #表示边数,用于绘多边形 +""" + + + + + +### datetime库的使用 ### +from datetime import datetime +today = datetime.now() +print(today) + +today_UTC = datetime.utcnow() #返回世界标准时间 +print(today_UTC) + +today_input = datetime(2022,9,15,23,55,43,1011)#用户自己输入的时间 +# 年 月 日 时 分 秒 微妙 +print(today_input) + +weekday = today.isoweekday() #返回星期 +print(weekday) + +day = today.strftime("%Y %m %d %H:%M:%S") #按特定格式输出时间————详见课本 +print(day) + + + +#### math()库的使用 #### +#函数库 分为————标准函数库/默认函数库————系统默认支持 +# ————需要进行安装的函数库 +##函数库的调用 +import math #调用数据库的方法A +math.ceil(10.2) #则需用此形式 来使用内置函数 + +from math import * #调用数据库的方法B +ceil(10.2) #则需用此形式 来使用内置函数 + +""" +## math()库中的 内置常数 + math.pi #圆周率 + math.e + math.inf #正无穷大 + -math.inf +""" + + +#### time()库的使用 #### +from time import * +time() #返回 自1970年至今的 秒数 +ctime() #返回 current时间 + +t = gmtime() #获取 各个单位的时间 +strftime("%Y-%m-%d %H:%M:%S",t) #按一定格式输出时间 + +timeStr = '2018-10-27 23:43:57' +strftime(timeStr,"%Y-%m-%d %H:%M:%S") #给时间解包,获取各个单位的时间 + +time.perf_counter() #开始计时 +time.sleep(0.3) #暂停 0.3秒 +t = time.perf_counter() #第二次调用,计时结束 + + + +### random库的使用 ### +from random import * #之后调用函数的时候,就不用再写 函数名称. + #从 random库里 引入 *(全部函数) +#导入random库 起一个别名 +import random as rd +import turtle +#随机生成 整数 +print(rd.randint(0,10)) +#随机生成 小数 +print(rd.uniform(0,10)) + +seed(125) #生成一个随机种子 +random() #生成[0.0 , 1.0)的小数 +randint(1,30) #生成[1,30]的整数 +randrange(1,40,3) #在range(1,40,3)中,随机取一个数 +uniform(1,20) #生成[1,20]的小数 +# choice(sequence) #从序列类型中,随机抽取一个 +# shuffle(sequence) #打乱 原序列类型 +getrandbits(16) #返回不大于 2**16 的整数 + + + +### jieba库的使用 ### +import jieba +text = '' +list = jieba.lcut(text) #对中文文本进行 词语划分,并以列表形式 返回——精确划分,不多不少 +list = jieba.lcut( text , cut_all = True) #增多型划分 +list = jieba.lcut_for_search(text) #搜索引擎式划分,对长词多次划分 + +jieba.add_word("牛逼") #向词典添加新词“牛逼” + + +### wordcloud库的使用 ### +import wordcloud +text = "you need python" +w = wordcloud.WordCloud() #创建对象,并设置相关参数 +w.generate(text) #进行加工 +w.to_file("pyw.png") #生成图片文件 diff --git "a/21_\346\250\241\345\235\227/08_Python\344\270\255\345\270\270\350\247\201\347\232\204\345\206\205\347\275\256\346\250\241\345\235\227.py" "b/21_\346\250\241\345\235\227/08_Python\344\270\255\345\270\270\350\247\201\347\232\204\345\206\205\347\275\256\346\250\241\345\235\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..a24ad02ae1ab958398888722c5ee1c15695a839e --- /dev/null +++ "b/21_\346\250\241\345\235\227/08_Python\344\270\255\345\270\270\350\247\201\347\232\204\345\206\205\347\275\256\346\250\241\345\235\227.py" @@ -0,0 +1,13 @@ +import sys +print(sys.getsizeof(24)) +print(sys.getsizeof(True)) # 返回对象所占的内存大小 + +import time +print(time.time()) # 获取时间戳 +print(time.localtime(time.time())) # 根据时间戳,返回当地时间 + +import urllib.request # 网络有关 +print(urllib.request.urlopen("http://www.baidu.com").read()) # 读取网页 + +import math +print(math.pi) \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/09_\347\254\254\344\270\211\346\226\271\346\250\241\345\235\227\347\232\204\345\256\211\350\243\205.py" "b/21_\346\250\241\345\235\227/09_\347\254\254\344\270\211\346\226\271\346\250\241\345\235\227\347\232\204\345\256\211\350\243\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..88835d1785ff7a5b5f8b5b984ae9b838ff50fbd3 --- /dev/null +++ "b/21_\346\250\241\345\235\227/09_\347\254\254\344\270\211\346\226\271\346\250\241\345\235\227\347\232\204\345\256\211\350\243\205.py" @@ -0,0 +1,10 @@ +import schedule + + +def job(): + print("哈哈~") + +schedule.every(3).seconds.do(job) # 每3秒执行一次job函数 + +while True: + schedule.run_pending() \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/my_module1.py" "b/21_\346\250\241\345\235\227/my_module1.py" new file mode 100644 index 0000000000000000000000000000000000000000..7c54e319e6250d35d5f1c813417c16fccac60a18 --- /dev/null +++ "b/21_\346\250\241\345\235\227/my_module1.py" @@ -0,0 +1,30 @@ +# 需求:一个函数,完成任意两个数字的加法运算 +def add(a, b): + print(a + b) + +def testA(): + print('testA') + +def testB(): + print('testB') + +def testC(): + print('testC') + + +# all列表 -- 作用详见 05_all列表 +__all__ = ['testA'] + + + +""" +__name__: + 是系统变量,是模块标识符。 + 如果是自身模块,值是__main__;否则是当前模块名 +""" +# 只在当前文件中执行if里的测试代码,若该文件导入其他文件中,则不执行测试代码 +if __name__ == '__main__': + # 测试代码 + add(10,20) + +print(__name__) \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/my_package/__init__.py" "b/21_\346\250\241\345\235\227/my_package/__init__.py" new file mode 100644 index 0000000000000000000000000000000000000000..c0becfe57511826620fda0a007466f69236d444a --- /dev/null +++ "b/21_\346\250\241\345\235\227/my_package/__init__.py" @@ -0,0 +1 @@ +__all__ = ['my_module2'] \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/my_package/my_module2.py" "b/21_\346\250\241\345\235\227/my_package/my_module2.py" new file mode 100644 index 0000000000000000000000000000000000000000..d245643fb632a51f86bf59463f8d2408eae4b83d --- /dev/null +++ "b/21_\346\250\241\345\235\227/my_package/my_module2.py" @@ -0,0 +1,4 @@ +print(2) + +def info_print2(): + print('my_module2') \ No newline at end of file diff --git "a/21_\346\250\241\345\235\227/my_package/my_module3.py" "b/21_\346\250\241\345\235\227/my_package/my_module3.py" new file mode 100644 index 0000000000000000000000000000000000000000..3c1bf67fc6d90adbaf53f0aaee276a8788cdc5e3 --- /dev/null +++ "b/21_\346\250\241\345\235\227/my_package/my_module3.py" @@ -0,0 +1,4 @@ +print(3) + +def info_print3(): + print('my_module3') \ No newline at end of file diff --git "a/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/ManagerSystem.py" "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/ManagerSystem.py" new file mode 100644 index 0000000000000000000000000000000000000000..6dd25ba1a5c34720a26a64b6bfec6a6c6205efb7 --- /dev/null +++ "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/ManagerSystem.py" @@ -0,0 +1,164 @@ +""" +需求: + 1、存储数据的位置:Student.data + + 2、系统功能: + 添加学生 + 删除学生 + 修改学生 + 查询学员信息 + 显示所有学生信息 + 退出系统 +""" +from Student import * + + +class StudentManager(object): + def __init__(self): + # 存储数据所用的列表 + self.students_list = [] + + # 一、程序入口函数,启动程序后执行的函数 + def run(self): + # 1、加载数据 + self.load_studnts() + + while True: + # 2、显示功能菜单 + self.show_menu() + + # 3、用户输入功能序号 + menu_num = eval(input('请输入您要选择的功能序号:\n')) + + # 4、根据用户输入的功能序号执行不同的功能 + if menu_num == 1: + # 添加学员 + self.add_student() + elif menu_num == 2: + # 删除学员 + self.del_student() + elif menu_num == 3: + # 修改学员 + self.modify_student() + elif menu_num == 4: + # 查询学员信息 + self.search_student() + elif menu_num == 5: + # 显示所有学员信息 + self.show_students() + elif menu_num == 6: + # 保存学员信息 + self.save_student() + elif menu_num == 7: + # 退出系统 + print('欢迎下次使用') + break + + def load_studnts(self): + """ 加载学生信息 """ + # 以r方式读取文件,文件存在则读取,不存在则创建 + try: + f = open('Student.data', 'r') + except: + f = open('Student.data', 'w') + else: + # 读取数据 + data = f.read() + + new_list = eval(data) + + self.students_list = [Student(dict['name'], dict['gender'], dict['phone']) for dict in new_list] + + finally: + f.close() + + @staticmethod + def show_menu(): + """ 显示功能菜单 -- 静态 """ + print('请选择如下功能----') + print('1、添加学员') + print('2、删除学员') + print('3、修改学员信息') + print('4、查询学员信息') + print('5、显示所有学员信息') + print('6、保存学员信息') + print('7、退出系统') + print('--------------') + + def add_student(self): + """ 添加学员 """ + # 1、用户输入姓名,性别,手机号 + name = input('请输入要添加的学生姓名:') + gender = input('请输入要添加的学生性别:') + phone = input('请输入要添加的学生手机号:') + + # 2、创建学生对象 -- 导入Student.py + stu = Student(name, gender, phone) + + # 3、将学生添加至列表 + self.students_list.append(stu) + + def del_student(self): + """ 删除学员 """ + # 1、用户输入要删除的学员姓名 + del_name = input('请输入您要删除的学生姓名:') + + # 2、存在则删除,不存在则提示 + for stu in self.students_list: + if del_name == stu.name: + self.students_list.remove(stu) + print('删除成功!') + break + else: + print('查无此人') + + def modify_student(self): + """ 修改学员信息 """ + # 1、用户输入要修改的学生姓名 + modify_name = input('请输入您要修改的学生姓名:') + + # 2、存在则修改,不存在则提示 + for stu in self.students_list: + if modify_name == stu.name: + stu.name = input('请输入修改之后的学生姓名:') + stu.gender = input('请输入修改之后的学生性别:') + stu.phone = input('请输入修改之后的学生手机号:') + + print(f'修改成功:姓名:{stu.name}, 性别:{stu.gender}, 手机号:{stu.phone}') + break + + else: + print('查无此人!') + + def search_student(self): + """ 查询学员信息 """ + # 1、用户输入要查询的学生姓名 + modify_name = input('请输入您要查询的学生姓名:') + + # 2、存在则显示,不存在则提示 + for stu in self.students_list: + if modify_name == stu.name: + print(f'姓名:{stu.name}, 性别:{stu.gender}, 手机号:{stu.phone}') + else: + print('查无此人!') + + def show_students(self): + """ 显示学员信息 """ + print('姓名\t性别\t手机号') + for stu in self.students_list: + print(f'{stu.name}\t{stu.gender}\t{stu.phone}') + + def save_student(self): + """ 保存学员信息 """ + # 1、打开文件 + f = open('Student.data', 'w') + + # 2、文件写入学员数据 + # 注意:文件写入的数据不能是学生对象的内存地址,需要把学生数据转化成字典数据存储 + new_list = [stu.__dict__ for stu in self.students_list] + + # 注意:文件内数据要求字符串类型 + f.write(str(new_list)) + + # 3、关闭文件 + f.close() diff --git "a/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.data" "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.data" new file mode 100644 index 0000000000000000000000000000000000000000..5752da0c0b0b5b1ef12e2b84ba629ed0d2dc16ac --- /dev/null +++ "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.data" @@ -0,0 +1 @@ +[{'name': 'smy', 'gender': '男', 'phone': '123'}, {'name': 'lyb', 'gender': '男', 'phone': '1234'}] \ No newline at end of file diff --git "a/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.py" "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.py" new file mode 100644 index 0000000000000000000000000000000000000000..3ca123890ebde6d4bb4353d4f635c85bc85d72ac --- /dev/null +++ "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/Student.py" @@ -0,0 +1,21 @@ +""" +学生信息: + 姓名、性别、手机号 + __str__魔法方法 +""" + + +class Student(object): + def __init__(self, name, gender, phone): + # 姓名、性别、手机号 + self.name = name + self.gender = gender + self.phone = phone + + def __str__(self): + return f'{self.name}, {self.gender}, {self.phone}' + +# 测试代码 +if __name__ == '__main__': + stu = Student('zhangsan', 'man', '119') + print(stu) diff --git "a/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/main.py" "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/main.py" new file mode 100644 index 0000000000000000000000000000000000000000..678f8702ee9b3b46d88016b45803bd7081ceb8ea --- /dev/null +++ "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/main.py" @@ -0,0 +1,7 @@ +# 1、导入ManagerSystem 模块 +from ManagerSystem import * + +# 2、启动学员管理系统 -- 在当前文件中才启动系统 +if __name__ == "__main__": + manager = StudentManager() + manager.run() \ No newline at end of file diff --git "a/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/\346\213\223\345\261\225_dict_.py" "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/\346\213\223\345\261\225_dict_.py" new file mode 100644 index 0000000000000000000000000000000000000000..1be8b493509c21d6ce1abc78bdf36b51ff0a68d0 --- /dev/null +++ "b/22_\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237_\351\235\242\345\220\221\345\257\271\350\261\241\347\211\210/\346\213\223\345\261\225_dict_.py" @@ -0,0 +1,16 @@ +# 1、定义类 +class A(object): + a = 0 + + def __init__(self): + self.b = 1 + +# 2、创建对象 +aa = A() + +# 3、调用__dict__ +# 返回实例属性和值组成的字典 +print(aa.__dict__) + +# 返回类内部所有属性和方法对应的字典 +print(A.__dict__) \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.DS_Store" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.DS_Store" new file mode 100644 index 0000000000000000000000000000000000000000..c6940a3f17e3985d94837fa8d71219aaeb81baf5 Binary files /dev/null and "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.DS_Store" differ diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/.gitignore" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..359bb5307e8535ab7d59faf27a7377033291821e --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/.gitignore" @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/Project_Default.xml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/Project_Default.xml" new file mode 100644 index 0000000000000000000000000000000000000000..9940ce427b4de987bf270c8bd92290d934e26cac --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/Project_Default.xml" @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/profiles_settings.xml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/profiles_settings.xml" new file mode 100644 index 0000000000000000000000000000000000000000..105ce2da2d6447d11dfe32bfb846c3d5b199fc99 --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/inspectionProfiles/profiles_settings.xml" @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/misc.xml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/misc.xml" new file mode 100644 index 0000000000000000000000000000000000000000..95701c4f97597e51484d1bcb36ba2c28aedf200a --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/misc.xml" @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/modules.xml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/modules.xml" new file mode 100644 index 0000000000000000000000000000000000000000..00abdedf5e892e0c0b394292fe45c015f03baaba --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/modules.xml" @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/workspace.xml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/workspace.xml" new file mode 100644 index 0000000000000000000000000000000000000000..7d6f2699b047c1d913d34ae151fb8037e7dc8702 --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/workspace.xml" @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "last_opened_file_path": "/Users/macbook/Documents/AI人工智能/01_Python基础-自学笔记/实战:贪吃蛇自动寻路" + } +} + + + + + 1696318270056 + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257.iml" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257.iml" new file mode 100644 index 0000000000000000000000000000000000000000..f024d007701967595d2b77b682577e2336867aa7 --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/.idea/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257.iml" @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/__pycache__/pathfinding.cpython-39.pyc" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/__pycache__/pathfinding.cpython-39.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..1b6c0cf9b004f576809ad7664c6fc7dad0190658 Binary files /dev/null and "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/__pycache__/pathfinding.cpython-39.pyc" differ diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/csdn\350\264\252\345\220\203\350\233\207\344\273\243\347\240\201.py" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/csdn\350\264\252\345\220\203\350\233\207\344\273\243\347\240\201.py" new file mode 100644 index 0000000000000000000000000000000000000000..9f10d08f6c80a22ab6d10d3a631e938afc004abf --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/csdn\350\264\252\345\220\203\350\233\207\344\273\243\347\240\201.py" @@ -0,0 +1,460 @@ +import pygame +import sys +import random +import time +from pygame.locals import * + +# 初始化Pygame库 +pygame.init() + +# 初始化一个游戏界面窗口 +DISPLAY = pygame.display.set_mode((300, 300)) + +# 设置游戏窗口的标题 +pygame.display.set_caption('贪吃蛇') + +# 定义一个变量来控制游戏速度 +FPSCLOCK = pygame.time.Clock() + +# 初始化游戏界面内使用的字体 +BASICFONT = pygame.font.SysFont("SIMYOU.TTF", 80) +font = pygame.font.SysFont('simsunnsimsun', 80) + +# 设置颜色 +BLACK = pygame.Color(0, 0, 0) +WHITE = pygame.Color(255, 255, 255) +RED = pygame.Color(255, 0, 0) +GREY = pygame.Color(150, 150, 150) +GREEN = pygame.Color(0, 255, 255) +YELLOW = pygame.Color(255, 255, 0) +FORESTGREEN = pygame.Color(34, 139, 34) +Purple3 = pygame.Color(125, 38, 205) + +# 贪吃蛇头的初始位置 +snake_Head = [100, 100] + +# 初始化贪吃蛇的身体 (注:这里以20*20为一个标准小格子) +snake_Body = [[80, 100], [60, 100], [40, 100]] + +# 指定蛇初始前进的方向 -- 向右 +direction = "RIGHT" + + +def randomfood(snake_Body): + """随机生成食物""" + + # 随机生成食物的位置 + x = random.randrange(1, 15) + y = random.randrange(1, 15) + Position = [int(x * 20), int(y * 20)] + + # 如果食物在贪吃蛇的身体里,则重新随机 + while Position in snake_Body: + x = random.randrange(1, 15) + y = random.randrange(1, 15) + Position = [int(x * 20), int(y * 20)] + return Position + + +# 随机出现第一个食物 +food_Position = randomfood(snake_Body) + +# 标记食物 +food_flag = 1 + + +def GameOver(): + """游戏结束""" + + # 设置GameOver的显示颜色与文字 + if (len(snake_Body) - 3) <= 10: + GameOver_Surf = font.render('太菜了', True, GREY) + elif 10 <= (len(snake_Body) - 3) <= 20: + GameOver_Surf = font.render('一般般', True, GREY) + else: + GameOver_Surf = font.render('很棒!', True, GREY) + + # 设置GameOver的位置 + GameOver_Rect = GameOver_Surf.get_rect() + GameOver_Rect.midtop = (320, 200) + + # 绑定以上设置到句柄 + DISPLAY.blit(GameOver_Surf, GameOver_Rect) + + # 刷新画面 + pygame.display.flip() + + # 等待3秒 + time.sleep(3) + # 退出游戏 + pygame.quit() + # 退出程序 + sys.exit() + + +def walkable(location0, snake_body0): + """返回下一步可以到达的位置""" + l = [] + + # 向右 + if location0[0] + 20 < 300 and [location0[0] + 20, location0[1]] not in snake_body0: + l.append([location0[0] + 20, location0[1]]) + + # 向左 + if location0[0] - 20 >= 0 and [location0[0] - 20, location0[1]] not in snake_body0: + l.append([location0[0] - 20, location0[1]]) + + # 向下 + if location0[1] + 20 < 300 and [location0[0], location0[1] + 20] not in snake_body0: + l.append([location0[0], location0[1] + 20]) + + # 向上 + if location0[1] - 20 >= 0 and [location0[0], location0[1] - 20] not in snake_body0: + l.append([location0[0], location0[1] - 20]) + + return l + + +def changexy(position0): + """改变位置""" + position = position0.copy() + return position[1] * 15 + position[0] + + +def BFS(food_position0, snake_body0): + # 获取食物的位置 + food_position = food_position0.copy() + # 获取贪吃蛇的身体信息 + snake_body = snake_body0.copy() + + # 是否可以吃到食物 + flag = 0 + + # 存储路径 + path = [] + + # 搜索栈,广度优先搜索 + search_stack = [] + + # {下一个位置:当前位置} + visited_dict = {} + search_stack.append([snake_body[0][0], snake_body[0][1]]) + visited_dict[changexy([snake_body[0][0], snake_body[0][1]])] = -1 + + # 进行广度优先遍历 + while len(search_stack) > 0: + # 获取贪吃蛇头当前位置 + currentlocation = search_stack.pop(0) + + # 如果当前没有吃到食物 + if currentlocation != food_position: + + # 记录下一步可以走到的位置,不考虑蛇身更新情况 + l1 = walkable(currentlocation, snake_body) + + # 遍历下一步的所有可能 + for i in l1: + if visited_dict.get(changexy(i)) == None: + search_stack.append(i) + visited_dict[changexy(i)] = currentlocation + + # 如果吃到该食物 + else: + flag = 1 + break + + # 添加食物位置进路径 + path.append(currentlocation) + + # 不断将前一个位置添加进路径 + while visited_dict[changexy(currentlocation)] != -1: + currentlocation = visited_dict[changexy(currentlocation)] + path.append(currentlocation) + + # 反转 --> 贪吃蛇的头到食物的路径 + path.reverse() + + return path, flag + + +def tailBFS(tail_position0, snake_body0): + # 蛇尾、蛇身 + tail_position = tail_position0.copy() + snake_body = snake_body0.copy() + + # 记录是否能到蛇尾 + flag = 0 + + # 记录到蛇尾的路径 + path = [] + + # 搜索栈 + search_stack = [] + + # {下一个位置:当前位置} + visited_dict = {} + + # 初始化 + search_stack.append([snake_body[0][0], snake_body[0][1]]) + visited_dict[changexy([snake_body[0][0], snake_body[0][1]])] = -1 + + # 开始广度优先遍历 + while len(search_stack) > 0: + # 获取当前位置 + currentlocation = search_stack.pop(0) + + if currentlocation != tail_position: + # l1存储下一步可走的位置,蛇尾是可到达的 + tail = snake_body.pop() + l1 = walkable(currentlocation, snake_body) + snake_body.append(tail) + + # 遍历各个可走位置 + for i1 in l1: + if visited_dict.get(changexy(i1)) == None: + # 更新搜索栈 + search_stack.append(i1) + # 记录位置点间的关系 + visited_dict[changexy(i1)] = currentlocation + else: + flag = 1 + break + + return flag + + +def exploretheway(food_position0, snake_body0): + # 获取蛇身与食物位置 + snake_body = snake_body0.copy() + food_position = food_position0.copy() + + while True: + # 获取觅食路径 + path, flag1 = BFS(food_position, snake_body) + + # 更新蛇头 + snake_body.insert(0, path[1]) + + # 如果吃到食物 + if snake_body[0][0] == food_position[0] and snake_body[0][1] == food_position[1]: + + # 当前位置是否能找到尾巴 + flag2 = tailBFS(snake_body[-1], snake_body) + + break + # 未吃到食物,则更新蛇尾 + else: + snake_body.pop() + return flag2 + + +def longest_tail_path(snake_body0): + """返回最长的蛇尾路径""" + + # 获取蛇身 + snake_body = snake_body0.copy() + + # 获取下一步可以走的路,蛇尾是可以走的 + tail = snake_body.pop() + l0 = walkable(snake_body[0], snake_body) + snake_body.append(tail) + + longest = -1 + + # 遍历下一步可能的情况,如A\B\C + # 再分别利用广度优先搜索,在A\B\C的情况下,找出到蛇尾的最短路径 + # 如果A情况下最短路径是最长的,则下一步往A处走 + for i0 in l0: + flag = 0 + path = [] + search_stack = [] + visited_dict = {} + + # 搜索栈 -- 广度优先遍历 + search_stack.append(i0) + + # {下一个位置:当前位置} + visited_dict[changexy(i0)] = -1 + + # 广度搜索 + while len(search_stack) > 0: + + # 更新当前位置 + currentlocation = search_stack.pop(0) + + # 如果不在蛇尾 + if currentlocation != snake_body[-1]: + + # l1记录所有下一位置的可能 + tail = snake_body.pop() + l1 = walkable(currentlocation, snake_body) + snake_body.append(tail) + + # 遍历下一位置的可能 + for j in l1: + if visited_dict.get(changexy(j)) == None: + + # 更新搜索栈 + search_stack.append(j) + + # 更新各位置的先后关系 + visited_dict[changexy(j)] = currentlocation + + else: + # 找到了一条到达蛇尾的路径 + flag = 1 + break + + # 记录路径信息 + path.append(currentlocation) + while visited_dict[changexy(currentlocation)] != -1: + currentlocation = visited_dict[changexy(currentlocation)] + path.append(currentlocation) + path.reverse() + + # 如果有更长的路径,则更新最长路径 + if flag == 1: + if len(path) > longest: + longest = len(path) + longest_path = path + + return longest_path + + +# 漫步函数,随机选择蛇头周围四个方向中可走的方向走一步 +def wander(snake_body0): + # 获取下一步可以走的路径 + l = walkable(snake_body0[0], snake_body0) + + # 随机走一步 + if len(l) > 1: + x = random.randrange(0, len(l)) + elif len(l) == 1: + x = 0 + else: + return l + return l[x] + +def drawSnake(snake_body0): + + # 画蛇头(x, y, width, length) + pygame.draw.rect(DISPLAY, Purple3, Rect( + snake_body0[0][0], snake_body0[0][1], 20, 20)) + + # 画蛇身 + for j in range(1, len(snake_body0)-1): + pygame.draw.rect(DISPLAY, FORESTGREEN, Rect( + snake_body0[j][0], snake_body0[j][1], 20, 20)) + + # 画蛇尾 + pygame.draw.rect(DISPLAY, GREEN, Rect( + snake_body0[-1][0], snake_body0[-1][1], 20, 20)) + +def drawFood(food_position0): + # 画食物 + pygame.draw.rect(DISPLAY, RED, Rect( + food_position0[0], food_position0[1], 20, 20)) + +def drawScore(score): + # 设置分数的显示颜色 + score_Surf = BASICFONT.render('%s' % (score), True, GREY) + # 设置分数的位置 + score_Rect = score_Surf.get_rect() + score_Rect.midtop = (50, 20) + # 绑定以上设置到句柄 + DISPLAY.blit(score_Surf, score_Rect) + +while True: + # 蛇身不动,蛇头动,是否可以吃到食物,返回最短路径 + food_path, food_flag = BFS(food_Position, snake_Body) + + # 蛇身不动,蛇头动,尾巴是否可到达 + tail_flag = tailBFS(snake_Body[-1], snake_Body) + + # 当前布局能找到吃food_path + if food_flag == 1: + # 虚拟蛇探路,吃到食物后如果能找到tail_path则返回1 + virtual_tail_flag = exploretheway(food_Position, snake_Body) + + # 虚拟蛇吃完食物后找得到tail_path + if virtual_tail_flag == 1: + print("安全路径吃食物") + + # 真实的蛇沿最短路径向食物走一步 + snake_Body.insert(0, food_path[1]) + + # 吃到食物,则重新随机食物位置 + if snake_Body[0][0] == food_Position[0] and snake_Body[0][1] == food_Position[1]: + food_Position = randomfood(snake_Body) + + # 未吃到食物,则更新蛇尾 + else: + snake_Body.pop() + + # 虚拟蛇吃完食物后找不到tail_path + else: + print("直接吃不安全,找蛇尾") + + # 真实的蛇沿最长路径向蛇尾走一步 + longest_path = longest_tail_path(snake_Body) + + # 更新蛇身 + snake_Body.insert(0, longest_path[0]) + if snake_Body[0][0] == food_Position[0] and snake_Body[0][1] == food_Position[1]: + food_Position = randomfood(snake_Body) + else: + snake_Body.pop() + + # 当前布局能找到tail_path(找不到food_path) + elif tail_flag == 1: + print("找到蛇尾") + # 真实的蛇沿最长路径向蛇尾走一步 + longest_path = longest_tail_path(snake_Body) + + # 更新蛇身 + snake_Body.insert(0, longest_path[0]) + snake_Body.pop() + + # 找不到food_path也找不到tail_path时。随机走一步 + elif food_flag != 1 and tail_flag != 1: + print("找不到食物也找不到蛇尾") + + # 无路可走 + if len(wander(snake_Body)) == 0: + print("走投无路") + GameOver() + break + + # 有路可走 + else: + snake_Body.insert(0, wander(snake_Body)) + snake_Body.pop() + + # 画布为黑色 + DISPLAY.fill(BLACK) + + # 画出贪吃蛇 + drawSnake(snake_Body) + + # 画出食物的位置 + drawFood(food_Position) + + # 打印出玩家的分数 + drawScore(len(snake_Body) - 3) + + # 刷新Pygame的显示层,贪吃蛇与食物的每一次移动,都会进行刷新显示层的操作来显示。 + pygame.display.flip() + + # 控制游戏速度 + FPSCLOCK.tick(20) + + # 蛇碰到边缘,游戏结束 + if snake_Body[0][0] < 0 or snake_Body[0][0] > 300: + GameOver() + if snake_Body[0][1] < 0 or snake_Body[0][1] > 300: + GameOver() + + # 贪吃蛇头触碰到自己,游戏结束 + for i in snake_Body[1:]: + if snake_Body[0][0] == i[0] and snake_Body[0][1] == i[1]: + GameOver() \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/main.py" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/main.py" new file mode 100644 index 0000000000000000000000000000000000000000..695d1e21c522f5191315d00eda257552f6efd039 --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/main.py" @@ -0,0 +1,7 @@ +import os + +def main(): + os.system("python snake.py para_a1 para_a2") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/pathfinding.py" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/pathfinding.py" new file mode 100644 index 0000000000000000000000000000000000000000..ec4ffc0f0c30ee5dbaeed41cde98d492dc30439a --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/pathfinding.py" @@ -0,0 +1,210 @@ +class GameGraph(): + """画面信息""" + def __init__(self, snake, food, scale_dict): + self.snake = snake + self.food = food + self.scale_dict = scale_dict + + def set_food(self, food): + """更新食物信息""" + self.food = food + + +def viable(snake, origin): + # 记录可走到的位置 + viable_positions = [] + + # 初始位置 + snake_head = origin + + # 向右 + if snake_head[0] + 10 <= 180 and ((snake_head[0] + 10, snake_head[1]) not in snake): + viable_positions.append((snake_head[0] + 10, snake_head[1])) + + # 向左 + if snake_head[0] - 10 >= -200 and ((snake_head[0] - 10, snake_head[1]) not in snake): + viable_positions.append((snake_head[0] - 10, snake_head[1])) + + # 向上 + if snake_head[1] + 10 <= 190 and ((snake_head[0], snake_head[1] + 10) not in snake): + viable_positions.append((snake_head[0], snake_head[1] + 10)) + + # 向下 + if snake_head[1] - 10 >= -190 and ((snake_head[0], snake_head[1] - 10) not in snake): + viable_positions.append((snake_head[0], snake_head[1] - 10)) + + return viable_positions + +# 我们用元组表示二维坐标,但元组不能作为字典的键,因此需要用 int 数据表示坐标 +def new_xy(position): + # return position[0]*15 + position[1] + return position + +def find_shortest_food_path(snake, food): + """寻找食物的最短路径""" + # 蛇头位置 + snake_head = snake[-1] + + # 是否找到食物 + food_flag = 0 + + # 存储找到食物的最短路径 + food_path = [food] + + # 是否找到蛇尾 + tail_flag = 0 + + ### 寻找食物 + # 搜索栈 -- 用于广度优先搜索 + search_stack = [snake_head] + + # 记录各个位置的先后关系 -- {当前位置:上一位置} + position_relation = {new_xy(snake_head): -1} + + while True: + # 获取当前蛇头下一步可走的位置 + origin = search_stack.pop(0) + viable_positions = viable(snake, origin) + + # 如果找到食物 + if origin[0] == food[0] and origin[1] == food[1]: + food_flag = 1 + break + + # 遍历下一步的可能位置 + for i in viable_positions: + # 如果该位置未被搜索过 + if position_relation.get(new_xy(i)) == None: + # 更新搜索栈 + search_stack.append(i) + # 更新位置关系 + position_relation[new_xy(i)] = origin + + # 如果找不到食物 + if len(search_stack) == 0: + break + + # 记录找到食物的最短路径 + if food_flag == 1: + while True: + p = position_relation[new_xy(food_path[-1])] + if p == -1: + break + else: + food_path.append(p) + + food_path.reverse() + + return food_flag, food_path + +def find_shortest_tail_path(snake): + """寻找蛇尾的最短路径""" + tail_flag, tail_path = find_shortest_food_path(snake[1:], snake[0]) + return tail_flag, tail_path + +def find_longest_path(snake): + """寻找蛇尾的最长路径""" + snake_head = snake[0] + + # 记录最长路径的长度 + longest = -1 + next_position = 0 + + # 搜索栈 -- 用于广度优先搜索 + search_stack = [snake_head] + + # 记录各个位置的先后关系 -- {当前位置:上一位置} + position_relation = {new_xy(snake_head): -1} + + # 获取当前蛇头下一步可走的位置 + origin = search_stack.pop(0) + viable_positions = viable(snake, origin) + + for i in viable_positions: + snake_temp = snake.copy() + snake_temp.append(i) + _, tail_path = find_shortest_tail_path(snake_temp) + + if len(tail_path) > longest: + next_position = i + longest = len(tail_path) + + return next_position + + +def exploation(snake_temp, food): + """虚拟蛇探路,看看吃到食物后,还能不能找到蛇尾""" + food_flag, food_path = find_shortest_food_path(snake_temp, food) + food_path.pop(0) + + while True: + p = food_path.pop(0) + snake_temp.append(p) + + if p[0] == food[0] and p[1] == food[1]: + break + + else: + snake_temp.pop(0) + + tail_flag, _ = find_shortest_tail_path(snake_temp) + return tail_flag + +def pathfinding(game_graph): + """寻找路径""" + # 蛇身信息 + snake = game_graph.snake + + # 蛇头坐标 + snake_head = game_graph.snake[-1] + + # 蛇尾坐标 + snake_tail = game_graph.snake[0] + + # 食物坐标 + food = game_graph.food + + # 找到最短食物路径 + food_flag, food_path = find_shortest_food_path(snake, food) + + # 如果当前存在 最短食物路径 + if food_flag == 1: + # 虚拟蛇探路,看看吃到食物后,还能不能找到蛇尾 + snake_temp = snake.copy() + tail_flag = exploation(snake_temp, food) + + if tail_flag == 1: + # 还能找到蛇尾,则这条路径可行 + print("路径安全,可以进食") + print("食物路径:",food_path) + # 更新蛇的位置 + food_path.pop(0) + p = food_path.pop(0) + snake.append(p) + snake.pop(0) + + else: + # 吃到食物过后,找不到蛇尾 -- 找到到达蛇尾的最长路径,向此方向走一步 + print('路径不安全,找蛇尾') + next_position = find_longest_path(snake) + snake.append(next_position) + snake.pop(0) + + else: + # 如果当前不存在,最短食物路径 + # 找到到达蛇尾的最长路径,向此方向走一步 + print('路径不安全,找蛇尾') + next_position = find_longest_path(snake) + snake.append(next_position) + snake.pop(0) + + # 返回方向 + return snake[-1][0] - snake_head[0], snake[-1][1] - snake_head[1] + + + + + + + + diff --git "a/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/snake.py" "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/snake.py" new file mode 100644 index 0000000000000000000000000000000000000000..c2a4a508fb999079632166c2fe2ade45986e8d6e --- /dev/null +++ "b/\345\256\236\346\210\230\357\274\232\350\264\252\345\220\203\350\233\207\350\207\252\345\212\250\345\257\273\350\267\257/snake.py" @@ -0,0 +1,158 @@ +from turtle import * +from random import * +from pathfinding import GameGraph, pathfinding + +def square(x, y, size, sq_color): + '''绘制小正方形, 代表一格''' + + # 将画笔移动到目标位置 + penup() + goto(x, y) + color(sq_color) + pendown() + + # 画小正方形,并填充颜色 + begin_fill() + for i in range(4): + forward(size) # 向前size个单位长度 + left(90) + end_fill() + +def frame(): + '''绘制边框''' + for i in range(-210, 200, 10): + square(i, -200, 10, 'gray') + square(i, 200, 10, 'gray') + for i in range(-200, 200, 10): + square(-210, i, 10, 'gray') + square(190, i, 10, 'gray') + +def change(direction): + '''改变蛇的运动方向''' + global aim_x, aim_y + x, y = direction + + if aim_x != -x or aim_y != -y: + aim_x = x + aim_y = y + +def inside(head_x, head_y): + '''判断蛇是否在边框内''' + if -210 < head_x < 190 and -200 < head_y < 200: + return True + else: + return False + +# 食物出现的所有可能位置 +all_food = [] +for x in range(-200, 190, 10): + for y in range(-190, 200, 10): + all_food.append((x, y)) + +def new_food(): + '''随机生成食物位置''' + # 食物可能出现的位置(去掉蛇所在的位置) + food = all_food.copy() + for i in set(snake): + food.remove(i) + + # 随机生成食物位置 + new_food_x, new_food_y = food.pop(randint(0, len(food) - 1)) + return new_food_x, new_food_y + +# 蛇的起始位置 +snake = [(0, 0)] + +# 食物的位置 +food_x, food_y = new_food() + +# 蛇的起始方向 -- 用位移向量表示 +aim_x, aim_y = 0, 0 + +def move(): + """移动""" + # 获取食物位置 + global food_x, food_y, snake + + # 获取图片画面 + game_graph = GameGraph(snake.copy(), (food_x, food_y), {'xmin': -200, 'xmax': 180, 'ymin': -190, 'ymax': 190}) + + # 更新下一步的前进方向 + aim_x, aim_y = pathfinding(game_graph) + + print('aim_x:', aim_x) + print('aim_y:', aim_y) + print('snake:', snake) + # 蛇头进行移动 + head_move_x = snake[-1][0] + aim_x + head_move_y = snake[-1][1] + aim_y + + # 如果撞到边框或者撞到自己 + if not inside(head_move_x, head_move_y) or (head_move_x, head_move_y) in snake: + # 绘制蛇头(红色) + square(head_move_x, head_move_y, 10, 'red') + + # 更新画面 + update() + print('得分: ', len(snake)) + return + + # 如果未发生碰撞,蛇头移动完成 + snake.append((head_move_x, head_move_y)) + + # 如果蛇吃到食物 + if head_move_x == food_x and head_move_y == food_y: + # 如果蛇跑满屏幕,则胜利 + if(len(snake) == len(all_food)): + print('YOU WIN!') + return + + # 否则,刷新食物位置 + else: + food_x, food_y = new_food() + + # 更新画面中的食物信息 + game_graph.set_food((food_x, food_y)) + + # 如果移动之后,没有吃到食物,蛇尾移动 + else: + snake.pop(0) + + # 清空窗口 + clear() + + # 绘制边框 + frame() + + # 绘制蛇身 + for body in snake: + square(body[0], body[1], 10, 'black') + + # 绘制食物 + square(food_x, food_y, 10, 'green') + + # 更新窗口 + update() + + # 每隔一段时间,调用move()函数 + ontimer(move) + +# 设置窗口大小 +setup(420, 420) + +# 设置窗口标题 +title('贪吃蛇') + +# 隐藏海龟 +hideturtle() + +# 关闭轨迹 +tracer(False) + + + +# 移动 +move() + +# 画面暂停 +done() \ No newline at end of file diff --git "a/\346\225\260\346\215\256\346\240\274\345\274\217\345\214\226.py" "b/\346\225\260\346\215\256\346\240\274\345\274\217\345\214\226.py" new file mode 100644 index 0000000000000000000000000000000000000000..b45d012d5adacfc9dfef0688d8ab582c8a761ae1 --- /dev/null +++ "b/\346\225\260\346\215\256\346\240\274\345\274\217\345\214\226.py" @@ -0,0 +1,23 @@ +## 数据格式化 ## + +""" +根据数据之间的关系不同,数据组织分为:一维数据、二维数据、高维数据 +一维数据————由对等关系的许多数据组成--线性的组织方式--如:数组、集合 +二维数据————通常用 表格 表示:一个大列表中,有许多小列表-- CSV格式:使用逗号分隔 +高维数据————用 键值对 灵活表示--如:JSON格式 +""" + +# CSV格式-->列表形式 # +fo = open('price2016.csv','r') #打开 CSV格式文件 +ls = [] +for line in fo: #一行行读取 + line = line.replace('\n','') #删除 \n换行符 + ls.append(line.split(',')) #生成一维列表 并加入到 最终列表ls中 +print(ls) +fo.close() + +# 列表形式-->CSV格式# +fo = open('price2022.csv','w') +ls = ['python','c++','java'] +fo.write(','.join(ls) + '\n') +fo.close() \ No newline at end of file