02-数据类型转换

2018-07-22  本文已影响0人  蛋挞先生L

数据类型转换

自动转换

自动转换是说不需要人工干预,程序会自动转换。

手动转换

手动转换是说随着开发者的意志进行转换

int 整数型转换

val = 1
print(int(val))

val = 1.6
print(int(val))

val = 1+1j
print(int(val))

val = False
print(int(val))

val = "-1"
print(int(val))

float类型转换

val = 5
print(float(val))

val = 5+0j
print(float(val))

val = False
print(float(val))

val = "1.23"
print(float(val))

complex转换

val = 6
print(complex(val))

val = 6.6
print(complex(val))

val = "1.2"
print(complex(val))

val = True
print(complex(val))

val = False
print(complex(val))

bool值的转换

val = 3j
print(bool(val))

val = ""
print(bool(val))

val = [ ]
print(bool(val))

val = (1,)
print(bool(val))

val = set()
print(bool(val))

list转换

val = 1
val1 = list(val)
print(val1,type(val1))

val = 1.6
val1 = list(val)
print(val1,type(val1))

val = True
val1 = list(val)
print(val1,type(val1))

val = "大金链子小手表"
val1 = list(val)
print(val1,type(val1))

val = (1,2)
val1 = list(val)
print(val1,type(val1))


val = {1,2,3,4}
val1 = list(val)
print(val1,type(val1))

val = {"a":1,"b":2,"c":3}
val1 = list(val)
print(val1,type(val1))

str 转换


# int
int1 = 2
str1 = str(int1)
print(str1,type(str1))

# float
float1 = 1.2
str2 = str(float1)
print(str2,type(str2))

# complex
complex1 = 1+2j
str3 = str(complex1)
print(str3,type(str3))

# bool
var = False
str4 = str(var)
print(str4,type(str4))

# list
list1 = [1,2,[3,4]]
str5 = str(list1)
print(str5,type(str5))

# tuple
tuple1 = (1,2,3,4)
str6 = str(tuple1)
print(str6,type(str6))

# set
set1 = {1,2,3,}
str7 = str(set1)
print(str7,type(str7))

# dict
dict1 = {1:"a",2:"b",3:"c",4:"四大名著"}
str8 = str(dict1)
print(str8,type(str8))

tuple 转换(与list转换相似)

# int
# int1 = 1323
# print(tuple(int1))

# float
# float1 = 3.1415926
# print(tuple(float1))

# complex
# complex1 = 1+3j
# print(tuple(complex1))

# str
str1 = "大金链子小手 表"
print(tuple(str1))

# list
list1 = [1,2,{3,4}]
print(tuple(list1))

# set
set1 = {3.4,5.6,234.5}
print(tuple(set1))

# dict
dict1 = {1:"a",2:"b",3:"c"}
print(tuple(dict1))

set 转换

# int
# int1 = 1
# print(set(int1))

# float
# float1 = 0.0
# print(set(float1))

# complex
# complex1 = 0j
# print(set(complex1))

# str
str1 = "有纹身的都 怕热"
print(set(str1))

# list
list1 = [1,23,4,(1,2)]
print(set(list1))

# tuple
tuple1 = 1,2,4,(1,23)
print(set(tuple1))

# dict
dict1 = {1:"a",2:"b",3:(1,23)}
print(set(dict1))

dict 转换

# int  不可转换
# int1 = 12
# dict1 = dict(int1)
# print(dict1,type(dict1))

# float  不可转换
# float1 = 12.3
# dict2 = dict(float1)
# print(dict2,type(float1))

# complex 不可转换
# complex1 = 1+2j
# dict3 = dict(complex1)
# print(dict3,type(dict3))

# bool  不可转换
# bool1 = True
# dict4 = dict(bool1)
# print(dict4,type(dict4))

# str   只有空字符串可以转换
str1 = ""
dict5 = dict(str1)
print(dict5,type(dict5))

# list
list1 = []
dict6 = dict(list1)
print(dict6,type(dict6))

list2 = [[1,2],[3,4],[5,6]]
dict6 = list2
print(dict6,type(dict6))

list3 = [[(1,2),[5,6]],(3,4),]
dict6 = list3
print(dict6,type(dict6))


# tuple
tuple1 = ()
dict7 = dict(tuple1)
print(dict7,type(dict7))

tuple2 = ({1,5},[2,3])
dict7 = dict(tuple2)
print(dict7,type(dict7))

tuple3 = (((1,),5),[2,3])
dict7 = dict(tuple3)
print(dict7,type(dict7))

# set
set1 = set()
dict8 = dict(set1)
print(dict8,type(dict8))

set2 = {(1,2),(3,4)}
dict8 = dict(set2)
print(dict8,type(dict8))
上一篇下一篇

猜你喜欢

热点阅读