运算符

2020-07-18  本文已影响0人  码农一号已就位

@[TOC]

算术运算符

运算符

赋值运算符

比较运算符

在这里插入图片描述

特殊的比较

逻辑运算符

not 逻辑-->⾮

not可以对符号右侧的值进⾏⾮运算对于布尔值,⾮运算会对其进⾏取反操 作,True变False,False变True
例:

a = True
b = False
c = not a
d = not b
print('c为',c)
print('d为',d)

结果:

c为 False
d为 True

and 逻辑-->与

and可以对符号两侧的值进⾏与运算。 只有在符号两侧的值都为True时,才 会返回True,只要有⼀个False就返回False 与运算是找False的,如果第⼀个值为False,则不再看第⼆个值
例如:

a = True
b = False
c = True
d = False
e = a and b
f = a and c
g = b and a
h = b and d
print('e为%s, f为%s, g为%s, h为%s' %(e, f,g,h))

结果:

e为False,  f为True,  g为False,  h为False

or 逻辑-->或

或运算两个值中只要有⼀个True,就会返回True 或运算是找True的

a = True
b = False
c = True
d = False
e = a or b
f = a or c
g = b or a
h = b or d
print('e为%s, f为%s, g为%s, h为%s' %(e, f,g,h))

结果:

e为True, f为True, g为True, h为False

条件运算符

条件运算符在执⾏时,会先对条件表达式进⾏求值判断 如果判断结果为True,则执⾏语句1,并返回执⾏结果 如果判断结果为False,则执⾏语句2,并返回执⾏结果
语法: 语句1 if 条件表达式 else 语句2


在这里插入图片描述
上一篇 下一篇

猜你喜欢

热点阅读