Breaking News

2

# Operators in Python¶

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Python language is rich in built-in operators and provides following types of operators.

### Types of Operators¶

We have the following types of operators in Python programming −

• Arithmetic operators
• Assignment operators
• Relational/Comparison Operators
• Logical Operators
• Bitwise Operators
• Identity Operator
• Membership Operator

## Arithmetic Operator¶

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name
Subtraction
* Multiplication
/ Division
** Exponent
% Modulus (Remainder from division)
// Floor Division
In [1]:
a=int(input("Enter First Number:"))
b=int(input("Enter First Number:"))
print("Subtraction:",a-b)
print("Multiplication:",a*b)
print("Division",a/b)
print("Floor Division:",a//b)
print("Modulus",a%b)
print("Exponentiation:",a**b)
Enter First Number:5
Enter First Number:2
Subtraction: 3
Multiplication: 10
Division 2.5
Floor Division: 2
Modulus 1
Exponentiation: 25

## Python Assignment Operators¶

Assignment operators are used to assign values to variables:

Operator Example Same As
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
//= x //= 5 x = x // 5
%= x %= 5 x = x % 5
**= x **= 5 x = x ** 5
In [2]:
a = 10
print(a)
10
In [3]:
a = 10
b = 20
a = a + b
print(a)
30
In [4]:
a = 10
b = 20
a += b
print(a)
30

## Python Comparison/Relational Operators¶

Comparison operators are used to compare two values:

Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
In [5]:
a=10
b=20
c = a < b
print(c)
True
In [6]:
a=10
b=20
c = a > b
print(c)
False
In [7]:
a=10
b=20
c = a <= b
print(c)
True
In [8]:
a=10
b=20
c = a >= b
print(c)
False
In [9]:
a=10
b=20
c = a == b
print(c)
False
In [10]:
a=10
b=20
c = a != b
print(c)
True

## Python Logical Operators¶

Logical operators are used to combine conditional statements:

Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

### Logial AND¶

In [11]:
x = 1
print(x < 5 and x < 10)
True
In [12]:
print(10 and 20)
20

### Logical OR¶

In [13]:
x = 1
print(x < 5 or x > 4)
True
In [14]:
print(10 or 20)
10

### Logical NOT¶

In [15]:
x = 1
print(not(x < 5 or x > 4))
False
In [16]:
a=0
print(not a)
True

## Python Bitwise Operators¶

Bitwise operators are used to compare (binary) numbers:

Operator Description
& Bitwise AND
l Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right Shift
~ 1’s Complement
In [17]:
a=10
b=6
print("Bitwise AND:",a&b)
print("Bitwise OR:",a|b)
print("Bitwise XOR:",a^b)
print("Left Shift:",a<<2)
print("Right Shift:",a>>2)
print("1's Complement:",~a)
Bitwise AND: 2
Bitwise OR: 14
Bitwise XOR: 12
Left Shift: 40
Right Shift: 2
1's Complement: -11

## Python 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:

Operator Description
is identical – Returns True if both variables are the same object
is not not identical – Returns True if both variables are not the same object
In [18]:
l1=[1,2,3]
l2=[1,2,3]
print(l1==l2)
print(l1 is l2)
True
False
In [19]:
l3=l2
print(l2)
print(l3)
print(l2==l3)
print(l2 is l3)
[1, 2, 3]
[1, 2, 3]
True
True
In [20]:
del l2
In [21]:
print(l3)
[1, 2, 3]

## Membership Operators¶

Membership operators are used to test if a sequence is presented in an object:

• in – Evaluates to true if it finds a variable in the specified sequence
• not in – Evaluates to true if it does not finds a variable in the specified sequence
Operator Description
in Returns True if a sequence with the specified value is present in the object
not in Returns True if a sequence with the specified value is not present in the object
In [22]:
L=[12,353,545,2,45,67,89,23]
n=2
print(n in L)
True
In [23]:
print(n not in L)
False