Detailed Explanation of Python Data Type Conversion

  • 2021-12-11 18:25:02
  • OfStack

Directory 1. Python Data Types2. Python Data Type Conversion 2.1 Automatic Type Conversion 2.2 Forced Type Conversion 2.2. 1 Other Turn Strings 2.2. 2 Other Turn Number Types 2.2. 3 Other Turn List Types 2.2. 4 Other Turn Tuple Types 2.2. 5 Other Turn Set Types 2.2. 6 Other Turn Dictionary Types Summary

1. Data type of Python

The data type of Python has been introduced in detail in the last blog post. For details, please refer to the variable naming and data type of Python.

Here, summarize the data types of Python under 1:

String type String Number type Number:

Shaping int

Floating point float

Complex 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: string, numeric type, Boolean type, tuple

Container type data: string, list, tuple, dictionary, collection

Non-container type data: numeric type, Boolean type

2. Python data type conversion

Since there is no operation between different data types, we need data type conversion. There are two types of data type conversion in Python, one is automatic type conversion, that is, Python will automatically convert different types of data into the same type of data for calculation; The other is forced type conversion, which requires us to force one data type to another based on different development requirements.

2.1 Automatic type conversion

When two different types of data are operated, the result will be calculated with higher precision. The precision level is Boolean < Integer type < Floating point type < Plural.


a = 10
b = True
print(a + b) # 11
'''
 When operating with numbers, True Convert to 1 , False Convert to 0
'''
a = 10
b = 3.14
print(a + b) # 13.14
'''
 When integer and floating-point operation, integer is converted to floating-point type, and the result is also floating-point type 
'''

2.2 Censor Type Conversion

str (): You can convert other types of data to string types int (): You can convert other types of data to integers float (): You can convert other types of data to floating-point type bool (): You can convert other types of data to Boolean types list (): You can convert other types of data to list types tuple (): You can convert other types of data to tuple types dict (): You can convert other types of data to dictionary types set (): You can convert other types of data to collection types

2.2. 1 Other Turn Strings

All types can be converted to string types.


a = 123 # int
res = str(a) 
print(res, type(res)) # 123 <class 'str'>
a = True # bool
res = str(a)
print(res, type(res)) # True <class 'str'>
a = [1, 2, 3] # list
res = str(a)
print(res, type(res)) # [1, 2, 3] <class 'str'>
a = (1, 2, 3) # tuple
res = str(a)
print(res, type(res)) # (1, 2, 3) <class 'str'>
a = {1, 2, 3} # set
res = str(a) # {1, 2, 3}
print(res, type(res)) # {1, 2, 3} <class 'str'>
a = {1: 'a', 2: 'b'} # dict
res = str(a)
print(res, type(res)) # {1: 'a', 2: 'b'} <class 'str'>

2.2. 2 Other types of conversions

Numeric types can be converted to each other, but only strings in container types can be converted to numeric types, and elements in strings must be pure numbers, otherwise they cannot be converted.


'''
1.  Conversion between numeric types 
'''
a = 123 # int
res = float(a)
print(res, type(res)) # 123.0 <class 'float'>
a = True # bool
res = float(a)
print(res, type(res)) # 1.0 <class 'float'>

'''
2.  String type to numeric type 
'''
a = '123' # str
res = int(a)
print(res, type(res)) # 123 <class 'int'>
a = '123abc' # str
res = int(a)
print(res, type(res)) #  At this time python Error will be reported, and the error type is TypeError
a = [1, 2, 3] # list
res = int(a)
print(res, type(res)) #  An error is also reported, because no container type other than a string can be converted to a numeric type 

There is a special case in which other types are converted to numeric types, that is, other types are converted to Boolean types. bool () can convert other types to True or False.


'''
1.  Container type to Boolean type :
    Empty in container  --> False
    There are elements in the container  --> True
'''
a = '' #  Empty string 
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = '0' #  There are elements in the string 
res = bool(a)
print(res, type(res)) # True <class 'bool'>
a = [] #  Empty list 
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = [1, 2, 3] #  There are elements in the list 
res = bool(a)
print(res, type(res)) # True <class 'bool'>
a = tuple() #  Empty tuple 
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = dict() #  Empty dictionary 
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = set() #  Empty set 
res = bool(a)
print(res, type(res)) # False <class 'bool'>

