运算符——Python

运算符的分类

  • 算数运算符
  • 赋值运算符
  • 复合赋值运算符
  • 比较运算符
  • 逻辑运算符
  • 三目运算符

1、算术运算符

运算符 描述
+
-
*
/
// 整除
% 取余数
** 指数(2**4等于222*2)
() 小括号(用来提高运算优先级)

2、赋值运算符

运算符 描述
= 赋值
  • 单个变量赋值
a=1
  • 多个变量赋值
int1,float1,str1=3,3.3,'易烊千玺'
print(type(int1))
print(type(float1))
print(type(str1))
print(int1)
print(float1)
print(str1)
输出:
<class 'int'>
<class 'float'>
<class 'str'>
3
3.3
易烊千玺
  • 多变量赋值
a=b=3(同时将3赋值给a和b)

3、复合赋值运算符

运算符 描述
+=
-=
*=
/=
//= c//=a等价于c=c//a
%=
**=

Python的撤销和反撤销操作:
ctrl+z、shift+z

c=10
c+=1+2
print(c)
b=10
b*=1+2#b=b*(1+2)
print(b)
输出:
13
30
#先算复合赋值运算右边的表达式,之后再进行赋值运算

4、比较运算符
比较运算符也叫关系运算符,通常用来判断

运算符 描述
==
!=
>
<
>=
<=

5、逻辑运算符

运算符 逻辑表达式
and x and y(若x为false,则返回false,否则,返回y的值)
or 若x是true,则返回true,否则,返回y的值
not 如果x为true,则返回false,若x为false,则返回true

tips

  • and运算符:只要有一个值为0,则结果为0,否则结果为最后一个非0数字
  • or运算符:只有所有值为0结尾才为0,否则结果为第一个非0数字
print(1 and 0)
print(0 and 2)
print(1 or 0)
print(0 or 2)
输出:
0
0
1
2
  • 运算优先级;()高于**高于* // / %高于+ -
    6、三目运算符
    条件成立执行的表达式 if 条件 else 条件不成立执行的表达式
发布了47 篇原创文章 · 获赞 12 · 访问量 7229

猜你喜欢

转载自blog.csdn.net/weixin_43717681/article/details/104074074