DEV Community

yugalakshmi
yugalakshmi

Posted on

Python Operators

Introduction

Operators are special symbols used to perform different operations on values and variables in Python.

Python has six main types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Special Operators

Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, and division.

no1 = 10
no2 = 2

print(no1 + no2)
print(no1 - no2)
print(no1 * no2)
print(no1 / no2)
print(no1 // no2)
print(no1 % no2)
print(no1 ** no2)

OUTPUT
12
8
20
5.0
5
0
100

Example 2: Difference Between /, // and %

no1 = 10
no2 = 3

print(no1 / no2)
print(no1 // no2)
print(no1 % no2)

Output:

3.3333333333333335
3
1

  • returns the division result as a floating-point number.
  • returns the floor value. % returns the remainder.

Exponent Operator (**)

The exponent operator is used to calculate the power of a number.

print(10 ** 2)

Output:

100

Explanation: 10 × 10 = 100.
We can also use the multiplication operator with strings.

name = "prem "
print(name * 3)

Output:

prem prem prem

ZeroDivisionError

We cannot divide a number by zero in Python.

no1 = 10
no2 = 0

print(no1 / no2)

Output:

ZeroDivisionError

Python raises a ZeroDivisionError when we try to divide a number by zero.

Relational Operators

Relational operators are also called comparison operators.

They are used to compare two values. The result will be either True or False.

Example

no1, no2 = 10, 20

print(no1 > no2)
print(no1 < no2)
print(no1 >= no2)
print(no1 <= no2)

Output:

False
True
False
True
String Comparison

Python compares strings using Lexicographical Order.

It compares characters one by one based on their Unicode values.

name1 = 'ajith'
name2 = 'ajithkumar'

print(name1 > name2)
print(name1 < name2)

Output:

False
True

True and False Comparison

In Python:

True is equal to 1.

False is equal to 0.

print(True > True)
print(False > True)
print(False < True)

Output:

False
False
True

Operator Chaining

Python allows us to use multiple comparison operators in a single expression. This is called Operator Chaining.

no1, no2, no3 = 100, 200, 300

print(no3 > no2 < no1)
print(no3 > no2 > no1)

Output:

False
True

Explanation:

no3 > no2 < no1

It checks:

300 > 200 → True

200 < 100 → False

Both conditions must be True. So the final result is False.

Equality Operator

The equality operator (==) checks whether two values are equal.

The not equal operator (!=) checks whether two values are different.

name1 = 'sanjay'
name2 = 'sanjay'

print(name1 == name2)
print(name1 != name2)

Output:

True
False

** Logical Operators**

Logical operators are used to combine multiple conditions.

Python has three logical operators:

AND

OR

NOT

AND Operator

The AND operator returns True only when both conditions are True.
print(True and True)
print(True and False)
print(False and True)

Output:

True
False
False

OR Operator

The OR operator returns True if at least one condition is True.
print(True or False)
print(False or True)
print(False or False)

Output:

True
True
False

NOT Operator

The NOT operator reverses the result.

True becomes False.

False becomes True.

print(not True)
print(not False)

Output:

False
True
Logical Operators with Values

Python logical operators can return values instead of only True or False.

no1, no2 = 10, 20

print(no1 and no2)
print(10 and 20)

Output:

20
20

Explanation:

Both 10 and 20 are non-zero values, so both are considered True.

The AND operator returns the second value when the first value is True.

NOT with Strings

print(not 'Vishnu')
print(not '')

Output:

False
True

  • A non-empty string is considered True.
  • An empty string is considered False.
  • The NOT operator reverses the result.

Top comments (0)