Python Operators: Types, Examples & Usage

Introduction

In Python, operators are special symbols or keywords that perform operations on variables and values. For example, the plus sign (+) adds two numbers, while the equal sign (=) assigns a value to a variable.

In this tutorial, you will learn what Python operators are, their syntax, different types, examples, real-life applications, common mistakes, interview questions, and best practices.

What are Python Operators?

Python operators are symbols or keywords used to perform operations on values and variables.

Example:


x = 10
y = 5
print(x + y)

Output:

15

In this example:

  • + is an operator.
  • x and y are operands.
  • The operator performs addition.

Syntax:

The general syntax of an operator is:


operand1 operator operand2

Example:


10 + 5

Here:

  • 10 is the first operand.
  • + is the operator.
  • 5 is the second operand.

Types of Python Operators

Python provides the following categories of operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

Let’s explore each type in detail.

1. Arithmetic Operators

Arithmetic operators perform mathematical calculations.

Operator Description Example
+ Addition x + y
– Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor Division x // y

Addition Operator (+)


x = 10
y = 5
print(x + y)

Output:

15

Subtraction Operator (-)


print(10 - 5)

Output:

5

Multiplication Operator (*)


print(10 * 5)

Output:

50

Division Operator (/)


print(10 / 5)

Output:

2.0

Division always returns a float value.

Modulus Operator (%)

Returns the remainder.


print(10 % 3)

Output:

1

Exponentiation Operator (**)

Calculates powers.


print(2 ** 3)

Output:

8

Floor Division Operator (//)

Returns the integer part of division.


print(10 // 3)

Output:

3

2. Assignment Operators

Assignment operators assign values to variables.

Operator Example
= x = 5
+= x += 5
-= x -= 5
*= x *= 5
/= x /= 5
%= x %= 5
**= x **= 5
//= x //= 5

Simple Assignment (=)


x = 10

Addition Assignment (+=)


x = 10
x += 5
print(x)

Output:

15

Subtraction Assignment (-=)


x = 10
x -= 3
print(x)

Output:

7

3. Comparison Operators

Comparison operators compare two values and return True or False.

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Equal To (==)


print(10 == 10)

Output:

True

Not Equal To (!=)


print(10 != 5)

Output:

True

Greater Than (>)


print(20 > 10)

Output:

True

Less Than (<)


print(20 < 10)
False

4. Logical Operators

Logical operators combine multiple conditions.

Operator Description
and Returns True if both conditions are True
or Returns True if at least one condition is True
not Reverses the result

AND Operator


print(20 < 10)

Output:

True

OR Operator


print(10 > 5 or 5 > 10)

Output:

True

NOT Operator


print(not True)

Output:

False

5. Identity Operators

Identity operators check whether two variables refer to the same object.

Operator Description
is Returns True if objects are identical
is not Returns True if objects are not identical

is Operator


x = [1, 2]
y = x
print(x is y)

Output:

True

is not Operator


x = [1, 2]
y = [1, 2]
print(x is not y)

Output:

True

6. Membership Operators

Membership operators check whether a value exists in a sequence.

Operator Description
in Value exists
not in Value does not exist

in Operator


text = "Python"
print("P" in text)

Output:

True

not in Operator


text = "Python"
print("J" not in text)

Output:

True

7. Bitwise Operators

Bitwise operators work on binary values.

Operator Description
& AND
| OR
^ XOR
~ NOT
<< Left Shift
>> Right Shift

Bitwise AND (&)


print(5 & 3)

Output:

1

Bitwise OR (|)


print(5 | 3)

Output:

7

Left Shift (<<)


print(5 << 1)

Output:

10

Right Shift (>>)


print(5 >> 1)

Output:

2

Operator Precedence

Python follows precedence rules when evaluating expressions.

Example:


print(10 + 5 * 2)

Output:

20

Multiplication is performed before addition.

Using Parentheses


print((10 + 5) * 2)

Output:

30

Parentheses have the highest priority.

Real-life Examples:

1. Calculate the Total Price

Suppose an online shopping website calculates the total price.


price = 500
quantity = 3
total = price * quantity
print(total)

Output:

1500

Here, the multiplication operator calculates the total amount.

2. Login Validation


username = "admin"
password = "1234"
is_valid = username == "admin" and password == "1234"
print(is_valid)

Output:

True

This example uses comparison and logical operators.

3. Voting Eligibility


age = 20
can_vote = age >= 18
print(can_vote)

Output:

True

Comparison operators are commonly used in such applications.

Common Mistakes

1. Using = Instead of ==

Incorrect:


if age = 18:
    print("Adult")

Output:

SyntaxError

Correct:


if age == 18:
    print("Adult")

2. Dividing by Zero

Incorrect:


print(10 / 0)

Output:

ZeroDivisionError

3. Confusing is and ==

Incorrect:


x = [1, 2]
y = [1, 2]
print(x is y)

Output:

False

Use == to compare values.


print(x == y)

Output:

True

4. Forgetting Operator Precedence


print(10 + 5 * 2)

Output:

20

Some beginners incorrectly expect 30.

5. Misusing Logical Operators

Incorrect:


age = 20
print(age > 18 and age > 30)

Output:

False

Both conditions must be True when using and.

Conclusion

Python operators are essential tools that allow programmers to perform calculations, assign values, compare data, evaluate conditions, and manipulate information efficiently. Python provides a wide range of operators, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators, each designed for specific tasks.

Python Operators – Interview Questions

Q 1: What are operators in Python?
Ans: Operators are symbols used to perform operations on variables and values.
Q 2: Name some types of Python operators.
Ans: Arithmetic, comparison, logical, assignment, and membership operators.
Q 3: Which operator is used for exponentiation?
Ans: The ** operator is used for exponentiation.
Q 4: What does the == operator do?
Ans: It checks whether two values are equal.
Q 5: What is the use of logical operators?
Ans: Logical operators are used to combine conditional statements.

Python Operators – Objective Questions (MCQs)

Q1. Which of the following is the assignment operator in Python?






Q2. What will be the output of the following code?

x = 10
y = 3
print(x // y)






Q3. Which operator is used to compare two values in Python?






Q4. What will be the output of the following code?

x = 5
x += 3
print(x)






Q5. Which of the following is a logical operator in Python?






Related Python Tutorials