Cast type
Cast of type Number (int float complex bool)
(1) int - force data to be integer
Data type that can be converted: int float bool pure numeric string
var1 = 13 var2 = 5.67 var3 = True var4 = "123456" var5 = "123abc" var6 = 3+5j res = int(var2) res = int(var3) # True => 1 res = int(False)# False => 0 res = int(var4) # res = int(var5) error # res = int(var6) error print(res , type(res))
(2) float forces data to be floating-point
Data type that can be converted: int float bool pure numeric string
res = float(var1) res = float(var3) # True => 1.0 res = float(False)# False => 0.0 res = float(var4) # 123456.0 print(res , type(res))
(3) complex forces data to be plural
Data types that can be converted: int float bool pure numeric string complex
res = complex(var1) # Add 0j plural res = complex(var2) res = complex(var3) # True => 1+0j res = complex(False) # False => 0j res = complex(var4) # 123456+0j print(res , type(res))
(4) bool # force the data type to be Boolean
Types that can be converted: Boolean can forcibly convert all data types
There are ten cases where Boolean is false: 0, 0.0, false, 0j '' [] () set () {} none
res = bool(None) print(res , type(res))
When initializing variables, I don't know what value to use. I don't have the brain to write None
a =None b =None
By default, it is converted to a value of the current data type int() float() complex() bool()
res = bool() print(res , type(res))
Additional extensions
strvar = "123" strvar = "3.134" strvar = "5+3j" # res = int(strvar) # print(res,type(res)) # res = float(strvar) # print(res,type(res)) # res = complex(strvar) # print( res,type(res) )
Number automatic type conversion (int float complex bool)
Note: low precision is converted to high precision by default
bool--> int --> float --> complex
# bool + int res = True + 100 print(res ,type(res)) # 1 + 100 => 101 # bool + float res = True + 344.565 # 1.0 + 344.565 => 345.565 print(res ,type(res)) # bool + complex res = True + 7 - 90j # 1 + 0j + 7 - 90j => 8 - 90j print(res ,type(res)) # int + float res = 5 + 7.88 # 5.0 + 7.88 => 12.88 print(res ,type(res)) # int + complex res = 5 + 6 + 8j # 5 + 0j 6 + 8j => 11 + 8j print(res ,type(res)) # float + complex res = 5.66 + 9.1 -90j # 5.66 + 0j + 9.1 -90j => 14.76-90j print(res ,type(res))
'''
Precision loss of decimals (15 ~ 18 digits are sometimes intercepted after decimals, but they are incomplete, and there is precision loss)
"" "do not use decimals for comparison, do not bite" ""
print(0.1 + 0.2 == 0.3)
print(5.1 + 5.9 == 11.0)
0.0999999999999999
'''
Cast of container type (str list tuple set dict)
var1 = "I love you!,Brother Wen" var2 = [1,2,3] var3 = (4,4,5) var4 = {"Chen Lu","Upward vitality","Liu Zitao","reasonable"} var5 = {"cl":"the ornamental and the combined plain properties,Sven scum","szq":"straight A student","lzt":"Basketball teenager","hl":"Martial arts master"} var6 = 90 var7 = True
str cast to string
Types that can be converted: all data types can be converted. Put quotation marks around the current data type
res = str(var2) res = str(var3) res = str(var4) res = str(var5) res = str(var6) res = str(var7) print(res ,type(res)) # repr does not transfer character prototypes to output strings print(repr(res))
Cast list to list
If it is a string: take out each element in the string separately as a new element in the list
If it is a dictionary: keep only the keys in the dictionary
For other container data, simply replace [] brackets in the metadata type
res = list(var1) res = list(var3) res = list(var4) # Dictionary: only get the key of the dictionary and ignore the value res = list(var5) # res = list(var6) error can only be the inter rotation between containers print(res ,type(res))
Tuple cast to tuple
If it is a string: take out each element in the string separately as a new element in the tuple
If it is a dictionary: keep only the keys in the dictionary
For other container data: simply replace () brackets on both sides of the original data type
res = tuple(var1) res = tuple(var2) res = tuple(var4) res = tuple(var5) print(res ,type(res))
Cast set to set
If it is a string: take out each element in the string separately as a new element in the collection
If it is a dictionary: keep only the keys in the dictionary
For other container data, simply replace {} brackets on both sides of the metadata type
res = set(var1) res = set(var2) res = set(var5) print(res ,type(res))
Cast of dict dictionary type
Requirements: it must be a secondary container with the same length, and the number of elements in it is 2; The outer layer is list, tuple and set, and the inner layer is equal length secondary container of list or tuple = > dictionary;
1. The outer layer is a list, and the inner layer is a list or tuple
lst = [ ["a",1] , ("b",2) ] dic = dict(lst) print(dic , type(dic)) # {'a': 1, 'b': 2} <class 'dict'>
2. The outer layer is tuple, and the inner layer is list or tuple
tup = ( ["a",1] , ("b",2) ) dic = dict(lst) print(dic , type(dic))
3. The outer layer is a set and the inner layer is a tuple
setvar = { ("a",1) , ("b",2) } dic = dict(setvar) print(dic , type(dic))
Exception 1: the outer layer is list / tuple, and the inner layer is set
It can be realized. It is not recommended because it can not achieve the desired purpose and the collection is disordered. It is not recommended
lst = [ ["a",1] , {"b","250"} ] dic = dict(lst) print(dic)
Exception 2: the outer layer is list / tuple, and the inner layer is string
The string length can only be 2 bits, which has great limitations and is not recommended
lst = ["a1","b2"] # lst = ["a11","b22"] error # dic = dict(lst) # print(dic)
Method to filter out all duplicate elements in the list
The list is converted into the data type of the set, which is converted back to the dictionary, because the set has the function of de duplication
lst = [1,222,3,3,3,44,88,999,77,88,1] res = set(lst) print(res) # Converting the current set to the original list res2 = list(res) print(res2)
By default, no value is added and converted to the null value str() list() tuple() set() dict() of this data type
res = dict() print(res ) print(type(res))