常见数据类型及其转换

常见的9种数据类型

1. 整型(int)

2. 浮点型(float)

3. 布尔型(bool)

4. 复数类型(complex)

5. 字符串(str)

6. 列表(list)

7. 元组(tuple)

8. 集合(set)

9. 字典(dict)

示例:

复制代码
 1 '''
 2 常用数据类型
 3 '''
 4 
 5 # 1. 整形,int表示一个类,是所有整型的类
 6 num_int = 88
 7 print(type(num_int))  # <class 'int'>
 8 print(int, type(int))  # <class 'int'> <class 'type'>
 9 
10 # 2. 浮点型,float表示一个类,代表所有浮点数的类
11 num_float = 66.6666
12 print(type(num_float))  # <class 'float'>
13 print(float, type(float))  # <class 'float'> <class 'type'>
14 
15 # 3. 字符串,str表示字符串类
16 first_str = '程序猿'
17 print(type(first_str))  # <class 'str'>
18 print(str, type(str))  # <class 'str'> <class 'type'>
19 
20 # 4. 列表,list表示一个类,代表所有的列表的类
21 first_list = [88, num_float, first_str]
22 print(first_list)  # [88, 66.6666, '程序猿']
23 print(type(first_list))  # <class 'list'>
24 print(list, type(list))  # <class 'list'> <class 'type'>
25 
26 # 5. 元组
27 first_tuple = (99, num_float, first_str, first_list)
28 print(first_tuple)  # (99, 66.6666, '程序猿', [88, 66.6666, '程序猿'])
29 print(type(first_tuple))  # <class 'tuple'>
30 print(tuple, type(tuple))  # <class 'tuple'> <class 'type'>
31 
32 # 6. 集合
33 '''
34 哈希是一种用于快速查找和比较的数据结构,因此它要求键或元素必须是可哈希的。
35 作为字典的键或集合的元素必须是可哈希的。
36 把不能哈希的列表、元组放入集合会报如下错误:
37 first_set = {35, num_float, first_str, first_list, first_tuple}
38 Traceback (most recent call last):
39   File "F:\allen_class\python\base\006数据类型.py", line 28, in <module>
40     first_set = {35, num_float, first_str, first_list, first_tuple}
41                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42 TypeError: unhashable type: 'list'
43 '''
44 first_set = {35, num_float, first_str}
45 print(first_set)  # {'程序猿', 66.6666, 35},说明无序
46 print(type(first_set))  # <class 'set'>
47 print(set, type(set))  # <class 'set'> <class 'type'>
48 
49 # 7. 字典,dict就代表字典类
50 first_dict = {'name': 'Allen', 'age': 18}
51 print(first_dict)  # {'name': 'Allen', 'age': 18}
52 print(type(first_dict))  # <class 'dict'>
53 print(dict, type(dict))  # <class 'dict'> <class 'type'>
54 
55 # 8. 布尔,bool就代表bool类
56 first_bool = True
57 print(first_bool)  # True
58 print(type(first_bool))  # <class 'bool'>
59 print(bool, type(bool))  # <class 'bool'> <class 'type'>
60 
61 # 9. 复数类型,complex就代表复合类complex
62 first_complex = 3 + 4j
63 print(first_complex)  # (3+4j)
64 print(type(first_complex))  # <class 'complex'>
65 print(complex, type(complex))  # <class 'complex'> <class 'type'>
复制代码

数据类型转换

说明

就是利用int()、float()、bool() 、str() 、complex() 、list() 、tuple() 、set() 、eval()等函数对其进行处理。

 与字符串之间的转换图:

 示例:

