Operators in Python

Operators in Python

Python Operators 

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus


Operators are special symbols in python that carries the arithmetic or logical operation. Operators are basically constructs which can manipulate the value of operands.
Lets consider the expression 6 + 2 = 8. Here, + is the operator that performs addition.

Types of Operator

In Python language there are operators listed below :

  • 1. Arithmetic Operators
  • 2. Comparison (Relational) Operators
  • 3. Logical Operators
  • 4. Assignment Operators 
  • 5. Bitwise Operators
  • 6. Membership Operators
  • 7. Identity Operators
Lets start with one by one with examples 

1. Arithmetic Operators

Arithmetic Operator performs the mathematical operations like addition, subtraction, division and multiplication.

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus


Lets see an example

# Examples of Arithmetic Operator  

a = 4

b = 3

# Addition of numbers  

add = a + b    

# Subtraction of numbers  

sub = a - b  

# Multiplication of number  

mul = a * b   

# Division(float) of number  

div = a / b   

# Modulo of both number  

mod = a % b  

# Power

exp = a **b  

# Division(floor) of number  

floor = a // b 

  
print(add)  

print(sub)  

print(mul)  

print(div)  

print(mod)  

print(exp) 

print(floor) 


    Output:

    7
    1
    12
    1.3333333333333333
    1
    64
    1

2. Comparison Operators

The Comparison operators actually compare the values on either sides of them and decide the relation among them.It either returns True or False according to the condition.

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus

Note : 
Even though == and is  might look same but they aren't , which is a big mistake often done by beginners. 
a is b checks whether the A and B point to the same object in memory or not whereas a==b checks for if the value stored in a and b are same.

# Examples of Relational Operators

a = 15

b = 20

 

# a == b is False

print(a == b)

# a > b is False

print(a > b)

# a < b is True

print(a < b)

# a != b is True

print(a != b)

# a >= b is False

print(a >= b)

# a <= b is True

print(a <= b)


    Output:

    False
    False
    True
    True
    False
    True

3. Logical Operators

Logical operators perform Logical ANDLogical OR and Logical NOT operations.
There are following logical operators supported by Python language.

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus


#Examples of Logical Operator

a = True

b = False

#Print a and b is False

print(a and b)

#Print a or b is True

print(a or b)

#Print not a is False

print(not a)


    Output:

    False
    True
    False

4. Assignment Operators

Assignment operators are used to assign values to the variables.

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus



5. Bitwise Operators

Bitwise operators acts on bits and performs bit by bit operation.

operators in python,python not equal,python ternary operator,python xor,python power,python and operator,python division,python logical operators,python modulus
Note :
In the last two rows i.e bitwise operations syntax, we will use a number on the right side of the operator which is a real
number (integer).


Lets see some examples

#Example of bitwise operator
a = 12
b = 6
# Print bitwise AND operation  
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation 
print(~a)
# print bitwise XOR operation 
print(a ^ b)
# print bitwise right shift operation 

print(a >>2)

# print bitwise left shift operation 

print(a << 2)

    Output:

    4
    14
    -13
    10
    3
    48
    


6. Membership Operators

Python’s membership operators test the membership in a sequence or not, such as strings, lists, or tuples. There are two membership operators as explained below −

in and not in are the membership operators; used to test whether a value or variable is in a sequence.

in            True if value is found in the sequence
not in        True if value is not found in the sequence

# Examples of Membership operator

x = 'Code Dudle'

y ={3:'a',4:'b'}

print('C' in x)

print('code' not in x)

print('Dudle' not in x)

print(3 in y)

print('b' in y)


  1. Output:

    True
    True
    False
    True
    False

7. Identity Operators

is and is not are the identity operators both are used to check if two values are located on the same part of the memory. 
There are two Identity operators explained below −

is          True if the operands are identical 
is not      True if the operands are not identical 

# Examples of Identity operators 

a1 = 3

b1 = 3

a2 = 'CodeDudle'

b2 = 'CodeDudle'

a3 = [1,2,3] 

b3 = [1,2,3] 

print(a1 is not b1) 

print(a2 is b2) 

# Output is False, since lists are mutable

print(a3 is b3) 


Output:

False
True
False

in this article i have explained brief details about Operators available in python

Recommended Post : 










#operators in python
#python not equal
#python ternary operator
#python xor
#python power
#python and operator
#python division
#python logical operators
#python modulus


























 

Post a Comment

0 Comments