Operators in Python

साइट: Dr. B.R. Ambedkar Open University Online Learning Portal
कोर्स: Basics of Programming with Python
पुस्तक: Operators in Python
द्वारा छापा गया: अतिथि उपयेागकर्ता
दिनांक: शनिवार, 30 मई 2026, 2:01 AM

विवरण

This Book deals with What is an Operator and what are the various operators in Python.

Note : This section is mandatory to complete.

1. Introduction about Python Operators

Operators are symbols used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

print(10 + 5)

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

2. Arithmetic Operators

  • Arithmetic Operators are operators which are used to perform mathematical calculations in a program.  The below table gives the clear idea about Arithmetic operators in Python.

Operator

Usage

Description

Example

Addition (+)

a+b

Add values on either side of operator

10+6=16

Subtraction (-)

a-b

Subtracts the right operand from left operand

11-3=8

Multiplication (*)

a*b

Multiply values on either side of operator

5*2=10

Division ( / )

a/b

Divides the left operand by the right operand (returns a float value)

5/2= 2.5 &

11/3 = 3.666666

Modulus ( % )

a%b

Returns a remainder of the division of the left operand by the right operand

5%2 = 1 &

11%3 = 2

Exponentiation ( ** )

a**b

Returns exponent – left operand raised to the right operand

3**2 = 9

5**3 = 125

Floor Division ( // )

a//b

Divides the left operand by the right operand (returns quotient without float value)

5//2 = 2

11/3 =2

The following video deals with best examples for Arithmetic Operators in Python.

"Python - Arithmetic Operators (Lesson 3)" by Andy Dolinski is licensed under CC BY-SA 3.0

3. Assignment Operators

The assignment operator is used to assign a specific value to a variable or an operand.

The equal to (=) operator is used to assign a value to an operand directly, if we use any arithmetic operator along with the equal to operator then it will perform the arithmetic operation on the given variable and then assign the resulting value to that variable itself.

Example: x=2
                  print(x)   Output: 2
                 x+=2
                 print(x)    Output: 4

Operator

Description

Example(s)

=

Assigns the right operand to the left operand

X=12

+=

Adds the right operand to the left operand and then assigns to the left operand

X=3

X+=2   (X=X+2)

-=

Subtracts the right operand from left operand and then the result is assigned to the left operand

X=3

X-=2      (X=X-2)

*=

Multiplies the right operand with left operand and then the result is assigned to the left operand

X=3

X*=4     (X=X*4)

/=

Divides the left operand with right operand and then the result (with decimals) is assigned to the left operand

X=3

X/=4     (X=X/4)

%=

Divides the left operand with right operand and then the remainder is assigned to the left operand

X=3

X%=4     (X=X%4)

//=

Divides the left operand with right operand and then the result (without decimals) is assigned to the left operand

X=3

X//=4     (X=X//4)

**=

Exponent – The left operand raised to the right operand will be calculated and result is assigned to left operand

X=3

X**=4     (X=X**4)


The following video deals with the examples of Assignment operators in Python.

"Assignment Operators in Python" by Chakradhar Dwivedi, prototype is licensed under CC BY-SA 3.0

4. Comparison Operators

  • These operators are used to compare the values of operands on either side of this type of operators. These operators return true or false Boolean values. They return true if the condition is satisfied otherwise the return false.

Operator

Usage

Description

Example

==

a==b

Returns True if either side of operands of the operator are equals to each other otherwise returns False

a=3, b=3 then a==b (returns True)

!=

a!=b

Returns True if either side of operands of the operator are equals to each other otherwise returns False

a=3, b=3 then a!=b (returns False)

a>b

Returns True if the left operand is greater than the right operand otherwise returns False

a=8, b=3 then a>b (returns True)

a<b

Returns True if the left operand is lesser than the right operand otherwise returns False

a=8, b=3 then a<b (returns True)

>=

a>=b

Returns True if the left operand is greater than or equals the right operand otherwise returns False

a=8, b=3 then a>=b (returns True)

<=

a<=b

Returns True if the left operand is lesser than the right operand otherwise returns False

a=8, b=3 then a<=b (returns False)


The following video resource provides you many examples for comparison operators in Python.

"# 22 Comparison Operators in Python" by , iTech e-Learn is licensed under CC BY-SA 3.0

5. Logical Operators

  • The logical operators are used to combine or manipulate  Boolean values to provide a specific result to be either true or false. Python supports three logical operators namely and, or, not.  These operators are used to make decisions based on multiple conditions or to negate a condition.

Operator

Usage

Description

Example

and

a and b

‘and’ operator returns True if only when both the operands are true. Otherwise returns False

If a=10, b=15

(a>5 and b<10) – True

(a>10 and b<10) – False

or

a or b

‘or’ operator returns True if at least one operand is true. If both operators are false, then it returns False.

If a=10, b=15

(a>12 or b<10) – True

(a>10 or b<10) – False

not

a not b

‘not’ operand negates the value of it’s operand. It returns True if the operand is False and it returns False if the operand is True.

If a=10, b=15

not a>b – True

not a<b - False


The following video resource provides you the information about logical operators and examples which will help you for better understanding of Logical operators in Python.

"2.7 - Logical Operators" by , CodeGeek Learning is licensed under CC BY-SA 3.0

6. Identity Operators

  • Identity operators are used to compare memory locations two objects to determine if they refers to the same object in memory. There are two identity operators in python. They are is and is not.

Operator

Description

Example

is

Returns True if the either side operands of the operator are referring to the same objects in memory. Otherwise, it returns False.

X=[1,2,3]

Y=X

X is Y - True

is not

Returns True if the operands on both sides of the operator are referring to the different objects in memory. Otherwise, it returns False.

X=[1,2,3]

Y=X

X is not Y - False

7. Membership Operators

  • Membership operators are used to test whether specific value or item is present is a sequence or a collection of items. There are two membership operators in python. They are 'in' , 'not in'. These operators are generally used with Strings, Lists, Tuples, Sets and Dictionaries to check for the existence of values.

Operator

Description

Example

     in

Returns True if specific value is present in a sequence or a collection. Otherwise, it returns False.

X=[1,2,3]

2 in X - True

10 in X  - False 

    not in

Returns True if specific value is not present in a sequence or a collection. Otherwise, it returns False.

Y=(1,2,3)

8 not in Y - True

2 not in Y - False





8. Bitwise Operators

  • Bitwise operators are used to manage individual bits with integers. These operators works on binary representation of integers and performs bit-level operations. Python supports several bitwise operators : AND, OR, XOR, NOT, left shift and right shift.
  • Bitwise operators work on bits and perform bit by bit operation. In binary operations, numbers are treated as string of bits. For example, 0 is represented by 0, 1 is represented by 1, and 2 is represented by 10 in binary.

Sl No.

Operators

Functions

1

x << y

Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y

2

x >> y

Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y

3

x & y

Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0

4

y

Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1

5

~ x

Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1

6

x ^ y

Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1


"3.3.3. Bitwise Operators" by wikibooks is licensed under CC BY-SA 4.0