复制代码
  1 '''
  2 常用数据类型转换
  3 '''
  4 
  5 # 1. 整形与浮点型的转换
  6 num_int = 0
  7 print(float(num_int), type(float(num_int)))  # 0.0 <class 'float'>
  8 # 1. 整型与字符串的转换
  9 print(str(num_int), type(str(num_int)))  # 0 <class 'str'>
 10 # 1. 整型与布尔类型的转换 如果整数为 0,则转换结果为 False;如果整数为非零,则转换结果为 True。
 11 print(bool(num_int), type(bool(num_int)))  # False <class 'bool'>
 12 
 13 # 2. 浮点型与整型的转换
 14 num_float = 66.6666
 15 print(int(num_float), type(int(num_float)))  # 66 <class 'int'>
 16 # 2. 浮点型与字符串的转换
 17 print(str(num_float), type(str(num_float)))  # 66.6666 <class 'str'>
 18 # 2. 浮点型与布尔的转换,如果浮点数为 0.0,则转换结果为 False;如果浮点数为非零,则转换结果为 True。
 19 print(bool(num_float), type(bool(num_float)))  # True <class 'bool'>
 20 
 21 # 3. 字符串,str表示字符串类
 22 first_str = '88'
 23 # 3. 字符串转整型,必须是整型字符串,浮点都不行 first_str = '88'
 24 print(int(first_str), type(int(first_str)))  # 88 <class 'int'>
 25 # 3. 字符串转浮点型
 26 print(float(first_str), type(float(first_str)))  # 88.0 <class 'float'>
 27 # 3. 字符串转复数类型
 28 print(complex(first_str), type(complex(first_str)))  # (88+0j) <class 'complex'>
 29 # 3. 字符串转布尔 空字符串(s = "" )会转换为False,非空字符串会转换为True
 30 print(bool(first_str), type(bool(first_str)))  # True <class 'bool'>
 31 # 3. 字符串在条件判断中的隐式转换
 32 if first_str:  # 隐式的类型转换规则。例如,当在条件判断语句中使用字符串时,非空字符串会被视为True,空字符串会被视为False
 33     print("在条件判断语句种,字符串隐式转换布尔")
 34 # 3. eval函数实现字符串转整型、列表、元组
 35 print(type(eval('10')))  # <class 'int'>
 36 print(type(eval('True')))  # <class 'bool'>
 37 print(type(eval('88.88')))  # <class 'float'>
 38 print(type(eval('[1,2,3]')))  # <class 'list'>
 39 print(type(eval('(1,2,3)')))  # <class 'tuple'>
 40 print(type(eval('{1,2,3}')))  # <class 'set'>
 41 print(type(eval('{"name":"Allen","age":18}')))  # <class 'dict'>
 42 print(type(eval('(3+4j)')))  # <class 'complex'>
 43 print(type(eval('3+4j')))  # <class 'complex'>
 44 
 45 # 4. 列表
 46 first_list = [88, num_float, first_str]
 47 # 4. 列表转字符串--使用join()函数
 48 my_list = ['Hello', 'World', 'Python']
 49 my_string = ' '.join(my_list)
 50 print(my_string)  # 输出 "Hello World Python"
 51 
 52 my_list = ['1', '2', '3']
 53 my_string = '-'.join(my_list)
 54 print(my_string)  # 输出 "1-2-3"
 55 # 4. 列表转字符串--使用列表推导式和join()函数:
 56 my_list = [1, 2, 3]
 57 my_string = ', '.join(str(x) for x in my_list)  # 使用列表推导式可以在将列表元素连接到字符串之前进行额外的处理
 58 print(my_string)  # 输出 "1, 2, 3"
 59 
 60 my_list = ['apple', 'banana', 'orange']
 61 my_string = ', '.join(item.upper() for item in my_list)  # 使用列表推导式可以在将列表元素连接到字符串之前进行额外的处理
 62 print(my_string)  # 输出 "APPLE, BANANA, ORANGE"
 63 
 64 # 4. 列表转字符串--使用str()和replace()函数:
 65 my_list = ['Hello', 'World', 'Python']
 66 my_string = str(my_list).replace("'", "")  # 使用str()函数将列表转换为字符串,然后使用replace()函数进行字符串替换
 67 print(my_string)  # 输出 "[Hello, World, Python]"
 68 
 69 my_list = [1, 2, 3]
 70 my_string = str(my_list).replace(",", "-").replace("[", "").replace("]", "")
 71 print(my_string)  # 输出 "1- 2- 3"
 72 
 73 # 4. 列表转元组:使用tuple()函数
 74 my_list = ['apple', 'banana', 'orange']
 75 my_tuple = tuple(my_list)
 76 print(my_tuple)  # 输出 ('apple', 'banana', 'orange')
 77 
 78 # 4. 列表转集合:set()
 79 my_list = ['apple', 'banana', 'banana', 'orange']
 80 my_set = set(my_list)
 81 # 集合是无序的,并且不允许重复的元素。通过将列表传递给set()函数,列表中的元素将被复制到新的集合中,并自动去除重复项。
 82 print(my_set)  # 输出 {'apple', 'banana', 'orange'}  # 去重
 83 
 84 # 5. 元组
 85 first_tuple = (99, num_float, first_str, first_list)
 86 # 5. 元组转字符串,跟列表转字符串一样
 87 my_tuple = ('H', 'e', 'l', 'l', 'o')
 88 my_str = ''.join(my_tuple)
 89 print(my_str)  # 输出 "Hello"
 90 
 91 my_tuple = ('a', 'p', 'p', 'l', 'e')
 92 my_str = '-'.join(my_tuple)
 93 print(my_str)  # 输出 "a-p-p-l-e"
 94 
 95 # 5. 元组转list:list()
 96 my_tuple = ('apple', 'banana', 'orange')
 97 my_list = list(my_tuple)
 98 print(my_list)  # 输出 ['apple', 'banana', 'orange']
 99 
