diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" index e53450aaa03babf84cc7ea0dce283e63164d8a16..0d8c53465b1207564a4ab7d7f1c591d51b566f64 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\227\350\241\250.py" @@ -2,8 +2,11 @@ 演示数据容器之:list列表 语法:[元素,元素,....] """ +num = [] + # 定义一个列表 list -my_list = ["itheima", "itcast", "python"] +my_list = ["itheima", "itcast", 100, 99.9] + print(my_list) print(type(my_list)) @@ -24,8 +27,10 @@ print(my_list[1]) print(my_list[2]) # 错误示范;通过下标索引取数据,一定不要超出范围 # print(my_list[3]) - +print('1111' * 100) # 通过下标索引取出数据(倒序取出) +print(my_list[len(my_list) - 1]) +print('-' * 100) print(my_list[-1]) print(my_list[-2]) print(my_list[-3]) diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\240\351\231\244.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\240\351\231\244.py" index 90d41dc221f34675bda440a8017be10ef18ba019..d023d34ddf882345e67e5c7aee6ec3327be3621d 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\240\351\231\244.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/01_list\345\210\240\351\231\244.py" @@ -1,6 +1,7 @@ # 删除第二个元素 my_list = [1, 2, 3, 4, 5] +# index deleted_element = my_list.pop(1) print("删除的元素:", deleted_element) diff --git "a/05_\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/05_\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" index 0a0a6ac732d3c7885cfd0f705bc4401de4758b2d..a33f9d9615503ed7696f4f7aba1ea7f9919b6190 100644 --- "a/05_\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/05_\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" @@ -1,7 +1,7 @@ """ 演示数据容器之:list列表的常用操作 """ -mylist = ["itcast", "itheima", "python"] +mylist = ["itcast", "itheima", "python", "itheima"] # 1.1 查找某元素在列表内的下标索引 index = mylist.index("itheima") print(f"itheima在列表中的下标索引值是:{index}") @@ -13,6 +13,7 @@ print(f"itheima在列表中的下标索引值是:{index}") mylist[0] = "传智教育" print(f"列表被修改元素值后,结果是:{mylist}") # 3. 在指定下标位置插入新元素 +print("-" * 100) mylist.insert(1, "best") print(f"列表插入元素后,结果是:{mylist}") # 4. 在列表的尾部追加```单个```新元素 @@ -20,6 +21,9 @@ mylist.append("黑马程序员") print(f"列表在追加了元素后,结果是:{mylist}") # 5. 在列表的尾部追加```一批```新元素 mylist2 = [1, 2, 3] +print("-" * 100) +mylist.append(1) +mylist.append([1, 2, 3]) mylist.extend(mylist2) print(f"列表在追加了一个新的列表后,结果是:{mylist}") # 6. 删除指定下标索引的元素(2种方式) @@ -35,6 +39,7 @@ print(f"通过pop方法取出元素后列表内容:{mylist}, 取出的元素 # 7. 删除某元素在列表中的第一个匹配项 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] mylist.remove("itheima") +# mylist.remove("2") print(f"通过remove方法移除元素后,列表的结果是:{mylist}") # 8. 清空列表 @@ -43,6 +48,7 @@ print(f"列表被清空了,结果是:{mylist}") # 9. 统计列表内某元素的数量 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] count = mylist.count("itheima") +count = mylist.count("itcast") print(f"列表中itheima的数量是:{count}") # 10. 统计列表中全部的元素数量 @@ -50,16 +56,12 @@ mylist = ["itcast", "itheima", "itcast", "itheima", "python"] count = len(mylist) print(f"列表的元素数量总共有:{count}个") - # 7. 删除某元素在列表中的第一个匹配项 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] mylist.reverse() print(f"通过reverse方法翻转后,列表的结果是:{mylist}") - # 7. 删除某元素在列表中的第一个匹配项 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] mylist.remove("itheima") print(f"通过remove方法移除元素后,列表的结果是:{mylist}") - - diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_01.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_01.py" new file mode 100644 index 0000000000000000000000000000000000000000..5ddfce94a5200bce15557a61b269feacd0a92ff2 --- /dev/null +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_01.py" @@ -0,0 +1,3 @@ +mylist = ["传智教育", "黑马程序员", "Python"] +for e in mylist: + print(e) diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_02.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_02.py" new file mode 100644 index 0000000000000000000000000000000000000000..c16e91b8f4eb802f262b93c9b5a2450d4a5823bf --- /dev/null +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/test_02.py" @@ -0,0 +1,8 @@ +mylist = ["传智教育", "黑马程序员", "Python"] + +# 列推导式 +mylist = [e + "ABC" for e in mylist] +print(mylist) + +for e in mylist: + print(e + 'abc') 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" index 99046819aa1a3a94ad75577f277b565fa6402c46..d7eda27e8cd85b8fd9eced0aa8d218222950fb4e 100644 --- "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" @@ -2,14 +2,18 @@ 演示函数作为参数传递 """ + # 定义一个函数,接收另一个函数作为传入参数 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/test.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test.py" index 769aa8d59a841616e71a12d3bbc9f1624b577a86..83b8fc75b725bdf034e410a8af58f5ae8cfcf814 100644 --- "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" @@ -1,8 +1,10 @@ - def compute(add): add(1, 2) -def add(x, y): + +def add(x, y) -> int: print(x + y) + return x + y + -compute(lambda x, y: print(x + y)) \ No newline at end of file +compute(lambda x, y: print(x + y)) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_01.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_01.py" new file mode 100644 index 0000000000000000000000000000000000000000..a926528985a77e0e22d07b481a2f01f909566ce2 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_01.py" @@ -0,0 +1,14 @@ +def add(a, b): + """ + 计算两个数的和 + """ + result = a + b + c = input('请输入c') + result = result + int(c) + return result + + +a = input('请输入a') +b = input('请输入b') +sum = add(int(a), int(b)) +print(sum) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_02.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_02.py" new file mode 100644 index 0000000000000000000000000000000000000000..de315d25ed1ec3bf198d1c3248595ed83588facf --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_02.py" @@ -0,0 +1,10 @@ +num = 200 + + +def test_b(): + num = 500 + print(f"test_b: {num}") + + +test_b() +print(num) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_03.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_03.py" new file mode 100644 index 0000000000000000000000000000000000000000..51f75a1b4347a350a59d658526f179a10b71606d --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_03.py" @@ -0,0 +1,20 @@ +# global关键字,在函数内声明变量为全局变量 + +global num + +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/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_04.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_04.py" new file mode 100644 index 0000000000000000000000000000000000000000..3dc4f2987c4cd5f2f7601ee4ad078986ce2f2d69 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_04.py" @@ -0,0 +1,9 @@ +# 不定长 - 位置不定长, *号 +# 不定长定义的形式参数会作为元组存在,接收不定长数量的参数传入 +def user_info(*args): + print(f"args参数的类型是:{type(args)},内容是:{args}") + + +user_info(1, 2, 3, '小明', '男孩') +user_info(1, 2, 3) +user_info() diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_05.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_05.py" new file mode 100644 index 0000000000000000000000000000000000000000..8cb2bbc93d319ebfe12dfd2be97a63b5899a0246 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_05.py" @@ -0,0 +1,7 @@ +# 定义一个函数,接受其它函数输入 +def test_func(compute): + result = compute(1, 2) + return print(f"结果是:{result}") + + +test_func(lambda x, y: x - y) diff --git "a/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_06.py" "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_06.py" new file mode 100644 index 0000000000000000000000000000000000000000..1889bb6e35ead152fac713475f1c6334493f6827 --- /dev/null +++ "b/06_\345\207\275\346\225\260\350\277\233\351\230\266/test_06.py" @@ -0,0 +1,17 @@ +for i in range(1, 10, 2): + print(i) + +for i in range(1, 10, 7): + print(i) + +# 奇偶数据集 + +matrix = [0 for i in range(4)] # [[1,1,1,1], 0, 0, 0] + +print(matrix) + +# 生成3*4 的二维数组 +print('-' * 20) +matrix = [[[0 for i in range(4)] for i in range(4)] for j in range(4)] + +print(matrix)