数据运算转换

运算符
print(2**3)  # 8
print(10/3)   # 3.3333333333333335
print(10//3)  # 3
print(10 % 3) # 1

# 在没有()的去情况下,优先级 not > and > or  同一个优先级从左到右计算
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # 返回True

#  or : x or y , 即x为真,值就是x,x为假,值是y;
#  and: x and y, 有0 就返回0 , 无0 返回and右边的。x为真,值是y,x为假,值是x。
print(1 and 2 or 3 and 4)  # (2 or 4) 2
print(1 > 2 and 3 or 6)  # false and 3 or 6 --->false or 6--->6


# str ---> int 只能是纯数字才能转化为int
s1 = '100'
print(int(s1))

# int ---> str  数字都能转为字符串
i1 = 100
print(str(i1), type(i1))  # 100 <class 'int'>

# int --> bool 非0即True
i = 100
print(bool(i))  # True
print(bool(0))  # False

# bool ---> int
print(int(True)) # 1
print(int(False)) # 0

# str--->bool 非空即True, 空格也是True
s = ' hello'
print(bool(s))
print(bool(''))

# bool--->str  没什么意义
t =str(True)
print(t, type(t)) # True <class 'str'> 
原文地址:https://www.cnblogs.com/jnsn/p/12719623.html