'''
2.  Numeric type to Boolean type: 
   int Type, 0 For False , others are true 
   float Type, 0.0 For False , others are true 
'''
a = 0 # int
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = 0.0 # float
res = bool(a)
print(res, type(res)) # False <class 'bool'>
a = 0.345 # float
res = bool(a)
print(res, type(res)) # True <class 'bool'>

2.2. 3 Other transfer list types

1. Numeric types are non-container types and cannot be converted to lists

2. When a string is converted to a list, every 1 character in the string is treated as an element of the list

3. When tuples are converted to lists, every 1 character in the string is treated as an element of the list

4. When a dictionary is transferred to a list, only the keys in the dictionary are kept

5. When a collection turns to a list, the result is unordered, because the collection itself is unordered


a = '123' # str
res = list(a)
print(res, type(res)) # ['1', '2', '3'] <class 'list'>
a = (1, 2, 3) # tuple
res = list(a)
print(res, type(res)) # ['1', '2', '3'] <class 'list'>
a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict
res = list(a)
print(res, type(res)) # ['name', 'Age, 'Sex'] <class 'list'>
a = {'a', 'b', 1, 2, 'c'} # set
res = print(a)
print(res, type(res)) # ['b', 2, 1, 'a', 'c'] <class 'list>

2.2. 4 Other tuple types

Other types of data go to tuple types with the same rules as other types of data go to list types.


a = 'abc' # str
res = tuple(a)
print(res, type(res)) # ('a', 'b', 'c') <class 'tuple>
a = [1, 2, 3] # list
res = tuple(a)
print(res, type(res)) # (1, 2, 3) <class 'tuple>
a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict
res = tuple(a)
print(res, type(res)) # ('name', 'Age, 'Sex') <class 'tuple>
a = {'a', 'b', 1, 2, 'c'} # set
res = tuple(a)
print(res, type(res)) # ('b', 2, 1, 'a', 'c') <class 'tuple>

2.2. 5 Other Transform Set Types

1. Numeric types are non-container types and cannot be converted to collections

2. 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 tuples are converted to sets, the result is unordered

5. When a dictionary is transferred to a set, only the keys in the dictionary are preserved, and the result is unordered


a = '123' # str
res = set(a)
print(res, type(res)) # {'3', '2', '1'} <class 'set'>
a = ['a', 'b', 2, 1] # list
res = set(a)
print(res, type(res)) # {2, 1, 'b', 'a'} <class 'set'>
a = ('a', 'b', 2, 1) # tuple
res = set(a)
print(res, type(res)) # {2, 1, 'b', 'a'} <class 'set'>
a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict
res = set(a)
print(res, type(res)) # {'Age', 'name', 'Sex'} <class 'tuple>

2.2. 6 Other types of transfer dictionaries

1. Numeric types are non-container types and cannot be converted to dictionaries

2. Strings cannot be converted to dictionary types because strings cannot generate level 2 containers

3. The list type is converted to dictionary type. The list must be an equal-length 2-level container, and the number of elements in the sub-container must be 2

4. When tuple type is transferred to dictionary type, the list must be equal-length 2-level container, and the number of elements in sub-container must be 2 sets

5. Collections cannot be converted to dictionary types because collections do not support hashes


a = '123' # str
res = dict(a)
print(res, type(res)) #  At this time python Will report an error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
a = [[1, 2], ['a','b']] #  Equal length 2 Level list 
res = dict(a)
print(res, type(res)) # {1: 2, 'a': 'b'} <class 'dict'>
a = ((1, 2), (3, 4), (5, 6)) #  Equal length 2 Level tuple 
res = dict(a)
print(res, type(res)) # {1: 2, 3: 4, 5: 6} <class 'tuple'>
a = {{1, 2}, {3, 4}} #  Equal length 2 Class set 
res = dict(a)
print(res,type(res)) #  At this time python Will report an error: TypeError: unhashable type: 'set'

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: