Skip to main content

2.2 - Operators & Expressions

2.2.1 - Operators

In Python, operators are special symbols or characters that perform various operations on operands (values or variables). Python supports a wide range of operators, including:

OperatorDescriptionExample
Arithmetic OperatorsPerform mathematical calculations
+Addition5 + 3 returns 8
-Subtraction5 - 3 returns 2
*Multiplication5 * 3 returns 15
/Division5 / 3 returns 1.6667
%Modulus (remainder)5 % 3 returns 2
**Exponentiation2 ** 3 returns 8
Comparison OperatorsCompare the values of operands
==Equal to5 == 3 returns False
!=Not equal to5 != 3 returns True
>Greater than5 > 3 returns True
<Less than5 < 3 returns False
>=Greater than or equal to5 >= 3 returns True
<=Less than or equal to5 <= 3 returns False
Logical OperatorsPerform logical operations
andLogical ANDTrue and False returns False
orLogical ORTrue or False returns True
notLogical NOTnot True returns False
Assignment OperatorsAssign values to variables
=Assignmentx = 5
+=Addition assignmentx += 3 (equivalent to x = x + 3)
-=Subtraction assignmentx -= 3 (equivalent to x = x - 3)
*=Multiplication assignmentx *= 3 (equivalent to x = x * 3)
/=Division assignmentx /= 3 (equivalent to x = x / 3)
`%=``Modulus assignmentx %= 3 (equivalent to x = x % 3)
Identity OperatorsCompare the memory addresses of objects
isIdentityx is y
is notNegated identityx is not y
Membership OperatorsCheck if a value is a member of a sequence
inMembership5 in [1, 2, 3] returns False
not inNegated membership5 not in [1, 2, 3] returns True
Bitwise OperatorsPerform bitwise operations
&Bitwise AND5 & 3 returns 1
``Bitwise OR
^Bitwise XOR5 ^ 3 returns 6
~Bitwise complement~5 returns -6
<<Left shift5 << 2 returns 20
>>Right shift5 >> 2 returns 1

2.2.2 - Expressions

Expressions in Python are combinations of values, variables, operators, and function calls that produce a result. Expressions can be as simple as a single value or more complex with multiple operators and operands.

For example, consider the following expressions:

x = 5
y = 10
z = x + y # Addition expression
result = (x * y) / z # Complex expression involving multiple operators

In the above code, the expression x + y adds the values of variables x and y. The expression (x * y) / z performs multiplication and division operations.

Expressions can also include function calls. For example:

import math

radius = 5
area = math.pi * radius ** 2 # Expression involving a function call and exponentiation

In this example, the expression math.pi * radius ** 2 calculates the area of a circle using the formula πr².


2.2.3 - Operator Precedence and Associativity

Python follows specific rules for operator precedence and associativity when evaluating expressions. Operator precedence determines the order in which operators are evaluated. For example, multiplication (*) has higher precedence than addition (+), so 2 + 3 * 4 is evaluated as 2 + (3 * 4), resulting in 14.

If multiple operators with the same precedence appear in an expression, the associativity determines the order of evaluation. For example, addition (+) and subtraction (-) have the same precedence and are left associative, so 5 - 3 + 2 is evaluated as (5 - 3) + 2, resulting in 4.

To override the default precedence and associativity, parentheses can be used to group expressions and enforce the desired order of evaluation.

Precedence LevelOperators
Highest** (Exponentiation)
+x, -x (Positive, Negative)
~x (Bitwise NOT)
await x (Await)
x[index], x[index:index], x(arguments...), x.attribute (Subscription, Slicing, Call, Attribute reference)
** (Exponentiation)
*, /, //, % (Multiplication, Division, Floor division, Modulo)
+, - (Addition, Subtraction)
<<, >> (Bitwise shift)
& (Bitwise AND)
^ (Bitwise XOR)
`
in, not in, is, is not, <, <=, >, >=, !=, == (Comparisons, Membership, Identity)
not x (Boolean NOT)
and (Boolean AND)
Lowestor (Boolean OR)

The operators with the highest precedence are evaluated first, followed by the operators with the next highest precedence, and so on. This ensures that expressions are evaluated in the correct order.

Here is a brief explanation of each operator precedence level:

  • Highest: Exponentiation, unary plus and minus, bitwise NOT, await, subscription, slicing, call, attribute reference.
  • Second highest: Multiplication, division, floor division, modulo.
  • Third highest: Addition, subtraction.
  • Fourth highest: Bitwise shift.
  • Fifth highest: Bitwise AND, bitwise XOR, bitwise OR.
  • Sixth highest: Comparisons, membership, identity.
  • Seventh highest: Boolean NOT.
  • Lowest: Boolean AND, Boolean OR.

2.2.4 - Summary

Operators and expressions are fundamental concepts in Python programming

. Operators perform operations on operands, which can be values or variables. Python supports various operators, including arithmetic, comparison, logical, assignment, identity, membership, and bitwise operators.

Expressions combine values, variables, operators, and function calls to produce results. They can be simple or complex, and their evaluation follows rules of operator precedence and associativity. Understanding operators and expressions is essential for writing effective and meaningful Python code.