官术网_书友最值得收藏!

Comparison operators

Like any other language, Python also supports comparison operators. Comparison operators return True or False:

Some examples of comparison operators and their outcome are shown in the following screenshot:

Let's evaluate the following expression:

a < b < = c is equivalent to a<b and b<=c 

Here, the and operator is used, b is evaluated only once, and c will not be evaluated if a < b is found False:

In the preceding example, 12<5 is evaluated first; if it is False, then the next expression will not be evaluated. If the operands are of different types, then they are converted to a common type. Otherwise, the == and != operators always consider objects of different types to be unequal.

Let's look at two different scenarios where we try to evaluate a float data type with an int data type, as shown:

Here, the operator evaluates to True in both the cases, as the interpreter converts one data type to another and then compares both the values.

Comparison between different data types:

  • Numbers are compared arithmetically
  • Strings are compared as per the alphabetical order, using the numeric equivalents
  • Tuples and lists are compared according to the alphabetical order using the comparison of corresponding elements, which we will see in the coming chapters
主站蜘蛛池模板: 芜湖县| 马鞍山市| 河源市| 香河县| 抚松县| 赤壁市| 麻江县| 斗六市| 京山县| 陇川县| 襄汾县| 唐海县| 通化市| 锦州市| 鞍山市| 杭锦旗| 满城县| 平和县| 亚东县| 腾冲县| 桃江县| 木兰县| 奉化市| 吉木萨尔县| 达拉特旗| 抚宁县| 巴南区| 张家港市| 肃南| 原阳县| 涡阳县| 陇川县| 怀安县| 平阴县| 台北县| 蒙城县| 新乡县| 衡阳市| 邵阳县| 眉山市| 巴中市|