Header Ads

Header ADS

Python operator


Python operator 


what is operator ?

A operator is a symbol (like +, /, *,< , > , etc. ) which is used to perform some operation between two operand (on which operation is have to perform).

ex 2+5 , 2 < 5  

why we choose operator topic for our first article ?

so the answer is before getting started with python variables we should  have some knowledge of operator and so that we can perform some operation on them. 


python supports many types of operators and some of them are as follows :

 

  1. Arithmetic Operator
  2. Relation operator
  3. Identity Operators
  4. Logical operator
  5. Assignment operator

We will discuss each one by one .

1. Arithmetic Operator :

 These operators are used for arithmetical / mathematical calculation like addition , division ,etc.

 

Arithmetic Operator available with python are :

+    : for addition

-     : for subtraction

/     : division

//    : floor division / Integer division

%   : modulo / Remainder

*    : multiplication .

**  : exponentiation .

lets use some of them and perform some operations

       			 
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 2+5
7
>>> 2-6
-4
>>> 3*6
18
>>> 6/3
2.0
>>> 6//3
2
>>> 5%2
1
>>> 
 

Note : All the code are written on Python IDLE .

 

if you write 2+5 and press enter key the output of that expression is printed on the next  line . 

Note : if you use multiple operators in one expression (like : 20*5+4-10 ) , you can use parentheses to clarify the sequence of operations. otherwise python applies its order of precedence.

 

# Order of precedence for arithmetic expressions

 Order   Operator             Direction

 1.           **                         Left to right

 2.           * , / , // , %           Left to right

 3.           + , -                     Left to right





2. Relation operator : 

These operators are used to find out the relation between two values .

Relation like , which value is bigger , which is lower etc.

 

Relational operator available with python are :

<                less than relational operator

>               Greater than relational operator

< =            less than equal to relational oprator

> =           Greater than equal to relational operator

!=             Not equal

==            equal

 

Note:  when the result of relational operator is correct it will return True otherwise False

( True and False are Boolean operator )

Code to demonstrate Relational Operator working.

       			 
>>> 2 < 5
True
>>> 3 > 6
False
>>> 3 != 5
True
>>> 3 == 3
True
>>> 2 <= 5
True
>>> 


3. Logical Operators :

Logical operators in Python are used for conditional statements are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied.

  • For AND operator – It returns TRUE if both the operands (right side and left side) are true
  • For OR operator- It returns TRUE if either of the operand (right side or left side) is true
  • For NOT operator- returns TRUE if operand is false
Code to demonstrate Logical Operator working.
       			 
       			 
>>> 2 < 4 and 4 >1
True
>>> 1 != 3 and 5<=10
True
>>> 1 != 3 or 5>10
True
>>> not 1==3
True
 



4. Identity Operators :

To compare the memory location of two objects, Identity Operators are used. The two identify operators used in Python are (is, is not).

 

  1.  is: It returns true if two variables point the same object and false otherwise
  2. is not: It returns false if two variables point the same object and true otherwise

Code to demonstrate Identity Operator working.

       			 
>>> 3 is 4
False
>>> 3 is 3
True
>>> 3 is not 6
True
>>> 
 

5. Assignment operator :

assignment operator is used to assign some value to a variable

= :

so what is variable and data-type ?

you will get it answer in next Article ..


This Article is Contributed by Yogesh singh .

Share your suggestions and feedback in comment sections. 

Thank you for visiting here ..


No comments

Powered by Blogger.