## 5.7 More on Conditions 

The conditions used in while and if statements can contain any operators, not just comparisons. 

The comparison operators$_{in}$ and$_{not in}$ check whether a value occurs (does not occur) in a sequence. The operators is and is not compare whether two objects are really the same object. All comparison operators have the same priority, which is lower than that of all numerical operators. 

Comparisons can be chained. For example, a < b == c tests whether a is less than b and moreover b equals c . 

Comparisons may be combined using the Boolean operators and and or , and the outcome of a comparison (or of any other Boolean expression) may be negated with not . These have lower priorities than comparison operators; between them,$_{not}$has the highest priority and$_{or}$ the lowest, so that$_{A and not B or C}$ is equivalent to$_{(A}$ and (not B)) or C . As always, parentheses can be used to express the desired composition. 

The Boolean operators$_{and}$and$_{or}$ are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if$_{A}$ and$_{C}$ are true but$_{B}$ is false,$_{A}$ and B and C does not evaluate the expression C . When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument. 

It is possible to assign the result of a comparison or other Boolean expression to a variable. For example, 

>>> string1, string2, string3 ='','Trondheim ','Hammer Dance' >>> non_null=string1 or string2 or string3 >>> non_null 'Trondheim' 

Note that in Python, unlike C, assignment inside expressions must be done explicitly with the walrus operator$_{:=}$ . This avoids a common class of problems encountered in C programs: typing$_{=}$in an expression when$_{==}$ was intended. 

## 5.8 Comparing Sequences and Other Types 

Sequence objects typically may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type: 

(1,2,3) <(1,2,4) [1,2,3] <[1,2,4] 'ABC '<'C'<'Pascal'<'Python' (1,2,3,4) <(1,2,4) (1,2) <(1,2,-1) (1,2,3) ==(1.0 ,2.0 ,3.0 ) (1,2, ( 'aa','ab')) <(1,2, ( 'abc','a'),4) 

Note that comparing objects of different types with$_{<}$or$_{>}$is legal provided that the objects have appropriate com- parison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a$_{TypeError}$ exception. 

