1. 程式人生 > >python for 資料型別轉換

python for 資料型別轉換

1.set()  --- list()

s = {12,4,3,4}
print(list(s)) # [3, 12, 4]

lst = [3, 12, 4]
print(set(lst)) # {3, 12, 4}

tu = (1,2,3,4)
print(list(tu)) #[1, 2, 3, 4]
print(set(tu)) #{1, 2, 3, 4}

s = {1,2,3}
print(tuple(s)) #(1, 2, 3)

tu = (1,2,3,4)
print(str(tu))

lst = [1,2,3,4,[5,6,7,[8,9,10]]]

print(str(lst))

print #面向使用者的
s = str(lst)
print(repr(s))

na = 'alex'
lst = list(na)
print(lst)

dic = {'name':'黑哥','age':18}
lst = list(dic) # 字典的鍵
print(lst)

s = set(dic)
print(s)

str
int
bool
list
tuple
dict
set

print(bool(' ')) True
print(bool('')) False

print(bool(0)) False
print(bool(56)) True

print(bool(True)) True

print(bool(False)) False

print(bool([1,2,3])) True
print(bool([])) # False

print(bool((1,2,3))) #True
print(bool(())) #False

print(bool())

0 '' None [ ] ( ) { } { } False

print(bool(None))

for i in 'alex':
if i:
print(44)