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:
Operator | Description | Example |
---|---|---|
Arithmetic Operators | Perform mathematical calculations | |
+ | Addition | 5 + 3 returns 8 |
- | Subtraction | 5 - 3 returns 2 |
* | Multiplication | 5 * 3 returns 15 |
/ | Division | 5 / 3 returns 1.6667 |
% | Modulus (remainder) | 5 % 3 returns 2 |
** | Exponentiation | 2 ** 3 returns 8 |
Comparison Operators | Compare the values of operands | |
== | Equal to | 5 == 3 returns False |
!= | Not equal to | 5 != 3 returns True |
> | Greater than | 5 > 3 returns True |
< | Less than | 5 < 3 returns False |
>= | Greater than or equal to | 5 >= 3 returns True |
<= | Less than or equal to | 5 <= 3 returns False |
Logical Operators | Perform logical operations | |
and | Logical AND | True and False returns False |
or | Logical OR | True or False returns True |
not | Logical NOT | not True returns False |
Assignment Operators | Assign values to variables | |
= | Assignment | x = 5 |
+= | Addition assignment | x += 3 (equivalent to x = x + 3 ) |
-= | Subtraction assignment | x -= 3 (equivalent to x = x - 3 ) |
*= | Multiplication assignment | x *= 3 (equivalent to x = x * 3 ) |
/= | Division assignment | x /= 3 (equivalent to x = x / 3 ) |
`%=`` | Modulus assignment | x %= 3 (equivalent to x = x % 3 ) |
Identity Operators | Compare the memory addresses of objects | |
is | Identity | x is y |
is not | Negated identity | x is not y |
Membership Operators | Check if a value is a member of a sequence | |
in | Membership | 5 in [1, 2, 3] returns False |
not in | Negated membership | 5 not in [1, 2, 3] returns True |
Bitwise Operators | Perform bitwise operations | |
& | Bitwise AND | 5 & 3 returns 1 |
` | ` | Bitwise OR |
^ | Bitwise XOR | 5 ^ 3 returns 6 |
~ | Bitwise complement | ~5 returns -6 |
<< | Left shift | 5 << 2 returns 20 |
>> | Right shift | 5 >> 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 Level | Operators |
---|---|
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) | |
Lowest | or (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.