4. Operators and Expressions

This chapter describes Python’s built-in operators, expressions, and evaluation rules. Although much of this chapter describes Python’s built-in types, user-defined objects can easily redefine any of the operators to provide their own behavior.

Operations on Numbers

The following operations can be applied to all numeric types:

Image

The truncating division operator (//, also known as floor division) truncates the result to an integer and works with both integers and floating-point numbers. In Python 2, the true division operator (/) also truncates the result to an integer if the operands are integers. Therefore, 7/4 is ...

Get Python: Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.