Python Operators
In Python, operators are symbols that enable you to perform different kinds of operations on data, such as calculations, comparisons, and logical evaluations. They work on values, variables, or a combination of both to produce a result.
The addition operator (+) is one of the most commonly used operators. It allows you to combine
numerical values efficiently.
Example: Adding Two Numbers
Here, the
+ operator
adds two integer values and stores the outcome in the variable result.
Example: Adding Variables and Values
The addition operator can also be applied when working with variables, making calculations more flexible and dynamic.
Explanation:
-
base_valueandincrementstore numeric data. -
total_oneadds two values. -
total_twouses a variable and a constant. -
total_threeadds two variables together.
This method improves code clarity and allows values to be reused across multiple calculations.
Categories of Python Operators
Python classifies operators into several groups based on their functionality:
-
Arithmetic Operators – Perform mathematical calculations
(e.g.,
+,-,*,/) -
Assignment Operators – Assign values to variables (e.g.,
=,+=,-=) -
Comparison Operators – Compare two values (e.g.,
==,!=,>,<) -
Logical Operators – Combine conditional statements (e.g.,
and,or,not) -
Identity Operators – Check if two variables reference the
same object (e.g.,
is,is not) -
Membership Operators – Test for presence in a sequence
(e.g.,
in,not in) -
Bitwise Operators – Operate at the binary level (e.g.,
&,|,^)
Arithmetic Operators in Python
Arithmetic operators in Python are used with numerical data types to carry out basic and advanced mathematical calculations. These operators allow programs to perform operations such as addition, subtraction, multiplication, and different types of division.
Types of Arithmetic Operators
| Operator | Operation Name | Description Example |
|---|---|---|
+
|
Addition | Adds two numbers |
-
|
Subtraction | Subtracts one value from another |
*
|
Multiplication | Multiplies values |
/
|
Division | Divides and returns a decimal value |
%
|
Modulus | Returns the remainder |
**
|
Exponentiation | Raises a number to a power |
//
|
Floor Division | Returns the quotient without decimals |
Example: Using Arithmetic Operators
Explanation: The program applies different arithmetic
operators to the same set of numbers. Division using
/ produces a
decimal value, even when the numbers divide evenly.
Division Operators in Python
Python supports two types of division, each serving a different purpose.
1. Standard Division (/)
Always returns a floating-point value. Useful when precision is required.
2. Floor Division (//)
Returns the largest integer less than or equal to the result. Decimal values are discarded by rounding downward.
Key Difference:
/ keeps the
decimal part, while
// removes
the decimal part by rounding down. This distinction is especially important
when working with loops, indexing, or calculations where whole numbers are
required.
Assignment Operators in Python
Assignment operators in Python are used to store values in variables. In addition to simple assignment, Python provides compound operators that perform an operation and assign the result back to the same variable in a single step.
| Operator | Purpose | Equivalent Expression |
|---|---|---|
=
|
Assign value |
a = 10
|
+=
|
Add and assign |
a = a + 5
|
-=
|
Subtract and assign |
a = a - 5
|
*=
|
Multiply and assign |
a = a * 5
|
/=
|
Divide and assign |
a = a / 5
|
%=
|
Modulus and assign |
a = a % 5
|
//=
|
Floor divide and assign |
a = a // 5
|
**=
|
Power and assign |
a = a ** 5
|
&=
|
Bitwise AND and assign |
a = a & 5
|
|=
|
Bitwise OR and assign |
a = a | 5
|
^=
|
Bitwise XOR and assign |
a = a ^ 5
|
>>=
|
Right shift and assign |
a = a >> 2
|
<<=
|
Left shift and assign |
a = a << 2
|
Example: Using Assignment Operators
The Walrus Operator (:=)
Python 3.8 introduced the walrus operator, which allows you to assign a value to a variable while using it within an expression. This is especially helpful in conditions and loops where a value would otherwise be calculated twice.
Example: Without the Walrus Operator
Example: With the Walrus Operator
Comparison Operators in Python
Comparison operators in Python are used to evaluate the relationship between
two values. These operators compare operands and always return a Boolean
result—either
True or
False.
| Operator | Description | Usage Example |
|---|---|---|
==
|
Checks if two values are equal |
a == b
|
!=
|
Checks if values are different |
a != b
|
>
|
Checks if left value is greater |
a > b
|
<
|
Checks if left value is smaller |
a < b
|
>=
|
Checks if left value is greater than or equal |
a >= b
|
<=
|
Checks if left value is less than or equal |
a <= b
|
Example: Using Comparison Operators
Chaining Comparison Operators
Python supports chained comparisons, allowing multiple conditions to be evaluated in a clean and readable way without repeating variables.
Equivalent Logical Expression:
Logical Operators in Python
Logical operators in Python are used to combine multiple conditions and evaluate them as a single Boolean expression.
| Operator | Meaning | Example Usage |
|---|---|---|
and
|
True when all conditions are true |
a > 2 and a < 8
|
or
|
True when at least one condition is true |
a < 3 or a > 10
|
not
|
Reverses the Boolean result |
not(a == 5)
|
Example: Using and Operator
Example: Using or Operator
Example: Using not Operator
Python Identity Operators
Identity operators in Python are used to determine whether two variables reference the same object in memory, rather than just containing equal values.
| Operator | Description | Example |
|---|---|---|
is
|
Returns True if both variables refer to the same object |
a is b
|
is not
|
Returns True if variables refer to different objects |
a is not b
|
Example: Using the is Operator
Example: Using the is not Operator
Difference Between is and ==
Membership Operators in Python
Membership operators in Python are used to verify whether a particular value exists within a collection or sequence.
| Operator | Meaning | Example |
|---|---|---|
in
|
Returns True if the value exists in the sequence |
item in collection
|
not in
|
Returns True if the value does not exist in the sequence |
item not in collection
|
Example: Checking Membership in a List
Example: Checking Non-Membership in a List
Membership Operators with Strings
Bitwise Operators in Python
Bitwise operators in Python work directly on the binary (bit-level) representation of integers. Instead of treating numbers as whole values, these operators manipulate individual bits.
| Operator | Name | Function |
|---|---|---|
&
|
AND | Sets a bit to 1 only if both corresponding bits are 1 |
|
|
OR | Sets a bit to 1 if either bit is 1 |
^
|
XOR | Sets a bit to 1 if the bits are different |
~
|
NOT | Flips all bits (bitwise inversion) |
<<
|
Left Shift | Shifts bits left, adding zeros on the right |
>>
|
Right Shift | Shifts bits right, preserving the sign bit |
Example: Bitwise AND (&)
Example: Bitwise OR (|)
Example: Bitwise XOR (^)
Example: Bitwise NOT (~)
Example: Bit Shifting
Operator Precedence in Python
Operator precedence determines the sequence in which different operators in an expression are evaluated.
Parentheses Have the Highest Priority
Multiplication Before Addition
Operator Precedence Order (Highest to Lowest)
| Operator | Description |
|---|---|
()
|
Parentheses |
**
|
Exponentiation |
+x, -x, ~x
|
Unary plus, unary minus, bitwise NOT |
*, /, //, %
|
Multiplication, division, floor division, modulus |
+, -
|
Addition and subtraction |
<<, >>
|
Bitwise shifts |
&
|
Bitwise AND |
^
|
Bitwise XOR |
==, !=, >, >=, <, <=, is, is not, in, not in
|
Comparison, identity, membership |
not
|
Logical NOT |
and
|
Logical AND |
or
|
Logical OR |
Left-to-Right Evaluation
When operators have the same precedence, Python evaluates the expression from left to right.
