Python语言零基础入门——运算符

目录

运算符汇总

一、算术运算符

二、赋值运算符

三、比较运算符

四、逻辑运算符

五、位运算符

六、成员运算符

七、身份运算符

八、运算符的优先级(由高到低)

九、习题


运算符汇总:

类型内容
算术运算符+   -   *   /   //   %   **
赋值运算符=   +=   -=   *=   /=   //=   %=
位运算符&   ~   <<   >>   ^
关系运算符(比较运算符)==   !=   <   >   <=   >=
逻辑运算符and   、 or   、  not
成员运算符in   、  not in
身份运算符is

一、算术运算符

运算符描述实例
+10 + 20 = 30
-

10 - 20 = -10

*10 * 20 = 200
/除(默认保留一位小数)10 / 20 = 0.5
//取整除(返回除法的整数部分)9 // 2 = 4
%取余数(返回除法的余数)9 % 2 = 1
**2 ** 3 = 8

1.算术运算表达式

  • 功能:进行符号对象的算术运算,不会修改变量的值
  • 值:相关算术运算的结果

2.优先级(由高到低)

运算符
**
*  /  %  //
+ - 

二、赋值运算符

运算符描述实例
=简单的赋值运算符c = a + b 将 a + b 的运算结果赋值为 c
+=加法赋值运算符c += a 等效于 c = c + a
-=减法赋值运算符c -= a 等效于 c = c - a
*=乘法赋值运算符c *= a 等效于 c = c * a
/=除法赋值运算符c /= a 等效于 c = c / a
//=取整除赋值运算符c //= a 等效于 c = c // a
%=取模(余数)赋值运算符c %= a 等效于 c = c % a
**=幂赋值运算符c **= a 等效于 c = c ** a

三、比较运算符

运算符描述
==检查两个操作数的值是否相等,如果是,则条件成立,返回 True
!=检查两个操作数的值是否不相等,如果是,则条件成立,返回True
>检查左操作数的值是否大于右操作数的值,如果是,则条件成立,返回True
<检查左操作数的值是否小于右操作数的值,如果是,则条件成立,返回True
>=检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立,返回True
<=检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立,返回True
print(3 != 3)
print(3 == 2)
print(3 >= 2)
print(3 <= 3)
print(3.0 == 3)
print(True == False)
print('hello' == 'hi')
print('hello' < 'hi')  # 字符串的比较
print(ord('a'))  # ord:查ASCII码值

运行结果:

False
False
True
True
True
False
False
True
97

四、逻辑运算符

运算符逻辑表达式描述
andx and y只有x和y的值都为True,才会返回True,否则只要x或者y有一个值为False,就返回False
orx or y只要x或者y有一个值为True,就返回True,只有x和y的值都为False,才会返回False
notnot x如果x为True,返回 False,如果x为 False,返回True
# and
print("*****and*****")
print(True and False)
print(True and True)
print(True and False and True)
print(1==1 and True and 2<3)
print('hello' and 'hi')  # 短路运算
print('' and 'hi')
print(False and 'hi')
print(0 and 1)

#or
print("*****or*****")
print(True or False)
print(False or False or True)
print(1 or 0)
print(2024 or 2025 or 0)
print(0 or '' or 888)

# not
print("*****not*****")
print(not True)
print(not 1)
print(not '')

# 优先级 not>and>or
print("*****优先级*****")
print(True and False and not False)
print(True or False and True or False)

运行结果:

*****and*****
False
True
False
True
hi

False
0
*****or*****
True
True
1
2024
888
*****not*****
False
False
True
*****优先级*****
False
True

优先级(由高到低):

运算符
not
and
or

五、位运算符

运算符描述功能
&按位与运算符参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0
|按位或运算符参与运算的两个值,如果两个相应位有一个为1时,则该位的结果为1,否则为0
^按位异或运算符参与运算的两个值,如果两个相应位不同时,则该位的结果为1,否则为0
~按位取反运算符对数据的每个二进制位进行取反操作,把1变为0,把0变为1
<<左移动运算符运算术的各二进制位全部向左移动若干位,由符号右侧的数字指定移动的位数,高位丢弃,低位补0
>>右移动运算符运算术的各二进制位全部向右移动若干位,由符号右侧的数字指定移动的位数,低位丢弃,高位补0
# 按位与&
print(5 & 7)
# 按位或 |
print(3 | 4)
# 按位异或^
print(2 ^ 4)
#按位取反~
print(~1)
# << >>
print(5 << 1)
print(8 >> 1)

运行结果:

5
7
6
-2
10
4

六、成员运算符

运算符描述实例
in如果在指定的序列中找到值,返回True,否则返回False

3 in(1,2,3)

返回True

not in如果在指定的序列中没有找到值,返回True,否则返回 False

3 not in(1,2,3)

返回False

print('12' in '123')
print('hi' not in 'hello')

运行结果:

True
True

七、身份运算符

运算符描述
is判断两个标识符是否引用同一个对象,是的话返回真,否则返回假
is not判断两个标识符是否不是引用同一个对象,是的话返回真,否则返回假
a = 1
b = 1
c = 17
print(a is b)
print(a is not c)

运行结果:

True
True

八、运算符的优先级(由高到低)

运算符
**
~      +(正号)      -(负号)
*     /     %     //
+(加)     -(减)
<<      >>
&
^
>     <     >=     <= 
==     !=
=     +=     -=     *=     /=     %=     //=     **=
is        is not
in       not in
not       or       and

九、习题

1.下列表达式的输出结果是:  False   

print('y'<'x'==False)

解析:等价于print('y'<'x' and 'x' == False)

2.下面代码的输出结果是:  False 

print(0.1+0.2==0.3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值