Let’s break down Python expressions, operators, and precedence rules:
1. Expressions
- Definition: An expression is any valid combination of values, variables, operators, and function calls that Python can evaluate to a single value.
- Examples:
5 + 3
(Evaluates to 8)"Hello" + " World!"
(Evaluates to “Hello World!”)x * 2
(Evaluates to twice the value of the variablex
)len("Python")
(Evaluates to 6, the length of the string “Python”)
2. Operators
Operators are special symbols that perform specific operations on values (operands). Here’s a breakdown:
-
Arithmetic Operators:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)//
(Floor Division - result is rounded down to the nearest whole number)%
(Modulo - returns the remainder of a division)**
(Exponentiation - raising a number to a power)
-
Comparison Operators:
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
-
Logical Operators:
and
(Logical AND - True if both operands are True)or
(Logical OR - True if at least one operand is True)not
(Logical NOT - Inverts the truth value of the operand)
-
Bitwise Operators (operate on binary representations of numbers):
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)~
(Bitwise NOT)<<
(Bitwise left shift)>>
(Bitwise right shift)
-
Assignment Operators:
=
(Assignment)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)//=
(Floor divide and assign)%=
(Modulo and assign)**=
(Exponentiate and assign)&=
(Bitwise AND and assign)|=
(Bitwise OR and assign)^=
(Bitwise XOR and assign)<<=
(Bitwise left shift and assign)>>=
(Bitwise right shift and assign)
-
Identity Operators:
is
(True if both variables refer to the same object)is not
(True if both variables refer to different objects)
-
Membership Operators:
in
(True if a value is found in a sequence)not in
(True if a value is not found in a sequence)
3. Operator Precedence (Order of Operations)
Python follows a specific order to evaluate operators within an expression. Higher precedence operators are evaluated before lower precedence operators:
- Parentheses
()
: Expressions inside parentheses are always evaluated first. - Exponentiation
**
- Unary Operators (
+
,-
,~
) - Multiplication, Division, Floor Division, Modulo (
*
,/
,//
,%
) - Addition, Subtraction (
+
,-
) - Bitwise Shift Operators (
<<
,>>
) - Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Comparison Operators (
==
,!=
,>
,<
,>=
,<=
) - Logical NOT
not
- Logical AND
and
- Logical OR
or
- Assignment Operators (
=
,+=
,-=
,*=
, etc.)
Important Notes:
- Left-to-Right Evaluation: Operators with the same precedence are usually evaluated from left to right.
- Parentheses for Clarity: Even if not strictly required by precedence rules, using parentheses can make your code much easier to read and understand.
1# Example
2result = 5 + 3 * 2 ** 2 - (8 // 4)
3print(result)
4# 15
Let’s break down the above example how Python evaluates this expression using precedence:
- Parentheses:
(8 // 4)
evaluates to2
. - Exponentiation:
2 ** 2
evaluates to4
. - Multiplication:
3 * 4
evaluates to12
. - Addition and Subtraction (left to right):
5 + 12
evaluates to17
.17 - 2
evaluates to15
.
Therefore, the value stored in the result
variable would be 15
.