Exploring New Techs!

Python - Operators


Hello Technotizers, this article is dedicated to Python Operators where various operators and their usage are discussed. Operators are used to perform certain operations on variables and get the desired result.

  • ·   Python Arithmetic Operators:

Arithmetic operators are used with numeric values to perform mathematical operations.

Operator

Name

Purpose

Example

+

Addition

Perform addition operation

x + y

-

Subtraction

Perform subtraction operation

x - y

*

Multiplication

Perform multiplication operation

x * y

/

Division

Perform division operation

x / y

%

Modulus

Perform modulus operation (returns remainder of division)

x % y

**

Exponentiation

Perform exponentiation operation (returns a certain power of a number)

x ** y

//

Floor division

Perform floor division operation (returns the integer part from the quotient)

x // y

 

Let’s see an example:

x = 5                        
y = 3                        
print("Addition:",x+y)       
print("Division:",x/y)       
print("Modulus:",x%y)        
print("Exponential:",x**y)   
print("Floor division:",x//y)

The output is obtained as follows:

Addition: 8                 
Division: 1.6666666666666667
Modulus: 2                  
Exponential: 125            
Floor division: 1           

·         

  •     Python Assignment Operators:

Assignment operators are used to assign values to a variable.

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x - 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

//=

x //= 3

x = x // 3

**=

x **= 3

x = x ** 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3

In assignment operators, the values are stored in the first variable itself.

Let’s see an example:

x = 5                                
x+=3                                 
print("Addition assignment:",x)      
x**= 3                               
print("Exponentiation assignment:",x)
       

The output is obtained as follows:

Addition assignment: 8        
Exponentiation assignment: 512


  • ·   Python Comparison Operators:

Comparison operators are used to compare two values.

Operator

Name

Example

==

Equal

x == y

!=

Not equal

x != y

> 

Greater than

x > y

< 

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y

Comparison operators are very useful when it comes to conditional programming. These operators return True or False based on the truthfulness of the given comparison.

Let’s see an example:

x = 5                             
y = 3                             
print("Equality comparison:",x==y)
print("Equality comparison:",x>=y)

The output is obtained as follows:

Equality comparison: False
Equality comparison: True 


  • ·  Python Logical Operators:

Logical operators are used to combine conditional or comparison statement to give combined truth value.

Operator

Purpose

Example

and 

Returns True if both statements are true

x < 5 and  x < 9

or

Returns True if one of the statements is true

x < 5 or x < 3

not

Reverse the result, i.e. returns False if the result is true

not(x < 5 and x < 3)

Let’s see an example:

x = 5                                                                      
print("AND operator:",x<10 and x>3)                                        
#returns True as 5 is less than 10 and greater than 3                      
print("OR operator:",x<10 or x<3)                                          
#returns True as one condition is True, 5 is less than 10 but not greater   than 3                                                                     
print("NOT operator:",not(x<10 and x>3))                                   
#returns False as the result is reversed                                   

The output is obtained as follows:

AND operator: True 
OR operator: True  
NOT operator: False


  • Python Identity Operators:

Identity operators are used to compare objects or variables based on their similarity and memory location.

Operator

Purpose

Example

is 

Returns True if both variables are the same object

x is y

is not

Returns True if both variables are not the same object

x is not y

Let’s see an example:

x = 5                                                                 
y = 3                                                                 
print("is operator:",x is y)                                          
#returns False because memory locations are different due to different variables even if values are same                                     
z = x                                                                 
print("is operator:",z is x)                                          
#returns True because memory locations and values both are same       
print("is not operator:",x is not y)                                  
#returns True because memory locations and values both are different  

The output is obtained as follows:

is operator: False    
is operator: True     
is not operator: True 


  • ·  Python Membership  Operators:

Membership operator checks whether a sequence is present in an object or not.

Operator

Purpose

Example

in 

Returns True if a sequence is present in the object

x in y

not in

Returns True if a sequence is not present in the object

x not in y

Let’s see an example:

x = [2,4,5,7,9]                    
y = 5                              
print("Is y present in x:",y in x) 
z = 10                             
print("Is z present in x:",z in x) 

The output is obtained as follows:

Is y present in x: True 
Is z present in x: False


  • ·  Python Bitwise  Operators:

Bitwise operators are used to compare and operate on binary numbers.

Operator

Name

Purpose

AND

Sets each bit to 1 if both bits are 1

|

OR

Sets each bit to 1 if one of two bits is 1

 ^

XOR

Sets each bit to 1 if only one of two bits is 1

NOT

Inverts all the bits

<< 

Zero fill left shift

Shift left by pushing zeros in from the right and let the leftmost bits fall off

>> 

Signed right shift

Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off


With this we come to an end of this article. Hope it was helpful. Do provide your feedback and ideas through comments, it would be highly appreciated. See you soon!
Keep coding and exploring new techs!!

0 comments:

Post a Comment