Python Operators
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Python Operators

Kishore V

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

Addition result: 50

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.

200 500 1000

Explanation:

  • base_value and increment store numeric data.
  • total_one adds two values.
  • total_two uses a variable and a constant.
  • total_three adds 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

Sum: 24 Difference: 12 Product: 108 Quotient: 3.0 Remainder: 0 Power: 34012224 Floor Result: 3

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.

Standard division result: 4.666666666666667

2. Floor Division (//)

Returns the largest integer less than or equal to the result. Decimal values are discarded by rounding downward.

Floor division result: 4

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

After addition: 30 After subtraction: 25 After multiplication: 50 After floor division: 16

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

Total items: 5

Example: With the Walrus Operator

Total items: 5

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

Equal: False Not Equal: True Greater Than: False Less Than: True Greater or Equal: False Less or Equal: True

Chaining Comparison Operators

Python supports chained comparisons, allowing multiple conditions to be evaluated in a clean and readable way without repeating variables.

True

Equivalent Logical Expression:

True

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

Within range: True

Example: Using or Operator

Outside range: False

Example: Using not Operator

Reversed result: False

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

True False True

Example: Using the is not Operator

True

Difference Between is and ==

Value comparison: True Identity comparison: False

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

True

Example: Checking Non-Membership in a List

True

Membership Operators with Strings

True False True

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 (&)

0

Example: Bitwise OR (|)

14

Example: Bitwise XOR (^)

14

Example: Bitwise NOT (~)

-6

Example: Bit Shifting

32 4

Operator Precedence in Python

Operator precedence determines the sequence in which different operators in an expression are evaluated.

Parentheses Have the Highest Priority

0

Multiplication Before Addition

70

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.

10
Our website uses cookies to enhance your experience. Learn More
Accept !