Online Book Reader

Home Category

Learning Python - Mark Lutz [83]

By Root 1365 0
2.0 >= 1 # Greater than or equal: mixed-type 1 converted to 1.0

True

>>> 2.0 == 2.0 # Equal value

True

>>> 2.0 != 2.0 # Not equal value

False

Notice again how mixed types are allowed in numeric expressions (only); in the second test here, Python compares values in terms of the more complex type, float.

Interestingly, Python also allows us to chain multiple comparisons together to perform range tests. Chained comparisons are a sort of shorthand for larger Boolean expressions. In short, Python lets us string together magnitude comparison tests to code chained comparisons such as range tests. The expression (A < B < C), for instance, tests whether B is between A and C; it is equivalent to the Boolean test (A < B and B < C) but is easier on the eyes (and the keyboard). For example, assume the following assignments:

>>> X = 2

>>> Y = 4

>>> Z = 6

The following two expressions have identical effects, but the first is shorter to type, and it may run slightly faster since Python needs to evaluate Y only once:

>>> X < Y < Z # Chained comparisons: range tests

True

>>> X < Y and Y < Z

True

The same equivalence holds for false results, and arbitrary chain lengths are allowed:

>>> X < Y > Z

False

>>> X < Y and Y > Z

False

>>> 1 < 2 < 3.0 < 4

True

>>> 1 > 2 > 3.0 > 4

False

You can use other comparisons in chained tests, but the resulting expressions can become nonintuitive unless you evaluate them the way Python does. The following, for instance, is false just because 1 is not equal to 2:

>>> 1 == 2 < 3 # Same as: 1 == 2 and 2 < 3

False # Not same as: False < 3 (which means 0 < 3, which is true)

Python does not compare the 1 == 2 False result to 3—this would technically mean the same as 0 < 3, which would be True (as we’ll see later in this chapter, True and False are just customized 1 and 0).

Division: Classic, Floor, and True

You’ve seen how division works in the previous sections, so you should know that it behaves slightly differently in Python 3.0 and 2.6. In fact, there are actually three flavors of division, and two different division operators, one of which changes in 3.0:

X / Y

Classic and true division. In Python 2.6 and earlier, this operator performs classic division, truncating results for integers and keeping remainders for floating-point numbers. In Python 3.0, it performs true division, always keeping remainders regardless of types.

X // Y

Floor division. Added in Python 2.2 and available in both Python 2.6 and 3.0, this operator always truncates fractional remainders down to their floor, regardless of types.

True division was added to address the fact that the results of the original classic division model are dependent on operand types, and so can be difficult to anticipate in a dynamically typed language like Python. Classic division was removed in 3.0 because of this constraint—the / and // operators implement true and floor division in 3.0.

In sum:

In 3.0, the / now always performs true division, returning a float result that includes any remainder, regardless of operand types. The // performs floor division, which truncates the remainder and returns an integer for integer operands or a float if any operand is a float.

In 2.6, the / does classic division, performing truncating integer division if both operands are integers and float division (keeping remainders) otherwise. The // does floor division and works as it does in 3.0, performing truncating division for integers and floor division for floats.

Here are the two operators at work in 3.0 and 2.6:

C:\misc> C:\Python30\python

>>>

>>> 10 / 4 # Differs in 3.0: keeps remainder

2.5

>>> 10 // 4 # Same in 3.0: truncates remainder

2

>>> 10 / 4.0 # Same in 3.0: keeps remainder

2.5

>>> 10 // 4.0 # Same in 3.0: truncates to floor

2.0

C:\misc> C:\Python26\python

>>>

>>> 10 / 4

2

>>> 10 // 4

2

>>> 10 / 4.0

2.5

>>> 10 // 4.0

2.0

Notice that the data type of the result for // is still dependent on the operand types in 3.0: if either is a float, the result is a float; otherwise, it is

Return Main Page Previous Page Next Page

®Online Book Reader