diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..d30ec4e --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/basic_learn/元组.py b/basic_learn/元组.py new file mode 100644 index 0000000..28b29b7 --- /dev/null +++ b/basic_learn/元组.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +@File : 元组.py +@Author : lichao +@Date : 2024/11/27 17:21 +@Software : PyCharm +""" + +tuple_data = (1, 2, 3, 4, 5) +print(tuple_data) + +# 元组不可变,不能修改,不能删除,不能添加 +# tuple_data[0] = 2 # TypeError: 'tuple' object does not support item assignment + +# 错误的推导式,会生成一个生成器 +tuple_g_data = (i for i in range(1, 10)) +print(tuple_g_data) # at 0x7f8f8f7b4d60> + +# 可以使用tuple来生成元组 +tuple_g1_data = tuple(i for i in range(1, 10)) +print(tuple_g1_data) diff --git a/basic_learn/列表.py b/basic_learn/列表.py index 8a65275..0d12111 100644 --- a/basic_learn/列表.py +++ b/basic_learn/列表.py @@ -9,19 +9,27 @@ # 创建列表的两种方式 # 1.使用[]创建 -list1 = [1,2,3,4,5] +list1 = [1, 2, 3, 4, 5] list1.append(6) # 2.使用list()创建 -list2 = list('hello') +list2 = list("hello") -print(list1,list2) +print(list1, list2) -# 3.使用+ 连接两个列表 +# 3.使用 + 连接两个列表 list3 = list1 + list2 print(list3) +# 4.使用extend()方法,将一个列表中的元素添加到另一个列表中 +list4 = [1, 2, 3] +list5 = [4, 5, 6] +list4.extend(list5) +print(list4) # 使用 + 不改变原列表, 使用+= 更改原列表 -# 使用append()改变原列表 \ No newline at end of file +# 使用append()改变原列表 + +insert_data = [1, 2, 4] +insert_data.insert(2, 3) # 在索引2的位置插入3 diff --git a/basic_learn/字典.py b/basic_learn/字典.py new file mode 100644 index 0000000..ca34a1c --- /dev/null +++ b/basic_learn/字典.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +@File : 字典.py +@Author : lichao +@Date : 2024/12/6 11:17 +@Software : PyCharm +""" +dict_data = {"name": "test", "age": 18, "address": "beijing"} +# 直接对字典进行遍历,获得是字典的key +for key in dict_data: + print(key) + +# 元组拆包 +for key, value in dict_data.items(): + print(key, value) + +# 获取字典数据,如果访问不存在的key,会报错 +print(dict_data["name"]) +# 使用get方法,如果访问不存在的key,不会报错,返回None,第二个参数用来设置默认值(建议使用) +# print(dict_data["height"]) +print(dict_data.get("height", 180)) + +dict_data["name"] = "test1" +dict_data["gender"] = "man" +print(dict_data) + +# 删除字典中的元素 +del dict_data["age"] + +# 清空字典 +dict_data.clear() + +# 空字典声明 +dict_data1 = {} +dict_data2 = dict() diff --git a/basic_learn/推导式.py b/basic_learn/推导式.py new file mode 100644 index 0000000..36be8fb --- /dev/null +++ b/basic_learn/推导式.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +@File : 推导式.py +@Author : lichao +@Date : 2024/12/9 11:46 +@Software : PyCharm +""" + +# 列表推导式 +list = [x for x in range(1, 15)] +list = [list[x : x + 3] for x in range(0, len(list), 3)] +print(list) + + +# 集合推导式 +gather = {x for x in range(1, 10)} +print(gather) # {1, 2, 3, 4, 5, 6, 7, 8, 9} + +# 字典推导式 +dict = {x: x**2 for x in range(1, 10)} +print(dict) + +# 没有元组推导式 diff --git a/basic_learn/集合.py b/basic_learn/集合.py new file mode 100644 index 0000000..96f5672 --- /dev/null +++ b/basic_learn/集合.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +@File : 集合.py.py +@Author : lichao +@Date : 2024/12/6 10:38 +@Software : PyCharm +""" + +# 集合是一个无序的、不重复的数据集合,列表是有序的,可以重复的数据集合 + +# 创建集合的两种方式 +# 1.使用{}创建 + +set_data = {1, 2, 3, 4, 5} +print(type(set_data)) + +# 集合不能使用下标访问 +# print(set_data[0]) # TypeError: 'set' object is not subscriptable + +# 不能使用{}创建空集合,因为{}创建的是空字典 +# set_data1 = {} # +set_data2 = set() +print(type(set_data2)) + + +# 集合方法 +# 1.添加元素 +set_data2.add(1) +# 2.删除元素 +set_data2.remove(1) +# 3.清空集合 +set_data2.clear() +# 4.删除集合 +del set_data2 +# 5.集合的交集、并集、差集、对称差集 +set1 = {1, 2, 3, 4, 5} +set2 = {4, 5, 6, 7, 8} +# 交集 +print(set1 & set2) # {4, 5} +print(set1.intersection(set2)) # {4, 5} +# 并集 +print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8} +print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8} +# 差集 +print(set1 - set2) # {1, 2, 3} +print(set1.difference(set2)) # {1, 2, 3} +# 对称差集 +print(set1 ^ set2) # {1, 2, 3, 6, 7, 8} +print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8} diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..0804f03 --- /dev/null +++ b/demo.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +@File : demo.py +@Author : lichao +@Date : 2024/11/27 15:40 +@Software : PyCharm +""" + +# 列表推导式 +list = [x for x in range(1, 15)] +list = [list[x : x + 3] for x in range(0, len(list), 3)] +print(list) + + +# 集合推导式 +gather = {x for x in range(1, 10)} +print(gather) # {1, 2, 3, 4, 5, 6, 7, 8, 9} + +# 字典推导式 +dict = {x: x**2 for x in range(1, 10)} +print(dict) + +# 没有元组推导式