Breaking

Monday 14 May 2018

Python: Operator

#Python04
Python:Operator


As other programming language has operators to operate or data.Python also has the same operators.Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. 

so In this article, you'll learn everything about different types of operators in Python, their syntax and how to use them with examples.

Types of Operator:

  1. Arithmetic operators
  2. Comparison (Relational) Operators
  3. Logical (Boolean) Operators 
  4. Bitwise Operators 
  5. Assignment Operators 
  6. Special Operators
1.Arithmetic operator

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.. there are used as same as we use in mathematics.
Arithmetic operators in Python:

Example:
            x = 5 y = 2
            print(x+y)    #7          
            print(x-y)     #3
            print(x*y)    #10 
            print(x/y)     #2.5
            print(x//y)    #2
            print(x**y)  #25

Note: # in used to give single line comment in python. 

2.Comparison operators

Comparison operators are used to compare values. It either returns True or False according to the condition.

Comparision operators in Python:


Example:
             x = 5 y = 2 
            print(x>y)       #True
            print(x<y)       #False
            print(x==y)     #False
            print(x!=y)      #True 
            print(x>=y)     #True  
            print(x<=y)     #False

3.Logical operators

Logical operators are the and, or, not operators.

Logical operators in Python



Example: 
           x = True
           y = False
          print(x and y)      #false
          print(x or y)        #True
          print(not x)         #false

4.Bitwise operators


Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)


5.Assignment operators 

Assignment operators are used in Python to assign values to variables. 
example a=50

compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.also called as shorthand assignments. 

6.Special operators

Python language offers some special type of operators like the identity operator or the membership operator. 

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary). In a dictionary we can only test for presence of key, not the value.



No comments:

Post a Comment