Python Operators

Python Operators

In Python, you will encounter the following types of operators:

1. Arithmetic
2. Comparison/Relational
3. Assignment
4. Logical
5. Bitwise
6. Membership
7. Identity

The following chart explains all the operators in detail:

ARITHMETIC:

Format:

a+b

EXAMPLE:

• a=10
• b=20
• c=a+b
• c

Output: 30

2. Subtraction

Format:

a-b

EXAMPLE:

• a=40
• b=10
• c=a-b
• c

Output: 30

3. Multiplication

Format:

a*b

EXAMPLE:

• a=3
• b=10
• c=a*b
• c

Output: 30

4. Division

Format:

a/b

EXAMPLE:

• a=300
• b=10
• c=a/b
• c

Output: 30

5. Modulus

Format:

a%b

EXAMPLE:

• a=300
• b=10
• c=a%b
• c

Output: 0

6. Exponent

Format:

**

EXAMPLE:

• a=2
• a**6

Output: 64

COMPARISON/RELATIONAL:

1. Returns true if two operands are equal

Format:

a==b #if equal returns true

EXAMPLE:

• a=2
• b=2
• if (a==b): print “Hello”

Output: Hello

2. Returns true if two operands are not equal

Format:

a!=b #if not equal returns true

EXAMPLE:

• a=2
• b=22
• if(a!=b):

print”Bye”

Output :Bye

3.Another operator similar to !=

Format:

a<>b #if not equal returns true

EXAMPLE:

• a=2
• b=22
• if(a<>b): print “Bye”

Output: Bye

4. Returns true is the variable on the left is greater than the variable on the right

Format:

a>b # returns true is value of a is greater than b

EXAMPLE:

• a=2
• b=1
• if(a>b): print “Hello”

Output: Hello

5. Returns true if the variable on the left is lesser than the variable on the right

Format:

a<b # returns true is value of a is lesser than

EXAMPLE:

if(a<2):

print “Hello”

Output: Hello

6.Returns true is the variable on the left is greater than or equal to the variable on the right

Format:

a>=b # returns true is value of a is greater than or equal to b

EXAMPLE:

• a= 22
• b=22
• if(a>=b): print “Hello”

Output: Hello

7.Returns true is the variable on the left is lesser than or equal to the variable on the right

Format:

a<=b # returns true is value of a is greater than or equal to b

EXAMPLE:

• a=2
• b=2
• if(a<=b): print “Hello”

Output: Hello

ASSIGNMENT:

1. The expression on the right hand side is evaluated and the value is assigned to the operand in the left

Format:

=

EXAMPLE:

• a=110
• b=20
• c=a+b
• c

Output: 130

2. The value on the right is added to the operand on the left and then the result is assigned to the left operand

Format:

+=

EXAMPLE:

• a=5
• b=7
• a+=b
• a

Output: 12

3. The value on the right is subtracted from the operand on the left and then the result is assigned to the left operand

Format:

-=

EXAMPLE:

• a=7
• b=5
• a-=b
• a

Output: 2

4. The value on the right is multiplied to the operand on the left and then the result is assigned to the left operand

Format:

*=

EXAMPLE:

• a=7
• b=5
• a*=b
• a

Output: 35

LOGICAL:

1. Logical AND operator

Format:

And

EXAMPLE:

a=3

b=3

c=3

if(a==b and b==c):

print ‘a=b=c’

Output: a=b=c

2. Logical OR operator

Format:

Or

EXAMPLE:

a=3

b=5

c=3

if(a==c or b==c):

print ‘either a or b is equal to c’

Output: either a or b is equal to c

3. Logical NOT operator

Format:

Not

EXAMPLE:

b=5

c=3

if not(b==c):

print ‘b is not equal to c’

Output: b is not equal to c

MEMBERSHIP:

1. Return true if the element is present in a sequence

Format:

In

EXAMPLE:

>>> if 1 in [1,2,3,4,5]:

print “yes, it is there”

Output: yes it is there

2. Return true if a variable is not present in the sequence

Format:

not in

EXAMPLE:

>>> if 1 not in [2,3,4,5,6,7,8]:

print “No, it is not in here”

Output: No it is not in here

You will get to see more live examples of Python Operators in Python Training.