top of page
Writer's pictureRajesh Singh

What is Python Operators?

Updated: May 18, 2020


Operators are used to perform operations on variables and values.

There are following operators in Python

  1. Arithmetic operators

  2. Assignment operators

  3. Comparison operators

  4. Logical operators

  5. Identity operators

  6. Membership operators

  7. Bitwise operators

1. Arithmetic Operators

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

Operator Name Example

+ Addition x + y

- Subtraction x - y

* Multiplication x * y

/ Division x / y

% Modulus x % y

** Exponentiation x ** y

// Floor division x // y

Example:

x = 15

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)

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

Output:

x + y = 19

x - y = 11

x * y = 60

x / y = 3.75

x%y = 3

x // y = 3

x ** y = 50625


2. Assignment Operators

Assignment operators are used to assign values to variables:

Operator Example Same As

= x = 5 x = 5

+= x += 3 x = x + 3

-= x -= 3 x = x - 3

*= x *= 3 x = x * 3

/= x /= 3 x = x / 3

%= x %= 3 x = x % 3

//= x //= 3 x = x // 3

**= x **= 3 x = x ** 3

&= x &= 3 x = x & 3

|= x |= 3 x = x | 3

^= x ^= 3 x = x ^ 3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

3. Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x > y

< Less than x < y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


4. 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)

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:

Operator Description Example

is Returns True if both variables are the same object x is y

is not Returns True if both variables are not the same object x is not y

Example:

x1 = 6

y1 = 6

x2 = 'Hey'

y2 = 'Hey'

x3 = [4,5,6]

y3 = [4,5,6]

print(x1 is not y1)

print(x2 is y2)

print(x3 is y3)

Output:

False

True

False



6. Membership Operators

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

Operator Description Example

in Returns True if a sequence with the specified value is present in the object x in y

not in Returns True if a sequence with the specified value is not present in the object x not in y

Example:

a = 'Hello world'

b = {1:'x',2:'y'}

print('H' in a)

print('hello' not in a)

print(1 in b)

print('x' in b)

Output:

True

True

True

False

7. Bitwise Operators

Bitwise operators are used to compare binary numbers:

Operator Name Description

& 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


28 views0 comments

Recent Posts

See All

Files MCQ in Python

File Processing MCQ Q1. Which function writes a list of lines in File? A.      Writelines() B.      Write() C.      Tell() D.      All of...

Python List MCQ Questions

Q1. What is output of below python code? dict= {1:'1', 2:'2', 3:'3'} del dict[1] dict[1] = '10' del dict[2] print (len(dict)) A. 1 B. 2...

Python NUMPY MCQ Questions

Python NumPy Chapter MCQ: 1. What will be print? Import numpy as np a = np. array([1,2,3,5,8]) b = np. array([0,3,4,2,1]) c = a+b c = c*a...

Yorumlar


bottom of page