12期:Python基础课
一木.溪桥学Python-03: Python 运算符
日期:2020年12月18日
+
-
*
/
% 返余
// 返商整
** 幂次
from decimal import Decimal
a = 1.33-1.111
b = Decimal('1.33')-Decimal('1.111')
print(a)
print(b)
run:
0.21900000000000008
0.219
>
<
==
!=
<=
>=
=
+=
-=
*=
/=
%=
//=
**=
and
or
not
print(10 and 20)
print(20 and 10)
run:
20
10
& 按位与:两者都为1才为1,否则为0
^ 按位或:只有一个为1则为1,否则为0
| 按位异或:两者相等则为0,否则为1
in 判断某个值是否在指定序列中,在则返回True
not in 判断某个值是否在指定序列中,不在则返回True
is 判断两个对象的内存地址是否一致,是则返回True
is not 判断两个对象的内存地址是否不一致,不是则返回True
a = 1
b = 2
print(a+b if a>b else b-a)
run:
1
name = 'amy'
print( 'amy 在逻辑大家庭' if name == ["bob","tony","Lisa","Steven"]else ' amy 不在 ... 好难过')
run:
amy 不在 ... 好难过
name = 'amy'
print( 'amy 在逻辑大家庭' if name in ["bob","tony","Lisa","Steven"]else ' amy 不在 ... 好难过')
run:
amy 不在 ... 好难过
print('ax' < 'xa' )
run:
True
str 与 str 之间比较的是 ASCII,并且 逐个比较
temp = input('请输入:')
if temp == 'Yes' or 'yes':
print('if执行了!')
else:
print('else执行了!')
run:
请输入:666
if执行了!
a = input('a = ')
b = input('b = ')
print(float(a)/float(b) if a and b !=0 else float(a)*float(b))
run:
a = 0
b = 3
0.0
a = 1
b = 5
0.2
a = 1
b = 0
Traceback (most recent call last):
File "F:/Program Files/Pycharm/Projects/Lesson03/作业.py", line 17, in
print(float(a)/float(b) if a and b !=0 else float(a)*float(b))
ZeroDivisionError: float division by zero
a = j
b = k
Traceback (most recent call last):
File "F:/Program Files/Pycharm/Projects/Lesson03/作业.py", line 17, in
print(float(a)/float(b) if a and b !=0 else float(a)*float(b))
ValueError: could not convert string to float: 'j'
a = input('a = ')
b = input('b = ')
print('输入较大数为:', a if a>b else b)
run:
a = 3
b = 5
输入较大数为: 5
a = 2
b = 2
输入较大数为: 2
a = j
b = k
输入较大数为: k
Amy老师两数相等时,三目运算符的判断是不是不符合题目的判断逻辑呀!