Operators In Python - Python Tutorials For Beginners #5


What Are Operators In Python?

Operators are a symbol or small word which is useful to perform operations in Python.


Type Of Operators

(1) Conditional Operators

(2) Logical Operators

(3) Identity Operators

(4) Arithmetic Operators

(5) Membership Operators

(6) Assignment Operators

(7) Bitwise Operator


Conditional / Comparison Operators In Python

1Conditional / Comparison Operators In Python
2
3<   Greater Than symbol            
4>   Smaller Than symbol            
5<=  Equal To And Greater Than      
6>=  Equal To And Smaller Than      
7==  Equal To                       
8!=  Not Equal To                   
1# Examples
2
31<2       # <   Greater Than symbol   
42>1       # >   Smaller Than symbol
5i<=10     # <=  Equal To And Greater Than
6i>=10     # >=  Equal To And Smaller Than 
7x == 10   # ==  Equal To   
8x != 10   # !=  Not Equal To

Conditional operators are symbols used for writing conditions in Python. Conditional operators are also known as Comparison Operators. Conditional Operators are one of the most essential operator types in Python.


Logical Operators

1Python Logical Operators
2
3and      
4or       
5not      
1# Example
2
3i<10 and i2<20  # and
4i<10 or i2<20   # or
5i is not 3      # not

We use logical operators for giving more than one condition in Python. Logical operators are used to assign multiple conditions while coding. They are used to writing logic in Python. That's why these operators are known as logical operators in Python.


Identity Operators

1Python Identity Operators
2
3is         
4is not    
1# Examples
2
3x is y (x=y=5) # is
4x is not y     # is not

Identity operators are used to compare the variables in Python. Generally, We compare the value of variables with the help of an identity operator. Identity operators contain is and is not as shown.


Arithmetic Operators

1Python Arithmetic Operators
2
3+   Addition  
4-   Subtraction
5*   Multiplication
6/   Division
7%   Modulus          
8**  Exponentiation   
9//  Floor division   
1# Examples
2
39+3   # Output: 12
49-3   # Output: 6
59*3   # Output: 27 
69/3   # Output: 3
79%3   # Output: 0
89**3  # Output: 729
99//2  # Output: 4

Arithmetic operators are also known as Mathematical Operators in Python. We use them to perform mathematical operations.


Membership Operators

1Python Membership Operators
2
3in       
4not in      
1# Examples
2
3x in y      
4x not in y  

Membership operators contain is and is not. We use it to show whether both variables same or not.


Assignment operators in Python

1assignment operators in Python 
2
3=   Simple assignment operator 
4+=  Add and equal operator
5-=  Subtract and equal operator 
6*=  Asterisk and equal operator
7/=  Divide and equal operator 
8%=  Modulus and equal operator 
9//= Double divide and equal operator 

We use assignment operators while assigning values to variables.


Example Code For Conditional Operators

 1x = 13
 2if (x <= 13):
 3    print("X is smaller or equal to 13.")
 4
 5elif (x >= 13):
 6    print("X is greater or equal to 13.")
 7
 8elif (x == 13):
 9    print("X is equal to 13.")
10
11elif (x != 1):
12    print("X is not equal to 13.")
13
14else:
15    print("Other Value")

Example Code For Arithmetic Operators

1print(" 1 + 2")
2print(" 3 - 2")
3print(" 1 * 2")
4print(" 2 / 2")
5print(" 3 % 2")
6print(" 4**2")
7print(" 5//2")

Watch The Video Tutorial On Youtube:-