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 |
2 | – | Subtract 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 arithmetic.py
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.
1 | < | Less than |
2 | <= | Less than or equal |
3 | > | Greater than |
4 | >= | Greater than or equal |
5 | != | Not equal |
6 | == | equality |
Example save the file as relational_operators.py
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
1 | and | Return true if all conditions are true otherwise false. |
2 | or | Return true if one of the conditions is true otherwise false. |
3 | not | Return invert of the result of a given condition. |
Example: Save the file as logical_operators.py
Example 1 (and operator)
email_validate=False
mobile_validate=True
if (email_validate==True and mobile_validate==True):
print("You can buy now.")
else:
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)
driving_licence=True
passport=False
if (driving_licence==True or passport==True):
print("You can open your bank account")
else:
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")
else:
print("Your name is ",name)
Output:
What is your name?
Please fill up your name
What is your name? John
Your name is John