基本运行符

基本的逻辑运算符有算术运算符,比较运算符,赋值运算符,逻辑运算符,位运算符,成员运算符,身份运算符。

有哪些运算符?

  • 算术运算符

  • 比较运算符

  • 赋值运算符

  • 逻辑运算符

  • 位运算符

  • 成员运算符

  • 身份运算符

  • 运算符优先级

算术运算符

算术运算符,就是加减乘除这些了,理解起来也比较容易;Lukas有两个朋友A和B,分别有10/20个玩具;定义两个整数nA和nB;假定nA = 10;nB = 20;我们看一下,他们加、减、乘、除、取模、幂、取整除法;我们看一下结果会怎么样。

符号

描述

示例

+

两个对象相加

nA + nB = 30

-

两个对象相减

nB - nA =10

*

两个对象相乘

nA * nB = 200

/

两个对象相除

nB/nA = 2

%

取模(返回除法的余数)

nB % nA =0

**

幂(nA的nB次幂)

nA ** nB = 10的20次方,太大了

//

除法的向下取整数

nA//nB = 0

'''
Welcome to LearnPython.NET

File Name: PDataType.py
Download from:https://www.learnpython.net/cn/python-code-samples.html
Author: LearnPython.Net
Editor: CoderChiu

'''

#定义两个整数nA和nB
nA = 10
nB = 20

print("nA(10) + nB(20) = ", nA + nB)
print("nA(10) - nB(20) = ", nA - nB)
print("nA(10) * nB(20) = ", nA * nB)
print("nA(10) / nB(20) = ", nA / nB)
print("nA(10) ** nB(20) = ", nA ** nB)
print("nA(10) // nB(20) = ", nA // nB)


执行结果如下:

>>> %Run POperators.py
nA(10) + nB(20) =  30
nA(10) - nB(20) =  -10
nA(10) * nB(20) =  200
nA(10) / nB(20) =  0.5
nA(10) ** nB(20) =  100000000000000000000
nA(10) // nB(20) =  0

跟你想的是否一致呢?

比较运算符

比较运算符,就是比较大小,比较长短;在惠州大亚湾旅行,Lukac抓了5个螃蟹,Robin抓了7只螃蟹,Alex抓了4只螃蟹;谁抓的最多呢?

#3个小朋友抓的螃蟹数量
nLukasCrabNum = 5
nRobinCrabNum = 7
nAlexCreabNum = 4

符号

描述

示例

>

大于

5 > 4 = Ture

>=

大于等于

5>=5 = True

<

小于

5 < 4 = Flase

<=

小于等于

5<=4 = Flase

<>

不等于

5<>4 = True

==

等于

5==4 = Flase

!=

不等于

5 != 4 = True

这个逻辑呢,跟数学比较大小的逻辑相近,所以比较好理解;

#3个小朋友抓的螃蟹数量
nLukasCrabNum = 5
nRobinCrabNum = 7
nAlexCreabNum = 4

if nLukasCrabNum > nRobinCrabNum :
    print("Lukas is great!")
elif nLukasCrabNum == nRobinCrabNum:
    print("Lukas and Robin is great!")
else:
    print("Robin is great!")

执行结果:

>>> %Run POperators.py
Robin is great!

比较运算符,一般情况,都是在 If 语句中使用。

赋值运算符

逻辑运算符

逻辑运算符有三种:and 和、or 或、not 非。

假定,bLukas = True,bRobin = False。

符号

表达式

说明

示例

and

x and y

x、y同时为Ture则为Ture;否则为False;

bLukas and bRobin = False

or

x or y

x是Ture就返回Ture,y为Ture也返回Ture;x、y都为False则返回False;

bLukas or bRobin = True

not

not x

x是Ture,则返回False;x为Flase,则返回Ture;

not bLukas = Flase

Lukas、Robin在海边捉螃蟹;如果两个人都捉到5是以上的话,就去吃饭。

#Lukas 和 Robin都捉到5只螃蟹,就去吃饭;
if (nLukasCrabNum >= 5) and (nRobinCrabNum >= 5):
    print("Go to eat something.")
else:
    print("Searing crabs.")

打印的结果如下:满足条件了,可以去吃饭了。

>>> %Run POperators.py
Go to eat something.

螃蟹太难捉了,Alex、Robin在海边捉螃蟹;只有某个人都捉到5是以上的话,就去吃饭。

#Alex 和 Robin都捉到5只螃蟹,就去吃饭;
if (nAlexCreabNum >= 5) or (nRobinCrabNum >= 5):
    print("Go to eat something.")
else:
    print("Searing crabs.")

打印的结果如下:满足条件了,可以去吃饭了。

>>> %Run POperators.py
Go to eat something.

Alex、Lukas、Robin,只有抓到了5只螃蟹的,就去吃饭;没有抓到的就继续抓螃蟹;但是到了7点中,还没有吃饭,就要去吃饭!

#捉到5只螃蟹的,就去吃饭;没有捉到的,就继续;
bLukas = nLukasCrabNum >= 5
bRobin = nRobinCrabNum >= 5
bAlex = nAlexCreabNum >= 5

#7点钟,还没有吃饭的,就要去吃饭;
if not bLukas:
    print("Lukas go to eat something after 7.")
if not bRobin:
    print("Robin go to eat something after 7.")
if not bAlex:
    print("Alex go to eat something after 7.")

执行结果:7点钟,还没有吃饭的Alex,要去吃饭了!

>>> %Run POperators.py
Alex go to eat something after 7.

逻辑运算,在生活中挺常见,你肯定遇见过;有没有可以用逻辑表达式,描述的东西,写出来试一下。

位运算符

成员运算符

身份运算符

运算符优先级

Last updated