Python operators

Python operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators

Arithmetic Operators

Arithmetic operators perform all mathematical operations like addition, subtraction, multiplication, and division.

1+Add two operand
2Subtract one operand from another
3*Perform multiplication of two operand
4/Perform division and calculate the floating quotient
5//Perform division and calculate the integer quotient
6%Perform division and calculate the remainder
7**Calculate the power of a given number

Example:  save the file as

print(2+3)  # 5
print(3-2)   # 1
print(3*2) # 6
print(3/2) # 1.5
print(3//2) # 1
print(3%2) # 1
print(3**2) # 9

Relational operators

Relational operators check the relation between two operands and return boolean value depends upon conditions.

1Less than
2<=Less than or equal
3Greater than
4>=Greater than or equal
5!=Not equal

Example save the file as

print(3<2) # False
print(3<=2) # False
print(3>2) # True
print(3>=2) # True
print(3!=2) # True
print(3==2) # False

Logical operators

1andReturn true if all conditions are true otherwise false.
2orReturn true if one of the conditions is true otherwise false.
3notReturn invert of the result of a given condition.

Example: Save the file as

Example 1 (and operator)

if (email_validate==True and mobile_validate==True):
           print("You can buy now.")
           print("Sorry, you can't buy, please verify the email or mobile number.")

Output: Sorry, you can’t buy, please verify the email or mobile number.

Example 2 (or operator)

if (driving_licence==True or passport==True):
           print("You can open your bank account")
           print("Sorry, you can't open your bank account")

Output: You can open your bank account. 

Example 3 (not operator)

name=input("What is your name?")
if(not name):
 print("Please fill up name")
 print("Your name is ",name)

What is your name?
Please fill up your name

What is your name? John
Your name is John

Leave a Reply

Your email address will not be published. Required fields are marked *