< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204172039119066042195214133

Standard Type Operators

Value Comparison

Comparison operators are used to determine equality of two data values between members of the same type. These comparison operators are supported for all built-in types. Comparisons yield true or false values, based on the validity of the comparison expression. Python chooses to interpret these values as the plain integers 0 and 1 for false and true, respectively, meaning that each comparison will result in one of those two possible values. A list of Python's value comparison operators is given in Table 4.1.

Table 4.1. Standard Type Value Comparison Operators

operator

function

expr1 < expr2

expr1 is less than expr2

expr1 > expr2

expr1 is greater than expr2

expr1 <= expr2

expr1 is less than or equal to expr2

expr1 >= expr2

expr1 is greater than or equal to expr2

expr1 == expr2

expr1 is equal to expr2

expr1 != expr2

expr1 is not equal to expr2 (C-style)

expr1 <> expr2

expr1 is not equal to expr2 (ABC/Pascal-style)[a]

[a] This "not equals" sign will slowly be phased out. Use != instead.

Note that comparisons performed are those that are appropriate for each data type. In other words, numeric types will be compared according to numeric value in sign and magnitude, strings will compare lexicographically, etc.

						
>>> 2 == 2
1
>>> 2.46 <= 8.33
1
>>> 5+4j >= 2-3j
1
>>> 'abc' == 'xyz'
0
>>> 'abc' > 'xyz'
0
>>> 'abc' < 'xyz'
1
>>> [3, 'abc'] == ['abc', 3]
0
>>> [3, 'abc'] == [3, 'abc']
1

					

Also, unlike many other languages, multiple comparisons can be made on the same line, evaluated in left-to-right order:

						
>>> 3 < 4 < 7       # same as ( 3 < 4 ) and ( 4 < 7 )
1
>>> 4 > 3 == 3      # same as ( 4 > 3 ) and ( 3 == 3 )
1
>>> 4 < 3 < 5 != 2 < 7
0

					

We would like to note here that comparisons are strictly between object values, meaning that the comparisons are between the data values and not the actual data objects themselves. For the latter, we will defer to the object identity comparison operators described next.

Object Identity Comparison

In addition to value comparisons, Python also supports the notion of directly comparing objects themselves. Objects can be assigned to other variables (by reference). Because each variable points to the same (shared) data object, any change effected through one variable will change the object and hence be reflected through all references to the same object.

In order to understand this, you will have to think of variables as linking to objects now and be less concerned with the values themselves. Let us take a look at three examples.

Example 1: foo1 and foo2 reference the same object
							
foo1 = foo2 = 4

						

When you look at this statement from the value point-of-view, it appears that you are performing a multiple assignment and assigning the numeric value of 4 to both the foo1 and foo2 variables. This is true to a certain degree, but upon lifting the covers, you will find that a numeric object with the contents or value of 4 has been created. Then that object's reference is assigned to both foo1 and foo2, resulting in both foo1 and foo2 aliased to the same object. Figure 4-1 shows an object with two references.

Figure 4-1. foo1 and foo2 Reference the Same Object
graphics/04fig01.gif
Example 2: foo1 and foo2 reference the same object
							
foo1 = 4
foo2 = foo1

						

This example is very much like the first: A numeric object with value 4 is created, then assigned to one variable. When foo2 = foo1 occurs, foo2 is directed to the same object as foo1 since Python deals with objects by passing references. foo2 then becomes a new and additional reference for the original value. So both foo1 and foo2 now point to the same object. The same figure above applies here as well.

Example 3: foo1 and foo2 reference different objects
							
foo1 = 4
foo2 = 1 + 3

						

This example is different. First, a numeric object is created, then assigned to foo1. Then a second numeric object is created, and this time assigned to foo2. Although both objects are storing the exact same value, there are indeed two distinct objects in the system, with foo1 pointing to the first, and foo2 being a reference to the second. Figure 4-2 below shows now we have two distinct objects even though both objects have the same value.

Figure 4-2. foo1 and foo2 Reference Different Objects
graphics/04fig02.gif

Why did we choose to use boxes in our diagrams above? Well, a good way to visualize this concept is to imagine a box (with contents inside) as an object. When a variable is assigned an object, that creates a "label" to stick on the box, indicating a reference has been made. Each time a new reference to the same object is made, another sticker is put on the box. When references are abandoned, then a label is removed. A box can be "recycled" only when all the labels have been peeled off the box. How does the system keep track of how many labels are on a box?

Each object has associated with it a counter that tracks the total number of references that exist to that object. This number simply indicates how many variables are "pointing to" any particular object. This is the reference count that we introduced in the last chapter in Sections 3.5.5–3.5.7. Python provides the is and is not operators to test if a pair of variables do indeed refer to the same object. Performing a check such as

							
a is b

						

is an equivalent expression to

							
id(a) == id(b)

						

The object identity comparison operators all share the same precedence level and are presented in Table 4.2.

Table 4.2. Standard Type Object Identity Comparison Operators

operator

function

obj1 is obj2

obj1 is the same object as obj2

obj1 is not obj2

obj1 is not the same object as obj2

In the example below, we create a variable, then another that points to the same object.

							
>>> a = [ 5, 'hat', -9.3]
>>> b = a
>>> a is b
1
>>> a is not b
0
>>>
>>> b = 2.5e-5
>>> b
2.5e-005
>>> a
[5, 'hat', -9.3]
>>> a is b
0
>>> a is not b
1

						

Both the is and not identifiers are Python keywords.

Boolean

Expressions may be linked together or negated using the boolean logical operators and, or, and not, all of which are Python keywords. These Boolean operations are in highest-to-lowest order of precedence in Table 4.3. The not operator has the highest precedence and is immediately one level below all the comparison operators. The and and or operators follow, respectively.

Table 4.3. Standard Type Boolean Operators

operator

function

not expr

logical NOT of expr (negation)

expr1 and expr2

logical AND of expr1 and expr2 (conjunction)

expr1 or expr2

logical OR of expr1 and expr2 (disjunction)

						
>>> x, y = 3.1415926536, -1024
>>> x < 5.0
1
>>> not (x < 5.0)
0
>>> (x < 5.0) or (y > 2.718281828)
1
>>> (x < 5.0) and (y > 2.718281828)
0
>>> not (x is y)
1

					

Earlier, we introduced the notion that Python supports multiple comparisons within one expression. These expressions have an implicit and operator joining them together.

						
>>> 3 < 4 < 7      # same as "( 3 < 4 ) and ( 4 < 7 )"
1

					


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.