In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the ways of Python data type conversion". In the daily operation, I believe that many people have doubts about the way of Python data type conversion. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the ways of Python data type conversion?" Next, please follow the editor to study!
1. Data type of Python
The last blog post has introduced the data types of Python in detail. For more information, see the variable naming and data types of the link Python.
Here is a summary of the data types of Python:
String type String
Numeric type Number:
Plastic int
Floating-point float
Plural complex
Boolean type Bool column
Table type List
Tuple type Tuple
Dictionary type Dictionary
Collection type Set
Variable data types: list, dictionary, collection
Immutable data types: strings, numeric types, Boolean types, tuples
Container type data: strings, lists, tuples, dictionaries, collections
Non-container type data: numeric type, Boolean type
2. Python data type conversion
Since operations cannot be performed between different data types, we need data type conversions. There are two kinds of data type conversion in Python, one is automatic type conversion, that is, Python will automatically convert different types of data to the same type of data for calculation; the other is mandatory type conversion, which requires us to forcibly convert one data type to another data type based on different development needs.
2.1 automatic type conversion
When two different types of data are calculated, the result is calculated with higher precision, Boolean.
< 整型 < 浮点型 < 复数。 a = 10b = Trueprint(a + b) # 11'''在和数字运算时,True转为1,False转为0'''a = 10b = 3.14print(a + b) # 13.14'''整型与浮点型运算时,整型转化为浮点型,结果也为浮点型'''2.2 强制类型转换 str( ):可以把其他类型数据转化为字符串类型 int( ):可以把其他类型数据转化为整型 float( ):可以把其他类型数据转化为浮点型 bool( ):可以把其他类型数据转化为布尔类型 list( ):可以把其他类型数据转化为列表类型 tuple( ):可以把其他类型数据转化为元组类型 dict( ):可以把其他类型数据转化为字典类型 set( ):可以把其他类型数据转化为集合类型 2.2.1 其他转字符串 所有类型都可以转化为字符串类型。 a = 123 # intres = str(a) print(res, type(res)) # 123 a = True # boolres = str(a)print(res, type(res)) # True a = [1, 2, 3] # listres = str(a)print(res, type(res)) # [1, 2, 3] a = (1, 2, 3) # tupleres = str(a)print(res, type(res)) # (1, 2, 3) a = {1, 2, 3} # setres = str(a) # {1, 2, 3}print(res, type(res)) # {1, 2, 3} a = {1: 'a', 2: 'b'} # dictres = str(a)print(res, type(res)) # {1: 'a', 2: 'b'} 2.2.2 其他转数字类型 数字类型之间可以相互转换,但容器类型中只有字符串可以转换为数字类型,并且字符串中的元素必须为纯数字,否则无法转换。 '''1. 数字类型之间相互转换'''a = 123 # intres = float(a)print(res, type(res)) # 123.0 a = True # boolres = float(a)print(res, type(res)) # 1.0 '''2. 字符串类型转数字类型'''a = '123' # strres = int(a)print(res, type(res)) # 123 a = '123abc' # strres = int(a)print(res, type(res)) # 此时python会报错,报错类型为TypeErrora = [1, 2, 3] # listres = int(a)print(res, type(res)) # 此时同样会报错,因为除了字符串以外的其他容器类型都不可以转换成数字类型 其他类型转数字类型中有一个特殊情况,就是其他类型转布尔类型。 bool( ) 可以把其他类型转为True或False。 '''1. 容器类型转布尔类型: 容器中为空 -->There are elements in the False container-- > True'''a =''# empty string res = bool (a) print (res, type (res)) # False a ='0' # element res = bool (a) print (res, type (res)) # True a = [] # empty list res = bool (a) print (res, type (res)) # False a = [1,2 3] # there are elements res = bool (a) print (res, type (res)) # True a = tuple () # empty tuple res = bool (a) print (res, type (res)) # False a = dict () # empty dictionary res = bool (a) print (res, type (res)) # False a = set () # empty collection res = bool (a) print (res, type (res)) # False''2. Numeric type to Boolean type: in int type, 0 is False, others are true float type, 0.0 is False, others are true'a = 0 # intres = bool (a) print (res, type (res)) # False a = 0.0 # floatres = bool (a) print (res, type (res)) # False a = 0.345 # floatres = bool (a) print (res, type (res)) # True 2.2.3 other transfer list types
1. The numeric type is not a container type and cannot be converted to a list
two。 When a string is transferred to a list, each character in the string is treated as an element of the list
3. When a tuple is transferred to a list, each character in the string is treated as an element of the list
4. When a dictionary turns to a list, only the keys in the dictionary are retained.
5. When the collection turns to the list, the result is unordered, because the set itself is unordered.
A = '123' # strres = list (a) print (res, type (res)) # ['1','2','3'] a = (1, 2, 3) # tupleres = list (a) print (res, type (res)) # ['1','2,'3'] a = {'name':' Alice', 'Age': 5,' Sex': 'Female'} # dictres = list (a) print (res) Type (res)) # ['name',' Age, 'Sex'] a = {' a', 'baked, 1, 2,' c'} # setres = print (a) print (res, type (res)) # ['baked, 2, 1,' a'] a = [1, 2, 3] # listres = tuple (a) print (res, type (res)) # (1, 2, 3) 2.2.5 other transfer types
1. The numeric type is not a container type and cannot be converted to a collection
two。 When a string is transferred to a collection, the result is unordered
3. When the list is transferred to the collection, the result is unordered
4. When a tuple is transferred to a collection, the result is unordered
5. When the dictionary is transferred to the collection, only the keys in the dictionary are protected, and the result is unordered.
A = '123' # strres = set (a) print (res, type (res)) # {' 3','2','1'} a = ['ABA,' baked, 2,1] # listres = set (a) print (res, type (res)) # {2, 1, 'baked,' a'} a = ('ABA,' baked, 2,1) # tupleres = set (a) print (res, type (res)) 'baked,'a'} a = {'name':' Alice', 'Age': 5,' Sex': 'Female'} # dictres = set (a) print (res, type (res)) # {' Age', 'name',' Sex'}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.