100 # 5. 元组转集合:set()
101 my_tuple = ('apple', 'banana', 'orange', 'apple')
102 my_set = set(my_tuple)
103 print(my_set)  # 输出 {'orange', 'apple', 'banana'} 去重
104 
105 # 6. 集合
106 # 6. 集合转字符串:与元组、列表一样
107 my_set = {'apple', 'banana', 'orange'}
108 my_string = ', '.join(my_set)
109 print(my_string)  # 输出 "apple, banana, orange"
110 
111 my_set = {'cat', 'dog', 'elephant'}
112 my_string = ' - '.join(my_set)
113 print(my_string)  # 输出 "cat - elephant - dog"
114 
115 # 6. 集合转列表
116 my_set = {'apple', 'banana', 'orange'}
117 my_list = list(my_set)
118 print(my_list)  # 输出 ['apple', 'banana', 'orange']
119 
120 my_set = {1, 2, 3, 4, 5}
121 my_list = list(my_set)
122 print(my_list)  # 输出 [1, 2, 3, 4, 5]
123 
124 # 6. 集合转元组
125 my_set = {'apple', 'banana', 'orange'}
126 my_tuple = tuple(my_set)
127 print(my_tuple)  # 输出 ('apple', 'banana', 'orange')
128 
129 my_set = {1, 2, 3, 4, 5}
130 my_tuple = tuple(my_set)
131 print(my_tuple)  # 输出 (1, 2, 3, 4, 5)
132 
133 # 7. 字典
134 # 7. 字典转字符串:使用 str() 函数将字典转换为字符串
135 my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
136 my_string = str(my_dict)
137 print(my_string)  # 输出 "{'name': 'John', 'age': 25, 'city': 'New York'}"
138 # 7. 字典转字符串:使用 json.dumps() 函数将字典转换为 JSON 字符串:
139 import json
140 
141 my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
142 my_string = json.dumps(my_dict)
143 print(my_string)  # 输出 '{"name": "John", "age": 25, "city": "New York"}'
144 
145 # 7.1 json格式的字符串转字典:使用 json.loads() 函数可以将符合 JSON 格式的字符串转换为 Python 字典
146 import json
147 
148 my_string = '{"name": "John", "age": 25, "city": "New York"}'
149 my_dict = json.loads(my_string)
150 print(my_dict)  # 输出 {'name': 'John', 'age': 25, 'city': 'New York'}
151 # 7.1 使用 eval() 函数将字符串转换为字典(不推荐)
152 # 使用 eval() 函数可以将字符串作为表达式进行求值,并将其转换为字典。但是,由于存在安全风险和潜在的副作用,不推荐使用该方法
153 my_string = "{'name': 'John', 'age': 25+7, 'city': 'New York'}"
154 my_dict = eval(my_string)
155 print(my_dict)  # 输出 {'name': 'John', 'age': 32, 'city': 'New York'}
156 
157 # 8. 布尔
158 # 8. 布尔转整型
159 my_bool = True
160 my_int = int(my_bool)
161 print(my_int)  # 输出 1
162 
163 my_bool = False
164 my_int = int(my_bool)
165 print(my_int)  # 输出 0
166 # 8. 布尔转浮点型
167 my_bool = True
168 my_float = float(my_bool)
169 print(my_float)  # 输出 1.0
170 
171 my_bool = False
172 my_float = float(my_bool)
173 print(my_float)  # 输出 0.0
174 # 8. 布尔转字符串
175 print(str(my_bool))  # False
176 
177 # 9. 复数类型
178 # 9. 复数无法直接转为整型
179 # 9. 复数转浮点型
180 my_complex = 3 + 4j
181 my_float = float(my_complex.real)
182 print(my_float)  # 输出 3.0
183 # 9. 复数转字符串
184 my_complex = 3 + 4j
185 my_string = str(my_complex)
186 print(my_string)  # 输出 '(3+4j)'
复制代码

 

posted @   Allen_Hao  阅读(70)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示