Python tutorial part 14 :- python Operators

Python Operators


 

Operators are particular symbols that are used to perform operations on operands. It returns result that can be used in application.

 

Example

1.    4 + 5 = 9  


 

Here 4 and 5 are Operands and (+) , (=) signs are the operators. This expression produces the output 9.

 

Types of Operators

Python supports the following operators

Arithmetic Operators.


Relational Operators.


Assignment Operators.


Logical Operators.


Membership Operators.


Identity Operators.


Bitwise Operators.


 

Arithmetic Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

 

Operators

Description

//

Perform Floor division(gives integer value after division)

+

To perform addition

-

To perform subtraction

*

To perform multiplication

/

To perform division

%

To return remainder after division(Modulus)

**

Perform exponent(raise to power)

 

Example

>>> 10+20  


30  

>>> 20-10  

10  

>>> 10*2  

20  

>>> 10/2  

5  

>>> 10%3  

1  

>>> 2**3  

8  

>>> 10//3  

3  

>>>  

 

Relational Operators

The following table contains the relational operators that are used to check relations.

 

Operators


Description


Less than

Greater than

<=

Less than or equal to

>=

Greater than or equal to

==

Equal to

!=

Not equal to

<> 

Not equal to(similar to !=)

 

eg:

>>> 10<20  


True  

>>> 10>20  

False  

>>> 10<=10  

True  

>>> 20>=15  

True  

>>> 5==6  

False  

>>> 5!=6  

True  

>>> 10<>2  

True  

>>>  

 

Assignment Operators

The following table contains the assignment operators that are used to assign values to the variables.

 

Operators


Description


=

Assignment

/=

Divide and Assign

+=

Add and assign

-=

Subtract and Assign

*=

Multiply and assign

%=

Modulus and assign

**=

Exponent and assign

//=

Floor division and assign

 

Example

1.    >>> c=10  


2.    >>> c  

3.    10  

4.    >>> c+=5  

5.    >>> c  

6.    15  

7.    >>> c-=5  

8.    >>> c  

9.    10  

10. >>> c*=2  

11. >>> c  

12. 20  

13. >>> c/=2  

14. >>> c  

15. 10  

16. >>> c%=3  

17. >>> c  

18. 1  

19. >>> c=5  

20. >>> c**=2  

21. >>> c  

22. 25  

23. >>> c//=2  

24. >>> c  

25. 12  

26. >>>  

 

Logical Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

 

Operators


Description


and

Logical AND(When both conditions are true output will be true)

or

Logical OR (If any one condition is true output will be true)

not

Logical NOT(Compliment the condition i.e., reverse)

 

Example

a=5>4 and 3>2  


print a  

b=5>4 or 3<2  

print b  

c=not(5>4)  

print c  

Output:

1.    >>>   

2.    True  

3.    True  

4.    False  

5.    >>>  

 

Membership Operators

The following table contains the membership operators.

 

Operators


Description


in

Returns true if a variable is in sequence of another variable, else false.

not in

Returns true if a variable is not in sequence of another variable, else false.

 

Example

a=10  


b=20  

list=[10,20,30,40,50];  

if (a in list):  

    print "a is in given list"  

else:  

    print "a is not in given list"  

if(b not in list):  

    print "b is not given in list"  

else:  

    print "b is given in list"  

Output:

>>>   

is in given list  

is given in list  

>>>  

 

Identity Operators

The following table contains the identity operators.

 

Operators


Description


is

Returns true if identity of two operands are same, else false

is not

Returns true if identity of two operands are not same, else false.

 

Example

a=20  


b=20  

if( a is b):  

    print  a,b have same identity  

else:  

    print a, b are different  

b=10  

if( a is not b):  

    print  a,b have different identity  

else:  

    print a,b have same identity  

Output

>>>   

a,b have same identity  

a,b have different identity  

>>>  

 

1 comment:

  1. If you're trying to lose kilograms then you certainly have to jump on this brand new custom keto meal plan.

    To create this keto diet service, licenced nutritionists, fitness couches, and top chefs have united to produce keto meal plans that are effective, convenient, price-efficient, and delicious.

    Since their launch in early 2019, 1000's of individuals have already completely transformed their body and health with the benefits a great keto meal plan can give.

    Speaking of benefits; clicking this link, you'll discover eight scientifically-tested ones offered by the keto meal plan.

    ReplyDelete

Quick Tech