Python基础入门学习小总结
Python类型转换
- str --> int
a = ‘123’
print(type(a))
b = int(a)
print(type(b)) - float --> int
c = 3.1415
print(type©) - float 转整数返回的小数点前面的数据
d = int©
print(d)
print(type(d)) - boolean --> int False – 0 True – 1
x = True
y = int(x)
print(y)
print(type(y))
当字符串中包含非法字符,则不能转换
- a = ‘1.123’ or a = ‘123dfg’
b = int(a)
print(b)
当获取的数据是字符串类型
- 字符串转浮点型
a=‘12.34’
b=float(a)
print(type(b)) - 整型转浮点型
t = 111
k = float(t)
print(type(k))
print(k)
强制类型转换 方法是 str()
-
整型转字符串
r = 80
s = str®
print(s)
print(type(s)) -
浮点型转字符串
m = 2.32
n = str(m)
print(n)
print(type(n)) -
布尔类型转字符串
g = True
h = str(g)
print(h)
print(type(h)) -
转换为布尔类型
整数非0的整数和负数 – True; 0 转布尔为 False
q = 3
p = bool(q)
print§
print(type§)
浮点数里非0的 – True; 0.0 转布尔为 False
q1 = 3.9
p2 = bool(q)
print§
print(type§)
字符串转布尔 字符串有内容是 True; 没内容是 False
test = ‘’ #‘werqree’ or ’ ’
test1 = bool(test)
print(test1)
print(type(test1))
列表 有数据是 – True 和 [] – False
List = [a,‘b’,c,d]
List1 = bool(List)
print(List)
print(type(List1))
元组 有数据是 – True 和 () – False
tuple = (‘w’,‘r’,‘t’)
tuple1 = bool(tuple)
print(tuple1)
print(type(tuple1))
字典 有数据是 – True 和 {} – False
dict = {“a”:‘a’}
dict1 = bool(dict)
print(dict1)
print(type(dict1))