All operators in python-Python

All operators in python-Python

 

Short-hand Assignment Operators


there are various  short-hand assignment operators in Python like a +=5 .It will add 5 to the variable a and later will assign the result to variable ‘a’. .It is equivalent to a =a +5. These short-hand assignment operators are also known as compound assignment operators.Following are the short-hand assignment operators in python:

Operator                                            meaning
+= It adds right operand to the left operand and assign the result to left operand
-= It subtracts right operand from the left operand and assign the result to left operand
*= It multiplies right operand with the left operand and assign the result to left operand
/ Divide left operand by the right one and returns results into float
/= It divides left operand with the right operand and assign the result to left operand
%= It takes modulus using two operands and assign the result to left operand
//= Performs exponential (power) calculation on operators and assign value to the left operand

Program for demonstration of short-hand assignment operators

a =2

a+=3

print (a)

a-=3

print(a)

a*=3

print(a)

a/=3

print(a)

a%=3

print(a)

a**=3

print(a)

a//=3

print(a)

Output

5

2

6

2.0

8.0

2.0

 

 Relational or Comparison Operators

Comparison operators are used to compare values on left hand side and right hand side  and fine relation between them. Following are the relational operators in python:

Operator           Name meaning
< less than operator Returns True if left operand is less than the right
> Greater than operator Returns True if left operand is greater than the right
== equal to operator Returns True if both operands are equal
!= Not equal to operator Returns True if operands are not equal
>= Greater than or equal to operator Return True if left operand is greater than or equal to the right
<= Less than or equal to operator Return True if left operand is less than or equal to the right

Program for demonstration of relational operators

a=2

b=3

c=a<b

print(c)              True

c=a>b

print(c)               False

c=a==b

print(c)              False

c=a!=b

print(c)             True

c=a<=b

print(c)            True

c=a>=b

print(c)          False

 Logical Operators

Logical operators are used to combine two or more conditions and perform the logical operations using Logical AND, Logical OR and Logical NOT. Following are the logical operators in python:

 

Operator Name meaning
and Logical AND operator Returns True if both operands return True
or Logical OR operator Returns True if either of the operands True
not Logical NOT operator Returns True if either of operands is False and returns False if operands is True

 

Program for demonstration of logical operators

a =2 <3   and   4>3

print(a)

a= 2 or 3 or 4> 3

print(a)

a=not a

print(a)

a = not a

print(a)

Output

True

True

False

True

 

Leave a Reply

Your email address will not be published. Required fields are marked *