diff --git a/chat_bot_opencat.py b/00-chartgpt/chat_bot_opencat.py similarity index 100% rename from chat_bot_opencat.py rename to 00-chartgpt/chat_bot_opencat.py diff --git "a/01_\344\275\240\345\245\275Python/01_\346\263\250\351\207\212.py" "b/01_\344\275\240\345\245\275Python/01_\346\263\250\351\207\212.py" new file mode 100644 index 0000000000000000000000000000000000000000..031372b3308389667f777cd4dbc387f07f3d156c --- /dev/null +++ "b/01_\344\275\240\345\245\275Python/01_\346\263\250\351\207\212.py" @@ -0,0 +1,6 @@ +# 单行注释 + +""" +多行注释 +文档注释 +""" \ No newline at end of file diff --git "a/01_\344\275\240\345\245\275Python/02_\345\217\230\351\207\217.py" "b/01_\344\275\240\345\245\275Python/02_\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..31102ac358d38bc4d497019513ad613eb87418df --- /dev/null +++ "b/01_\344\275\240\345\245\275Python/02_\345\217\230\351\207\217.py" @@ -0,0 +1,8 @@ +""" +知识点:变量 +变量的定义格式: 变量名 = 变量值 +""" +# 定义一个变量:钱包, 用来存储余额数字 +money = 5 +# 查看一下,钱包还剩余多少钱 +print(money) diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/01_Python\344\270\255\347\232\204\345\255\227\351\235\242\351\207\217.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/01_Python\344\270\255\347\232\204\345\255\227\351\235\242\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..e7c930b57b7abe8e1035cac7fb9a1bc1b6a1cf05 --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/01_Python\344\270\255\347\232\204\345\255\227\351\235\242\351\207\217.py" @@ -0,0 +1,7 @@ +666 +13.14 +"黑马程序员" + +print(666) +print(13.14) +print("黑马程序员") \ No newline at end of file diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/02_Python\344\270\255\347\232\204\346\263\250\351\207\212.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/02_Python\344\270\255\347\232\204\346\263\250\351\207\212.py" new file mode 100644 index 0000000000000000000000000000000000000000..d6fc79704daa8c5e0c0b5190c7b18593e1e79dcd --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/02_Python\344\270\255\347\232\204\346\263\250\351\207\212.py" @@ -0,0 +1,21 @@ +name = """ +多行注释 +""" +print(name) + +# 写一个整数字面量 +666 +# 写一个浮点数字面量 +13.14 +# 写一个字符串字面量 +"黑马程序员" + +# 通过print语句输出各类字面量 +print(666) +print(13.14) +print("黑马程序员") + +# 单行注释 +# +# +"""多行注释""" diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/03_Python\344\270\255\347\232\204\345\217\230\351\207\217.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/03_Python\344\270\255\347\232\204\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..ce8b2585a22c7c29c1b7860bcc6bd7542fbc8d0e --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/03_Python\344\270\255\347\232\204\345\217\230\351\207\217.py" @@ -0,0 +1,32 @@ +""" +演示Python中变量的相关操作 +""" + +# 定义一个变量,用来记录钱包余额 +money = 50 +# 通过print语句,输出变量记录的内容 +print("钱包还有:", money) + +# 买了一个冰淇淋,花费10元 +money = money - 20 +print("买了冰淇淋花费10元,还剩余:", money, "元") + +# 假设,每隔一小时,输出一下钱包的余额 +print("现在是下午1点,钱包余额剩余:", money) +print("现在是下午2点,钱包余额剩余:", money) +print("现在是下午3点,钱包余额剩余:", money) +print("现在是下午4点,钱包余额剩余:", money) + + +Name = "张三" +Age = 11 + + +name = "张三" +age = 11 + + + + + + diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/04_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/04_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..c1c7e0f6983afb31723cbfc01f8a073ae1b669a1 --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/04_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213.py" @@ -0,0 +1,19 @@ +# 方式1: 使用print直接输出类型信息 +print(type("黑马程序员")) +print(type(666)) +print(type(11.345)) + + +# 方式2: 使用变量存储type()语句的结果 +string_type = type("黑马程序员") +int_type = type(666) +float_type = type(11.345) +print(string_type) +print(int_type) +print(float_type) + + +# 方式3: 使用type()语句,查看变量中存储的数据类型信息 +name = "黑马程序员" +name_type = type(name) +print(name_type) diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213\350\275\254\346\215\242.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213\350\275\254\346\215\242.py" new file mode 100644 index 0000000000000000000000000000000000000000..da729d1233e17e0116f1c49c45071ea71ce658e8 --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\225\260\346\215\256\347\261\273\345\236\213\350\275\254\346\215\242.py" @@ -0,0 +1,25 @@ +# 将数字类型转换成字符串 +num_str = str(11) +print(type(num_str), num_str) + +float_str = str(11.345) +print(type(float_str), float_str) +# 将字符串转换成数字 +num = int("11") +print(type(num), num) + +num2 = float("11.345") +print(type(num2), num2) + +# 错误示例,想要将字符串转换成数字,必须要求字符串内的内容都是数字 +# num3 = int("黑马程序员") +# print(type(num3), num3) + +# 整数转浮点数 +float_num = float(11) +print(type(float_num), float_num) + +# 浮点数转整数 +int_num = int(11.345) +print(type(int_num), int_num) + diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\240\207\350\257\206\347\254\246.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\240\207\350\257\206\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..cea94cda38f422766c970630a18db168bd3e7ffc --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/05_Python\344\270\255\347\232\204\346\240\207\350\257\206\347\254\246.py" @@ -0,0 +1,21 @@ +# 规则1:内容限定,限定只能使用:中文、英文、数字、下划线,注意:不能以数字开头 +# 错误的代码示范:1_name = "张三" +# 错误的代码示范:name_! = "张三" +name_ = "张三" +_name = "张三" +name_1 = "张三" + + +# 规则2:大小写敏感 +Itheima = "黑马程序员" +itheima = 666 +print(Itheima) +print(itheima) + + + +# 规则3:不可使用关键字 +# 错误的示例,使用了关键字:class = 1 +# 错误的示例,使用了关键字:def = 1 +Class = 1 + diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/06_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\213\274\346\216\245.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/06_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\213\274\346\216\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..d8cf612e3682aa706c254c70957c41b867af526e --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/06_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\213\274\346\216\245.py" @@ -0,0 +1,12 @@ +# 字符串字面量之间的拼接 +print("学IT来黑马" + "月薪过万") +# 字符串字面量和字符串变量的拼接 +name = "黑马程序员" +address = "建材城东路9号院" +tel = 4006189090 + +# 不报错 +print("我是:" + name + ",我的地址是:" + address + ",我的电话是:" + str(tel)) + +# 报错 +print("我是:" + name + ",我的地址是:" + address + ",我的电话是:" + tel) diff --git "a/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/07_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\345\214\226.py" "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/07_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\345\214\226.py" new file mode 100644 index 0000000000000000000000000000000000000000..15f07bcc271a79f670578dba53d9cae8542b43aa --- /dev/null +++ "b/02_Python\345\205\245\351\227\250\350\257\255\346\263\225/07_Python\347\232\204\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\345\214\226.py" @@ -0,0 +1,23 @@ +# 通过占位的形式,完成拼接 +name = "黑马程序员" +message = "学IT来:%s" % name +print(message) + +# 通过占位的形式,完成数字和字符串的拼接 +class_num = 57 +avg_salary = 16781 +message = "Python大数据学科,北京%s期,毕业平均工资:%s" % (class_num, avg_salary) +print(message) + +name = "传智播客" +setup_year = 2006 +stock_price = 19.99 +message = "%s,成立于:%d,我今天的股价是:%f" % (name, setup_year, stock_price) +print(message) + +num1 = 11 +num2 = 11.345 +print("数字11宽度限制5,结果是:%5d" % num1) +print("数字11宽度限制1,结果是:%1d" % num1) +print("数字11.345宽度限制7,小数精度2,结果是:%7.2f" % num2) +print("数字11.345不限制,小数精度2,结果是:%.2f" % num2) diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/01_\345\270\203\345\260\224\347\261\273\345\236\213\345\222\214\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/01_\345\270\203\345\260\224\347\261\273\345\236\213\345\222\214\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..563317fb4775be2c32882c0f53313e28db00591a --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/01_\345\270\203\345\260\224\347\261\273\345\236\213\345\222\214\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.py" @@ -0,0 +1,34 @@ +""" +演示布尔类型的定义 +以及比较运算符的应用 +""" +# 定义变量存储布尔类型的数据 +bool_1 = True +bool_2 = False +print(f"bool_1变量的内容是:{bool_1}, 类型是:{type(bool_1)}") +print(f"bool_2变量的内容是:{bool_2}, 类型是:{type(bool_2)}") +# 比较运算符的使用 +# == , !=, >, <, >=, <= +# 演示进行内容的相等比较 +num1 = 10 +num2 = 10 +print(f"10 == 10的结果是:{num1 == num2}") + +num1 = 10 +num2 = 15 +print(f"10 != 15的结果是:{num1 != num2}") + +name1 = "itcast" +name2 = "itheima" +print(f"itcast == itheima 结果是:{name1 == name2}") + +# 演示大于小于,大于等于小于等于的比较运算 +num1 = 10 +num2 = 5 +print(f"10 > 5结果是:{num1 > num2}") +print(f"10 < 5的结果是:{num1 < num2}") + +num1 = 10 +num2 = 11 +print(f"10 >= 11的结果是:{num1 >= num2}") +print(f"10 <= 11的结果是:{num1 <= num2}") diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_2_\346\210\220\345\271\264\344\272\272\345\210\244\346\226\255\347\273\203\344\271\240\351\242\230.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_2_\346\210\220\345\271\264\344\272\272\345\210\244\346\226\255\347\273\203\344\271\240\351\242\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..bb04a3459c746a22c3a8ad0522f92d96123f4935 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_2_\346\210\220\345\271\264\344\272\272\345\210\244\346\226\255\347\273\203\344\271\240\351\242\230.py" @@ -0,0 +1,12 @@ +""" +演示练习题:成年人判断 +""" + +# 获取键盘输入 +age = int(input("请输入你的年龄:")) + +# 通过if判断是否是成年人 +if age >= 18: + print("您已成年,游玩需要买票,10元.") + +print("祝您游玩愉快") diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_if\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\237\272\347\241\200\346\240\274\345\274\217.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_if\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\237\272\347\241\200\346\240\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..6bf742fd268056d8121b08f510f6ce7d637e3c4a --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/02_if\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\237\272\347\241\200\346\240\274\345\274\217.py" @@ -0,0 +1,10 @@ +""" +演示Python判断语句:if语句的基本格式应用 +""" +age = 10 + +if age >= 18: + print("我已经成年了") + print("即将步入大学生活") + +print("时间过的真快呀") diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_2_\346\210\221\350\246\201\344\271\260\347\245\250\345\220\227\347\273\203\344\271\240\351\242\230.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_2_\346\210\221\350\246\201\344\271\260\347\245\250\345\220\227\347\273\203\344\271\240\351\242\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..4bd62284d11e87d0cf1a487bcce5940a6d8acc28 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_2_\346\210\221\350\246\201\344\271\260\347\245\250\345\220\227\347\273\203\344\271\240\351\242\230.py" @@ -0,0 +1,15 @@ +""" +演示if else练习题:我要买票吗 +""" + +# 定义键盘输入获取身高数据 +height = int(input("请输入你的身高(cm):")) + +# 通过if进行判断 +if height > 120: + print("您的身高超出120CM,需要买票,10元。") +else: + print("您的身高低于120CM,可以免费游玩。") + +print("祝您游玩愉快") + diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_if else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_if else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..12a72b3a326d7413d447ae4813a4d09e95e9a39e --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/03_if else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" @@ -0,0 +1,13 @@ +""" +演示Python中 +if else的组合判断语句 +""" +age = int(input("请输入你的年龄:")) + +if age >= 18: + print("您已成年,需要买票10元。") +else: + print("您未成年,可以免费游玩。") + + + diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_2_\347\214\234\347\214\234\345\277\203\351\207\214\346\225\260\345\255\227\347\273\203\344\271\240\351\242\230.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_2_\347\214\234\347\214\234\345\277\203\351\207\214\346\225\260\345\255\227\347\273\203\344\271\240\351\242\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..e053fe478ede3c83318d15636511ec96b88eca83 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_2_\347\214\234\347\214\234\345\277\203\351\207\214\346\225\260\345\255\227\347\273\203\344\271\240\351\242\230.py" @@ -0,0 +1,18 @@ +""" +演示if elif else练习题:猜猜心里数字 +""" + +# 定义一个变量数字 +num = 5 + +# 通过键盘输入获取猜想的数字,通过多次if 和 elif的组合进行猜想比较 +if int(input("请猜一个数字:")) == num: + print("恭喜第一次就猜对了呢") +elif int(input("猜错了,再猜一次:")) == num: + print("猜对了") +elif int(input("猜错了,再猜一次:")) == num: + print("恭喜,最后一次机会,你猜对了") +else: + print("Sorry 猜错了") + + diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_if_elif_else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_if_elif_else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..5215e1f1843c06452805056b1b3c8cf9d15b74b2 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/04_if_elif_else\350\257\255\345\217\245\347\232\204\344\275\277\347\224\250.py" @@ -0,0 +1,17 @@ +""" +演示if elif else 多条件判断语句的使用 +""" + +# 通过if判断,可以使用多条件判断的语法 +# 第一个条件就是if +if int(input("请输入你的身高(cm):")) < 120: + print("身高小于120cm,可以免费。") +elif int(input("请输入你的VIP等级(1-5):")) > 3: + print("vip级别大于3,可以免费。") +elif int(input("请告诉我今天几号:")) == 1: + print("今天是1号免费日,可以免费") +else: + print("不好意思,条件都不满足,需要买票10元。") + + + diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\265\214\345\245\227.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\265\214\345\245\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..8d9b62fcaaa3cb6ae00e2e1f93b5d51266341be7 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/05_\345\210\244\346\226\255\350\257\255\345\217\245\347\232\204\345\265\214\345\245\227.py" @@ -0,0 +1,38 @@ +""" +演示判断语句的嵌套使用 +""" + +# if int(input("你的身高是多少:")) > 120: +# print("身高超出限制,不可以免费") +# print("但是,如果vip级别大于3,可以免费") +# +# if int(input("你的vip级别是多少:")) > 3: +# print("恭喜你,vip级别达标,可以免费") +# else: +# print("Sorry 你需要买票10元") +# else: +# print("欢迎小朋友,免费游玩。") + + +age = 11 +year = 1 +level = 1 +if age >= 18: + print("你是成年人") + if age < 30: + print("你的年龄达标了") + if year > 2: + print("恭喜你,年龄和入职时间都达标,可以领取礼物") + elif level > 3: + print("恭喜你,年龄和级别达标,可以领取礼物") + else: + print("不好意思,尽管年龄达标,但是入职时间和级别都不达标。") + + else: + print("不好意思,年龄太大了") + +else: + print("不好意思,小朋友不可以领取。") + + + diff --git "a/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/06_\345\210\244\346\226\255\350\257\255\345\217\245\345\256\236\346\210\230\346\241\210\344\276\213.py" "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/06_\345\210\244\346\226\255\350\257\255\345\217\245\345\256\236\346\210\230\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..55879b698e1cda6f6efcb6f7a3c0c49496058518 --- /dev/null +++ "b/03_Python\345\210\244\346\226\255\350\257\255\345\217\245/06_\345\210\244\346\226\255\350\257\255\345\217\245\345\256\236\346\210\230\346\241\210\344\276\213.py" @@ -0,0 +1,29 @@ +""" +演示判断语句的实战案例:终极猜数字 +""" +# 1. 构建一个随机的数字变量 +import random + +num = random.randint(1, 10) +guess_num = int(input("输入你要猜测的数字:")) +# 2. 通过if判断语句进行数字的猜测 +if guess_num == num: + print("恭喜,第一次就猜中了") +else: + if guess_num > num: + print("你猜测的数字大了") + else: + print("你猜测的数字小了") + guess_num = int(input("再次输入你要猜测的数字:")) + if guess_num == num: + print("恭喜,第二次猜中了") + else: + if guess_num > num: + print("你猜测的数字大了") + else: + print("你猜测的数字小了") + guess_num = int(input("第三次输入你要猜测的数字:")) + if guess_num == num: + print("第三次猜中了") + else: + print("三次机会用完了,没有猜中。") diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_2_while\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230_\346\261\2021-100\347\232\204\345\222\214.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_2_while\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230_\346\261\2021-100\347\232\204\345\222\214.py" new file mode 100644 index 0000000000000000000000000000000000000000..66454681786296b4926f5913122e4dd4848a49a8 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_2_while\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230_\346\261\2021-100\347\232\204\345\222\214.py" @@ -0,0 +1,10 @@ +""" +演示while循环基础练习题:求1-100的和 +""" +sum = 0 +i = 1 +while i <= 100: + sum += i + print(i) + i += 1 +print(f"1-100累加的和是:{sum}") diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..7462bbc1c7547a226d3c07e66b9c1c108985293a --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/01_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" @@ -0,0 +1,7 @@ +""" +演示while循环的基础应用 +""" +i = 0 +while i < 100: + print("小美,我喜欢你") + i += 1 diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/02_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\346\241\210\344\276\213-\347\214\234\346\225\260\345\255\227.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/02_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\346\241\210\344\276\213-\347\214\234\346\225\260\345\255\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..35512cf13bdd43249728cc006e167fe7b7139acf --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/02_while\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\346\241\210\344\276\213-\347\214\234\346\225\260\345\255\227.py" @@ -0,0 +1,27 @@ +""" +演示while循环的基础案例 - 猜数字 +""" + +# 获取范围在1-100的随机数字 +import random +num = random.randint(1, 100) +# 定义一个变量,记录总共猜测了多少次 +count = 0 + +# 通过一个布尔类型的变量,做循环是否继续的标记 +flag = True +while flag: + guess_num = int(input("请输入你猜测的数字:")) + count += 1 + if guess_num == num: + print("猜中了") + # 设置为False就是终止循环的条件 + flag = False + else: + if guess_num > num: + print("你猜的大了") + else: + print("你猜的小了") + +print(f"你总共猜测了{count}次") + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/03_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\345\272\224\347\224\250.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/03_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\345\272\224\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..32c89bf02d9a54c09c1fae2e2e347037604192a6 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/03_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\345\272\224\347\224\250.py" @@ -0,0 +1,22 @@ +""" +演示while循环的嵌套使用 +""" + +# 外层:表白100天的控制 +# 内层:每天的表白都送10只玫瑰花的控制 + +i = 1 +while i <= 100: + print(f"今天是第{i}天,准备表白.....") + + # 内层循环的控制变量 + j = 1 + while j <= 10: + print(f"送给小美第{j}只玫瑰花") + j += 1 + + print("小美,我喜欢你") + i += 1 + +print(f"坚持到第{i - 1}天,表白成功") + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/04_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\346\241\210\344\276\213-\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/04_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\346\241\210\344\276\213-\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..96fbf237e02e3a392faa10e7e71f51c12f6d04dc --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/04_while\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\346\241\210\344\276\213-\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" @@ -0,0 +1,21 @@ +""" +演示使用while的嵌套循环 +打印输出九九乘法表 +""" + +# 定义外层循环的控制变量 +i = 1 +while i <= 9: + + # 定义内层循环的控制变量 + j = 1 + while j <= i: + # 内层循环的print语句,不要换行,通过\t制表符进行对齐 + print(f"{j} * {i} = {j * i}\t", end='') + j += 1 + + i += 1 + print() # print空内容,就是输出一个换行 + + + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_2_for\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230-\346\225\260\344\270\200\346\225\260\346\234\211\345\207\240\344\270\252a.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_2_for\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230-\346\225\260\344\270\200\346\225\260\346\234\211\345\207\240\344\270\252a.py" new file mode 100644 index 0000000000000000000000000000000000000000..7bcd4f37c34b4299a764362436c0be175b10896a --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_2_for\345\276\252\347\216\257\347\273\203\344\271\240\351\242\230-\346\225\260\344\270\200\346\225\260\346\234\211\345\207\240\344\270\252a.py" @@ -0,0 +1,23 @@ +""" +演示for循环的练习题:数一数有几个a +""" +# 统计如下字符串中,有多少个字母a +name = "itheima is a brand of itcast" + +# 定义一个变量,用来统计有多少个a +count = 0 + +# for 循环统计 +# for 临时变量 in 被统计的数据: +for x in name: + if x == "a": + count += 1 + +print(f"被统计的字符串中有{count}个a") + + + + + + + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_for\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_for\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..3effdd8c4384c77155efbe07dd62a635351890e9 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/05_for\345\276\252\347\216\257\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" @@ -0,0 +1,13 @@ +""" +演示for循环的基础语法 +""" + +name = "itheima" + +for x in name: + # 将name的内容,挨个取出赋予x临时变量 + # 就可以在循环体内对x进行处理 + print(x) + + + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/06_range\350\257\255\345\217\245.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/06_range\350\257\255\345\217\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..b8e9ae610aa1d49d4b7ceeabe951e7df2857af41 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/06_range\350\257\255\345\217\245.py" @@ -0,0 +1,20 @@ +""" +演示Python中的range()语句的基本使用 +""" + +# range语法1 range(num) +# for x in range(10): +# print(x) + +# range 语法2 range(num1, num2) +# for x in range(5, 10): +# # 从5开始,到10结束(不包含10本身)的一个数字序列,数字之间间隔是1 +# print(x) + +# range 语法3 range(num1, num2, step) +# for x in range(5, 10, 2): +# # 从5开始,到10结束(不包含10本身)的一个数字序列,数字之间的间隔是2 +# print(x) + +for x in range(10): + print("送玫瑰花") diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/07_\345\217\230\351\207\217\344\275\234\347\224\250\345\237\237.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/07_\345\217\230\351\207\217\344\275\234\347\224\250\345\237\237.py" new file mode 100644 index 0000000000000000000000000000000000000000..b9c91d586bebec747fe560233d0442233b88763e --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/07_\345\217\230\351\207\217\344\275\234\347\224\250\345\237\237.py" @@ -0,0 +1,9 @@ +""" +演示Python for循环临时变量的作用域 +""" +i = 0 +for i in range(5): + print(i) + +print(i) + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_2_for\345\276\252\347\216\257\346\211\223\345\215\260\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_2_for\345\276\252\347\216\257\346\211\223\345\215\260\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..d1ad53928b22d2c5dd8db2bbcfd82914067864a6 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_2_for\345\276\252\347\216\257\346\211\223\345\215\260\344\271\235\344\271\235\344\271\230\346\263\225\350\241\250.py" @@ -0,0 +1,14 @@ +""" +演示for循环打印九九乘法表 +""" + +# 通过外层循环控制行数 +for i in range(1, 10): + # 通过内层循环控制每一行的数据 + for j in range(1, i + 1): + # 在内层循环中输出每一行的内容 + print(f"{j} * {i} = {j * i}\t", end='') + + # 外层循环可以通过print输出一个回车符 + print() + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_for\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\344\275\277\347\224\250.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_for\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\344\275\277\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..d8131f3ca252e89ddb889fe88292c4998d07e0b1 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/08_for\345\276\252\347\216\257\347\232\204\345\265\214\345\245\227\344\275\277\347\224\250.py" @@ -0,0 +1,20 @@ +""" +演示嵌套应用for循环 +""" + +# 坚持表白100天,每天都送10朵花 +# range +i = 0 +for i in range(1, 101): + print(f"今天是向小美表白的第{i}天,加油坚持。") + + # 写内层的循环了 + for j in range(1, 11): + print(f"给小美送的第{j}朵玫瑰花") + + print("小美我喜欢你") + +print(f"第{i}天,表白成功") + + + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/09_\345\276\252\347\216\257\344\270\255\346\226\255.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/09_\345\276\252\347\216\257\344\270\255\346\226\255.py" new file mode 100644 index 0000000000000000000000000000000000000000..1be148e68fe48774aa6fdd928a03f99c9753d1a5 --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/09_\345\276\252\347\216\257\344\270\255\346\226\255.py" @@ -0,0 +1,51 @@ +""" +演示循环语句的中断控制:break和continue +""" +import random +num = random.randint(1, 10) + + + +# 演示循环中断语句 continue +# for i in range(1, 6): +# print("语句1") +# continue +# print("语句2") + +# 演示continue的嵌套应用 +# for i in range(1, 6): +# print("语句1") +# for j in range(1, 6): +# print("语句2") +# continue +# print("语句3") +# +# print("语句4") + +# 演示循环中断语句 break +# for i in range(1, 101): +# print("语句1") +# break +# print("语句2") +# +# print("语句3") + + +# 演示break的嵌套应用 +for i in range(1, 6): + print("语句1") + for j in range(1, 6): + print("语句2") + break + print("语句3") + + print("语句4") + + + + + +print(num) + + + diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/10_\345\276\252\347\216\257\347\273\274\345\220\210\346\241\210\344\276\213-\345\217\221\345\267\245\350\265\204.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/10_\345\276\252\347\216\257\347\273\274\345\220\210\346\241\210\344\276\213-\345\217\221\345\267\245\350\265\204.py" new file mode 100644 index 0000000000000000000000000000000000000000..385907886f1f3545f34206ab9a85b2c1ce6f4a9a --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/10_\345\276\252\347\216\257\347\273\274\345\220\210\346\241\210\344\276\213-\345\217\221\345\267\245\350\265\204.py" @@ -0,0 +1,17 @@ +age = int(input("请输入您的年龄:")) +if age >= 18: + if age < 30: + print("年龄满足继续判断") + if int(input("请输入您入职已有多少年:")) > 2: + print("年龄符合且入职超过2年,满足条件,可以领取") + exit() + else: + print("入职时间不满足继续判断") + if int(input("请输入您的职位级别:")) > 3: + print("年龄符合且级别超过3级,满足条件,可以领取") + else: + print("不好意思,您不满足领取条件") + else: + print("不好意思,年龄大于30岁,不满足领取条件。") +else: + print("不好意思,年龄小于18岁,不满足领取条件。") \ No newline at end of file diff --git "a/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/test.py" "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..7b02e54c2be3573395977ac0cfe45e7771541a5f --- /dev/null +++ "b/04_Python\345\276\252\347\216\257\350\257\255\345\217\245/test.py" @@ -0,0 +1,6 @@ + +# print("Hello", end='') +# print("world", end='') + +print("Hello\tWorld") +print("itheima\tbest") diff --git "a/05_\345\207\275\346\225\260/01_\345\207\275\346\225\260\345\277\253\351\200\237\344\275\223\351\252\214.py" "b/05_\345\207\275\346\225\260/01_\345\207\275\346\225\260\345\277\253\351\200\237\344\275\223\351\252\214.py" new file mode 100644 index 0000000000000000000000000000000000000000..b0c05b5540d68e3c5016ac3b309302420eb16aa3 --- /dev/null +++ "b/05_\345\207\275\346\225\260/01_\345\207\275\346\225\260\345\277\253\351\200\237\344\275\223\351\252\214.py" @@ -0,0 +1,9 @@ +""" +演示:快速体验函数的开发及应用 +""" +# 需求,统计字符串的长度,不使用内置函数len() + +print("欢迎来到黑马程序员!请出示您的健康码以及72小时核酸证明,并配合测量体温!") +print("体温测量中,您的体温是:37.3度,体温正常请进!") + + diff --git "a/05_\345\207\275\346\225\260/02_\345\207\275\346\225\260\345\256\232\344\271\211.py" "b/05_\345\207\275\346\225\260/02_\345\207\275\346\225\260\345\256\232\344\271\211.py" new file mode 100644 index 0000000000000000000000000000000000000000..f54188b240f8977015c337c36e1988b16ddddfbc --- /dev/null +++ "b/05_\345\207\275\346\225\260/02_\345\207\275\346\225\260\345\256\232\344\271\211.py" @@ -0,0 +1,13 @@ +""" +演示函数的定义语法 +""" + +# 定义一个函数,输出相关信息 +def say_hi(): + print("Hi 我是黑马程序员,学Python来黑马") + +# 调用函数,让定义的函数开始工作 +say_hi() + + + diff --git "a/05_\345\207\275\346\225\260/03_\345\207\275\346\225\260\345\237\272\347\241\200\345\256\232\344\271\211\347\273\203\344\271\240\346\241\210\344\276\213.py" "b/05_\345\207\275\346\225\260/03_\345\207\275\346\225\260\345\237\272\347\241\200\345\256\232\344\271\211\347\273\203\344\271\240\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..944590fb89801e2e9075df76d5b06277d561f1b0 --- /dev/null +++ "b/05_\345\207\275\346\225\260/03_\345\207\275\346\225\260\345\237\272\347\241\200\345\256\232\344\271\211\347\273\203\344\271\240\346\241\210\344\276\213.py" @@ -0,0 +1,11 @@ +""" +演示函数基础定义练习案例:自动查核酸 +""" + +# 定义函数 +def check(): + # 编写函数体输出信息 + print("欢迎来到黑马程序员!\n请出示您的健康码以及72小时核酸证明!") + +# 调用函数 +check() diff --git "a/05_\345\207\275\346\225\260/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" "b/05_\345\207\275\346\225\260/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..95761ceb8eef568239ef94bf5ff462021d9aac2b --- /dev/null +++ "b/05_\345\207\275\346\225\260/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" @@ -0,0 +1,39 @@ +""" +演示对list列表的循环,使用while和for循环2种方式 +""" + + +def list_while_func(): + """ + 使用while循环遍历列表的演示函数 + :return: None + """ + my_list = ["传智教育", "黑马程序员", "Python"] + # 循环控制变量通过下标索引来控制,默认0 + # 每一次循环将下标索引变量+1 + # 循环条件:下标索引变量 < 列表的元素数量 + + # 定义一个变量用来标记列表的下标 + index = 0 # 初始值为0 + while index < len(my_list): + # 通过index变量取出对应下标的元素 + element = my_list[index] + print(f"列表的元素:{element}") + + # 至关重要 将循环变量(index)每一次循环都+1 + index += 1 + + +def list_for_func(): + """ + 使用for循环遍历列表的演示函数 + :return: None + """ + my_list = [1, 2, 3, 4, 5] + # for 临时变量 in 数据容器: + for element in my_list: + print(f"列表的元素有:{element}") + + +# list_while_func() +list_for_func() diff --git "a/05_\345\207\275\346\225\260/04_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260.py" "b/05_\345\207\275\346\225\260/04_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..017f53e2b3951434102854102ae644d6489eaccc --- /dev/null +++ "b/05_\345\207\275\346\225\260/04_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260.py" @@ -0,0 +1,11 @@ +""" +演示函数使用参数 +""" + +# 定义2数相加的函数,通过参数接收被计算的2个数字 +def add(x, y, z): + result = x + y + z + print(f"{x} + {y} + {z}的计算结果是:{result}") + +# 调用函数,传入被计算的2个数字 +add(5, 6, 7) diff --git "a/05_\345\207\275\346\225\260/05_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\347\273\203\344\271\240\346\241\210\344\276\213.py" "b/05_\345\207\275\346\225\260/05_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\347\273\203\344\271\240\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..91900936cdb07eb55118ec1dce2a14b3fdfd52a0 --- /dev/null +++ "b/05_\345\207\275\346\225\260/05_\345\207\275\346\225\260\347\232\204\345\217\202\346\225\260\347\273\203\344\271\240\346\241\210\344\276\213.py" @@ -0,0 +1,15 @@ +""" +演示函数的参数练习案例:升级自动查核酸 +""" + +# 定义函数,接收1个形式参数,数字类型,表示体温 +def check(num): + # 在函数体内进行判断体温 + print("欢迎来到黑马程序员!请出示您的健康码以及72小时核酸证明,并配合测量体温!") + if num <= 37.5: + print(f"体温测量中,您的体温是:{num}度,体温正常请进!") + else: + print(f"体温测量中,您的体温是:{num}度,需要隔离!") + +# 调用函数,传入实际参数 +check(37.6) diff --git "a/05_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274\345\256\232\344\271\211\350\257\255\346\263\225.py" "b/05_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274\345\256\232\344\271\211\350\257\255\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..859e805fca9567c3a65ffa1b02f8006eb19fd37f --- /dev/null +++ "b/05_\345\207\275\346\225\260/06_\345\207\275\346\225\260\347\232\204\350\277\224\345\233\236\345\200\274\345\256\232\344\271\211\350\257\255\346\263\225.py" @@ -0,0 +1,18 @@ +""" +演示:定义函数返回值的语法格式 +""" + + +# 定义一个函数,完成2数相加功能 +def add(a, b): + result = a + b + # 通过返回值,将相加的结果返回给调用者 + return result + # 返回结果后,还想输出一句话 + print("我完事了") + + +# 函数的返回值,可以通过变量去接收 +r = add(5, 6) +print(r) + diff --git "a/05_\345\207\275\346\225\260/07_None.py" "b/05_\345\207\275\346\225\260/07_None.py" new file mode 100644 index 0000000000000000000000000000000000000000..05efff0528e490bc3154bee39f4c7df1d095a332 --- /dev/null +++ "b/05_\345\207\275\346\225\260/07_None.py" @@ -0,0 +1,35 @@ +""" +演示特殊字面量:None +""" + +# 无return语句的函数返回值 +def say_hi(): + print("你好呀") + +result = say_hi() +print(f"无返回值函数,返回的内容是:{result}") +print(f"无返回值函数,返回的内容类型是:{type(result)}") + +# 主动返回None的函数 +def say_hi2(): + print("你好呀") + return None + +result = say_hi2() +print(f"无返回值函数,返回的内容是:{result}") +print(f"无返回值函数,返回的内容类型是:{type(result)}") +# None用于if判断 +def check_age(age): + if age > 18: + return "SUCCESS" + else: + return None + +result = check_age(16) +if not result: + # 进入if表示result是None值 也就是False + print("未成年,不可以进入") + + +# None用于声明无初始内容的变量 +name = None diff --git "a/05_\345\207\275\346\225\260/08_\345\207\275\346\225\260\347\232\204\350\257\264\346\230\216\346\226\207\346\241\243.py" "b/05_\345\207\275\346\225\260/08_\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..8c9c08e03bf304e38bb3991e66e742635494751e --- /dev/null +++ "b/05_\345\207\275\346\225\260/08_\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,18 @@ +""" +演示对函数进行文档说明 +""" + +# 定义函数,进行文档说明 +def add(x, y): + """ + add函数可以接收2个参数,进行2数相加的功能 + :param x: 形参x表示相加的其中一个数字 + :param y: 形参y表示相加的另一个数字 + :return: 返回值是2数相加的结果 + """ + result = x + y + print(f"2数相加的结果是:{result}") + return result + +add(5, 6) + diff --git "a/05_\345\207\275\346\225\260/09_\345\207\275\346\225\260\347\232\204\345\265\214\345\245\227\350\260\203\347\224\250.py" "b/05_\345\207\275\346\225\260/09_\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..5539aba8fafff4dde3ac63328594f70c8630d5c5 --- /dev/null +++ "b/05_\345\207\275\346\225\260/09_\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,17 @@ +""" +演示嵌套调用函数 +""" + +# 定义函数func_b +def func_b(): + print("---2---") +# 定义函数func_a,并在内部调用func_b +def func_a(): + print("---1---") + + # 嵌套调用func_b + func_b() + + print("---3---") +# 调用函数func_a +func_a() diff --git "a/05_\345\207\275\346\225\260/10_\345\217\230\351\207\217\347\232\204\344\275\234\347\224\250\345\237\237.py" "b/05_\345\207\275\346\225\260/10_\345\217\230\351\207\217\347\232\204\344\275\234\347\224\250\345\237\237.py" new file mode 100644 index 0000000000000000000000000000000000000000..ac69cc158911576bb884161c4fe2e8818ce0f90c --- /dev/null +++ "b/05_\345\207\275\346\225\260/10_\345\217\230\351\207\217\347\232\204\344\275\234\347\224\250\345\237\237.py" @@ -0,0 +1,55 @@ +""" +演示在函数使用的时候,定义的变量作用域 +""" + +# 演示局部变量 +# def test_a(): +# num = 100 +# print(num) +# +# +# test_a() +# 出了函数体,局部变量就无法使用了 +# print(num) + +# 演示全局变量 +# num = 200 +# +# def test_a(): +# print(f"test_a: {num}") +# +# def test_b(): +# print(f"test_b: {num}") +# +# test_a() +# test_b() +# print(num) + +# 在函数内修改全局变量 +# num = 200 +# +# def test_a(): +# print(f"test_a: {num}") +# +# def test_b(): +# num = 500 # 局部变量 +# print(f"test_b: {num}") +# +# test_a() +# test_b() +# print(num) + +# global关键字,在函数内声明变量为全局变量 +num = 200 + +def test_a(): + print(f"test_a: {num}") + +def test_b(): + global num # 设置内部定义的变量为全局变量 + num = 500 + print(f"test_b: {num}") + +test_a() +test_b() +print(num) diff --git "a/05_\345\207\275\346\225\260/11_\345\207\275\346\225\260\347\273\274\345\220\210\346\241\210\344\276\213.py" "b/05_\345\207\275\346\225\260/11_\345\207\275\346\225\260\347\273\274\345\220\210\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..12446ea3a9197cb053e431906292a5c65a1e163a --- /dev/null +++ "b/05_\345\207\275\346\225\260/11_\345\207\275\346\225\260\347\273\274\345\220\210\346\241\210\344\276\213.py" @@ -0,0 +1,62 @@ +""" +演示函数综合案例开发 +""" + +# 定义全局变量money name +money = 5000000 +name = None +# 要求客户输入姓名 +name = input("请输入您的姓名:") +# 定义查询函数 +def query(show_header): + if show_header: + print("-------------查询余额------------") + print(f"{name},您好,您的余额剩余:{money}元") + + +# 定义存款函数 +def saving(num): + global money # money在函数内部定义为全局变量 + money += num + print("-------------存款------------") + print(f"{name},您好,您存款{num}元成功。") + + # 调用query函数查询余额 + query(False) + +# 定义取款函数 +def get_money(num): + global money + money -= num + print("-------------取款------------") + print(f"{name},您好,您取款{num}元成功。") + + # 调用query函数查询余额 + query(False) +# 定义主菜单函数 +def main(): + print("-------------主菜单------------") + print(f"{name},您好,欢迎来到黑马银行ATM。请选择操作:") + print("查询余额\t[输入1]") + print("存款\t\t[输入2]") + print("取款\t\t[输入3]") # 通过\t制表符对齐输出 + print("退出\t\t[输入4]") + return input("请输入您的选择:") + +# 设置无限循环,确保程序不退出 +while True: + keyboard_input = main() + if keyboard_input == "1": + query(True) + continue # 通过continue继续下一次循环,一进来就是回到了主菜单 + elif keyboard_input == "2": + num = int(input("您想要存多少钱?请输入:")) + saving(num) + continue + elif keyboard_input == "3": + num = int(input("您想要取多少钱?请输入:")) + get_money(num) + continue + else: + print("程序退出啦") + break # 通过break退出循环 diff --git "a/05_\345\207\275\346\225\260/test.py" "b/05_\345\207\275\346\225\260/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..cd24844875f3e07f8fabe68c3b91de8d42704a0c --- /dev/null +++ "b/05_\345\207\275\346\225\260/test.py" @@ -0,0 +1,17 @@ + +print("----------------主菜单------------------") +print("周杰轮,您好,欢迎来到黑马银行ATM。请选择操作:") +print("查询余额\t[输入1]") +print("存款\t\t[输入2]") +print("取款\t\t[输入3]") +print("退出\t\t[输入4]") +input("请输入您的选择:") + +print() +print("----------------查询余额------------------") +print("周杰轮,您好,您的余额剩余:5000000元") +print("------------------------------") + +print("----------------取款------------------") +print("周杰轮,您好,您取款50000元成功") +print("周杰轮,您好,您的余额剩余:4950000元") diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/01_\345\207\275\346\225\260\347\232\204\345\244\232\350\277\224\345\233\236\345\200\274.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/01_\345\207\275\346\225\260\347\232\204\345\244\232\350\277\224\345\233\236\345\200\274.py" new file mode 100644 index 0000000000000000000000000000000000000000..c4d0209b3909bbb837981ca39119ad52d4e43b85 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/01_\345\207\275\346\225\260\347\232\204\345\244\232\350\277\224\345\233\236\345\200\274.py" @@ -0,0 +1,14 @@ +""" +演示函数的多返回值示例 +""" + + +# 演示使用多个变量,接收多个返回值 +def test_return(): + return 1, "hello", True + + +x, y, z = test_return() +print(x) +print(y) +print(z) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/02_\345\207\275\346\225\260\345\217\202\346\225\260\347\232\204\345\244\232\347\247\215\344\275\277\347\224\250\345\275\242\345\274\217.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/02_\345\207\275\346\225\260\345\217\202\346\225\260\347\232\204\345\244\232\347\247\215\344\275\277\347\224\250\345\275\242\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..29bff8b142be9e5532458a908a210d9bd9d7637e --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/02_\345\207\275\346\225\260\345\217\202\346\225\260\347\232\204\345\244\232\347\247\215\344\275\277\347\224\250\345\275\242\345\274\217.py" @@ -0,0 +1,31 @@ +""" +演示多种传参的形式 +""" +def user_info(name, age, gender): + print(f"姓名是:{name}, 年龄是:{age}, 性别是:{gender}") +# 位置参数 - 默认使用形式 +user_info('小明', 20, '男') + +# 关键字参数 +user_info(name='小王', age=11, gender='女') +user_info(age=10, gender='女', name='潇潇') # 可以不按照参数的定义顺序传参 +user_info('甜甜', gender='女', age=9) + +# 缺省参数(默认值) +def user_info(name, age, gender): + print(f"姓名是:{name}, 年龄是:{age}, 性别是:{gender}") + +user_info('小天', 13, '男') + + +# 不定长 - 位置不定长, *号 +# 不定长定义的形式参数会作为元组存在,接收不定长数量的参数传入 +def user_info(*args): + print(f"args参数的类型是:{type(args)},内容是:{args}") + +user_info(1, 2, 3, '小明', '男孩') + +# 不定长 - 关键字不定长, **号 +def user_info(**kwargs): + print(f"args参数的类型是:{type(kwargs)},内容是:{kwargs}") +user_info(name='小王', age=11, gender='男孩') diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/03_\345\207\275\346\225\260\344\275\234\344\270\272\345\217\202\346\225\260\344\274\240\351\200\222.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/03_\345\207\275\346\225\260\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..99046819aa1a3a94ad75577f277b565fa6402c46 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/03_\345\207\275\346\225\260\344\275\234\344\270\272\345\217\202\346\225\260\344\274\240\351\200\222.py" @@ -0,0 +1,15 @@ +""" +演示函数作为参数传递 +""" + +# 定义一个函数,接收另一个函数作为传入参数 +def test_func(compute): + result = compute(1, 2) # 确定compute是函数 + print(f"compute参数的类型是:{type(compute)}") + print(f"计算结果:{result}") + +# 定义一个函数,准备作为参数传入另一个函数 +def compute(x, y): + return x + y +# 调用,并传入函数 +test_func(compute) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/04_lambda\345\214\277\345\220\215\345\207\275\346\225\260.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/04_lambda\345\214\277\345\220\215\345\207\275\346\225\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..45cb849d2d7d822879e378062c3843d75407b06f --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/04_lambda\345\214\277\345\220\215\345\207\275\346\225\260.py" @@ -0,0 +1,18 @@ +""" +演示lambda匿名函数 +""" + + +# 定义一个函数,接受其它函数输入 +def test_func(compute): + result = compute(1, 2) + print(f"结果是:{result}") + + +# 通过lambda匿名函数的形式,将匿名函数作为参数传入 +def add(x, y): + return x + y + + +test_func(add) +test_func(lambda x, y: x + y) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..769aa8d59a841616e71a12d3bbc9f1624b577a86 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test.py" @@ -0,0 +1,8 @@ + +def compute(add): + add(1, 2) + +def add(x, y): + print(x + y) + +compute(lambda x, y: print(x + y)) \ No newline at end of file diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..5aa8f3e8ad8273205e1d6e976c56e29c474f4251 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" @@ -0,0 +1,36 @@ +""" +演示数据容器之:list列表 +语法:[元素,元素,....] +""" +# 定义一个列表 list +my_list = ["itheima", "itcast", "python"] +print(my_list) +print(type(my_list)) + +my_list = ["itheima", 666, True] +print(my_list) +print(type(my_list)) + +# 定义一个嵌套的列表 +my_list = [ [1, 2, 3], [4, 5, 6]] +print(my_list) +print(type(my_list)) + +# 通过下标索引取出对应位置的数据 +my_list = ["Tom", "Lily", "Rose"] +# 列表[下标索引], 从前向后从0开始,每次+1, 从后向前从-1开始,每次-1 +print(my_list[0]) +print(my_list[1]) +print(my_list[2]) +# 错误示范;通过下标索引取数据,一定不要超出范围 +# print(my_list[3]) + +# 通过下标索引取出数据(倒序取出) +print(my_list[-1]) +print(my_list[-2]) +print(my_list[-3]) + + +# 取出嵌套列表的元素 +my_list = [ [1, 2, 3], [4, 5, 6]] +print(my_list[1][1]) diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" new file mode 100644 index 0000000000000000000000000000000000000000..c8595b1b90b7d7e1df186270939e8611ee1e445d --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" @@ -0,0 +1,57 @@ +""" +演示数据容器之:list列表的常用操作 +""" +mylist = ["itcast", "itheima", "python"] +# 1.1 查找某元素在列表内的下标索引 +index = mylist.index("itheima") +print(f"itheima在列表中的下标索引值是:{index}") +# 1.2如果被查找的元素不存在,会报错 +# index = mylist.index("hello") +# print(f"hello在列表中的下标索引值是:{index}") + +# 2. 修改特定下标索引的值 +mylist[0] = "传智教育" +print(f"列表被修改元素值后,结果是:{mylist}") +# 3. 在指定下标位置插入新元素 +mylist.insert(1, "best") +print(f"列表插入元素后,结果是:{mylist}") +# 4. 在列表的尾部追加```单个```新元素 +mylist.append("黑马程序员") +print(f"列表在追加了元素后,结果是:{mylist}") +# 5. 在列表的尾部追加```一批```新元素 +mylist2 = [1, 2, 3] +mylist.extend(mylist2) +print(f"列表在追加了一个新的列表后,结果是:{mylist}") +# 6. 删除指定下标索引的元素(2种方式) +mylist = ["itcast", "itheima", "python"] + +# 6.1 方式1:del 列表[下标] +del mylist[2] +print(f"列表删除元素后结果是:{mylist}") +# 6.2 方式2:列表.pop(下标) +mylist = ["itcast", "itheima", "python"] +element = mylist.pop(2) +print(f"通过pop方法取出元素后列表内容:{mylist}, 取出的元素是:{element}") +# 7. 删除某元素在列表中的第一个匹配项 +mylist = ["itcast", "itheima", "itcast", "itheima", "python"] +mylist.remove("itheima") +print(f"通过remove方法移除元素后,列表的结果是:{mylist}") + +# 8. 清空列表 +mylist.clear() +print(f"列表被清空了,结果是:{mylist}") +# 9. 统计列表内某元素的数量 +mylist = ["itcast", "itheima", "itcast", "itheima", "python"] +count = mylist.count("itheima") +print(f"列表中itheima的数量是:{count}") + +# 10. 统计列表中全部的元素数量 +mylist = ["itcast", "itheima", "itcast", "itheima", "python"] +count = len(mylist) +print(f"列表的元素数量总共有:{count}个") + + + + + + diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/03_list\345\210\227\350\241\250\345\270\270\347\224\250\346\223\215\344\275\234\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/03_list\345\210\227\350\241\250\345\270\270\347\224\250\346\223\215\344\275\234\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..4b2b67a1d8893efb6020402e960559381bc75244 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/03_list\345\210\227\350\241\250\345\270\270\347\224\250\346\223\215\344\275\234\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,24 @@ +""" +演示List常用操作的课后练习 +""" + +# 1. 定义这个列表,并用变量接收它, 内容是:[21, 25, 21, 23, 22, 20] +mylist = [21, 25, 21, 23, 22, 20] + +# 2. 追加一个数字31,到列表的尾部 +mylist.append(31) + +# 3. 追加一个新列表[29, 33, 30],到列表的尾部 +mylist.extend([29, 33, 30]) +# 4. 取出第一个元素(应是:21) +num1 = mylist[0] +print(f"从列表中取出来第一个元素,应该是21,实际上是:{num1}") + +# 5. 取出最后一个元素(应是:30) +num2 = mylist[-1] +print(f"从列表中取出来最后一个元素,应该是30,实际上是:{num2}") + +# 6. 查找元素31,在列表中的下标位置 +index = mylist.index(31) +print(f"元素31在列表的下标位置是:{index}") +print(f"最后列表的内容是:{mylist}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..bd5867127277bba08070a16359d07ab22d778d17 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" @@ -0,0 +1,23 @@ +""" +演示使用while和for循环遍历列表 +""" + + +def list_while_func(): + """ + 使用while循环遍历列表的演示函数 + :return: None + """ + mylist = ["传智教育", "黑马程序员", "Python"] + # 循环控制变量:通过下标索引来控制,默认0 + # 每一次循环将下标苏姚 + + + + +def list_for_func(): + """ + 使用for循环遍历列表的演示函数 + :return: + """ + diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/05_tuple\345\205\203\347\273\204.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/05_tuple\345\205\203\347\273\204.py" new file mode 100644 index 0000000000000000000000000000000000000000..799da0c9aedb047c4a5c85c684512a44545ebc58 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/05_tuple\345\205\203\347\273\204.py" @@ -0,0 +1,55 @@ +""" +演示tuple元组的定义和操作 +""" + +# 定义元组 +t1 = (1, "Hello", True) +t2 = () +t3 = tuple() +print(f"t1的类型是:{type(t1)}, 内容是:{t1}") +print(f"t2的类型是:{type(t2)}, 内容是:{t2}") +print(f"t3的类型是:{type(t3)}, 内容是:{t3}") + +# 定义单个元素的元素 +t4 = ("hello", ) +print(f"t4的类型是:{type(t4)}, t4的内容是:{t4}") +# 元组的嵌套 +t5 = ( (1, 2, 3), (4, 5, 6) ) +print(f"t5的类型是:{type(t5)}, 内容是:{t5}") + +# 下标索引去取出内容 +num = t5[1][2] +print(f"从嵌套元组中取出的数据是:{num}") + +# 元组的操作:index查找方法 +t6 = ("传智教育", "黑马程序员", "Python") +index = t6.index("黑马程序员") +print(f"在元组t6中查找黑马程序员,的下标是:{index}") +# 元组的操作:count统计方法 +t7 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python") +num = t7.count("黑马程序员") +print(f"在元组t7中统计黑马程序员的数量有:{num}个") +# 元组的操作:len函数统计元组元素数量 +t8 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python") +num = len(t8) +print(f"t8元组中的元素有:{num}个") +# 元组的遍历:while +index = 0 +while index < len(t8): + print(f"元组的元素有:{t8[index]}") + # 至关重要 + index += 1 + +# 元组的遍历:for +for element in t8: + print(f"2元组的元素有:{element}") + +# 修改元组内容 +# t8[0] = "itcast" + +# 定义一个元组 +t9 = (1, 2, ["itheima", "itcast"]) +print(f"t9的内容是:{t9}") +t9[2][0] = "黑马程序员" +t9[2][1] = "传智教育" +print(f"t9的内容是:{t9}") \ No newline at end of file diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000000000000000000000000000000000000..aa8e4297ccece691c11ac211675364e70cac6b60 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,40 @@ +""" +演示以数据容器的角色,学习字符串的相关操作 +""" +my_str = "itheima and itcast" +# 通过下标索引取值 +value = my_str[2] +value2 = my_str[-16] +print(f"从字符串{my_str}取下标为2的元素,。值是:{value},取下标为-16的元素。值是:{value2}") + +# my_str[2] = "H" + +# index方法 +value = my_str.index("and") +print(f"在字符串{my_str}中查找and,其起始下标是:{value}") + +# replace方法 +new_my_str = my_str.replace("it", "程序") +print(f"将字符串{my_str},进行替换后得到:{new_my_str}") + +# split方法 +my_str = "hello python itheima itcast" +my_str_list = my_str.split(" ") +print(f"将字符串{my_str}进行split切分后得到:{my_str_list}, 类型是:{type(my_str_list)}") + +# strip方法 +my_str = " itheima and itcast " +new_my_str = my_str.strip() # 不传入参数,去除首尾空格 +print(f"字符串{my_str}被strip后,结果:{new_my_str}") + +my_str = "12itheima and itcast21" +new_my_str = my_str.strip("12") +print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}") + +# 统计字符串中某字符串的出现次数, count +my_str = "itheima and itcast" +count = my_str.count("it") +print(f"字符串{my_str}中it出现的次数是:{count}") +# 统计字符串的长度, len() +num = len(my_str) +print(f"字符串{my_str}的长度是:{num}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/07_\345\255\227\347\254\246\344\270\262\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/07_\345\255\227\347\254\246\344\270\262\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..c7143faef0e917fc73e275c74a0f9f7ff876e19f --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/07_\345\255\227\347\254\246\344\270\262\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,14 @@ +""" +字符串课后练习演示 +"itheima itcast boxuegu" +""" +my_str = "itheima itcast boxuegu" +# 统计字符串内有多少个"it"字符 +num = my_str.count("it") +print(f"字符串{my_str}中有{num}个it字符") +# 将字符串内的空格,全部替换为字符:"|" +new_my_str = my_str.replace(" ", "|") +print(f"字符串{my_str}被替换空格后,结果是:{new_my_str}") +# 并按照"|"进行字符串分割,得到列表 +my_str_list = new_my_str.split("|") +print(f"字符串{new_my_str}按照|分割后结果是:{my_str_list}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" new file mode 100644 index 0000000000000000000000000000000000000000..fb047f2b944d1d1ea56f7ec2107f222becdfabf2 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" @@ -0,0 +1,37 @@ +""" +演示对序列进行切片操作 +""" + +# 对list进行切片,从1开始,4结束,步长1 +my_list = [0, 1, 2, 3, 4, 5, 6] +result1 = my_list[1:4] # 步长默认是1,所以可以省略不写 +print(f"结果1:{result1}") + +# 对tuple进行切片,从头开始,到最后结束,步长1 +my_tuple = (0, 1, 2, 3, 4, 5, 6) +result2 = my_tuple[:] # 起始和结束不写表示从头到尾,步长为1可以省略 +print(f"结果2:{result2}") + +# 对str进行切片,从头开始,到最后结束,步长2 +my_str = "01234567" +result3 = my_str[::2] +print(f"结果3:{result3}") + + +# 对str进行切片,从头开始,到最后结束,步长-1 +my_str = "01234567" +result4 = my_str[::-1] # 等同于将序列反转了 +print(f"结果4:{result4}") + + +# 对列表进行切片,从3开始,到1结束,步长-1 +my_list = [0, 1, 2, 3, 4, 5, 6] +result5 = my_list[3:1:-1] +print(f"结果5:{result5}") + + +# 对元组进行切片,从头开始,到尾结束,步长-2 +my_tuple = (0, 1, 2, 3, 4, 5, 6) +result6 = my_tuple[::-2] +print(f"结果6:{result6}") + diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/09_\345\272\217\345\210\227\345\210\207\347\211\207\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/09_\345\272\217\345\210\227\345\210\207\347\211\207\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..0bc155952a591f73471c22dda21c7cfe48d2c665 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/09_\345\272\217\345\210\227\345\210\207\347\211\207\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,15 @@ +""" +演示序列的切片的课后练习 +"万过薪月,员序程马黑来,nohtyP学" +""" +my_str = "万过薪月,员序程马黑来,nohtyP学" +# 倒序字符串,切片取出 +result1 = my_str[::-1][9:14] +print(f"方式1结果:{result1}") +# 切片取出,然后倒序 +result2 = my_str[5:10][::-1] +print(f"方式2结果:{result2}") + +# split分隔"," replace替换"来"为空,倒序字符串 +result3 = my_str.split(",")[1].replace("来", "")[::-1] +print(f"方式3结果:{result3}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/10_\351\233\206\345\220\210.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/10_\351\233\206\345\220\210.py" new file mode 100644 index 0000000000000000000000000000000000000000..0d4688652912600c7437d9d43787c416d8946165 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/10_\351\233\206\345\220\210.py" @@ -0,0 +1,64 @@ +""" +演示数据容器集合的使用 +""" + +# 定义集合 +my_set = {"传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima"} +my_set_empty = set() # 定义空集合 +print(f"my_set的内容是:{my_set}, 类型是:{type(my_set)}") +print(f"my_set_empty的内容是:{my_set_empty}, 类型是:{type(my_set_empty)}") + +# 添加新元素 +my_set.add("Python") +my_set.add("传智教育") # +print(f"my_set添加元素后结果是:{my_set}") +# 移除元素 +my_set.remove("黑马程序员") +print(f"my_set移除黑马程序员后,结果是:{my_set}") +# 随机取出一个元素 +my_set = {"传智教育", "黑马程序员", "itheima"} +element = my_set.pop() +print(f"集合被取出元素是:{element}, 取出元素后:{my_set}") + +# 清空集合, clear +my_set.clear() +print(f"集合被清空啦,结果是:{my_set}") + +# 取2个集合的差集 +set1 = {1, 2, 3} +set2 = {1, 5, 6} +set3 = set1.difference(set2) +print(f"取出差集后的结果是:{set3}") +print(f"取差集后,原有set1的内容:{set1}") +print(f"取差集后,原有set2的内容:{set2}") + + +# 消除2个集合的差集 +set1 = {1, 2, 3} +set2 = {1, 5, 6} +set1.difference_update(set2) +print(f"消除差集后,集合1结果:{set1}") +print(f"消除差集后,集合2结果:{set2}") + +# 2个集合合并为1个 +set1 = {1, 2, 3} +set2 = {1, 5, 6} +set3 = set1.union(set2) +print(f"2集合合并结果:{set3}") +print(f"合并后集合1:{set1}") +print(f"合并后集合2:{set2}") + +# 统计集合元素数量len() +set1 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5} +num = len(set1) +print(f"集合内的元素数量有:{num}个") + +# 集合的遍历 +# 集合不支持下标索引,不能用while循环 +# 可以用for循环 +set1 = {1, 2, 3, 4, 5} +for element in set1: + print(f"集合的元素有:{element}") + + + diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/11_\351\233\206\345\220\210\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/11_\351\233\206\345\220\210\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..7224efb39d35c4843141643192c857640d52af91 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/11_\351\233\206\345\220\210\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,19 @@ +""" +演示集合的课后练习题 +my_list = ['黑马程序员', '传智播客', '黑马程序员', '传智播客', + 'itheima', 'itcast', 'itheima', 'itcast', 'best'] +""" +my_list = ['黑马程序员', '传智播客', '黑马程序员', '传智播客', + 'itheima', 'itcast', 'itheima', 'itcast', 'best'] + +# 定义一个空集合 +my_set = set() + +# 通过for循环遍历列表 +for element in my_list: + # 在for循环中将列表的元素添加至集合 + my_set.add(element) + +# 最终得到元素去重后的集合对象,并打印输出 +print(f"列表的内容是:{my_list}") +print(f"通过for循环后,得到的集合对象是:{my_set}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/12_\345\255\227\345\205\270.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/12_\345\255\227\345\205\270.py" new file mode 100644 index 0000000000000000000000000000000000000000..5ce537c8a836d954518b9bd4f8d3bf561e6681b7 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/12_\345\255\227\345\205\270.py" @@ -0,0 +1,47 @@ +""" +演示数据容器字典的定义 +""" + +# 定义字典 +my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77} +# 定义空字典 +my_dict2 = {} +my_dict3 = dict() +print(f"字典1的内容是:{my_dict1}, 类型:{type(my_dict1)}") +print(f"字典2的内容是:{my_dict2}, 类型:{type(my_dict2)}") +print(f"字典3的内容是:{my_dict3}, 类型:{type(my_dict3)}") + +# 定义重复Key的字典 +my_dict1 = {"王力鸿": 99, "王力鸿": 88, "林俊节": 77} +print(f"重复key的字典的内容是:{my_dict1}") + +# 从字典中基于Key获取Value +my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77} +score = my_dict1["王力鸿"] +print(f"王力鸿的考试分数是:{score}") +score = my_dict1["周杰轮"] +print(f"周杰轮的考试分数是:{score}") +# 定义嵌套字典 +stu_score_dict = { + "王力鸿": { + "语文": 77, + "数学": 66, + "英语": 33 + }, "周杰轮": { + "语文": 88, + "数学": 86, + "英语": 55 + }, "林俊节": { + "语文": 99, + "数学": 96, + "英语": 66 + } +} +print(f"学生的考试信息是:{stu_score_dict}") + +# 从嵌套字典中获取数据 +# 看一下周杰轮的语文信息 +score = stu_score_dict["周杰轮"]["语文"] +print(f"周杰轮的语文分数是:{score}") +score = stu_score_dict["林俊节"]["英语"] +print(f"林俊节的英语分数是:{score}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/13_\345\255\227\345\205\270\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/13_\345\255\227\345\205\270\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" new file mode 100644 index 0000000000000000000000000000000000000000..9a51ce936a224534e62921f2ef9a6b1c37c55a3a --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/13_\345\255\227\345\205\270\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" @@ -0,0 +1,37 @@ +""" +演示字典的常用操作 +""" +my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77} +# 新增元素 +my_dict["张信哲"] = 66 +print(f"字典经过新增元素后,结果:{my_dict}") +# 更新元素 +my_dict["周杰轮"] = 33 +print(f"字典经过更新后,结果:{my_dict}") +# 删除元素 +score = my_dict.pop("周杰轮") +print(f"字典中被移除了一个元素,结果:{my_dict}, 周杰轮的考试分数是:{score}") + +# 清空元素, clear +my_dict.clear() +print(f"字典被清空了,内容是:{my_dict}") + +# 获取全部的key +my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77} +keys = my_dict.keys() +print(f"字典的全部keys是:{keys}") +# 遍历字典 +# 方式1:通过获取到全部的key来完成遍历 +for key in keys: + print(f"字典的key是:{key}") + print(f"字典的value是:{my_dict[key]}") + +# 方式2:直接对字典进行for循环,每一次循环都是直接得到key +for key in my_dict: + print(f"2字典的key是:{key}") + print(f"2字典的value是:{my_dict[key]}") + +# 统计字典内的元素数量, len()函数 +num = len(my_dict) +print(f"字典中的元素数量有:{num}个") + diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/14_\345\255\227\345\205\270\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/14_\345\255\227\345\205\270\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..ad0ed031da10f426c394dfb67ba4229f86a41425 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/14_\345\255\227\345\205\270\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,50 @@ +""" +演示字典的课后练习:升职加薪,对所有级别为1级的员工,级别上升1级,薪水增加1000元 +""" + +# 组织字典记录数据 +info_dict = { + "王力鸿": { + "部门": "科技部", + "工资": 3000, + "级别": 1 + }, + "周杰轮": { + "部门": "市场部", + "工资": 5000, + "级别": 2 + }, + "林俊节": { + "部门": "市场部", + "工资": 7000, + "级别": 3 + }, + "张学油": { + "部门": "科技部", + "工资": 4000, + "级别": 1 + }, + "刘德滑": { + "部门": "市场部", + "工资": 6000, + "级别": 2 + } +} + +print(f"员工在升值加薪之前的结果:{info_dict}") + +# for循环遍历字典 +for name in info_dict: + # if条件判断符合条件员工 + if info_dict[name]["级别"] == 1: + # 升职加薪操作 + # 获取到员工的信息字典 + employee_info_dict = info_dict[name] + # 修改员工的信息 + employee_info_dict["级别"] = 2 # 级别+1 + employee_info_dict["工资"] += 1000 # 工资+1000 + # 将员工的信息更新回info_dict + info_dict[name] = employee_info_dict + +# 输出结果 +print(f"对员工进行升级加薪后的结果是:{info_dict}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/15_\346\225\260\346\215\256\345\256\271\345\231\250\351\200\232\347\224\250\345\212\237\350\203\275.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/15_\346\225\260\346\215\256\345\256\271\345\231\250\351\200\232\347\224\250\345\212\237\350\203\275.py" new file mode 100644 index 0000000000000000000000000000000000000000..e0b1ba87a48f3bf11cc676460e7e8e01b198e72d --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/15_\346\225\260\346\215\256\345\256\271\345\231\250\351\200\232\347\224\250\345\212\237\350\203\275.py" @@ -0,0 +1,70 @@ +""" +演示数据容器的通用功能 +""" +my_list = [1, 2, 3, 4, 5] +my_tuple = (1, 2, 3, 4, 5) +my_str = "abcdefg" +my_set = {1, 2, 3, 4, 5} +my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5} + +# len元素个数 +print(f"列表 元素个数有:{len(my_list)}") +print(f"元组 元素个数有:{len(my_tuple)}") +print(f"字符串元素个数有:{len(my_str)}") +print(f"集合 元素个数有:{len(my_set)}") +print(f"字典 元素个数有:{len(my_dict)}") + +# max最大元素 +print(f"列表 最大的元素是:{max(my_list)}") +print(f"元组 最大的元素是:{max(my_tuple)}") +print(f"字符串最大的元素是:{max(my_str)}") +print(f"集合 最大的元素是:{max(my_set)}") +print(f"字典 最大的元素是:{max(my_dict)}") +# min最小元素 +print(f"列表 最小的元素是:{min(my_list)}") +print(f"元组 最小的元素是:{min(my_tuple)}") +print(f"字符串最小的元素是:{min(my_str)}") +print(f"集合 最小的元素是:{min(my_set)}") +print(f"字典 最小的元素是:{min(my_dict)}") +# 类型转换: 容器转列表 +print(f"列表转列表的结果是:{list(my_list)}") +print(f"元组转列表的结果是:{list(my_tuple)}") +print(f"字符串转列表结果是:{list(my_str)}") +print(f"集合转列表的结果是:{list(my_set)}") +print(f"字典转列表的结果是:{list(my_dict)}") +# 类型转换: 容器转元组 +print(f"列表转元组的结果是:{tuple(my_list)}") +print(f"元组转元组的结果是:{tuple(my_tuple)}") +print(f"字符串转元组结果是:{tuple(my_str)}") +print(f"集合转元组的结果是:{tuple(my_set)}") +print(f"字典转元组的结果是:{tuple(my_dict)}") +# 类型转换: 容器转字符串 +print(f"列表转字符串的结果是:{str(my_list)}") +print(f"元组转字符串的结果是:{str(my_tuple)}") +print(f"字符串转字符串结果是:{str(my_str)}") +print(f"集合转字符串的结果是:{str(my_set)}") +print(f"字典转字符串的结果是:{str(my_dict)}") +# 类型转换: 容器转集合 +print(f"列表转集合的结果是:{set(my_list)}") +print(f"元组转集合的结果是:{set(my_tuple)}") +print(f"字符串转集合结果是:{set(my_str)}") +print(f"集合转集合的结果是:{set(my_set)}") +print(f"字典转集合的结果是:{set(my_dict)}") +# 进行容器的排序 +my_list = [3, 1, 2, 5, 4] +my_tuple = (3, 1, 2, 5, 4) +my_str = "bdcefga" +my_set = {3, 1, 2, 5, 4} +my_dict = {"key3": 1, "key1": 2, "key2": 3, "key5": 4, "key4": 5} + +print(f"列表对象的排序结果:{sorted(my_list)}") +print(f"元组对象的排序结果:{sorted(my_tuple)}") +print(f"字符串对象的排序结果:{sorted(my_str)}") +print(f"集合对象的排序结果:{sorted(my_set)}") +print(f"字典对象的排序结果:{sorted(my_dict)}") + +print(f"列表对象的反向排序结果:{sorted(my_list, reverse=True)}") +print(f"元组对象的反向排序结果:{sorted(my_tuple, reverse=True)}") +print(f"字符串对象反向的排序结果:{sorted(my_str, reverse=True)}") +print(f"集合对象的反向排序结果:{sorted(my_set, reverse=True)}") +print(f"字典对象的反向排序结果:{sorted(my_dict, reverse=True)}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/16_\345\255\227\347\254\246\344\270\262\345\244\247\345\260\217\346\257\224\350\276\203.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/16_\345\255\227\347\254\246\344\270\262\345\244\247\345\260\217\346\257\224\350\276\203.py" new file mode 100644 index 0000000000000000000000000000000000000000..fcd1c623eb930bbc4ffb2c8dc5647f6817bc7ae2 --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/16_\345\255\227\347\254\246\344\270\262\345\244\247\345\260\217\346\257\224\350\276\203.py" @@ -0,0 +1,12 @@ +""" +演示字符串大小比较 +""" + +# abc 比较 abd +print(f"abd大于abc,结果:{'abd' > 'abc'}") +# a 比较 ab +print(f"ab大于a,结果:{'ab' > 'a'}") +# a 比较 A +print(f"a 大于 A,结果:{'a' > 'A'}") +# key1 比较 key2 +print(f"key2 > key1,结果:{'key2' > 'key1'}") diff --git "a/07_\346\225\260\346\215\256\345\256\271\345\231\250/test.py" "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..bfead5181d3159b2cc2d1b9c55a7465b943afe6a --- /dev/null +++ "b/07_\346\225\260\346\215\256\345\256\271\345\231\250/test.py" @@ -0,0 +1,12 @@ + +my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +new_list = [] +i = 0 +while i < len(my_list): + if my_list[i] % 2 == 0: + new_list.append(my_list[i]) + i += 1 + +print(f"偶数形成的新列表:{new_list}") + + diff --git "a/08_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\347\232\204\350\257\273\345\217\226.py" "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\347\232\204\350\257\273\345\217\226.py" new file mode 100644 index 0000000000000000000000000000000000000000..9322f97fe0898e78b97fef03e9431c8627757f34 --- /dev/null +++ "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/01_\346\226\207\344\273\266\347\232\204\350\257\273\345\217\226.py" @@ -0,0 +1,38 @@ +""" +演示对文件的读取 +""" + +# 打开文件 +import time + +f = open("D:/测试.txt", "r", encoding="UTF-8") +print(type(f)) +# 读取文件 - read() +# print(f"读取10个字节的结果:{f.read(10)}") +# print(f"read方法读取全部内容的结果是:{f.read()}") +print("-----------------------------------------------") +# 读取文件 - readLines() +# lines = f.readlines() # 读取文件的全部行,封装到列表中 +# print(f"lines对象的类型:{type(lines)}") +# print(f"lines对象的内容是:{lines}") + +# 读取文件 - readline() +# line1 = f.readline() +# line2 = f.readline() +# line3 = f.readline() +# print(f"第一行数据是:{line1}") +# print(f"第二行数据是:{line2}") +# print(f"第三行数据是:{line3}") + +# for循环读取文件行 +# for line in f: +# print(f"每一行数据是:{line}") +# # 文件的关闭 +# f.close() +# time.sleep(500000) +# with open 语法操作文件 +with open("D:/测试.txt", "r", encoding="UTF-8") as f: + for line in f: + print(f"每一行数据是:{line}") + +time.sleep(500000) \ No newline at end of file diff --git "a/08_\346\226\207\344\273\266\346\223\215\344\275\234/02_\346\226\207\344\273\266\350\257\273\345\217\226\347\232\204\350\257\276\345\220\216\347\273\203\344\271\240.py" "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/02_\346\226\207\344\273\266\350\257\273\345\217\226\347\232\204\350\257\276\345\220\216\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..ad7c49971fff683269b0b03d1a50de68081f8f17 --- /dev/null +++ "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/02_\346\226\207\344\273\266\350\257\273\345\217\226\347\232\204\350\257\276\345\220\216\347\273\203\344\271\240.py" @@ -0,0 +1,22 @@ +""" +演示读取文件,课后练习题 +""" + +# 打开文件,以读取模式打开 +f = open("D:/word.txt", "r", encoding="UTF-8") +# 方式1:读取全部内容,通过字符串count方法统计itheima单词数量 +# content = f.read() +# count = content.count("itheima") +# print(f"itheima在文件中出现了:{count}次") +# 方式2:读取内容,一行一行读取 +count = 0 # 使用count变量来累计itheima出现的次数 +for line in f: + line = line.strip() # 去除开头和结尾的空格以及换行符 + words = line.split(" ") + for word in words: + if word == "itheima": + count += 1 # 如果单词是itheima,进行数量的累加加1 +# 判断单词出现次数并累计 +print(f"itheima出现的次数是:{count}") +# 关闭文件 +f.close() diff --git "a/08_\346\226\207\344\273\266\346\223\215\344\275\234/03_\346\226\207\344\273\266\347\232\204\345\206\231\345\205\245.py" "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/03_\346\226\207\344\273\266\347\232\204\345\206\231\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..508531edd370842f960d9aaaf78091e144bacfd4 --- /dev/null +++ "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/03_\346\226\207\344\273\266\347\232\204\345\206\231\345\205\245.py" @@ -0,0 +1,21 @@ +""" +演示文件的写入 +""" + +# 打开文件,不存在的文件, r, w, a +import time + +# f = open("D:/test.txt", "w", encoding="UTF-8") +# # write写入 +# f.write("Hello World!!!") # 内容写入到内存中 +# # flush刷新 +# # f.flush() # 将内存中积攒的内容,写入到硬盘的文件中 +# # close关闭 +# f.close() # close方法,内置了flush的功能的 +# 打开一个存在的文件 +f = open("D:/test.txt", "w", encoding="UTF-8") +# write写入、flush刷新 +f.write("黑马程序员") +# close关闭 +f.close() + diff --git "a/08_\346\226\207\344\273\266\346\223\215\344\275\234/04_\346\226\207\344\273\266\347\232\204\350\277\275\345\212\240\345\206\231\345\205\245.py" "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/04_\346\226\207\344\273\266\347\232\204\350\277\275\345\212\240\345\206\231\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..4d2e9e5d8cff0a29d490b5474691f86a30de21d4 --- /dev/null +++ "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/04_\346\226\207\344\273\266\347\232\204\350\277\275\345\212\240\345\206\231\345\205\245.py" @@ -0,0 +1,18 @@ +""" +演示文件的追加写入 +""" + +# 打开文件,不存在的文件 +# f = open("D:/test.txt", "a", encoding="UTF-8") +# # write写入 +# f.write("黑马程序员") +# # flush刷新 +# f.flush() +# # close关闭 +# f.close() +# 打开一个存在的文件 +f = open("D:/test.txt", "a", encoding="UTF-8") +# write写入、flush刷新 +f.write("\n月薪过万") +# close关闭 +f.close() diff --git "a/08_\346\226\207\344\273\266\346\223\215\344\275\234/05_\346\226\207\344\273\266\346\223\215\344\275\234\347\232\204\347\273\274\345\220\210\346\241\210\344\276\213.py" "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/05_\346\226\207\344\273\266\346\223\215\344\275\234\347\232\204\347\273\274\345\220\210\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..e58e9b82c4cb6de6ce78c054976d8e26a8617fa7 --- /dev/null +++ "b/08_\346\226\207\344\273\266\346\223\215\344\275\234/05_\346\226\207\344\273\266\346\223\215\344\275\234\347\232\204\347\273\274\345\220\210\346\241\210\344\276\213.py" @@ -0,0 +1,22 @@ +""" +演示文件操作综合案例:文件备份 +""" + +# 打开文件得到文件对象,准备读取 +fr = open("D:/bill.txt", "r", encoding="UTF-8") +# 打开文件得到文件对象,准备写入 +fw = open("D:/bill.txt.bak", "w", encoding="UTF-8") +# for循环读取文件 +for line in fr: + line = line.strip() + # 判断内容,将满足的内容写出 + if line.split(",")[4] == "测试": + continue # continue进入下一次循环,这一次后面的内容就跳过了 + # 将内容写出去 + fw.write(line) + # 由于前面对内容进行了strip()的操作,所以要手动的写出换行符 + fw.write("\n") + +# close2个文件对象 +fr.close() +fw.close() # 写出文件调用close()会自动flush() diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/01_\346\274\224\347\244\272\345\274\202\345\270\270\347\232\204\345\207\272\347\216\260.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/01_\346\274\224\347\244\272\345\274\202\345\270\270\347\232\204\345\207\272\347\216\260.py" new file mode 100644 index 0000000000000000000000000000000000000000..4f43b00e7e644b7a0424c99e9ffc7c56fc66ff64 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/01_\346\274\224\347\244\272\345\274\202\345\270\270\347\232\204\345\207\272\347\216\260.py" @@ -0,0 +1,6 @@ +""" +主动写一段错误代码,演示异常的出现 +""" + +# 通过open,读取一个不存在的文件 +f = open("D:/abc.txt", "r", encoding="UTF-8") diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/02_\345\274\202\345\270\270\347\232\204\346\215\225\350\216\267.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/02_\345\274\202\345\270\270\347\232\204\346\215\225\350\216\267.py" new file mode 100644 index 0000000000000000000000000000000000000000..6df68e316c1cad7a2b96631fb90c23e9384fc29a --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/02_\345\274\202\345\270\270\347\232\204\346\215\225\350\216\267.py" @@ -0,0 +1,37 @@ +""" +演示捕获异常 +""" + +# 基本捕获语法 +# try: +# f = open("D:/abc.txt", "r", encoding="UTF-8") +# except: +# print("出现异常了,因为文件不存在,我将open的模式,改为w模式去打开") +# f = open("D:/abc.txt", "w", encoding="UTF-8") + +# 捕获指定的异常 +# try: +# print(name) +# # 1 / 0 +# except NameError as e: +# print("出现了变量未定义的异常") +# print(e) +# 捕获多个异常 +# try: +# # 1 / 0 +# print(name) +# except (NameError, ZeroDivisionError) as e: +# print("出现了变量未定义 或者 除以0的异常错误") +# 未正确设置捕获异常类型,将无法捕获异常 + +# 捕获所有异常 +try: + f = open("D:/123.txt", "r", encoding="UTF-8") +except Exception as e: + print("出现异常了") + f = open("D:/123.txt", "w", encoding="UTF-8") +else: + print("好高兴,没有异常。") +finally: + print("我是finally,有没有异常我都要执行") + f.close() diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/03_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/03_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" new file mode 100644 index 0000000000000000000000000000000000000000..1915b838263274e8cf74d045b51ee61968cc0d36 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/03_\345\274\202\345\270\270\347\232\204\344\274\240\351\200\222.py" @@ -0,0 +1,24 @@ +""" +演示异常的传递性 +""" + +# 定义一个出现异常的方法 +def func1(): + print("func1 开始执行") + num = 1 / 0 # 肯定有异常,除以0的异常 + print("func1 结束执行") +# 定义一个无异常的方法,调用上面的方法 + +def func2(): + print("func2 开始执行") + func1() + print("func2 结束执行") +# 定义一个方法,调用上面的方法 + +def main(): + try: + func2() + except Exception as e: + print(f"出现异常了,异常的信息是:{e}") + +main() diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/04_\346\250\241\345\235\227\347\232\204\345\257\274\345\205\245.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/04_\346\250\241\345\235\227\347\232\204\345\257\274\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..9f2a4dbcc46f3b4ed1d2c829beea01da87b98a94 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/04_\346\250\241\345\235\227\347\232\204\345\257\274\345\205\245.py" @@ -0,0 +1,46 @@ +""" +演示Python的模块导入 +""" + +from time import sleep + + + + + + + + + + + + + +# 使用import导入time模块使用sleep功能(函数) +# import time # 导入Python内置的time模块(time.py这个代码文件) +# print("你好") +# time.sleep(5) # 通过. 就可以使用模块内部的全部功能(类、函数、变量) +# print("我好") + +# 使用from导入time的sleep功能(函数) +# from time import sleep +# print("你好") +# sleep(5) +# print("我好") + +# 使用 * 导入time模块的全部功能 +# from time import * # *表示全部的意思 +# print("你好") +# sleep(5) +# print("我好") + +# 使用as给特定功能加上别名 +# import time as t +# print("你好") +# t.sleep(5) +# print("我好") + +from time import sleep as sl +print("你好") +sl(5) +print("我好") diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/05_\350\207\252\345\256\232\344\271\211\346\250\241\345\235\227.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/05_\350\207\252\345\256\232\344\271\211\346\250\241\345\235\227.py" new file mode 100644 index 0000000000000000000000000000000000000000..aceca00716206360d5f9a763ccd1378e6afae6d5 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/05_\350\207\252\345\256\232\344\271\211\346\250\241\345\235\227.py" @@ -0,0 +1,21 @@ +""" +演示自定义模块 +""" + +# 导入自定义模块使用 +# import my_module1 +# from my_module1 import test +# test(1, 2) + +# 导入不同模块的同名功能 +# from my_module1 import test +# from my_module2 import test +# test(1, 2) +# __main__变量 +# from my_module1 import test + +# __all__变量 +from my_module1 import * +test_a(1, 2) +# test_b(2, 1) + diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/06_\345\214\205.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/06_\345\214\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..e82360adfc4acb3de54e318614e63eb4bc216425 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/06_\345\214\205.py" @@ -0,0 +1,26 @@ +""" +演示Python的包 +""" + +# 创建一个包 +# 导入自定义的包中的模块,并使用 +# import my_package.my_module1 +# import my_package.my_module2 +# +# my_package.my_module1.info_print1() +# my_package.my_module2.info_print2() + +# from my_package import my_module1 +# from my_package import my_module2 +# my_module1.info_print1() +# my_module2.info_print2() + +# from my_package.my_module1 import info_print1 +# from my_package.my_module2 import info_print2 +# info_print1() +# info_print2() + +# 通过__all__变量,控制import * +from my_package import * +my_module1.info_print1() +my_module2.info_print2() diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/07_\347\273\274\345\220\210\346\241\210\344\276\213\347\273\203\344\271\240.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/07_\347\273\274\345\220\210\346\241\210\344\276\213\347\273\203\344\271\240.py" new file mode 100644 index 0000000000000000000000000000000000000000..0e83d0cecbdfe5783fae6fe2a1a82a50f3b95bc7 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/07_\347\273\274\345\220\210\346\241\210\344\276\213\347\273\203\344\271\240.py" @@ -0,0 +1,14 @@ +""" +演示异常、模块、包的综合案例练习 +""" +# 创建my_utils 包, 在包内创建:str_util.py 和 file_util.py 2个模块,并提供相应的函数 + +import my_utils.str_util +from my_utils import file_util + +print(my_utils.str_util.str_reverse("黑马程序员")) +print(my_utils.str_util.substr("itheima", 0, 4)) + + +file_util.append_to_file("D:/test_append.txt", "itheima") +file_util.print_file_info("D:/test_append.txt") diff --git "a/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/08_\346\211\251\345\261\225_Python\345\270\270\347\224\250\346\250\241\345\235\227\345\212\237\350\203\275.py" "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/08_\346\211\251\345\261\225_Python\345\270\270\347\224\250\346\250\241\345\235\227\345\212\237\350\203\275.py" new file mode 100644 index 0000000000000000000000000000000000000000..20f0054722c1348b1d26f6194d42c51af0310c64 --- /dev/null +++ "b/09_\345\274\202\345\270\270_\346\250\241\345\235\227_\345\214\205/08_\346\211\251\345\261\225_Python\345\270\270\347\224\250\346\250\241\345\235\227\345\212\237\350\203\275.py" @@ -0,0 +1,18 @@ +""" +演示常用的模块功能 +""" +import time +# time模块 +ts = time.time() # 当前时间戳 +print(f"当前时间戳是:{ts}") + +# 获取当前时间以指定的格式显示,2000-01-01 10:00:00 +print(time.strftime("%Y-%m-%d %H:%M:%S")) + +# 将指定的时间戳转换为格式化的日期字符串 +print(time.strftime("%Y-%m-%d %H:%M:%S")) +# random模块 + +# os模块 + +# sys模块 diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/01_json\346\225\260\346\215\256\346\240\274\345\274\217.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/01_json\346\225\260\346\215\256\346\240\274\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..6485e8469ad4c5d5eda30bc96aa3ce241c21846c --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/01_json\346\225\260\346\215\256\346\240\274\345\274\217.py" @@ -0,0 +1,24 @@ +""" +演示JSON数据和Python字典的相互转换 +""" +import json +# 准备列表,列表内每一个元素都是字典,将其转换为JSON +data = [{"name": "张大山", "age": 11}, {"name": "王大锤", "age": 13}, {"name": "赵小虎", "age": 16}] +json_str = json.dumps(data, ensure_ascii=False) +print(type(json_str)) +print(json_str) +# 准备字典,将字典转换为JSON +d = {"name":"周杰轮", "addr":"台北"} +json_str = json.dumps(d, ensure_ascii=False) +print(type(json_str)) +print(json_str) +# 将JSON字符串转换为Python数据类型[{k: v, k: v}, {k: v, k: v}] +s = '[{"name": "张大山", "age": 11}, {"name": "王大锤", "age": 13}, {"name": "赵小虎", "age": 16}]' +l = json.loads(s) +print(type(l)) +print(l) +# 将JSON字符串转换为Python数据类型{k: v, k: v} +s = '{"name": "周杰轮", "addr": "台北"}' +d = json.loads(s) +print(type(d)) +print(d) diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/02_pyecharts\345\237\272\347\241\200\345\205\245\351\227\250.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/02_pyecharts\345\237\272\347\241\200\345\205\245\351\227\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..37f100c8506d7622e3e4a9f7909d89a504e87fc8 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/02_pyecharts\345\237\272\347\241\200\345\205\245\351\227\250.py" @@ -0,0 +1,23 @@ +""" +演示pyecharts的基础入门 +""" +# 导包 +from pyecharts.charts import Line +from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts +# 创建一个折线图对象 +line = Line() +# 给折线图对象添加x轴的数据 +line.add_xaxis(["中国", "美国", "英国"]) +# 给折线图对象添加y轴的数据 +line.add_yaxis("GDP", [30, 20, 10]) + +# 设置全局配置项set_global_opts来设置, +line.set_global_opts( + title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"), + legend_opts=LegendOpts(is_show=True), + toolbox_opts=ToolboxOpts(is_show=True), + visualmap_opts=VisualMapOpts(is_show=True), +) + +# 通过render方法,将代码生成为图像 +line.render() diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/03_\346\212\230\347\272\277\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/03_\346\212\230\347\272\277\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..9f892a6d7f52f12bff8167835686bda67058476c --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/03_\346\212\230\347\272\277\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,68 @@ +""" +演示可视化需求1:折线图开发 +""" +import json +from pyecharts.charts import Line +from pyecharts.options import TitleOpts, LabelOpts + +# 处理数据 +f_us = open("D:/美国.txt", "r", encoding="UTF-8") +us_data = f_us.read() # 美国的全部内容 + +f_jp = open("D:/日本.txt", "r", encoding="UTF-8") +jp_data = f_jp.read() # 日本的全部内容 + +f_in = open("D:/印度.txt", "r", encoding="UTF-8") +in_data = f_in.read() # 印度的全部内容 + +# 去掉不合JSON规范的开头 +us_data = us_data.replace("jsonp_1629344292311_69436(", "") +jp_data = jp_data.replace("jsonp_1629350871167_29498(", "") +in_data = in_data.replace("jsonp_1629350745930_63180(", "") + +# 去掉不合JSON规范的结尾 +us_data = us_data[:-2] +jp_data = jp_data[:-2] +in_data = in_data[:-2] + +# JSON转Python字典 +us_dict = json.loads(us_data) +jp_dict = json.loads(jp_data) +in_dict = json.loads(in_data) + +# 获取trend key +us_trend_data = us_dict['data'][0]['trend'] +jp_trend_data = jp_dict['data'][0]['trend'] +in_trend_data = in_dict['data'][0]['trend'] + +# 获取日期数据,用于x轴,取2020年(到314下标结束) +us_x_data = us_trend_data['updateDate'][:314] +jp_x_data = jp_trend_data['updateDate'][:314] +in_x_data = in_trend_data['updateDate'][:314] + +# 获取确认数据,用于y轴,取2020年(到314下标结束) +us_y_data = us_trend_data['list'][0]['data'][:314] +jp_y_data = jp_trend_data['list'][0]['data'][:314] +in_y_data = in_trend_data['list'][0]['data'][:314] + +# 生成图表 +line = Line() # 构建折线图对象 +# 添加x轴数据 +line.add_xaxis(us_x_data) # x轴是公用的,所以使用一个国家的数据即可 +# 添加y轴数据 +line.add_yaxis("美国确诊人数", us_y_data, label_opts=LabelOpts(is_show=False)) # 添加美国的y轴数据 +line.add_yaxis("日本确诊人数", jp_y_data, label_opts=LabelOpts(is_show=False)) # 添加日本的y轴数据 +line.add_yaxis("印度确诊人数", in_y_data, label_opts=LabelOpts(is_show=False)) # 添加印度的y轴数据 + +# 设置全局选项 +line.set_global_opts( + # 标题设置 + title_opts=TitleOpts(title="2020年美日印三国确诊人数对比折线图", pos_left="center", pos_bottom="1%") +) + +# 调用render方法,生成图表 +line.render() +# 关闭文件对象 +f_us.close() +f_jp.close() +f_in.close() diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/04_\345\234\260\345\233\276\345\217\257\350\247\206\345\214\226\347\232\204\345\237\272\346\234\254\344\275\277\347\224\250.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/04_\345\234\260\345\233\276\345\217\257\350\247\206\345\214\226\347\232\204\345\237\272\346\234\254\344\275\277\347\224\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..1209843e2b5db2d114ccb825eaec3f4a769eefe4 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/04_\345\234\260\345\233\276\345\217\257\350\247\206\345\214\226\347\232\204\345\237\272\346\234\254\344\275\277\347\224\250.py" @@ -0,0 +1,34 @@ +""" +演示地图可视化的基本使用 +""" +from pyecharts.charts import Map +from pyecharts.options import VisualMapOpts + +# 准备地图对象 +map = Map() +# 准备数据 +data = [ + ("北京", 99), + ("上海", 199), + ("湖南", 299), + ("台湾", 399), + ("广东", 499) +] +# 添加数据 +map.add("测试地图", data, "china") + +# 设置全局选项 +map.set_global_opts( + visualmap_opts=VisualMapOpts( + is_show=True, + is_piecewise=True, + pieces=[ + {"min": 1, "max": 9, "label": "1-9", "color": "#CCFFFF"}, + {"min": 10, "max": 99, "label": "10-99", "color": "#FF6666"}, + {"min": 100, "max": 500, "label": "100-500", "color": "#990033"} + ] + ) +) + +# 绘图 +map.render() diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/05_\345\205\250\345\233\275\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/05_\345\205\250\345\233\275\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..f78620c91c0055202e47896b54a2c7ac0d7ad21c --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/05_\345\205\250\345\233\275\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,47 @@ +""" +演示全国疫情可视化地图开发 +""" +import json +from pyecharts.charts import Map +from pyecharts.options import * + +# 读取数据文件 +f = open("D:/疫情.txt", "r", encoding="UTF-8") +data = f.read() # 全部数据 +# 关闭文件 +f.close() +# 取到各省数据 +# 将字符串json转换为python的字典 +data_dict = json.loads(data) # 基础数据字典 +# 从字典中取出省份的数据 +province_data_list = data_dict["areaTree"][0]["children"] +# 组装每个省份和确诊人数为元组,并各个省的数据都封装入列表内 +data_list = [] # 绘图需要用的数据列表 +for province_data in province_data_list: + province_name = province_data["name"] # 省份名称 + province_confirm = province_data["total"]["confirm"] # 确诊人数 + data_list.append((province_name, province_confirm)) + + +# 创建地图对象 +map = Map() +# 添加数据 +map.add("各省份确诊人数", data_list, "china") +# 设置全局配置,定制分段的视觉映射 +map.set_global_opts( + title_opts=TitleOpts(title="全国疫情地图"), + visualmap_opts=VisualMapOpts( + is_show=True, # 是否显示 + is_piecewise=True, # 是否分段 + pieces=[ + {"min": 1, "max": 99, "lable": "1~99人", "color": "#CCFFFF"}, + {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"}, + {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"}, + {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"}, + {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"}, + {"min": 100000, "lable": "100000+", "color": "#990033"}, + ] + ) +) +# 绘图 +map.render("全国疫情地图.html") diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/06_\347\234\201\347\272\247\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/06_\347\234\201\347\272\247\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..4a2e4d3ba1cd8954f56c0a81b40aa25c1485818f --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/06_\347\234\201\347\272\247\347\226\253\346\203\205\345\217\257\350\247\206\345\214\226\345\234\260\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,50 @@ +""" +演示河南省疫情地图开发 +""" +import json +from pyecharts.charts import Map +from pyecharts.options import * + +# 读取文件 +f = open("D:/疫情.txt", "r", encoding="UTF-8") +data = f.read() +# 关闭文件 +f.close() +# 获取河南省数据 +# json数据转换为python字典 +data_dict = json.loads(data) +# 取到河南省数据 +cities_data = data_dict["areaTree"][0]["children"][3]["children"] + +# 准备数据为元组并放入list +data_list = [] +for city_data in cities_data: + city_name = city_data["name"] + "市" + city_confirm = city_data["total"]["confirm"] + data_list.append((city_name, city_confirm)) + +# 手动添加济源市的数据 +data_list.append(("济源市", 5)) + +# 构建地图 +map = Map() +map.add("河南省疫情分布", data_list, "河南") +# 设置全局选项 +map.set_global_opts( + title_opts=TitleOpts(title="河南省疫情地图"), + visualmap_opts=VisualMapOpts( + is_show=True, # 是否显示 + is_piecewise=True, # 是否分段 + pieces=[ + {"min": 1, "max": 99, "lable": "1~99人", "color": "#CCFFFF"}, + {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"}, + {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"}, + {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"}, + {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"}, + {"min": 100000, "lable": "100000+", "color": "#990033"}, + ] + ) +) + +# 绘图 +map.render("河南省疫情地图.html") diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/07_\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/07_\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..fba677a44208090fc241e85ae17c77ff91c1d6a7 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/07_\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,19 @@ +""" +演示基础柱状图的开发 +""" +from pyecharts.charts import Bar +from pyecharts.options import LabelOpts +# 使用Bar构建基础柱状图 +bar = Bar() +# 添加x轴的数据 +bar.add_xaxis(["中国", "美国", "英国"]) +# 添加y轴数据 +bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right")) +# 反转x和y轴 +bar.reversal_axis() +# 绘图 +bar.render("基础柱状图.html") + +# 反转x轴和y轴 + +# 设置数值标签在右侧 \ No newline at end of file diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/08_\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/08_\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..6da82adfef268000c10d1f1fb89fdac49961d940 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/08_\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,40 @@ +""" +演示带有时间线的柱状图开发 +""" +from pyecharts.charts import Bar, Timeline +from pyecharts.options import LabelOpts +from pyecharts.globals import ThemeType + +bar1 = Bar() +bar1.add_xaxis(["中国", "美国", "英国"]) +bar1.add_yaxis("GDP", [30, 30, 20], label_opts=LabelOpts(position="right")) +bar1.reversal_axis() + +bar2 = Bar() +bar2.add_xaxis(["中国", "美国", "英国"]) +bar2.add_yaxis("GDP", [50, 50, 50], label_opts=LabelOpts(position="right")) +bar2.reversal_axis() + +bar3 = Bar() +bar3.add_xaxis(["中国", "美国", "英国"]) +bar3.add_yaxis("GDP", [70, 60, 60], label_opts=LabelOpts(position="right")) +bar3.reversal_axis() + +# 构建时间线对象 +timeline = Timeline({"theme": ThemeType.LIGHT}) +# 在时间线内添加柱状图对象 +timeline.add(bar1, "点1") +timeline.add(bar2, "点2") +timeline.add(bar3, "点3") + +# 自动播放设置 +timeline.add_schema( + play_interval=1000, + is_timeline_show=True, + is_auto_play=True, + is_loop_play=True +) + +# 绘图是用时间线对象绘图,而不是bar对象了 +timeline.render("基础时间线柱状图.html") + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/09_\346\213\223\345\261\225\347\237\245\350\257\206\347\202\271-\345\210\227\350\241\250\347\232\204sort\346\226\271\346\263\225.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/09_\346\213\223\345\261\225\347\237\245\350\257\206\347\202\271-\345\210\227\350\241\250\347\232\204sort\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..ec3662a3adb9cf878ca98b6fc23a6ebf1823486b --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/09_\346\213\223\345\261\225\347\237\245\350\257\206\347\202\271-\345\210\227\350\241\250\347\232\204sort\346\226\271\346\263\225.py" @@ -0,0 +1,17 @@ +""" +扩展列表的sort方法 +在学习了将函数作为参数传递后,我们可以学习列表的sort方法来对列表进行自定义排序 +""" + +# 准备列表 +my_list = [["a", 33], ["b", 55], ["c", 11]] + +# 排序,基于带名函数 +# def choose_sort_key(element): +# return element[1] +# +# my_list.sort(key=choose_sort_key, reverse=True) +# 排序,基于lambda匿名函数 +my_list.sort(key=lambda element: element[1], reverse=True) + +print(my_list) diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/10_GDP\345\212\250\346\200\201\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/10_GDP\345\212\250\346\200\201\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..f1458aa52bb26433d0774e1e39f1c97a55002455 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/10_GDP\345\212\250\346\200\201\346\237\261\347\212\266\345\233\276\345\274\200\345\217\221.py" @@ -0,0 +1,72 @@ +""" +演示第三个图表:GDP动态柱状图开发 +""" +from pyecharts.charts import Bar, Timeline +from pyecharts.options import * +from pyecharts.globals import ThemeType + +# 读取数据 +f = open("D:/1960-2019全球GDP数据.csv", "r", encoding="GB2312") +data_lines = f.readlines() +# 关闭文件 +f.close() +# 删除第一条数据 +data_lines.pop(0) +# 将数据转换为字典存储,格式为: +# { 年份: [ [国家, gdp], [国家,gdp], ...... ], 年份: [ [国家, gdp], [国家,gdp], ...... ], ...... } +# { 1960: [ [美国, 123], [中国,321], ...... ], 1961: [ [美国, 123], [中国,321], ...... ], ...... } +# 先定义一个字典对象 +data_dict = {} +for line in data_lines: + year = int(line.split(",")[0]) # 年份 + country = line.split(",")[1] # 国家 + gdp = float(line.split(",")[2]) # gdp数据 + # 如何判断字典里面有没有指定的key呢? + try: + data_dict[year].append([country, gdp]) + except KeyError: + data_dict[year] = [] + data_dict[year].append([country, gdp]) + +# print(data_dict[1960]) +# 创建时间线对象 +timeline = Timeline({"theme": ThemeType.LIGHT}) +# 排序年份 +sorted_year_list = sorted(data_dict.keys()) +for year in sorted_year_list: + data_dict[year].sort(key=lambda element: element[1], reverse=True) + # 取出本年份前8名的国家 + year_data = data_dict[year][0:8] + x_data = [] + y_data = [] + for country_gdp in year_data: + x_data.append(country_gdp[0]) # x轴添加国家 + y_data.append(country_gdp[1] / 100000000) # y轴添加gdp数据 + + # 构建柱状图 + bar = Bar() + x_data.reverse() + y_data.reverse() + bar.add_xaxis(x_data) + bar.add_yaxis("GDP(亿)", y_data, label_opts=LabelOpts(position="right")) + # 反转x轴和y轴 + bar.reversal_axis() + # 设置每一年的图表的标题 + bar.set_global_opts( + title_opts=TitleOpts(title=f"{year}年全球前8GDP数据") + ) + timeline.add(bar, str(year)) + + +# for循环每一年的数据,基于每一年的数据,创建每一年的bar对象 +# 在for中,将每一年的bar对象添加到时间线中 + +# 设置时间线自动播放 +timeline.add_schema( + play_interval=1000, + is_timeline_show=True, + is_auto_play=True, + is_loop_play=False +) +# 绘图 +timeline.render("1960-2019全球GDP前8国家.html") diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/1960-2019\345\205\250\347\220\203GDP\345\211\2158\345\233\275\345\256\266.html" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/1960-2019\345\205\250\347\220\203GDP\345\211\2158\345\233\275\345\256\266.html" new file mode 100644 index 0000000000000000000000000000000000000000..780f2c362e12f62d5c42c3817fac3090c7050bde --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/1960-2019\345\205\250\347\220\203GDP\345\211\2158\345\233\275\345\256\266.html" @@ -0,0 +1,9546 @@ + + + + + Awesome-pyecharts + + + + +
+ + + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/test.py" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..d0e3b5628236d3a915820055d952f590b8095327 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/test.py" @@ -0,0 +1,90 @@ +import time + + +# account_amount = 0 # 账户余额 +# def atm(num, deposit=True): +# global account_amount +# if deposit: +# account_amount += num +# print(f"存款:+{num},账户余额:{account_amount}") +# else: +# account_amount -= num +# print(f"取款:-{num},账户余额:{account_amount}") +# +# +# atm(300) +# atm(300) +# atm(100, False) +# +# +# def account_create(initial_amount=0): +# def atm(num, deposit=True): +# nonlocal initial_amount +# if deposit: +# initial_amount += num +# print(f"存款:+{num},账户余额:{initial_amount}") +# else: +# initial_amount -= num +# print(f"取款:-{num},账户余额:{initial_amount}") +# +# return atm +# +# +# fn = account_create() +# fn(300) +# fn(200) +# fn(300, False) +# + +# def outer(logo): +# +# def inner(msg): +# print(f"<{logo}>{msg}<{logo}>") +# +# return inner +# +# +# fn1 = outer("黑马程序员") +# fn1("大家好呀") +# fn1("学Python就来") +# +# fn2 = outer("传智教育") +# fn2("IT职业教育培训") +# fn2("学Python就来") + +# def outer(num1): +# +# def inner(num2): +# nonlocal num1 +# num1 += num2 +# print(num1) +# +# return inner +# +# +# fn = outer(10) +# fn(10) +# fn(10) + +# def outer(func): +# def inner(): +# print("我要睡觉了") +# func() +# print("我起床了") +# +# return inner +# +# +# @outer +# def sleep(): +# import random +# import time +# print("睡眠中......") +# time.sleep(random.randint(1, 5)) +# +# +# sleep() + + + + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\205\250\345\233\275\347\226\253\346\203\205\345\234\260\345\233\276.html" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\205\250\345\233\275\347\226\253\346\203\205\345\234\260\345\233\276.html" new file mode 100644 index 0000000000000000000000000000000000000000..54687adacd8573c517e1de62dec59740523fa625 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\205\250\345\233\275\347\226\253\346\203\205\345\234\260\345\233\276.html" @@ -0,0 +1,309 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276.html" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276.html" new file mode 100644 index 0000000000000000000000000000000000000000..4e4ae0f06f5781e38229815f8297a4b134bcc97e --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\227\266\351\227\264\347\272\277\346\237\261\347\212\266\345\233\276.html" @@ -0,0 +1,521 @@ + + + + + Awesome-pyecharts + + + + +
+ + + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276.html" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276.html" new file mode 100644 index 0000000000000000000000000000000000000000..f333a04d1f6153b1c995b797e2427bdf903d722c --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\345\237\272\347\241\200\346\237\261\347\212\266\345\233\276.html" @@ -0,0 +1,159 @@ + + + + + Awesome-pyecharts + + + + +
+ + + diff --git "a/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\346\262\263\345\215\227\347\234\201\347\226\253\346\203\205\345\234\260\345\233\276.html" "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\346\262\263\345\215\227\347\234\201\347\226\253\346\203\205\345\234\260\345\233\276.html" new file mode 100644 index 0000000000000000000000000000000000000000..b36b5ae119a3d9ab34c8e34b43bc85c170018952 --- /dev/null +++ "b/10_\345\217\257\350\247\206\345\214\226\346\241\210\344\276\213/\346\262\263\345\215\227\347\234\201\347\226\253\346\203\205\345\234\260\345\233\276.html" @@ -0,0 +1,257 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/02_\346\210\220\345\221\230\346\226\271\346\263\225.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/02_\346\210\220\345\221\230\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..765ca02f9ad02a115a5a33e10361ca00fad6e7c1 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/02_\346\210\220\345\221\230\346\226\271\346\263\225.py" @@ -0,0 +1,23 @@ +""" +演示面向对象类中的成员方法定义和使用 +""" + +# 定义一个带有成员方法的类 +class Student: + name = None # 学生的姓名 + + def say_hi(self): + print(f"大家好呀,我是{self.name},欢迎大家多多关照") + + + def say_hi2(self, msg): + print(f"大家好,我是:{self.name},{msg}") + + +stu = Student() +stu.name = "周杰轮" +stu.say_hi2("哎哟不错哟") + +stu2 = Student() +stu2.name = "林俊节" +stu2.say_hi2("小伙子我看好你") diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/03_\347\261\273\345\222\214\345\257\271\350\261\241.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/03_\347\261\273\345\222\214\345\257\271\350\261\241.py" new file mode 100644 index 0000000000000000000000000000000000000000..99eade82ab081eb1e1e0e6d9fcb382a97b7712bd --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/03_\347\261\273\345\222\214\345\257\271\350\261\241.py" @@ -0,0 +1,30 @@ +""" +演示类和对象的关系,即面向对象的编程套路(思想) +""" + +# 设计一个闹钟类 +class Clock: + id = None # 序列化 + price = None # 价格 + + + def ring(self): + import winsound + winsound.Beep(2000, 3000) + +# 构建2个闹钟对象并让其工作 +clock1 = Clock() +clock1.id = "003032" +clock1.price = 19.99 +print(f"闹钟ID:{clock1.id},价格:{clock1.price}") +# clock1.ring() + +clock2 = Clock() +clock2.id = "003033" +clock2.price = 21.99 +print(f"闹钟ID:{clock2.id},价格:{clock2.price}") +clock2.ring() + + + + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/04_\346\236\204\351\200\240\346\226\271\346\263\225.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/04_\346\236\204\351\200\240\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..d7528426756d1c71fdc753cd1a94827e4425d8cf --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/04_\346\236\204\351\200\240\346\226\271\346\263\225.py" @@ -0,0 +1,19 @@ +""" +演示类的构造方法 +""" +# 演示使用构造方法对成员变量进行赋值 +# 构造方法的名称:__init__ + +class Student: + + def __init__(self, name, age ,tel): + self.name = name + self.age = age + self.tel = tel + print("Student类创建了一个类对象") + +stu = Student("周杰轮", 31, "18500006666") +print(stu.name) +print(stu.age) +print(stu.tel) + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/05_\345\205\266\345\256\203\345\206\205\347\275\256\346\226\271\346\263\225.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/05_\345\205\266\345\256\203\345\206\205\347\275\256\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..f946600d93254a083543406e8b49cf17d3342544 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/05_\345\205\266\345\256\203\345\206\205\347\275\256\346\226\271\346\263\225.py" @@ -0,0 +1,37 @@ +""" +演示Python内置的各类魔术方法 +""" + + +class Student: + def __init__(self, name, age): + self.name = name # 学生姓名 + self.age = age # 学生年龄 + + # __str__魔术方法 + def __str__(self): + return f"Student类对象,name:{self.name}, age:{self.age}" + + # __lt__魔术方法 + def __lt__(self, other): + return self.age < other.age + + # __le__魔术方法 + def __le__(self, other): + return self.age <= other.age + + # __eq__魔术方法 + def __eq__(self, other): + return self.age == other.age + + +stu1 = Student("周杰轮", 31) +stu2 = Student("林俊节", 36) +print(stu1 == stu2) + + +# + + + + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/06_\345\260\201\350\243\205.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/06_\345\260\201\350\243\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..eae02f2d120ad93449c7739736803e8d1425c501 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/06_\345\260\201\350\243\205.py" @@ -0,0 +1,24 @@ +""" +演示面向对象封装思想中私有成员的使用 +""" + +# 定义一个类,内含私有成员变量和私有成员方法 +class Phone: + __current_voltage = 0.5 # 当前手机运行电压 + + + def __keep_single_core(self): + print("让CPU以单核模式运行") + + + def call_by_5g(self): + if self.__current_voltage >= 1: + print("5g通话已开启") + else: + self.__keep_single_core() + print("电量不足,无法使用5g通话,并已设置为单核运行进行省电。") + + +phone = Phone() +phone.call_by_5g() + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/07_\345\260\201\350\243\205\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/07_\345\260\201\350\243\205\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..c2b350146ceee2976ecd15ab0037b03b968a4a5f --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/07_\345\260\201\350\243\205\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230.py" @@ -0,0 +1,27 @@ +""" +讲解面向对象-封装特性课后练习题 +设计带有私有成员的手机 +""" + +# 设计一个类,用来描述手机 +class Phone: + # 提供私有成员变量:__is_5g_enable + __is_5g_enable = True # 5g状态 + + + # 提供私有成员方法:__check_5g() + def __check_5g(self): + if self.__is_5g_enable: + print("5g开启") + else: + print("5g关闭,使用4g网络") + + # 提供公开成员方法:call_by_5g() + def call_by_5g(self): + self.__check_5g() + print("正在通话中") + + +phone = Phone() +phone.call_by_5g() + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/08_\347\273\247\346\211\277\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/08_\347\273\247\346\211\277\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..b4dbb799af8267509c88529f160763fd94a70683 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/08_\347\273\247\346\211\277\347\232\204\345\237\272\347\241\200\350\257\255\346\263\225.py" @@ -0,0 +1,61 @@ +""" +演示面向对象:继承的基础语法 +""" + +# 演示单继承 +class Phone: + IMEI = None # 序列号 + producer = "ITCAST" # 厂商 + + + def call_by_4g(self): + print("4g通话") + + +class Phone2022(Phone): + face_id = "10001" # 面部识别ID + + + def call_by_5g(self): + print("2022年新功能:5g通话") + + +phone = Phone2022() +print(phone.producer) +phone.call_by_4g() +phone.call_by_5g() +# 演示多继承 +class NFCReader: + nfc_type = "第五代" + producer = "HM" + + def read_card(self): + print("NFC读卡") + + def write_card(self): + print("NFC写卡") + + +class RemoteControl: + rc_type = "红外遥控" + + def control(self): + print("红外遥控开启了") + + +class MyPhone(Phone, NFCReader, RemoteControl): + pass + + +phone = MyPhone() +phone.call_by_4g() +phone.read_card() +phone.write_card() +phone.control() + +print(phone.producer) + + + +# 演示多继承下,父类成员名一致的场景 + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/09_\347\273\247\346\211\277_\345\244\215\345\206\231\345\222\214\344\275\277\347\224\250\347\210\266\347\261\273\346\210\220\345\221\230.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/09_\347\273\247\346\211\277_\345\244\215\345\206\231\345\222\214\344\275\277\347\224\250\347\210\266\347\261\273\346\210\220\345\221\230.py" new file mode 100644 index 0000000000000000000000000000000000000000..40d18eac67ac8ce77bf41e3a2cb83c9b6ed2e755 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/09_\347\273\247\346\211\277_\345\244\215\345\206\231\345\222\214\344\275\277\347\224\250\347\210\266\347\261\273\346\210\220\345\221\230.py" @@ -0,0 +1,34 @@ +""" +演示面向对象:继承中 +对父类成员的复写和调用 +""" + + +class Phone: + IMEI = None # 序列号 + producer = "ITCAST" # 厂商 + + def call_by_5g(self): + print("使用5g网络进行通话") + + +# 定义子类,复写父类成员 +class MyPhone(Phone): + producer = "ITHEIMA" # 复写父类的成员属性 + + def call_by_5g(self): + print("开启CPU单核模式,确保通话的时候省电") + # 方式1 + # print(f"父类的厂商是:{Phone.producer}") + # Phone.call_by_5g(self) + # 方式2 + print(f"父类的厂商是:{super().producer}") + super().call_by_5g() + print("关闭CPU单核模式,确保性能") + +phone = MyPhone() +phone.call_by_5g() +print(phone.producer) + +# 在子类中,调用父类成员 + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/10_\347\261\273\345\236\213\346\263\250\350\247\243_\345\217\230\351\207\217.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/10_\347\261\273\345\236\213\346\263\250\350\247\243_\345\217\230\351\207\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..d7fe4bd67db1fb9efbede915486f8ba9f475701f --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/10_\347\261\273\345\236\213\346\263\250\350\247\243_\345\217\230\351\207\217.py" @@ -0,0 +1,35 @@ +""" +演示变量的类型注解 +""" + +# 基础数据类型注解 +import json +import random + +# var_1: int = 10 +# var_2: str = "itheima" +# var_3: bool = True +# 类对象类型注解 +class Student: + pass +stu: Student = Student() + +# 基础容器类型注解 +# my_list: list = [1, 2, 3] +# my_tuple: tuple = (1, 2, 3) +# my_dict: dict = {"itheima": 666} +# 容器类型详细注解 +my_list: list[int] = [1, 2, 3] +my_tuple: tuple[int, str, bool] = (1, "itheima", True) +my_dict: dict[str, int] = {"itheima": 666} +# 在注释中进行类型注解 +var_1 = random.randint(1, 10) # type: int +var_2 = json.loads('{"name": "zhangsan"}') # type: dict[str, str] +def func(): + return 10 +var_3 = func() # type: int +# 类型注解的限制 +var_4: int = "itheima" +var_5: str = 123 + + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/11_\347\261\273\345\236\213\346\263\250\350\247\243_\345\207\275\346\225\260\345\222\214\346\226\271\346\263\225.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/11_\347\261\273\345\236\213\346\263\250\350\247\243_\345\207\275\346\225\260\345\222\214\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..edab1500f8ee9419262659b1117862e1deb1881a --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/11_\347\261\273\345\236\213\346\263\250\350\247\243_\345\207\275\346\225\260\345\222\214\346\226\271\346\263\225.py" @@ -0,0 +1,13 @@ +""" +演示对函数(方法)进行类型注解 +""" + +# 对形参进行类型注解 +def add(x: int, y: int): + return x + y + +# 对返回值进行类型注解 +def func(data: list) -> list: + return data + +print(func(1)) diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/12_\347\261\273\345\236\213\346\263\250\350\247\243_Union\350\201\224\345\220\210\347\261\273\345\236\213.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/12_\347\261\273\345\236\213\346\263\250\350\247\243_Union\350\201\224\345\220\210\347\261\273\345\236\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..840e964003a244167f674b182e068579cf034a4b --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/12_\347\261\273\345\236\213\346\263\250\350\247\243_Union\350\201\224\345\220\210\347\261\273\345\236\213.py" @@ -0,0 +1,11 @@ +""" +演示Union联合类型注解 +""" +# 使用Union类型,必须先导包 +from typing import Union + +my_list: list[Union[int, str]] = [1, 2, "itheima", "itcast"] + +def func(data: Union[int, str]) -> Union[int, str]: + pass + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/13_\345\244\232\346\200\201.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/13_\345\244\232\346\200\201.py" new file mode 100644 index 0000000000000000000000000000000000000000..e2ea4e336f8e48367b34e9b044626c8460bee401 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/13_\345\244\232\346\200\201.py" @@ -0,0 +1,80 @@ +""" +演示面向对象的多态特性以及抽象类(接口)的使用 +""" + + +class Animal: + def speak(self): + pass + + +class Dog(Animal): + def speak(self): + print("汪汪汪") + + +class Cat(Animal): + def speak(self): + print("喵喵喵") + + +def make_noise(animal: Animal): + """制造点噪音,需要传入Animal对象""" + animal.speak() + + +# 演示多态,使用2个子类对象来调用函数 +dog = Dog() +cat = Cat() + +make_noise(dog) +make_noise(cat) + + +# 演示抽象类 +class AC: + def cool_wind(self): + """制冷""" + pass + + def hot_wind(self): + """制热""" + pass + + def swing_l_r(self): + """左右摆风""" + pass + + +class Midea_AC(AC): + def cool_wind(self): + print("美的空调制冷") + + def hot_wind(self): + print("美的空调制热") + + def swing_l_r(self): + print("美的空调左右摆风") + + +class GREE_AC(AC): + def cool_wind(self): + print("格力空调制冷") + + def hot_wind(self): + print("格力空调制热") + + def swing_l_r(self): + print("格力空调左右摆风") + + +def make_cool(ac: AC): + ac.cool_wind() + + +midea_ac = Midea_AC() +gree_ac = GREE_AC() + + +make_cool(midea_ac) +make_cool(gree_ac) diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/data_define.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/data_define.py" new file mode 100644 index 0000000000000000000000000000000000000000..6d3428f142047869246d8770cefdff8bd7a924e4 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/data_define.py" @@ -0,0 +1,16 @@ +""" +数据定义的类 +""" + + +class Record: + + def __init__(self, date, order_id, money, province): + self.date = date # 订单日期 + self.order_id = order_id # 订单ID + self.money = money # 订单金额 + self.province = province # 销售省份 + + + def __str__(self): + return f"{self.date}, {self.order_id}, {self.money}, {self.province}" diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/file_define.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/file_define.py" new file mode 100644 index 0000000000000000000000000000000000000000..625000d8e5ee89c81d9a39899aafc1698c3a5b30 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/file_define.py" @@ -0,0 +1,66 @@ +""" +和文件相关的类定义 +""" +import json + +from data_define import Record + + +# 先定义一个抽象类用来做顶层设计,确定有哪些功能需要实现 +class FileReader: + + def read_data(self) -> list[Record]: + """读取文件的数据,读到的每一条数据都转换为Record对象,将它们都封装到list内返回即可""" + pass + + +class TextFileReader(FileReader): + + def __init__(self, path): + self.path = path # 定义成员变量记录文件的路径 + + # 复写(实现抽象方法)父类的方法 + def read_data(self) -> list[Record]: + f = open(self.path, "r", encoding="UTF-8") + + record_list: list[Record] = [] + for line in f.readlines(): + line = line.strip() # 消除读取到的每一行数据中的\n + data_list = line.split(",") + record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3]) + record_list.append(record) + + f.close() + return record_list + + +class JsonFileReader(FileReader): + + def __init__(self, path): + self.path = path # 定义成员变量记录文件的路径 + + + def read_data(self) -> list[Record]: + f = open(self.path, "r", encoding="UTF-8") + + record_list: list[Record] = [] + for line in f.readlines(): + data_dict = json.loads(line) + record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"]) + record_list.append(record) + + f.close() + return record_list + + +if __name__ == '__main__': + text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") + json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") + list1 = text_file_reader.read_data() + list2 = json_file_reader.read_data() + + for l in list1: + print(l) + + for l in list2: + print(l) \ No newline at end of file diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/main.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/main.py" new file mode 100644 index 0000000000000000000000000000000000000000..934a10746d64268ac8d7ea7a209bd693fc8612ad --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/main.py" @@ -0,0 +1,44 @@ +""" +面向对象,数据分析案例,主业务逻辑代码 +实现步骤: +1. 设计一个类,可以完成数据的封装 +2. 设计一个抽象类,定义文件读取的相关功能,并使用子类实现具体功能 +3. 读取文件,生产数据对象 +4. 进行数据需求的逻辑计算(计算每一天的销售额) +5. 通过PyEcharts进行图形绘制 +""" +from file_define import FileReader, TextFileReader, JsonFileReader +from data_define import Record +from pyecharts.charts import Bar +from pyecharts.options import * +from pyecharts.globals import ThemeType + +text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") +json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") + +jan_data: list[Record] = text_file_reader.read_data() +feb_data: list[Record] = json_file_reader.read_data() +# 将2个月份的数据合并为1个list来存储 +all_data: list[Record] = jan_data + feb_data + +# 开始进行数据计算 +# {"2011-01-01": 1534, "2011-01-02": 300, "2011-01-03": 650} +data_dict = {} +for record in all_data: + if record.date in data_dict.keys(): + # 当前日期已经有记录了,所以和老记录做累加即可 + data_dict[record.date] += record.money + else: + data_dict[record.date] = record.money + +# 可视化图表开发 +bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT)) + +bar.add_xaxis(list(data_dict.keys())) # 添加x轴的数据 +bar.add_yaxis("销售额", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) # 添加了y轴数据 +bar.set_global_opts( + title_opts=TitleOpts(title="每日销售额") +) + +bar.render("每日销售额柱状图.html") + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/\346\257\217\346\227\245\351\224\200\345\224\256\351\242\235\346\237\261\347\212\266\345\233\276.html" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/\346\257\217\346\227\245\351\224\200\345\224\256\351\242\235\346\237\261\347\212\266\345\233\276.html" new file mode 100644 index 0000000000000000000000000000000000000000..8b77b43538ef041fb228f950913331fb02f92af4 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\346\225\260\346\215\256\345\210\206\346\236\220\346\241\210\344\276\213/\346\257\217\346\227\245\351\224\200\345\224\256\351\242\235\346\237\261\347\212\266\345\233\276.html" @@ -0,0 +1,257 @@ + + + + + Awesome-pyecharts + + + + +
+ + + diff --git "a/11_\351\235\242\345\220\221\345\257\271\350\261\241/\351\273\221\351\251\254\351\223\266\350\241\214ATM\346\241\210\344\276\213/main.py" "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\351\273\221\351\251\254\351\223\266\350\241\214ATM\346\241\210\344\276\213/main.py" new file mode 100644 index 0000000000000000000000000000000000000000..07a2d1679301376402f8c05244c696c7df8b7360 --- /dev/null +++ "b/11_\351\235\242\345\220\221\345\257\271\350\261\241/\351\273\221\351\251\254\351\223\266\350\241\214ATM\346\241\210\344\276\213/main.py" @@ -0,0 +1,11 @@ +""" +面向对象,数据分析案例,主业务逻辑代码 +实现步骤: +1. 设计一个类,可以完成数据的封装 +2. 设计一个抽象类,定义文件读取的相关功能,并使用子类实现具体功能 +3. 读取文件,生产数据对象 +4. 进行数据需求的逻辑计算(计算每一天的销售额) +5. 通过PyEcharts进行图形绘制 +""" + + diff --git "a/12_sql/01_pymysql\345\205\245\351\227\250.py" "b/12_sql/01_pymysql\345\205\245\351\227\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..d6fdb8c9a1e4006996926f6bc3717156b01c0a00 --- /dev/null +++ "b/12_sql/01_pymysql\345\205\245\351\227\250.py" @@ -0,0 +1,23 @@ +""" +演示Python pymysql库的基础操作 +""" +from pymysql import Connection + +# 构建到MySQL数据库的链接 +conn = Connection( + host="localhost", # 主机名(IP) + port=3306, # 端口 + user="root", # 账户 + password="123456", # 密码 + autocommit=True # 设置自动提交 +) + +# print(conn.get_server_info()) +# 执行非查询性质SQL +cursor = conn.cursor() # 获取到游标对象 +# 选择数据库 +conn.select_db("world") +# 执行sql +cursor.execute("insert into student values(10001, '周杰轮', 31, '男')") +# 关闭链接 +conn.close() diff --git "a/12_sql/02_pymysql_\346\225\260\346\215\256\346\217\222\345\205\245.py" "b/12_sql/02_pymysql_\346\225\260\346\215\256\346\217\222\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..856ef7b1954a842f70a3afdea7f1b973c99780cb --- /dev/null +++ "b/12_sql/02_pymysql_\346\225\260\346\215\256\346\217\222\345\205\245.py" @@ -0,0 +1,25 @@ +""" +演示使用pymysql库进行数据插入的操作 +""" +from pymysql import Connection + +# 构建到MySQL数据库的链接 +conn = Connection( + host="localhost", # 主机名(IP) + port=3306, # 端口 + user="root", # 账户 + password="123456", # 密码 + autocommit=True # 自动提交(确认) +) + +# print(conn.get_server_info()) +# 执行非查询性质SQL +cursor = conn.cursor() # 获取到游标对象 +# 选择数据库 +conn.select_db("world") +# 执行sql +cursor.execute("insert into student values(10002, '林俊节', 31, '男')") +# # 通过commit确认 +# conn.commit() +# 关闭链接 +conn.close() diff --git a/12_sql/data_define.py b/12_sql/data_define.py new file mode 100644 index 0000000000000000000000000000000000000000..27b5f43cca1626f67df4174d33c9cc908594a704 --- /dev/null +++ b/12_sql/data_define.py @@ -0,0 +1,22 @@ +""" +数据定义的类 +""" + + +class Record: + + def __init__(self, date, order_id, money, province): + self.date = date # 订单日期 + self.order_id = order_id # 订单ID + self.money = money # 订单金额 + self.province = province # 销售省份 + + + def __str__(self): + return f"{self.date}, {self.order_id}, {self.money}, {self.province}" + + + def to_json(self): + d = {"date": self.date, "order_id": self.order_id, "money": self.money, "province": self.province} + import json + return json.dumps(d) diff --git a/12_sql/file_define.py b/12_sql/file_define.py new file mode 100644 index 0000000000000000000000000000000000000000..625000d8e5ee89c81d9a39899aafc1698c3a5b30 --- /dev/null +++ b/12_sql/file_define.py @@ -0,0 +1,66 @@ +""" +和文件相关的类定义 +""" +import json + +from data_define import Record + + +# 先定义一个抽象类用来做顶层设计,确定有哪些功能需要实现 +class FileReader: + + def read_data(self) -> list[Record]: + """读取文件的数据,读到的每一条数据都转换为Record对象,将它们都封装到list内返回即可""" + pass + + +class TextFileReader(FileReader): + + def __init__(self, path): + self.path = path # 定义成员变量记录文件的路径 + + # 复写(实现抽象方法)父类的方法 + def read_data(self) -> list[Record]: + f = open(self.path, "r", encoding="UTF-8") + + record_list: list[Record] = [] + for line in f.readlines(): + line = line.strip() # 消除读取到的每一行数据中的\n + data_list = line.split(",") + record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3]) + record_list.append(record) + + f.close() + return record_list + + +class JsonFileReader(FileReader): + + def __init__(self, path): + self.path = path # 定义成员变量记录文件的路径 + + + def read_data(self) -> list[Record]: + f = open(self.path, "r", encoding="UTF-8") + + record_list: list[Record] = [] + for line in f.readlines(): + data_dict = json.loads(line) + record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"]) + record_list.append(record) + + f.close() + return record_list + + +if __name__ == '__main__': + text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") + json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") + list1 = text_file_reader.read_data() + list2 = json_file_reader.read_data() + + for l in list1: + print(l) + + for l in list2: + print(l) \ No newline at end of file diff --git a/12_sql/main.py b/12_sql/main.py new file mode 100644 index 0000000000000000000000000000000000000000..2115534f3d1fdd89f38ebf40de9bd28856912732 --- /dev/null +++ b/12_sql/main.py @@ -0,0 +1,39 @@ +""" +SQL 综合案例,读取文件,写入MySQL数据库中 +""" +from file_define import TextFileReader, JsonFileReader +from data_define import Record +from pymysql import Connection + +text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") +json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") + +jan_data: list[Record] = text_file_reader.read_data() +feb_data: list[Record] = json_file_reader.read_data() +# 将2个月份的数据合并为1个list来存储 +all_data: list[Record] = jan_data + feb_data + +# 构建MySQL链接对象 +conn = Connection( + host="localhost", + port=3306, + user="root", + password="123456", + autocommit=True +) +# 获得游标对象 +cursor = conn.cursor() +# 选择数据库 +conn.select_db("py_sql") +# 组织SQL语句 +for record in all_data: + sql = f"insert into orders(order_date, order_id, money, province) " \ + f"values('{record.date}', '{record.order_id}', {record.money}, '{record.province}')" + # 执行SQL语句 + cursor.execute(sql) + +# 关闭MySQL链接对象 +conn.close() + + + diff --git a/12_sql/main2.py b/12_sql/main2.py new file mode 100644 index 0000000000000000000000000000000000000000..8ad4fb49ab8c4c9774d27d6a175e3c497e337d9e --- /dev/null +++ b/12_sql/main2.py @@ -0,0 +1,27 @@ +from data_define import Record +from pymysql import Connection + +f = open("d:/output.json", "w", encoding="UTF-8") +# 构建MySQL链接对象 +conn = Connection( + host="localhost", + port=3306, + user="root", + password="123456", + autocommit=True +) +# 获得游标对象 +cursor = conn.cursor() +# 选择数据库 +conn.select_db("py_sql") +# 查询 +cursor.execute("SELECT * FROM orders") +result = cursor.fetchall() +for r in result: + record = Record(r[0], r[1], r[2], r[3]) + f.write(record.to_json()) + f.write("\n") + +# 关闭MySQL链接对象 +conn.close() +f.close() diff --git "a/13_pyspark/01_\345\237\272\347\241\200\345\207\206\345\244\207.py" "b/13_pyspark/01_\345\237\272\347\241\200\345\207\206\345\244\207.py" new file mode 100644 index 0000000000000000000000000000000000000000..359b2ce2989cc909eb06af596cdf477f0d2478d2 --- /dev/null +++ "b/13_pyspark/01_\345\237\272\347\241\200\345\207\206\345\244\207.py" @@ -0,0 +1,15 @@ +""" +演示获取PySpark的执行环境入库对象:SparkContext +并通过SparkContext对象获取当前PySpark的版本 +""" + +# 导包 +from pyspark import SparkConf, SparkContext +# 创建SparkConf类对象 +conf = SparkConf().setMaster("local[*]").setAppName("test_spark_app") +# 基于SparkConf类对象创建SparkContext对象 +sc = SparkContext(conf=conf) +# 打印PySpark的运行版本 +print(sc.version) +# 停止SparkContext对象的运行(停止PySpark程序) +sc.stop() diff --git "a/13_pyspark/02_\346\225\260\346\215\256\350\276\223\345\205\245.py" "b/13_pyspark/02_\346\225\260\346\215\256\350\276\223\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..946df233e6bdbe48e435acdfc6feca12cb0bbd2b --- /dev/null +++ "b/13_pyspark/02_\346\225\260\346\215\256\350\276\223\345\205\245.py" @@ -0,0 +1,27 @@ +""" +演示通过PySpark代码加载数据,即数据输入 +""" +from pyspark import SparkConf, SparkContext + +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# # 通过parallelize方法将Python对象加载到Spark内,成为RDD对象 +# rdd1 = sc.parallelize([1, 2, 3, 4, 5]) +# rdd2 = sc.parallelize((1, 2, 3, 4, 5)) +# rdd3 = sc.parallelize("abcdefg") +# rdd4 = sc.parallelize({1, 2, 3, 4, 5}) +# rdd5 = sc.parallelize({"key1": "value1", "key2": "value2"}) +# +# # 如果要查看RDD里面有什么内容,需要用collect()方法 +# print(rdd1.collect()) +# print(rdd2.collect()) +# print(rdd3.collect()) +# print(rdd4.collect()) +# print(rdd5.collect()) + +# 用过textFile方法,读取文件数据加载到Spark内,成为RDD对象 +rdd = sc.textFile("D:/hello.txt") +print(rdd.collect()) +rdd.map() +sc.stop() diff --git "a/13_pyspark/03_\346\225\260\346\215\256\350\256\241\347\256\227_map\346\226\271\346\263\225.py" "b/13_pyspark/03_\346\225\260\346\215\256\350\256\241\347\256\227_map\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..7a38c907ed458646ab9b42b28e8d165c2645d540 --- /dev/null +++ "b/13_pyspark/03_\346\225\260\346\215\256\350\256\241\347\256\227_map\346\226\271\346\263\225.py" @@ -0,0 +1,23 @@ +""" +演示RDD的map成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备一个RDD +rdd = sc.parallelize([1, 2, 3, 4, 5]) +# 通过map方法将全部数据都乘以10 +# def func(data): +# return data * 10 + +rdd2 = rdd.map(lambda x: x * 10).map(lambda x: x + 5) + +print(rdd2.collect()) +# (T) -> U +# (T) -> T + +# 链式调用 + diff --git "a/13_pyspark/04_\346\225\260\346\215\256\350\256\241\347\256\227_flatMap\346\226\271\346\263\225.py" "b/13_pyspark/04_\346\225\260\346\215\256\350\256\241\347\256\227_flatMap\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..4fb080262e7b451e0116ccf0763303dd6d41887c --- /dev/null +++ "b/13_pyspark/04_\346\225\260\346\215\256\350\256\241\347\256\227_flatMap\346\226\271\346\263\225.py" @@ -0,0 +1,15 @@ +""" +演示RDD的flatMap成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备一个RDD +rdd = sc.parallelize(["itheima itcast 666", "itheima itheima itcast", "python itheima"]) + +# 需求,将RDD数据里面的一个个单词提取出来 +rdd2 = rdd.flatMap(lambda x: x.split(" ")) +print(rdd2.collect()) diff --git "a/13_pyspark/05_\346\225\260\346\215\256\350\256\241\347\256\227_reduceByKey\346\226\271\346\263\225.py" "b/13_pyspark/05_\346\225\260\346\215\256\350\256\241\347\256\227_reduceByKey\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..f9969b9d0c513c8a74185aefd14968b44189797c --- /dev/null +++ "b/13_pyspark/05_\346\225\260\346\215\256\350\256\241\347\256\227_reduceByKey\346\226\271\346\263\225.py" @@ -0,0 +1,14 @@ +""" +演示RDD的reduceByKey成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备一个RDD +rdd = sc.parallelize([('男', 99), ('男', 88), ('女', 99), ('女', 66)]) +# 求男生和女生两个组的成绩之和 +rdd2 = rdd.reduceByKey(lambda a, b: a + b) +print(rdd2.collect()) diff --git "a/13_pyspark/06_\347\273\203\344\271\240\346\241\210\344\276\2131.py" "b/13_pyspark/06_\347\273\203\344\271\240\346\241\210\344\276\2131.py" new file mode 100644 index 0000000000000000000000000000000000000000..4112d0c365b7f1e8bc4445c13e3ddf55c56c2bcd --- /dev/null +++ "b/13_pyspark/06_\347\273\203\344\271\240\346\241\210\344\276\2131.py" @@ -0,0 +1,21 @@ +""" +完成练习案例:单词计数统计 +""" + +# 1. 构建执行环境入口对象 +from pyspark import SparkContext, SparkConf +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) +# 2. 读取数据文件 +rdd = sc.textFile("D:/hello.txt") +# 3. 取出全部单词 +word_rdd = rdd.flatMap(lambda x: x.split(" ")) +# 4. 将所有单词都转换成二元元组,单词为Key,value设置为1 +word_with_one_rdd = word_rdd.map(lambda word: (word, 1)) +# 5. 分组并求和 +result_rdd = word_with_one_rdd.reduceByKey(lambda a, b: a + b) +# 6. 打印输出结果 +print(result_rdd.collect()) + diff --git "a/13_pyspark/07_\346\225\260\346\215\256\350\256\241\347\256\227_filter\346\226\271\346\263\225.py" "b/13_pyspark/07_\346\225\260\346\215\256\350\256\241\347\256\227_filter\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..9375cf4cf6257b66e28321ffbe9bcf481485b110 --- /dev/null +++ "b/13_pyspark/07_\346\225\260\346\215\256\350\256\241\347\256\227_filter\346\226\271\346\263\225.py" @@ -0,0 +1,16 @@ +""" +演示RDD的filter成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备一个RDD +rdd = sc.parallelize([1, 2, 3, 4, 5]) +# 对RDD的数据进行过滤 +rdd2 = rdd.filter(lambda num: num % 2 == 0) + +print(rdd2.collect()) + diff --git "a/13_pyspark/08_\346\225\260\346\215\256\350\256\241\347\256\227_distinct\346\226\271\346\263\225.py" "b/13_pyspark/08_\346\225\260\346\215\256\350\256\241\347\256\227_distinct\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..d63aab0bb022f9f9f4557761c95042b8d2ee4fd3 --- /dev/null +++ "b/13_pyspark/08_\346\225\260\346\215\256\350\256\241\347\256\227_distinct\346\226\271\346\263\225.py" @@ -0,0 +1,16 @@ +""" +演示RDD的distinct成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备一个RDD +rdd = sc.parallelize([1, 1, 3, 3, 5, 5, 7, 8, 8, 9, 10]) +# 对RDD的数据进行去重 +rdd2 = rdd.distinct() + +print(rdd2.collect()) + diff --git "a/13_pyspark/09_\346\225\260\346\215\256\350\256\241\347\256\227_sortBy\346\226\271\346\263\225.py" "b/13_pyspark/09_\346\225\260\346\215\256\350\256\241\347\256\227_sortBy\346\226\271\346\263\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..891d07801b6b26551959dad477d3b28be11a4925 --- /dev/null +++ "b/13_pyspark/09_\346\225\260\346\215\256\350\256\241\347\256\227_sortBy\346\226\271\346\263\225.py" @@ -0,0 +1,23 @@ +""" +演示RDD的sortBy成员方法的使用 +""" +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = "D:/dev/python/python310/python.exe" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 1. 读取数据文件 +rdd = sc.textFile("D:/hello.txt") +# 2. 取出全部单词 +word_rdd = rdd.flatMap(lambda x: x.split(" ")) +# 3. 将所有单词都转换成二元元组,单词为Key,value设置为1 +word_with_one_rdd = word_rdd.map(lambda word: (word, 1)) +# 4. 分组并求和 +result_rdd = word_with_one_rdd.reduceByKey(lambda a, b: a + b) +# 5. 对结果进行排序 +final_rdd = result_rdd.sortBy(lambda x: x[1], ascending=True, numPartitions=1) +print(final_rdd.collect()) + + + diff --git "a/13_pyspark/10_\347\273\203\344\271\240\346\241\210\344\276\2132.py" "b/13_pyspark/10_\347\273\203\344\271\240\346\241\210\344\276\2132.py" new file mode 100644 index 0000000000000000000000000000000000000000..06722744e17ddd3c3e776bc95e52700f3e72152d --- /dev/null +++ "b/13_pyspark/10_\347\273\203\344\271\240\346\241\210\344\276\2132.py" @@ -0,0 +1,42 @@ +""" +完成练习案例:JSON商品统计 +需求: +1. 各个城市销售额排名,从大到小 +2. 全部城市,有哪些商品类别在售卖 +3. 北京市有哪些商品类别在售卖 +""" +from pyspark import SparkConf, SparkContext +import os +import json +os.environ['PYSPARK_PYTHON'] = 'D:/dev/python/python310/python.exe' +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# TODO 需求1: 城市销售额排名 +# 1.1 读取文件得到RDD +file_rdd = sc.textFile("D:/orders.txt") +# 1.2 取出一个个JSON字符串 +json_str_rdd = file_rdd.flatMap(lambda x: x.split("|")) +# 1.3 将一个个JSON字符串转换为字典 +dict_rdd = json_str_rdd.map(lambda x: json.loads(x)) +# 1.4 取出城市和销售额数据 +# (城市,销售额) +city_with_money_rdd = dict_rdd.map(lambda x: (x['areaName'], int(x['money']))) +# 1.5 按城市分组按销售额聚合 +city_result_rdd = city_with_money_rdd.reduceByKey(lambda a, b: a + b) +# 1.6 按销售额聚合结果进行排序 +result1_rdd = city_result_rdd.sortBy(lambda x: x[1], ascending=False, numPartitions=1) +print("需求1的结果:", result1_rdd.collect()) +# TODO 需求2: 全部城市有哪些商品类别在售卖 +# 2.1 取出全部的商品类别 +category_rdd = dict_rdd.map(lambda x: x['category']).distinct() +print("需求2的结果:", category_rdd.collect()) +# 2.2 对全部商品类别进行去重 +# TODO 需求3: 北京市有哪些商品类别在售卖 +# 3.1 过滤北京市的数据 +beijing_data_rdd = dict_rdd.filter(lambda x: x['areaName'] == '北京') +# 3.2 取出全部商品类别 +result3_rdd = beijing_data_rdd.map(lambda x: x['category']).distinct() +print("需求3的结果:", result3_rdd.collect()) +# 3.3 进行商品类别去重 + diff --git "a/13_pyspark/11_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\344\270\272Python\345\257\271\350\261\241.py" "b/13_pyspark/11_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\344\270\272Python\345\257\271\350\261\241.py" new file mode 100644 index 0000000000000000000000000000000000000000..57aee07d86d971a67285b75a691ec37b569890e9 --- /dev/null +++ "b/13_pyspark/11_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\344\270\272Python\345\257\271\350\261\241.py" @@ -0,0 +1,29 @@ +""" +演示将RDD输出为Python对象 +""" + +from pyspark import SparkConf, SparkContext +import os +import json +os.environ['PYSPARK_PYTHON'] = 'D:/dev/python/python310/python.exe' +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +sc = SparkContext(conf=conf) + +# 准备RDD +rdd = sc.parallelize([1, 2, 3, 4, 5]) + +# collect算子,输出RDD为list对象 +rdd_list: list = rdd.collect() +print(rdd_list) +print(type(rdd_list)) +# reduce算子,对RDD进行两两聚合 +num = rdd.reduce(lambda a, b: a + b) +print(num) +# take算子,取出RDD前N个元素,组成list返回 +take_list = rdd.take(3) +print(take_list) +# count,统计rdd内有多少条数据,返回值为数字 +num_count = rdd.count() +print(f"rdd内有{num_count}个元素") + +sc.stop() diff --git "a/13_pyspark/12_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\345\210\260\346\226\207\344\273\266.py" "b/13_pyspark/12_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\345\210\260\346\226\207\344\273\266.py" new file mode 100644 index 0000000000000000000000000000000000000000..82ebd71ad5fb140049ac8715e7b706e21b8615a1 --- /dev/null +++ "b/13_pyspark/12_\346\225\260\346\215\256\350\276\223\345\207\272_\350\276\223\345\207\272\345\210\260\346\226\207\344\273\266.py" @@ -0,0 +1,26 @@ +""" +演示将RDD输出到文件中 +""" + +from pyspark import SparkConf, SparkContext +import os +import json +os.environ['PYSPARK_PYTHON'] = 'D:/dev/python/python310/python.exe' +os.environ['HADOOP_HOME'] = "D:/dev/hadoop-3.0.0" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") + +sc = SparkContext(conf=conf) + +# 准备RDD1 +rdd1 = sc.parallelize([1, 2, 3, 4, 5], numSlices=1) + +# 准备RDD2 +rdd2 = sc.parallelize([("Hello", 3), ("Spark", 5), ("Hi", 7)], 1) + +# 准备RDD3 +rdd3 = sc.parallelize([[1, 3, 5], [6, 7, 9], [11, 13, 11]], 1) + +# 输出到文件中 +rdd1.saveAsTextFile("D:/output1") +rdd2.saveAsTextFile("D:/output2") +rdd3.saveAsTextFile("D:/output3") diff --git "a/13_pyspark/13_\347\273\274\345\220\210\346\241\210\344\276\213.py" "b/13_pyspark/13_\347\273\274\345\220\210\346\241\210\344\276\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..02747ab0162b3d359e040a9d98c99cca91b01c8c --- /dev/null +++ "b/13_pyspark/13_\347\273\274\345\220\210\346\241\210\344\276\213.py" @@ -0,0 +1,59 @@ +""" +演示PySpark综合案例 +""" + +from pyspark import SparkConf, SparkContext +import os +import json +os.environ['PYSPARK_PYTHON'] = 'D:/dev/python/python310/python.exe' +os.environ['HADOOP_HOME'] = "D:/dev/hadoop-3.0.0" +conf = SparkConf().setMaster("local[*]").setAppName("test_spark") +conf.set("spark.default.parallelism", "1") +sc = SparkContext(conf=conf) + +# 读取文件转换成RDD +file_rdd = sc.textFile("D:/search_log.txt") +# TODO 需求1: 热门搜索时间段Top3(小时精度) +# 1.1 取出全部的时间并转换为小时 +# 1.2 转换为(小时, 1) 的二元元组 +# 1.3 Key分组聚合Value +# 1.4 排序(降序) +# 1.5 取前3 +result1 = file_rdd.map(lambda x: (x.split("\t")[0][:2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(3) +print("需求1的结果:", result1) + +# TODO 需求2: 热门搜索词Top3 +# 2.1 取出全部的搜索词 +# 2.2 (词, 1) 二元元组 +# 2.3 分组聚合 +# 2.4 排序 +# 2.5 Top3 +result2 = file_rdd.map(lambda x: (x.split("\t")[2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(3) +print("需求2的结果:", result2) + +# TODO 需求3: 统计黑马程序员关键字在什么时段被搜索的最多 +# 3.1 过滤内容,只保留黑马程序员关键词 +# 3.2 转换为(小时, 1) 的二元元组 +# 3.3 Key分组聚合Value +# 3.4 排序(降序) +# 3.5 取前1 +result3 = file_rdd.map(lambda x: x.split("\t")).\ + filter(lambda x: x[2] == '黑马程序员').\ + map(lambda x: (x[0][:2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(1) +print("需求3的结果:", result3) + +# TODO 需求4: 将数据转换为JSON格式,写出到文件中 +# 4.1 转换为JSON格式的RDD +# 4.2 写出为文件 +file_rdd.map(lambda x: x.split("\t")).\ + map(lambda x: {"time": x[0], "user_id": x[1], "key_word": x[2], "rank1": x[3], "rank2": x[4], "url": x[5]}).\ + saveAsTextFile("D:/output_json") diff --git "a/13_pyspark/14_\347\273\274\345\220\210\346\241\210\344\276\213_\351\233\206\347\276\244\350\277\220\350\241\214.py" "b/13_pyspark/14_\347\273\274\345\220\210\346\241\210\344\276\213_\351\233\206\347\276\244\350\277\220\350\241\214.py" new file mode 100644 index 0000000000000000000000000000000000000000..ab282595459fae708a79bf08d6d16ba011a3be34 --- /dev/null +++ "b/13_pyspark/14_\347\273\274\345\220\210\346\241\210\344\276\213_\351\233\206\347\276\244\350\277\220\350\241\214.py" @@ -0,0 +1,58 @@ +""" +演示PySpark综合案例 +""" + +from pyspark import SparkConf, SparkContext +import os +os.environ['PYSPARK_PYTHON'] = '/export/server/anaconda3/bin/python' +os.environ['HADOOP_HOME'] = "/export/server/hadoop-3.3.1" +conf = SparkConf().setAppName("spark_cluster") +conf.set("spark.default.parallelism", "24") +sc = SparkContext(conf=conf) + +# 读取文件转换成RDD +file_rdd = sc.textFile("hdfs://m1:8020/data/search_log.txt") +# TODO 需求1: 热门搜索时间段Top3(小时精度) +# 1.1 取出全部的时间并转换为小时 +# 1.2 转换为(小时, 1) 的二元元组 +# 1.3 Key分组聚合Value +# 1.4 排序(降序) +# 1.5 取前3 +result1 = file_rdd.map(lambda x: (x.split("\t")[0][:2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(3) +print("需求1的结果:", result1) + +# TODO 需求2: 热门搜索词Top3 +# 2.1 取出全部的搜索词 +# 2.2 (词, 1) 二元元组 +# 2.3 分组聚合 +# 2.4 排序 +# 2.5 Top3 +result2 = file_rdd.map(lambda x: (x.split("\t")[2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(3) +print("需求2的结果:", result2) + +# TODO 需求3: 统计黑马程序员关键字在什么时段被搜索的最多 +# 3.1 过滤内容,只保留黑马程序员关键词 +# 3.2 转换为(小时, 1) 的二元元组 +# 3.3 Key分组聚合Value +# 3.4 排序(降序) +# 3.5 取前1 +result3 = file_rdd.map(lambda x: x.split("\t")).\ + filter(lambda x: x[2] == '黑马程序员').\ + map(lambda x: (x[0][:2], 1)).\ + reduceByKey(lambda a, b: a + b).\ + sortBy(lambda x: x[1], ascending=False, numPartitions=1).\ + take(1) +print("需求3的结果:", result3) + +# TODO 需求4: 将数据转换为JSON格式,写出到文件中 +# 4.1 转换为JSON格式的RDD +# 4.2 写出为文件 +file_rdd.map(lambda x: x.split("\t")).\ + map(lambda x: {"time": x[0], "user_id": x[1], "key_word": x[2], "rank1": x[3], "rank2": x[4], "url": x[5]}).\ + saveAsTextFile("hdfs://m1:8020/output/output_json") diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/01_\351\227\255\345\214\205.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/01_\351\227\255\345\214\205.py" new file mode 100644 index 0000000000000000000000000000000000000000..806aa29db01c8649080503145d6c567e3c3259de --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/01_\351\227\255\345\214\205.py" @@ -0,0 +1,57 @@ +""" +演示Python的闭包特性 +""" + +# 简单闭包 +# def outer(logo): +# +# def inner(msg): +# print(f"<{logo}>{msg}<{logo}>") +# +# return inner +# +# +# fn1 = outer("黑马程序员") +# fn1("大家好") +# fn1("大家好") +# +# fn2 = outer("传智教育") +# fn2("大家好") +# 使用nonlocal关键字修改外部函数的值 +# def outer(num1): +# +# def inner(num2): +# nonlocal num1 +# num1 += num2 +# print(num1) +# +# return inner +# +# fn = outer(10) +# fn(10) +# fn(10) +# fn(10) +# fn(10) + +# 使用闭包实现ATM小案例 +def account_create(initial_amount=0): + + def atm(num, deposit=True): + nonlocal initial_amount + if deposit: + initial_amount += num + print(f"存款:+{num}, 账户余额:{initial_amount}") + else: + initial_amount -= num + print(f"取款:-{num}, 账户余额:{initial_amount}") + + return atm + +atm = account_create() + +atm(100) +atm(200) +atm(100, deposit=False) + + + diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/02_\350\243\205\351\245\260\345\231\250.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/02_\350\243\205\351\245\260\345\231\250.py" new file mode 100644 index 0000000000000000000000000000000000000000..a1397f445928453a05c4110ca497e5127bb6ef1b --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/02_\350\243\205\351\245\260\345\231\250.py" @@ -0,0 +1,44 @@ +""" +演示装饰器的写法 +""" + +# 装饰器的一般写法(闭包) +# def outer(func): +# def inner(): +# print("我睡觉了") +# func() +# print("我起床了") +# +# return inner +# +# +# def sleep(): +# import random +# import time +# print("睡眠中......") +# time.sleep(random.randint(1, 5)) +# +# +# fn = outer(sleep) +# fn() + +# 装饰器的快捷写法(语法糖) +def outer(func): + def inner(): + print("我睡觉了") + func() + print("我起床了") + + return inner + + +@outer +def sleep(): + import random + import time + print("睡眠中......") + time.sleep(random.randint(1, 5)) + + +sleep() + diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/str_tools_py.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/str_tools_py.py" new file mode 100644 index 0000000000000000000000000000000000000000..a8c66f08afa8d517e6e316aaf436917fc1471525 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/str_tools_py.py" @@ -0,0 +1,11 @@ + +class StrTools: + pass + +str_tool = StrTools() + + + + + + diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test.py" new file mode 100644 index 0000000000000000000000000000000000000000..118c0353b634fc50fd1cf0ba3f369ea41ddfd940 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test.py" @@ -0,0 +1,7 @@ +from str_tools_py import str_tool + +s1 = str_tool +s2 = str_tool + +print(id(s1)) +print(id(s2)) \ No newline at end of file diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test2.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test2.py" new file mode 100644 index 0000000000000000000000000000000000000000..118c0353b634fc50fd1cf0ba3f369ea41ddfd940 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/test2.py" @@ -0,0 +1,7 @@ +from str_tools_py import str_tool + +s1 = str_tool +s2 = str_tool + +print(id(s1)) +print(id(s2)) \ No newline at end of file diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/\351\235\236\345\215\225\344\276\213\346\265\213\350\257\225.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/\351\235\236\345\215\225\344\276\213\346\265\213\350\257\225.py" new file mode 100644 index 0000000000000000000000000000000000000000..9b024540b2435a82a748b389cad51fc66b5bac12 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/03_\345\215\225\344\276\213\346\250\241\345\274\217/\351\235\236\345\215\225\344\276\213\346\265\213\350\257\225.py" @@ -0,0 +1,11 @@ +""" +演示非单例模式的效果 +""" + +class StrTools: + pass + +s1 = StrTools() +s2 = StrTools() +print(id(s1)) +print(id(s2)) diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/04_\345\267\245\345\216\202\346\250\241\345\274\217.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/04_\345\267\245\345\216\202\346\250\241\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..0764b52d5e569a85aac3a8c8cf8fb034a0ab26a4 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/04_\345\267\245\345\216\202\346\250\241\345\274\217.py" @@ -0,0 +1,32 @@ +""" +演示设计模式之工厂模式 +""" + +class Person: + pass + +class Worker(Person): + pass + +class Student(Person): + pass + +class Teacher(Person): + pass + + +class PersonFactory: + def get_person(self, p_type): + if p_type == 'w': + return Worker() + elif p_type == 's': + return Student() + else: + return Teacher() + + +pf = PersonFactory() +worker = pf.get_person('w') +stu = pf.get_person('s') +teacher = pf.get_person('t') + diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/05_\345\244\232\347\272\277\347\250\213\347\274\226\347\250\213.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/05_\345\244\232\347\272\277\347\250\213\347\274\226\347\250\213.py" new file mode 100644 index 0000000000000000000000000000000000000000..1cf80a15a92674a1733e35329ca9609af95fe0ca --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/05_\345\244\232\347\272\277\347\250\213\347\274\226\347\250\213.py" @@ -0,0 +1,26 @@ +""" +演示多线程编程的使用 +""" +import time +import threading + +def sing(msg): + print(msg) + time.sleep(1) + + + +def dance(msg): + print(msg) + time.sleep(1) + + +if __name__ == '__main__': + # 创建一个唱歌的线程 + sing_thread = threading.Thread(target=sing, args=("我要唱歌 哈哈哈", )) + # 创建一个跳舞的线程 + dance_thread = threading.Thread(target=dance, kwargs={"msg": "我在跳舞哦 啦啦啦"}) + + # 让线程去干活吧 + sing_thread.start() + dance_thread.start() diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/06_socket\347\274\226\347\250\213_\346\234\215\345\212\241\347\253\257\345\274\200\345\217\221.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/06_socket\347\274\226\347\250\213_\346\234\215\345\212\241\347\253\257\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..01c81e7c9d616e27a104a003d6f8616a99ccb653 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/06_socket\347\274\226\347\250\213_\346\234\215\345\212\241\347\253\257\345\274\200\345\217\221.py" @@ -0,0 +1,36 @@ +""" +演示Socket服务端开发 +""" +import socket +# 创建Socket对象 +socket_server = socket.socket() +# 绑定ip地址和端口 +socket_server.bind(("localhost", 8888)) +# 监听端口 +socket_server.listen(1) +# listen方法内接受一个整数传参数,表示接受的链接数量 +# 等待客户端链接 +# result: tuple = socket_server.accept() +# conn = result[0] # 客户端和服务端的链接对象 +# address = result[1] # 客户端的地址信息 +conn, address = socket_server.accept() +# accept方法返回的是二元元组(链接对象, 客户端地址信息) +# 可以通过 变量1, 变量2 = socket_server.accept()的形式,直接接受二元元组内的两个元素 +# accept()方法,是阻塞的方法,等待客户端的链接,如果没有链接,就卡在这一行不向下执行了 + +print(f"接收到了客户端的链接,客户端的信息是:{address}") + +while True: + # 接受客户端信息,要使用客户端和服务端的本次链接对象,而非socket_server对象 + data: str = conn.recv(1024).decode("UTF-8") + # recv接受的参数是缓冲区大小,一般给1024即可 + # recv方法的返回值是一个字节数组也就是bytes对象,不是字符串,可以通过decode方法通过UTF-8编码,将字节数组转换为字符串对象 + print(f"客户端发来的消息是:{data}") + # 发送回复消息 + msg = input("请输入你要和客户端回复的消息:") + if msg == 'exit': + break + conn.send(msg.encode("UTF-8")) +# 关闭链接 +conn.close() +socket_server.close() diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/07_socket\347\274\226\347\250\213_\345\256\242\346\210\267\347\253\257\345\274\200\345\217\221.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/07_socket\347\274\226\347\250\213_\345\256\242\346\210\267\347\253\257\345\274\200\345\217\221.py" new file mode 100644 index 0000000000000000000000000000000000000000..d3724d9014b4c9799dc64d9678a6b757142dfe18 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/07_socket\347\274\226\347\250\213_\345\256\242\346\210\267\347\253\257\345\274\200\345\217\221.py" @@ -0,0 +1,20 @@ +""" +演示Socket客户端开发 +""" +import socket +# 创建socket对象 +socket_client = socket.socket() +# 连接到服务端 +socket_client.connect(("localhost", 8888)) + +while True: + # 发送消息 + msg = input("请输入要给服务端发送的消息:") + if msg == 'exit': + break + socket_client.send(msg.encode("UTF-8")) + # 接收返回消息 + recv_data = socket_client.recv(1024) # 1024是缓冲区的大小,一般1024即可。 同样recv方法是阻塞的 + print(f"服务端回复的消息是:{recv_data.decode('UTF-8')}") +# 关闭链接 +socket_client.close() diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/08_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\237\272\347\241\200\345\214\271\351\205\215.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/08_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\237\272\347\241\200\345\214\271\351\205\215.py" new file mode 100644 index 0000000000000000000000000000000000000000..41a1060ed43968828b52b9ab2290143ca9cc97da --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/08_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\237\272\347\241\200\345\214\271\351\205\215.py" @@ -0,0 +1,19 @@ +""" +演示Python正则表达式re模块的3个基础匹配方法 +""" +import re + +s = "1python itheima python python" +# match 从头匹配 +result = re.match("python", s) +print(result) +# print(result.span()) +# print(result.group()) +# search 搜索匹配 + +result = re.search("python2", s) +print(result) +# findall 搜索全部匹配 +result = re.findall("python", s) +print(result) + diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/09_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\205\203\345\255\227\347\254\246\345\214\271\351\205\215.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/09_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\205\203\345\255\227\347\254\246\345\214\271\351\205\215.py" new file mode 100644 index 0000000000000000000000000000000000000000..dfa251e74202209b1ec5155538967c5af5e0868b --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/09_\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217_\345\205\203\345\255\227\347\254\246\345\214\271\351\205\215.py" @@ -0,0 +1,26 @@ +""" +演示Python正则表达式使用元字符进行匹配 +""" +import re + +# s = "itheima1 @@python2 !!666 ##itccast3" +# +# result = re.findall(r'[b-eF-Z3-9]', s) # 字符串前面带上r的标记,表示字符串中转义字符无效,就是普通字符的意思 +# print(result) + +# 匹配账号,只能由字母和数字组成,长度限制6到10位 +# r = '^[0-9a-zA-Z]{6,10}$' +# s = '123456_' +# print(re.findall(r, s)) +# 匹配QQ号,要求纯数字,长度5-11,第一位不为0 +# r = '^[1-9][0-9]{4,10}$' +# s = '123453678' +# print(re.findall(r, s)) +# 匹配邮箱地址,只允许qq、163、gmail这三种邮箱地址 +# abc.efg.daw@qq.com.cn.eu.qq.aa.cc +# abc@qq.com +# {内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}@{内容}.{内容}.{内容} +r = r'(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)' +# s = 'a.b.c.d.e.f.g@qq.com.a.z.c.d.e' +s = 'a.b.c.d.e.f.g@126.com.a.z.c.d.e' +print(re.match(r, s)) diff --git "a/14_\351\253\230\347\272\247\346\212\200\345\267\247/10_\351\200\222\345\275\222.py" "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/10_\351\200\222\345\275\222.py" new file mode 100644 index 0000000000000000000000000000000000000000..1229f4d9fbc17bc0c07e4ae8529ac863bfe74c99 --- /dev/null +++ "b/14_\351\253\230\347\272\247\346\212\200\345\267\247/10_\351\200\222\345\275\222.py" @@ -0,0 +1,46 @@ +""" +演示Python递归操作 +需求:通过递归,找出一个指定文件夹内的全部文件 + +思路:写一个函数,列出文件夹内的全部内容,如果是文件就收集到list +如果是文件夹,就递归调用自己,再次判断。 + +""" +import os + + +def test_os(): + """演示os模块的3个基础方法""" + print(os.listdir("D:/test")) # 列出路径下的内容 + # print(os.path.isdir("D:/test/a")) # 判断指定路径是不是文件夹 + # print(os.path.exists("D:/test")) # 判断指定路径是否存在 + + +def get_files_recursion_from_dir(path): + """ + 从指定的文件夹中使用递归的方式,获取全部的文件列表 + :param path: 被判断的文件夹 + :return: list,包含全部的文件,如果目录不存在或者无文件就返回一个空list + """ + print(f"当前判断的文件夹是:{path}") + file_list = [] + if os.path.exists(path): + for f in os.listdir(path): + new_path = path + "/" + f + if os.path.isdir(new_path): + # 进入到这里,表明这个目录是文件夹不是文件 + file_list += get_files_recursion_from_dir(new_path) + else: + file_list.append(new_path) + else: + print(f"指定的目录{path},不存在") + return [] + + return file_list + +if __name__ == '__main__': + print(get_files_recursion_from_dir("D:/test")) + + +def a(): + a() diff --git a/data/input/Audio.wav b/data/input/Audio.wav new file mode 100644 index 0000000000000000000000000000000000000000..926ece07c984831363d47d7abff9b7269c1cdf5d Binary files /dev/null and b/data/input/Audio.wav differ diff --git a/data/input/order.csv b/data/input/order.csv new file mode 100644 index 0000000000000000000000000000000000000000..43e053710182cc765c874e155ae1bd4f26b12e9c --- /dev/null +++ b/data/input/order.csv @@ -0,0 +1,10 @@ +user_001,1621718199,10.1,电脑 +user_001,1621718201,14.1,手机 +user_002,1621718202,82.5,手机 +user_001,1621718205,15.6,电脑 +user_004,1621718207,10.2,家电 +user_001,1621718208,15.8,电脑 +user_005,1621718212,56.1,电脑 +user_002,1621718260,40.3,家电 +user_001,1621718580,11.5,家居 +user_001,1621718860,61.6,家居 \ No newline at end of file diff --git a/data/input/wordcount.txt b/data/input/wordcount.txt new file mode 100644 index 0000000000000000000000000000000000000000..c50a86bbac98753f32e58d61a15c170ffdb38274 --- /dev/null +++ b/data/input/wordcount.txt @@ -0,0 +1,12 @@ +Total,time,BUILD,SUCCESS +Final,Memory,Finished,at +Total,time,BUILD,SUCCESS +Final,Memory,Finished,at +Total,time,BUILD,SUCCESS +Final,Memory,Finished,at +BUILD,SUCCESS +BUILD,SUCCESS +BUILD,SUCCESS +BUILD,SUCCESS +BUILD,SUCCESS +BUILD,SUCCESS diff --git a/my_package/__init__.py b/my_package/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..245f70f8df72407a7c41f0cc8ba41cb9ec604ec4 --- /dev/null +++ b/my_package/__init__.py @@ -0,0 +1 @@ +__all__ = ['my_module1'] diff --git a/my_package/my_module1.py b/my_package/my_module1.py new file mode 100644 index 0000000000000000000000000000000000000000..1b6b48482f0fa08e8054f10caa9e57f4d55e1e58 --- /dev/null +++ b/my_package/my_module1.py @@ -0,0 +1,8 @@ +""" +演示自定义模块1 +""" + +def info_print1(): + print("我是模块1的功能函数代码") + +info_print1() \ No newline at end of file diff --git a/my_package/my_module2.py b/my_package/my_module2.py new file mode 100644 index 0000000000000000000000000000000000000000..20887a44ce8f222ade00ddf76b4be64d88bbfe18 --- /dev/null +++ b/my_package/my_module2.py @@ -0,0 +1,7 @@ +""" +自定义模块2 +""" + +def info_print2(): + print("我是模块2的功能函数代码") + diff --git a/my_utils/__init__.py b/my_utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/my_utils/file_util.py b/my_utils/file_util.py new file mode 100644 index 0000000000000000000000000000000000000000..947690d64c970d872f09e4c8c6cc33ea69bc316c --- /dev/null +++ b/my_utils/file_util.py @@ -0,0 +1,40 @@ +""" +文件处理相关的工具模块 +""" + + +def print_file_info(file_name): + """ + 功能是:将给定路径的文件内容输出到控制台中 + :param file_name: 即将读取的文件路径 + :return: None + """ + f = None + try: + f = open(file_name, "r", encoding="UTF-8") + content = f.read() + print("文件的全部内容如下:") + print(content) + except Exception as e: + print(f"程序出现异常了,原因是:{e}") + finally: + if f: # 如果变量是None,表示False,如果有任何内容,就是True + f.close() + + +def append_to_file(file_name, data): + """ + 功能:将指定的数据追加到指定的文件中 + :param file_name: 指定的文件的路径 + :param data: 指定的数据 + :return: None + """ + f = open(file_name, "a", encoding="UTF-8") + f.write(data) + f.write("\n") + f.close() + + +if __name__ == '__main__': + # print_file_info("D:/bill.txtxxx") + append_to_file("D:/test_append.txt", "传智教育") diff --git a/my_utils/str_util.py b/my_utils/str_util.py new file mode 100644 index 0000000000000000000000000000000000000000..230890f114826bc08929d690e53e8eb0a306fda6 --- /dev/null +++ b/my_utils/str_util.py @@ -0,0 +1,28 @@ +""" +字符串相关的工具模块 +""" + + +def str_reverse(s): + """ + 功能是将字符串完成反转 + :param s: 将被反转的字符串 + :return: 反转后的字符串 + """ + return s[::-1] + + +def substr(s, x, y): + """ + 功能是按照给定的下标完成给定字符串的切片 + :param s: 即将被切片的字符串 + :param x: 切片的开始下标 + :param y: 切片的结束下标 + :return: 切片完成后的字符串 + """ + return s[x:y] + + +if __name__ == '__main__': + print(str_reverse("黑马程序员")) + print(substr("黑马程序员", 1, 3)) diff --git a/test.py b/test.py index 66158055f09790169b111fa810111bfe35b0ecf4..9b6d40216ed3b380ae418d0efc36cd22db82e830 100644 --- a/test.py +++ b/test.py @@ -1,13 +1,22 @@ -""" -对文档进行多行注释 -""" +import random +import uuid - -# 使用#进行单行注释,且#和注释之间有个空格 -# 定义方法 -def sayHello(): - str = "hello" - print(str) - - -sayHello() +dates = ["2011-02-01", "2011-02-02", "2011-02-03", "2011-02-04", "2011-02-05", "2011-02-06", "2011-02-07", "2011-02-08" + , "2011-02-09", "2011-02-10", "2011-02-11", "2011-02-12", "2011-02-13", "2011-02-14", "2011-02-15", "2011-02-16" + , "2011-02-17", "2011-02-18", "2011-02-19", "2011-02-20", "2011-02-21", "2011-02-22", "2011-02-23", "2011-02-24" + , "2011-02-25", "2011-02-26", "2011-02-27", "2011-02-28"] +provinces = ["安徽省", "河南省", "江苏省", "福建省", "广东省", "广西省", "湖北省", "湖南省", "山东省", "山西省", "河北省", + "陕西省", "浙江省", "贵州省", "云南省", "四川省", "辽宁省", "江西省"] +f = open("C:\\Users\\caoyu\\Desktop\\2011年2月销售数据JSON.txt", "w", encoding="UTF-8") +for d in dates: + base_num = random.randint(30, 36) + if d == '2011-02-02' or d == '2011-02-03': + base_num = random.randint(60, 66) + for i in range(base_num): + oid = str(uuid.uuid4()) + money = random.randint(100, 3000) + province = provinces[random.randint(0, len(provinces) - 1)] + msg = '{"date": "%s", "order_id": "%s", "money": %d, "province": "%s"}' % (d, oid, money, province) + f.write(msg) + f.write("\n") +f.close() diff --git a/test_mysql.py b/test_mysql.py new file mode 100644 index 0000000000000000000000000000000000000000..ea2121d493a44f9c120b8d863695dd4cf944249f --- /dev/null +++ b/test_mysql.py @@ -0,0 +1,19 @@ +from pymysql import Connection + +# 获取到MySQL数据库的链接对象 +conn = Connection( + host='localhost', # 主机名(或IP地址) + port=3306, # 端口,默认3306 + user='root', # 账户名 + password='716288qwe' # 密码 +) +# 获取游标对象 +cursor = conn.cursor() +conn.select_db("kwan") # 先选择数据库 +# 使用游标对象,执行sql语句 +cursor.execute("SELECT * FROM user") +# 获取查询结果的第一条 +result: tuple = cursor.fetchone() +print(result) +# 关闭到数据库的链接 +conn.close() diff --git a/tmp/my.py b/tmp/my.py new file mode 100644 index 0000000000000000000000000000000000000000..c94df6a954d8d407168225d17a6068ca6af9aab1 --- /dev/null +++ b/tmp/my.py @@ -0,0 +1,29 @@ + + +class Person: + pass + +class Worker(Person): + pass +class Student(Person): + pass +class Teacher(Person): + pass + +class Factory: + def get_person(self, p_type): + if p_type == 'w': + return Worker() + elif p_type == 's': + return Student() + else: + return Teacher() + +factory = Factory() +worker = factory.get_person('w') +stu = factory.get_person('s') +teacher = factory.get_person('t') + +worker = Worker() +stu = Student() +teacher = Teacher() diff --git a/tmp/t2.py b/tmp/t2.py new file mode 100644 index 0000000000000000000000000000000000000000..c449c271f1dc6738c71f710ae90424030374a1e1 --- /dev/null +++ b/tmp/t2.py @@ -0,0 +1,10 @@ +import re +s = "itheima1 @@python2 !!666 ##itcast3" +print(re.findall(r'[itc]', s)) + + +def func(): + + func() + + return ... diff --git a/tmp/test.py b/tmp/test.py new file mode 100644 index 0000000000000000000000000000000000000000..fdf5f4e5ec58a7a24bdc6f15f876b0b2e89d80b4 --- /dev/null +++ b/tmp/test.py @@ -0,0 +1,29 @@ +import os + + +def get_files_recursion_from_dir(path): + """ + 从指定文件夹中获取全部文件,使用递归 + :param path: 文件夹路径 + :return: list,包含全部的文件,如果目录不存在或无文件,返回空list + """ + file_list = [] + if os.path.exists(path): + for f in os.listdir(path): + new_path = path + "/" + f + if os.path.isdir(new_path): + file_list += get_files_recursion_from_dir(new_path) + else: + file_list.append(new_path) + else: + print(f"指定目录:{path},不存在") + return [] + + return file_list + + +files = get_files_recursion_from_dir("D:/test") +for f in files: + print(f) + print(os.path.exists(f)) + diff --git a/tmp/test2.py b/tmp/test2.py new file mode 100644 index 0000000000000000000000000000000000000000..78d38b1836f8c24c9b5d1d83a43f7322edd4b3ac --- /dev/null +++ b/tmp/test2.py @@ -0,0 +1,18 @@ +import socket + +socket_client = socket.socket() + +socket_client.connect(("localhost", 8888)) + +while True: + send_msg = input("请输入要发送的消息:") + if send_msg == 'exit': + socket_client.send('exit'.encode("UTF-8")) + break + socket_client.send(send_msg.encode("UTF-8")) + + recv_data = socket_client.recv(1024) + print("服务端回复消息为:", recv_data.decode("UTF-8")) + +socket_client.close() +