Getting started with PYTHON!!! #2.3 - Operators

Getting started with PYTHON!!! #2.3 - Operators

Hello People, In this blog we'll talk about :

  • What are operators in python and how many types of operators are present in python ?
  • Usage of operators in Python!

So, everyone has used some kind of operator in Mathematics. Let, us take the addition operator(+). What does addition operator do? It helps us in additing two or more numbers. In other words, it helps us in performing some kind of function(logical or arithmetic) over the numerics. Operators help us in solving certain statements.

OPERATORS : Operators are special symbols in Python that carry out arithmetic or logical computation.

Types of Operators :

  • Arithmetic Operator - These operators are used to carry out arithmetic calculations such as addition, subtraction, multiplication, division , ec.

    a=5  #equal to is assignment operator
    b=6 
    c=a+b   #addition operator 
    print(c)
    c=a-b   #subtraction operator
    print(c)
    c=a*b   #multiplication operator
    print(c)
    c=a/b   #division operator
    print(c)
    c=a%b   #modulo operator
    print(c)
    c=a**b  #exponential operator
    print(c)
    c= a//b #floor division operator
    
  • Assignment Operator - These operators are used to assign data to identifiers.

    a=5  #equal to assignment operator
    a+=5  #same as a=a+5
    print(c)
    a-=5  #same as a=a-5
    print(c)
    a*=5  #same as a=a*5
    print(c)
    a/=5  #same as a=a/5
    print(c)
    a%=5  #same as a=a%5
    print(c)
    a// =5  #same as a=a//5
    
  • Comparison Operator - These operators are used to compare two or more identifiers.

    a=5
    b=7
    print(a==b)  #returns True if a is equal to b or not
    print()  #returns True if a is not equal to b or not
    print(a!=b)   #returns True if a is greater than b or not
    print(a<b)   #returns True if a is lesser than b or not
    print(a>=b)   #returns True if a is greater than or equal to b or not
    print(a<=b)   #returns True if a is lesser than or equal to b or not
    
  • Logical Operator - These operators are used for logical operations such as AND,OR,NOT,etc.

a=5
b=6
# AND Operator - It is a binary operator.
print(a==5 && b<7 )   #returns True if both the statements are True. If the first statement is False then always returns False without even checking the second statement.
#OR Operator - It is a binary operator.
print(a==5 | b<7 )   #returns True if one of the statement is True.If the first statement is False then always returns False without even checking the second statement.
#NOT Operator - It is a unary operator.
print(!(a==5 && b<7)) # Reverses the result , i.e. , if the result is true then returns False and vice versa .

Identity Operator

x=4
y=7
print(x is y)    #Returns True if both variables are the same object
print(x is not y)   #Returns True if both variables are not the same object

Membership Operator - These operators are used to check if a object is present insider another object or not.

x = [1, 2,3,4,5]

print(3 in x)  #Returns True because the value 3 is present in the list.
print(6 not in x) #Returns True because the value 6 is not present in the list.

Bitwise Operator : These operators are used to perform some bit by bit operations.

  • & - 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