Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Introduction of Python
  5. Operators in Python

Operators in Python

The operations being carried out on data, are represented by operators. The symbol that trigger the operations on data are called operators.

Types of operators in python:-

  • Arithmetic operator
  • Assignment Operator
  • Comparison operator
  • Logical operators
  • Membership operators
  • Identity operators
  • Bitwise operator

1)Arithmetic operator- It is used to perform arithmetic operations between variables.

+Additiona+b
Subtractiona-b
*Multiplicationa*b
/Divisiona/b
%Modulusa%b
**Exponentiala**b
//Floor Divisiona//b

2) Assignment operators- It is used to assign values to variable.

=x=5x=5
+=x+=5x=x+5
-=x-=5x=x-5
*=x*=5x=x*5
/=x/=5x=x/5
**=x**=5x=x**5
%=x%=5x=x%5
//=x//=5x=x//5
&=x&=5x=x&5
|=x|=5x=x|5
^=x^=5x=x^5
>>=x>>=5x=x>>5
<<=x<<=5x=x<<5

3) Comparison operator- These operators are used to compare two variables.

==equals tox==y
!=not equal tox!=y
>Greater thanx>y
<Less thanx<y
>=Greater than or equal tox>=y
<=Less than or equal tox<=y

4) Logical operators- These operators are used to combine conditional statements.

andreturns true if both statements are true
orReturns true if one of the statement is true
notReturns false if the result is true

5) Identity operators- Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y

6) Membership operator- Membership operators are used to check if a sequence is present in an object.

inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y

7) Bitwise operator- Bitwise operators are used to compare binary numbers.

&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInvert all the bits.
<<Shift left by pushing zeros in from the right and let the leftmost bits fall of
>>Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

This is all about operators in python.

Was this article helpful to you? Yes No

How can we help?