常见数据类型及其转换
1.python基础之注释2.python语言学习路线(基础)3.python源码结构4.python发展史及python应用场景5.变量6.bug及debug
7.常见数据类型及其转换
8.字符串格式化9.转义符10.字符串在货币、日期、精度的处理11.字符编解码12.字符编解码及其相互转换13.内置函数-help-帮助文档14.内置函数print-输出15.内置函数input16.运算符17.int的缓存策略(特别容易混)18.判断语句19.random模块20.判断语句+ random的应用-剪刀石头布游戏21.三元运算符(if else的简写)22.循环语句-while23.循环语句-while-练习题24.循环语句for循环25.循环语句 + else26.字符串类str27.正则在字符串中的简单应用28.列表(List)29.列表list的sort方法的坑30.列表综合应用-8个教室随机分配到3个教室31.元组32.自动组包 & 自动解包33.字典34.集合35.公共操作36.公共操作-推导式(集合、列表、字典)37.函数定义、调用、闭包38.拆包和交换变量值39.函数的变量作用域、返回值、参数40.函数递归41.匿名函数(lambda表达式)0142.内置函数abs43.内置函数round44.内置函数abs0245.高阶函数46.内置高阶函数map47.内置高阶函数reduce48.内置函数reduce49.学生管理系统-函数&循环&运算符综合练习50.文件操作之语法51.文件操作2-最佳实践52.文件操作之seek53.文件及文件夹操作54.面向对象基本概念55.面向对象之类、对象的定义56.添加和获取对象属性57.面向对象之魔法方法58.魔法函数 __repr__() 和 __str__()的区别59.魔法方法__len__60.魔法方法之__getitem__(self, key)、__setitem__(self, key, value) 和 __delitem__(self, key) 61.魔法方法之__iter__(self) && __next__(self)62.TypeError: iter() returned non-iterator of type 'MyIterable'63.魔法方法之__contains__()64.魔法方法之__call__65.魔法方法之__getattr__(), __setattr__(), 和 __delattr__66.__eq__(), __lt__(), 和 __gt__() 67.案例:烤地瓜68.案例:给房子置办家具69.继承70.继承之子类复用父类的属性、方法71.继承的分类72.MRO73.继承之子类重写父类同名属性和方法74.⼦类调⽤⽗类的同名⽅法和属性75.私有属性&&方法的定义&访问限制76.setter&getter方法访问私有属性77.__mro__属性78.python 面向对象3大特征详解79. 类属性和实例属性80.类属性和实例属性最佳实践81.类方法82.静态方法83.异常基础84.多组异常处理基础85.多组异常处理86.使用Python内置的错误信息87.捕获所有异常88.手动抛异常(raise)89.try-except-else-finally90.断言assert在异常中应用91.自定义异常类92.模块93.常见的5种模块导入94.模块的定位顺序95.__all__变量限制、明确导入范围96.包97.时间模块datetime98.多任务编程之并发、并行概念99.进程实现多任务(进程概念、单进程、多进程执行多任务)常见的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)'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!