Arithmetic and Comparison Operators
Introduction
This chapter covers the various built-in operators, which Python has to offer.Operators
These operations (operators) can be applied to all numeric types:| Operator | Description | Example |
|---|---|---|
| +, - | Addition, Subtraction | 10 -3 |
| *, % | Multiplication, Modulo |
27 % 7 Result: 6 |
| / |
Division
This operation results in different results for Python 2.x (like floor division) and Python 3.x |
Python3:
>>> 10 / 3 3.3333333333333335and in Python 2.x: >>> 10 / 3 3 |
| // |
Truncation Division (also known as floor division) The result of the division is truncated to an integer. Works for integers and floating-point numbers as well |
27 % 7 Result: 6 |
| +x, -x | Unary minus and Unary plus (Algebraic signs) | -3 |
| ~x | Bitwise negation |
~3 - 4 Result: -8 |
| ** | Exponentiation |
10 ** 3 Result: 1000 |
| or, and, not | Boolean Or, Boolean And, Boolean Not | (a or b) and c |
| in | "Element of" | 1 in [3, 2, 1] |
| <, <=, >, >=, !=, == | The usual comparison operators | 2 <= 3 |
| |, &, ^ | Bitwise Or, Bitwise And, Bitwise XOR | 6 ^ 3 |
| <<, >> | Shift Operatoren | 6 << 3 |
