Python Operators

Operators are used to perform operations on variables and values. Python provides different types of operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Arithmetic Operators Arithmetic operators are used to perform mathematical calculations. Operator Name Example + Addition x + y - Subtraction x - y * Multiplication x * y […]

2 mins read Lesson 28 of 43 65% through track

Operators are used to perform operations on variables and values.

Python provides different types of operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor Divisionx // y

Example:

x = 10
y = 3

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)

Output:

13
7
30
3.3333333333333335
1
1000
3

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExample
=x = 5
+=x += 3
-=x -= 3
*=x *= 3
/=x /= 3
%=x %= 3

Example:

x = 5

x += 3

print(x)

Output:

8

Comparison Operators

Comparison operators compare two values and return True or False.

OperatorName
==Equal
!=Not Equal
>Greater Than
<Less Than
>=Greater Than or Equal
<=Less Than or Equal

Example:

x = 10
y = 5

print(x == y)
print(x > y)
print(x < y)

Output:

False
True
False

Logical Operators

Logical operators combine conditional statements.

OperatorDescription
andReturns True if both statements are true
orReturns True if one statement is true
notReverses the result

Example:

x = 10

print(x > 5 and x < 20)
print(x > 20 or x < 20)
print(not(x > 20))

Output:

True
True
True

Identity Operators

Identity operators compare the memory location of two objects.

OperatorDescription
isReturns True if both variables are the same object
is notReturns True if both variables are not the same object

Example:

x = ["apple", "banana"]
y = x

print(x is y)

Output:

True

Membership Operators

Membership operators check whether a value exists in a sequence.

OperatorDescription
inReturns True if value exists
not inReturns True if value does not exist

Example:

fruits = ["apple", "banana", "mango"]

print("apple" in fruits)
print("orange" not in fruits)

Output:

True
True

Bitwise Operators

Bitwise operators work on binary numbers.

OperatorName
&AND
``
^XOR
~NOT
<<Left Shift
>>Right Shift

Example:

x = 5
y = 3

print(x & y)

Output:

1

Operator Precedence

Python follows a specific order when evaluating expressions.

Example:

print(5 + 2 * 3)

Output:

11

Multiplication is performed before addition.

Use parentheses to change the order:

print((5 + 2) * 3)

Output:

21

Summary

  • Operators are used to perform operations on values and variables.
  • Arithmetic operators perform mathematical calculations.
  • Assignment operators assign values to variables.
  • Comparison operators return boolean values.
  • Logical operators combine conditions.
  • Identity operators compare object identities.
  • Membership operators check for values in collections.
  • Bitwise operators work with binary numbers.
  • Operator precedence determines the order of execution.