# python-data-deep **Repository Path**: Python_good/python-data-deep ## Basic Information - **Project Name**: python-data-deep - **Description**: python数据挖掘与可视化,基于python可视化与数据建模。 - **Primary Language**: Python - **License**: AFL-3.0 - **Default Branch**: main - **Homepage**: https://blog.csdn.net/2301_76574743/article/details/148460738?sharetype=blog&shareId=148460738&sharerefer=APP&sharesource=2301_76574743&sharefrom=link - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-06-05 - **Last Updated**: 2025-06-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # python-data-deep # 第二章 数据类型,运算符,内置函数 ## 填空题 ```python a=-68//7#//为整除,有向下取整特点 print(a) ``` -10 ```python b = {40, 50, 60} c = {40, 60, 70} #并集 print("|:",b|c) #交集 print("&:",b&c) #差集=b并c-b交c print("-:",b-c) ``` |: {50, 70, 40, 60} &: {40, 60} -: {50} ```python print(chr(ord('0')+3)) ``` 3 ## 判断题 ```python print(3>5 and math.sin(0)) ``` False ```python print(4<5==5) ``` True ## 编程题 ```python data = eval(input('请输入包含若干自然数的列表:')) avg = sum(data) / len(data) avg = round(avg, 3) print('平均值为:', avg) ``` 请输入包含若干自然数的列表:平均值为: 2.0 ```python data = eval(input('请输入包含若干自然数的列表:')) print('降序排列后的列表:', sorted(data, reverse=True)) ``` 请输入包含若干自然数的列表:降序排列后的列表: [3, 2, 1] ```python data = eval(input('请输入包含若干自然数的列表:')) data = map(str, data) length = list(map(len, data)) print('每个元素的位数:', length) ``` 请输入包含若干自然数的列表:每个元素的位数: [1, 1, 1] ```python data = eval(input('请输入包含若干自然数的列表:')) print('绝对值最大的数字:', max(data, key=abs)) ``` 请输入包含若干自然数的列表:绝对值最大的数字: 3 ```python from operator import mul from functools import reduce data = eval(input('请输入包含若干自然数的列表:')) print('乘积:', reduce(mul, data)) ``` 请输入包含若干自然数的列表:乘积: 6 ```python from operator import mul from functools import reduce vec1 = eval(input('请输入第一个向量:')) vec2 = eval(input('请输入第二个向量:')) print('内积:', sum(map(mul, vec1, vec2))) ``` 请输入第一个向量:请输入第二个向量:内积: 11 ## 第三章练习 # 第三章 列表、元组、字典、集合与字符串 ## 列表 ```python list_1 = []#空列表 print(list_1) list_2 = [1,2,3,4,5,6]#包含整数的列表 print(list_2) list_3 = [1.1,1.2,1.3,1.4,1.5,1.6]#包含浮点数的列表 print(list_3) list_4 = ["apple","banana","cherry"]#包含字符串的列表 print(list_4) list_5 = [True,False,True]#包含布尔值的列表 print(list_5) list_6 = [[1,2],[3,4]]#嵌套列表 print(list_6) list_7 = [1,"apple",3.14,True] print(list_7) list_8 = list(range(10)) print(list_8) list_9 = [x*2 for x in range(10)] print(list_9) list_10 = list_9[:]#复制列表 print(list_10) list_11 = [0]*10 print(list_11) list_12 = list_1+list_2 print(list_12) list_13 = "hello world".split() print(list_13) list_14 = [(1,2),(3,4),(4,5)] print(list_14) list_15 = [{"name":"Lao"},{"name":"WZT"}] print(list_15) #利用append() list_16 = [] list_16.append(1) list_16.append(2) print(list_16) #利用extend方法扩张列表扩张列表 list_17 = [1,2] list_17.extend([3,4,5]) print(list_17) #利用insert方法输入元素 list_18 = [1,3,4] list_18.insert(1,2) print(list_18) #利用remove方法上删除元素 list_19 = [1,2,3,4,5,6] list_19.remove(3) print(list_19) #利用pop方法烫除元素弹出元素 list_20 = [1,2,3,4,5,6] list_20.pop() print(list_20) ``` [] [1, 2, 3, 4, 5, 6] [1.1, 1.2, 1.3, 1.4, 1.5, 1.6] ['apple', 'banana', 'cherry'] [True, False, True] [[1, 2], [3, 4]] [1, 'apple', 3.14, True] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 2, 3, 4, 5, 6] ['hello', 'world'] [(1, 2), (3, 4), (4, 5)] [{'name': 'Lao'}, {'name': 'WZT'}] [1, 2] [1, 2, 3, 4, 5] [1, 2, 3, 4] [1, 2, 4, 5, 6] [1, 2, 3, 4, 5] ## 元组 ```python tuple_1 = () print(tuple_1) tuple_2 = (1,2,3,4,5,6) print(tuple_2) tuple_3 = (1.1,1.2,1.3,1.5) print(tuple_3) tuple_4 = ("apple","banana","cherry") print(tuple_4) tuple_5 = (True,False,True) print(tuple_5) tuple_6 = ((1,2),(3,4)) print(tuple_6) tuple_7 = (1,"apple",3.14,True) print(tuple_7) tuple_8 = (1,) print(tuple_8) tuple_9 = tuple([1,2,3,4]) print(tuple_9) tuple_10 = ([1,2],[2,3],[4,5]) print(tuple_10) tuple_11 = ({"name":"Lao"},{"name":"WZT"}) print(tuple_11) tuple_12 = ({1,2},{3,4}) print(tuple_12) a,b,c = 1,2,3 tuple_13 = (a,b,c) print(tuple_13) tuple_14 = tuple("hello") print(tuple_14) def func(): return "hello" tuple_15 = (func,func()) print(tuple_15) tuple_16 = ((1,2),(3,4)) print(tuple_16) tuple_17 = (None,1,None,2,3,"apple") print(tuple_17) tuple_18 = (1,2,3) + (2,3,4) print(tuple_18) tuple_19 = (1,)*5 print(tuple_19) tuple_20 = (1,2,3,4,5,6,7,8)[1:4] print(tuple_20) ``` () (1, 2, 3, 4, 5, 6) (1.1, 1.2, 1.3, 1.5) ('apple', 'banana', 'cherry') (True, False, True) ((1, 2), (3, 4)) (1, 'apple', 3.14, True) (1,) (1, 2, 3, 4) ([1, 2], [2, 3], [4, 5]) ({'name': 'Lao'}, {'name': 'WZT'}) ({1, 2}, {3, 4}) (1, 2, 3) ('h', 'e', 'l', 'l', 'o') (, 'hello') ((1, 2), (3, 4)) (None, 1, None, 2, 3, 'apple') (1, 2, 3, 2, 3, 4) (1, 1, 1, 1, 1) (2, 3, 4) ## 字典 ```python dict_1 = {} print(dict_1) dict_2 = {1: "one", 2: "two", 3: "three"} print(dict_2) dict_3 = {"name": "Alice", "age": 25, "city": "New York"} print(dict_3) dict_4 = {1: "one", "name": "Alice", 3.14: "pi"} print(dict_4) dict_5 = {"person": {"name": "Alice", "age": 25}, "city": "New York"} print(dict_5) dict_6 = {"fruits": ["apple", "banana", "cherry"], "numbers": [1, 2, 3]} print(dict_6) dict_7 = {"coordinates": (10, 20), "dimensions": (200, 400)} print(dict_7) dict_8 = dict(name="Alice", age=25, city="New York") print(dict_8) dict_9 = dict([("name", "Alice"), ("age", 25), ("city", "New York")]) print(dict_9) dict_10 = {x: x**2 for x in range(5)} print(dict_10) keys = ["name", "age", "city"] values = ["Alice", 25, "New York"] dict_11 = dict(zip(keys, values)) print(dict_11) dict_12 = {"is_student": True, "is_employed": False} print(dict_12) dict_13 = {"name": None, "age": 25, "city": "New York"} print(dict_13) keys = ["name", "age", "city"] values = ["Alice", 25, "New York"] dict_14 = {k: v for k, v in zip(keys, values)} print(dict_14) dict_15 = {"even_numbers": {2, 4, 6, 8}, "odd_numbers": {1, 3, 5, 7}} print(dict_15) dict_16 = {"name": "Alice", "age": 25} dict_16["age"] = 26 print(dict_16) dict_17 = {"name": "Alice", "age": 25} del dict_17["age"] print(dict_17) dict_18 = {"name": "Alice", "age": 25} age = dict_18.pop("age") print(dict_18) dict_19 = {"name": "Alice"} dict_19.setdefault("age", 25) print(dict_19) dict20 = {"name": "Alice", "age": 25} dict21 = {"city": "New York", "country": "USA"} merged_dict = {**dict20, **dict21} print(merged_dict) ``` {} {1: 'one', 2: 'two', 3: 'three'} {'name': 'Alice', 'age': 25, 'city': 'New York'} {1: 'one', 'name': 'Alice', 3.14: 'pi'} {'person': {'name': 'Alice', 'age': 25}, 'city': 'New York'} {'fruits': ['apple', 'banana', 'cherry'], 'numbers': [1, 2, 3]} {'coordinates': (10, 20), 'dimensions': (200, 400)} {'name': 'Alice', 'age': 25, 'city': 'New York'} {'name': 'Alice', 'age': 25, 'city': 'New York'} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} {'name': 'Alice', 'age': 25, 'city': 'New York'} {'is_student': True, 'is_employed': False} {'name': None, 'age': 25, 'city': 'New York'} {'name': 'Alice', 'age': 25, 'city': 'New York'} {'even_numbers': {8, 2, 4, 6}, 'odd_numbers': {1, 3, 5, 7}} {'name': 'Alice', 'age': 26} {'name': 'Alice'} {'name': 'Alice'} {'name': 'Alice', 'age': 25} {'name': 'Alice', 'age': 25, 'city': 'New York', 'country': 'USA'} ## 集合 ```python empty_set = set() print(empty_set) int_set = {1, 2, 3, 4, 5} print(int_set) float_set = {1.1, 2.2, 3.3, 4.4, 5.5} print(float_set) str_set = {"apple", "banana", "cherry"} print(str_set) bool_set = {True, False} print(bool_set) mixed_set = {1, "apple", 3.14, True} print(mixed_set) list_to_set = set([1, 2, 3, 4, 5]) print(list_to_set) tuple_to_set = set((1, 2, 3, 4, 5)) print(tuple_to_set) string_to_set = set("hello") print(string_to_set) comprehension_set = {x * 2 for x in range(10)} print(comprehension_set) add_element_set = {1, 2, 3} add_element_set.add(4) print(add_element_set) remove_element_set = {1, 2, 3, 4} remove_element_set.remove(3) print(remove_element_set) discard_element_set = {1, 2, 3, 4} discard_element_set.discard(5) print(discard_element_set) pop_element_set = {1, 2, 3, 4, 5} popped_element = pop_element_set.pop() print(popped_element) clear_set = {1, 2, 3} clear_set.clear() print(clear_set) set_a = {1, 2, 3} set_b = {3, 4, 5} union_set = set_a | set_b print(union_set) set_a = {1, 2, 3} set_b = {3, 4, 5} intersection_set = set_a & set_b print(intersection_set) set_a = {1, 2, 3} set_b = {3, 4, 5} difference_set = set_a - set_b print(difference_set) set_a = {1, 2, 3} set_b = {3, 4, 5} symmetric_difference_set = set_a ^ set_b print(symmetric_difference_set) subset_a = {1, 2} superset_b = {1, 2, 3, 4} is_subset = subset_a.issubset(superset_b) print(is_subset) ``` set() {1, 2, 3, 4, 5} {1.1, 2.2, 3.3, 4.4, 5.5} {'apple', 'banana', 'cherry'} {False, True} {'apple', 3.14, 1} {1, 2, 3, 4, 5} {1, 2, 3, 4, 5} {'o', 'l', 'h', 'e'} {0, 2, 4, 6, 8, 10, 12, 14, 16, 18} {1, 2, 3, 4} {1, 2, 4} {1, 2, 3, 4} 1 set() {1, 2, 3, 4, 5} {3} {1, 2} {1, 2, 4, 5} True ## 字符串 ```python empty_str = "" print(empty_str) simple_str = "hello, world" print(simple_str) multi_line_str = """This is a multi-line string""" print(multi_line_str) quote_str = 'He said, "Hello!"' print(quote_str) single_quote_str = "It's a sunny day" print(single_quote_str) escaped_str = "第一步 line\nSecond line" print(escaped_str) raw_str = r"C:\Users\Name" print(raw_str) formatted_str = "Hello, %s" % "Alice" print(formatted_str) name = "Charlie" f_str = f"Hello, {name}" print(f_str) repeated_str = "ha" * 3 print(repeated_str) concatenated_str = "Hello, " + "world" print(concatenated_str) sliced_str = "今天天气不错"[0:5] print(sliced_str) find_str = "Hello, world".find("world") print(find_str) replace_str = "Hello, world".replace("world", "Python") print(replace_str) split_str = "apple,banana,cherry".split(",") print(split_str) joined_str = ",".join(["apple", "banana", "cherry"]) print(joined_str) upper_str = "hello".upper() print(upper_str) lower_str = "HELLO".lower() print(lower_str) strip_str = " hello ".strip() print(strip_str) ``` hello, world This is a multi-line string He said, "Hello!" It's a sunny day 第一步 line Second line C:\Users\Name Hello, Alice Hello, Charlie hahaha Hello, world 今天天气不 7 Hello, Python ['apple', 'banana', 'cherry'] apple,banana,cherry HELLO hello hello ```