The Basic Operators In Python

Introduction

 
In this article, I will explain the basic operators in Python.
 

Definition

 
An operator is a symbol which represents some operations that can be performed on data. There are different types of operators in Python. There are:
  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Assignment operators

Arithmetic operators

 
Arithmetic operators are used to perform arithmetic operations.
 
Syntax
  1. Variable 1 arithmetic operator variable 2    
The arithmetic operators and their meaning are given in the below table:
 
operators
operations
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo division
 
Example
  1. x = 10    
  2. y = 2    
  3.     
  4. print(x + y)#Addition    
  5. print(x - y)#Subtraction    
  6. print(x * y)#Multiplication    
  7. print(x / y)#Division    
  8. print(x % y)#Modulo division   
Output
 
The Basic Operators In Python
 

Relational operators

 
Relational operators are used to compare two or more operans. It evaluates whether the given expression is true or false.
 
operators
operations
<
is less than
<=
is less than or equal
>
is greater t
>=
is greater than or equal
==
is equal to
!=
is not equal to
 
Syntax
  1. Variable 1 relational operator variable 2    
Example
  1. x = 20    
  2. y = 10    
  3.     
  4. print(x == y)# returns False because 20 is not equal to 10    
  5. print(x != y)#returns True because 20 is not equal to 10    
  6. print(x > y)#returns True because 20 is greater than 10    
  7. print(x < y)#returns False because 20 is not less than 10    
  8. print(x >= y)#returns True because 20 is greater, or equal, to 10    
  9. print(x <= y)#returns False because 20 is neither less than or equal to 10   
Output
 
The Basic Operators In Python
 

Logical operators

 
They are used to combine the result of two or more conditions.
 
operators
operations
&&
And
||
Or
!
Not
 
Syntax
  1. Variable 1 logical operator variable 2    
Example
  1. x = 8      
  2.       
  3. print(x > 3 and x < 10)# returns True because 8 is greater than 3 AND 8 is less than 10      
  4. print(x > 3 or x < 1)# returns True because one of the conditions are true      
  5. print(not(x > 3 and x < 11))#returns False because not is used to reverse the result       
Output
 
The Basic Operators In Python
 

Assignment operators

 
Assignment operators have to assign a value or an expression or a value of a variable to another variable. Assignment operators use arithmetic operators (+, -, *, /) along with assignment operator (=).
 
operators
Example
Meaning
+=
X+=Y
X=X+Y
-=
X-=Y
X=X-Y
*=
X*=Y
X=X*Y
/=
X/=Y
X=X/Y
%=
X%=Y
X=X%Y
 
Syntax
  1. Variable 1 assignment operator variable 2   
Example
  1. a = 10    
  2. b = 10    
  3. c = 10    
  4. d = 10    
  5. e = 10    
  6.     
  7. a += 20#Addition    
  8. b -= 20#Subtraction    
  9. c *= 20#Multiplication    
  10. d /= 20#Division    
  11. e %= 20#Modulo division    
  12.     
  13. print(a)    
  14. print(b)    
  15. print(c)    
  16. print(d)    
  17. print(e)    
Output
 
The Basic Operators In Python
 

Conclusion

 
In this article, we have seen basic operators in Python. I hope this article was useful to you. Thanks for reading!


Similar Articles