比较运算符

Python 金融进阶

Kennedy Behrman

Data Engineer, Author, Founder

Python 比较运算符

  • 相等性: ==,!=
  • 大小: <,>,<=,>=
Python 金融进阶

相等运算符 vs 赋值

判断相等: ==

赋值: =

Python 金融进阶

相等运算符 vs 赋值

13 == 13
True
count = 13
print(count)
13
Python 金融进阶

相等性比较

  • 日期时间(datetime)
  • 数值(浮点、整数)
  • 字典
  • 字符串
  • 其他大多数对象
Python 金融进阶

比较日期时间

date_close_high = datetime(2019, 11, 27)
date_intra_high = datetime(2019, 11, 27)
print(date_close_high == date_intra_high)
True
Python 金融进阶

比较字典

d1 = {'high':56.88, 'low':33.22, 'closing':56.88}
d2 = {'high':56.88, 'low':33.22, 'closing':56.88}
print(d1 == d2)
True
d1 = {'high':56.88, 'low':33.22, 'closing':56.88}
d2 = {'high':56.88, 'low':33.22, 'closing':12.89}
print(d1 == d2)
False
Python 金融进阶

比较不同类型

print(3 == 3.0)
True
print(3 == '3')
False
Python 金融进阶

不等运算符

print(3 != 4)
True
print(3 != 3)
False
Python 金融进阶

大小比较运算符

  • 小于 <
  • 小于等于 <=
  • 大于 >
  • 大于等于 >=
Python 金融进阶

小于运算符

print(3 < 4)
True
print(3 < 3.6)
True
print('a' < 'b')
True
Python 金融进阶

小于运算符

date_close_high = datetime(2019, 11, 27)
date_intra_high = datetime(2019, 11, 27)
print(date_close_high < date_intra_high)
False
Python 金融进阶

小于等于运算符

print(1 <= 4)
True
print(1.0 <= 1)
True
print('e' <= 'a')
False
Python 金融进阶

大于运算符

print(6 > 5)
print(4 > 4)
True
False
Python 金融进阶

大于等于运算符

print(6 >= 5)
print(4 >= 4)
True
True
Python 金融进阶

跨类型大小比较

print(3.45454 < 90)
True
print('a' < 23)
<hr />----------------------------------------------
TypeError      Traceback (most recent call last)
...
TypeError: '<' not supported between instances of 'str' and 'int'
Python 金融进阶

Passons à la pratique !

Python 金融进阶

Preparing Video For Download...