# Python Expression Operators

The most commonly used tool to processes numbers is the `expression`. It is a combination of numbers (or other objects) and operators that computes a value when executed. Expressions are written using the mathematical notation and operator symbols.

The table below lists all the operator expressions and their description available in Python.

Note:
Not all expressions are numeric in nature.

OperatorDescription
yield xGenerator function send protocol
lambda args: expressionAnonymous function generation
x if y else zTernary selection ( x is evaluated only if y is true)
x or yLogical OR (y is evaluated only if x is false)
x and yLogical AND (y is evaluated only if x is true)
not xLogical negation
x in y , x not in yMembership (iterables, sets)
x is y , x is not yObject identity tests
x < y , x <= y , x > y , x >= yMagnitude comparison, set subset and superset;
x == y , x != yValue equality operators
x | yBitwise OR, set union
x ^ yBitwise XOR, set symmetric difference
x & yBitwise AND, set intersection
x << y , x >> yShift x left or right by y bits
x – ySubtraction, set difference
x * yMultiplication, repetition;
x % yRemainder, format;
x / y , x // yDivision: true and floor
−x , +xNegation, identity
~xBitwise NOT (inversion)
x ** yPower (exponentiation)
x[i]Indexing (sequence, mapping, others)
x[i:j:k]Slicing
x(…)Call (function, method, class, other callable)
x.attrAttribute reference
(…)Tuple, expression, generator expression
[…]List, list comprehension
{…}Dictionary, set, set and dictionary comprehensions

Examples:

``````# Declaring the variables
>>> a = 5
>>> b = 7

>>> a + 8
13
>>> a + b
12

# Subtraction
>>> b - 4
3
>>> b - a
2

# Created a tuple
>>> a + 8, b - 4
(13, 3)

# Multiplication
>>> a * 3
15
>>> a * b
35

# Division
>>> a / 2
2.5
>>> a / b
0.7142857142857143

# Reminder (Modulus))
>>> b % a
2
>>> a % b
5
>>> b % 7
0

# Power
>>> a ** 3
125
>>> b ** 2
49
>>> a ** b
78125

# Mixed-type conversions
>>> a + b * 3, (a + b) * 3, 2 ** b, (a / b) ** 2
(26, 36, 128, 0.5102040816326531)``````

Examples:
Comparisons: Normal and Chained

``````>>> d = 6
>>> e = 8
>>> f = 10
>>> d > e
False
>>> f > e
True
>>> f - e < d
True
>>> d < e < f
True
>>> d > e and e < f
False
>>> d > e or e < f
True
>>> d < 7 < e
True
>>> d < 7.34 < e
True
>>> d == e < f
False
>>> d == (f + e) / 2 - 3
True
>>> 3.3 + 4.4 == 7.7
True
>>> int(3.3 + 4.4) == int(7.7)
True``````

Examples:
Division: Classic, Floor, and True

``````>>> f / e
1.25
>>> 10 / 2
5.0
>>> 10 / 2.0
5.0
>>> 10.8 / 5.4
2.0
>>> 10 // 4
2
>>> 10 // 3
3
>>> 10 // 3.0
3.0``````

Example:
Import math, floor, truncate

``````>>> import math
>>> math.floor(2.5) # rounds to first lower integer
2
>>> math.floor(-2.5)
-3
>>> math.trunc(2.5) # rounds towards zero
2
>>> math.trunc(-2.5)
-2
>>> math.floor(2.5)
2
>>> math.floor(-2.5)
-3
>>> math.trunc(2.5)
2
>>> math.trunc(-2.5)
-2
>>> math.trunc(10 / 4)
2
>>> math.trunc(10 / -4)
-2
>>> math.floor(10 / 3)
3
>>> math.floor(-10 / 3)
-4
>>> math.trunc(10 / -4)
-2
>>> math.floor(10 / 6), math.trunc(1 * 3.5)
(1, 3)``````