Python基础--运算符和表达式

时间:2021-05-23 23:49:33   收藏:0   阅读:0

运算符

print(1)
print(‘aa‘)
print(1+2)

算数运算符

赋值运算符

比较运算符

逻辑运算符

a and b

(上文:表列)a and b(下文:表行)真(True)假(0|False)
真(True) 结果返回b 结果返回b
假(0|False) 结果返回a 结果返回a
# 0,False,空格" ",空元组(),空列表[],空集合|字典{},None为假,其他为真
print("a=True,b=True:",True and True,1 and 3)
print("a=True,b=False:",3 and 0)
print("a=False,b=True:",0 and ‘ss‘)
print("a=False,b=False:",False and ())

总结:and:如果a和b都为真,就返回b的值;如果a和b一真一假,返回假的值;都为假,返回a的值。

a or b

(上文:表列)a or b(下文:表行)真(True)假(0|False)
真(True) 结果返回a 结果返回a
假(0|False) 结果返回b 结果返回b
# 0,False,空格" ",空元组(),空列表[],空集合|字典{},None为假,其他为真
print("a=True,b=True:",True or True,1 or 3)
print("a=True,b=False:",3 or 0)
print("a=False,b=True:",0 or ‘ss‘)
print("a=False,b=False:",0 or ())

总结:or:如果a和b都为真,就返回a的值;如果a和b一真一假,返回真的值;都为假,返回b的值。

位运算符

成员操作符

a = [1,2,3,4,5]
b = {"name":"xwg",2:3}
c = (1,)
d = {1,2,3}
print("in:",1 in a,1 in b,1 in c,1 in d)
print("not in:",0 not in a,0 not in b,0 not in c,0 not in d)

条件操作符

a = [1,2,3,4,5]
b = {"name":"xwg",2:3}
c = (1,)
d = {1,2,3}
print("is:",1 is a[0],1 is b[2],1 is c[0],1 is d)
print("is not:",0 is not a,0 is not b,0 is not c,0 is not d)

isinstance

type

id

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!