比較演算子

Intermediate Python for Finance

Kennedy Behrman

Data Engineer, Author, Founder

Python の比較演算子

  • 等価: ==,!=
  • 大小: <,>,<=,>=
Intermediate Python for Finance

等価演算子と代入の違い

等価テスト: ==

代入: =

Intermediate Python for Finance

等価演算子と代入の違い

13 == 13
True
count = 13
print(count)
13
Intermediate Python for Finance

等価比較が可能なもの

  • datetime
  • 数値(float, int)
  • 辞書
  • 文字列
  • ほぼその他すべて
Intermediate Python for Finance

datetime の比較

date_close_high = datetime(2019, 11, 27)
date_intra_high = datetime(2019, 11, 27)
print(date_close_high == date_intra_high)
True
Intermediate Python for Finance

辞書の比較

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
Intermediate Python for Finance

異なる型の比較

print(3 == 3.0)
True
print(3 == '3')
False
Intermediate Python for Finance

不等号(!=)

print(3 != 4)
True
print(3 != 3)
False
Intermediate Python for Finance

大小比較演算子

  • より小さい <
  • 以下 <=
  • より大きい >
  • 以上 >=
Intermediate Python for Finance

より小さい演算子

print(3 < 4)
True
print(3 < 3.6)
True
print('a' < 'b')
True
Intermediate Python for Finance

より小さい演算子

date_close_high = datetime(2019, 11, 27)
date_intra_high = datetime(2019, 11, 27)
print(date_close_high < date_intra_high)
False
Intermediate Python for Finance

以下演算子

print(1 <= 4)
True
print(1.0 <= 1)
True
print('e' <= 'a')
False
Intermediate Python for Finance

より大きい演算子

print(6 > 5)
print(4 > 4)
True
False
Intermediate Python for Finance

以上演算子

print(6 >= 5)
print(4 >= 4)
True
True
Intermediate Python for Finance

型をまたぐ大小比較

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

Let's practice!

Intermediate Python for Finance

Preparing Video For Download...