Python Operators: Arithmetic,Comparison,logical,Bitwise,Identity and Membership Operators in Python

Types of python operators. Arithmetic operators Assignment operators. Comparison operators Logical operators. Identity operators. Membership Operators

In this python series you will learn about different types of python operators , Syntax with an Examples. Let's Start

In python have Seven main Operators, there are


These are seven different types of operators in Python.

Let's see one by one. Before we should know 

What are Operators in Python?

Generally Operators are special symbol in the python. Also you should know what is Operands? let's see example you can easy understand.

Example:

>>>2+8

10

In above example, '+" plus symbol is a Operator and "2 " and "8" are Operands.

Let's see different types of Operators one by one.


1. Arithmetic Operator:

Arithmetic Operators perform mathematical operation such as Addition, Subtraction, Multiplication and Division. Not only these also Modulo, Exponential.

 Operator

 Meanings

 Example

 +

Adds two operands or unary plus

2+5

 -

subtracts two operands or unary minus  

10-5 

5

*

Multiple two operands 

2*3

 /

Divide left operand with right. This result always float values. 

6/2

3.0 

 %

Remainder value of division  

5/2

1 #Remainder value

 //

Divide and shows the result as whole value 

 7/3

2   # exact value 2.33

 **

It is Exponential operator 

2**3

#Arithmetic program

x = 17

y = 4

print('x + y =',x+y)

print('x - y =',x-y)

print('x * y =',x*y)

print('x / y =',x/y)

print('x // y =',x//y)

print('x ** y =',x**y)

Output

x + y = 21

x - y = 13

x * y = 68

x / y = 4.25

x // y = 4

x ** y = 83521

2. Comparison Operators

Comparison Operators are compare the value. It returns either True or False depends upon our conditions.

Operators

Meaning

Example

> 

Greater than

True if left operand greater right operand, otherwise false

3>2

True

2>3

False

< 

Lesser than

True if right operand lesser than left operand

Otherwise false

1<2

True

17<10

False

==

Equal to

True both left and right operand value equal

3==3

True

4==3

False

!=

True if left is not equal to right operands

X=10,Y=2

True



# Example Program Comparison

x = 7

y = 12

# It returns true or false.

# If condition is true it returns true otherwise false

print('x > y is',x>y)

print('x < y is',x<y)

print('x == y is',x==y)

print('x != y is',x!=y)

print('x >= y is',x>=y)

print('x <= y is',x<=y)


Output:

x > y is False

x < y is True

x == y is False

x != y is True

x >= y is False

x <= y is True


3. Logical Operators

Operators

Meaning

AND

True if both operands are true

OR

True if either of the operand true

NOT

True if operands are false

#Example

AND:

x = 1

print (x > 3 and x < 10)

Output:

True

OR:

x = 7

print(x > 3 or x < 4)

Output:

True

NOT:

x = 7

print(not(x > 3 and x < 10))

Output:

False


4. Bitwise Operators

In python, Bitwise operators are first integer convert into binary 

form after operations will perform bit by bit. These are the bitwise operators in Python. In below example,

7=0111 and 5=0101

In above image, 

a | b operator, 1+1=1,1+0=1,1+1=1 --> 111-->7

a & b operator, 1+1=0, 1+0=0, 1+1=1 -->101-->5

a ^ b operator, 1+1=0(1+1=1, after 1 change to 0 similarly, 1+0=0 it changes to 1)

As a result, 0010 is the value 2 



Operators

 

Meaning

 

&                  Bitwise AND   x & y

|                   Bitwise OR      x | y

~                   Bitwise NOT   ~x

^                   Bitwise XOR    x ^ y

>>                Bitwise right shift      x>>

<<                Bitwise left shift        x<<



Example

#Bitwise Operators Example Program

a = 7  #0111

b = 4  #0100

   

# Print bitwise AND operation   

print("a & b =", a & b) 

   

# Print bitwise OR operation 

print("a | b =", a | b) 

   

# Print bitwise NOT operation  

print("~a =", ~a) 

   

# print bitwise XOR operation  

print("a ^ b =", a ^ b) 


Output:

a & b = 4

a | b = 7

~a = -8

a ^ b = 3


Shift Operators


>> Bitwise right shift x >> 2 = 2 (0000 0010)

<< Bitwise left shift x << 2 = 40 (0010 1000)


5. Assignment Operator

Assignment Operators are used to assign a values to a variable. These are assignment operators in Python.


6. Identical Operators:

In Python have two identical operators. There are “Is “and “Is not” 

Operators.

IS          #True if the operands are identical which means same object.

IS NOT #True if the operand are not identical which means does not same object.

Example:

X=7

X is 7

Output:

True

X=7

X is not 7

Output: False


7. Membership Operators

In python, Membership Operators are used to check if value or 

variable in the sequences (Lists, Tuples, Dictionary, Sets).

Upcoming Topics we will learn about Python Data Types.

Python has two Membership Operators.

1.  1.   In   # True if it finds the element in the sequences.

2.   2.  In Not # True if it does not find the element in the sequences.

Examples:

X= [2,4,6,8,10]

>>> 6 in X 

True

>>>3 in not X

False



#Program Example

x = ["python", "Hrs"]

print("python" in x)

print(1 in x)

# returns True because a sequence with the value "python" is in the list

# similary, 1 is not in x so it returns false.


Output:

True
False


If you have any queries please leave a comment or contact us (contact us page in the footer section.)

#Python Programming #Python Operators

Was this articles helpful for learning Python? Please leave a comment.