Python Basic Operator



Operators in Python :

This is according the latest version of Python 3.7. 

Operators are the constructs or signs which can manipulate and applied on the value of operands.

Consider the expression 2 * 3 = 6. Here, 2 and 3 are called operands and * is called operator.


Types of Operator :

Python language have following types of operators.
  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bit-wise Operators
  • Membership Operators 
  • Identity Operators


Python Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations between two operands. 
It includes +(addition), - (subtraction), *(multiplication), /(divide), %(reminder)  and exponent (**).


OperatorWorking
+ (Add)It is used to add two operands.
Example, if a = 20, b = 10 => a+b = 30
- (Sub)It is used to subtract the second operand from the first operand.
Example, if a = 20, b = 10 => a - b = 10
/ (div)It returns the quotient after dividing the first operand by the second operand.
Example, if a = 20, b = 10 => a/b = 2 and for float {if a = 5,b = 2 => 5/2 = 2.5}
it give floating value.
* (Mul)It is used to multiply one operand with the other.
Example,if a = 20, b = 10 => a * b = 200
% (rem)It returns the reminder after dividing the first operand by the second operand.
Example, if a = 20, b = 10 => a%b = 0
** (Exp)It is an exponent operator.It calculates the first operand power to second operand.
Example, if a = 2, b = 2 => a**b = 4



Python Comparison operator:


Comparison operators are used for comparing the value of the two operands and returns 
boolean value which is true or false accordingly. 

OperatorDescription
==If the value of two operands is equal, then the condition becomes true.
!=If the value of two operands is not equal then the condition becomes true.
<=If the first operand is less than or equal to the second operand, then the condition becomes true.
>=If the first operand is greater than or equal to the second operand, then the condition becomes true.
<>If the value of two operands is not equal, then the condition becomes true.
>If the first operand is greater than the second operand, then the condition becomes true.
<If the first operand is less than the second operand, then the condition becomes true.


Python assignment operators:

The assignment operators are used to assign the value of the right side expression to the
 left side operand.
For example : [X = 5] Here, X is on left side and 5 is on right side so it means we have 
assigned 5 to X.  

OperatorWorking
=It assigns the the value of the right expression to the left operand.
+=It increases the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b and therefore, a = 30.
-=It decreases the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a = a- b and therefore, a = 10.
*=It multiplies the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 10, b = 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%=It divides the value of the left operand by the value of the right operand and assign the reminder back to left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b and therefore, a = 0.
**=a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.

Python Bit-wise Operators:

Bit-wise operator works on bits and performs bit by bit operation.
For example the operation on a and b will be as follows :-
Let a and b be:-
a = 0011 1100
b = 0000 1101 

OperatorWorking
& (binary and)If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied.
| (binary or)The resulting bit will be 0 if both the bits are zero otherwise the resulting bit will be 1.
^ (binary xor)The resulting bit will be 1 if both the bits are different otherwise the resulting bit will be 0.
~ (negation)It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa.
<< (left shift)The left operand value is moved left by the number of bits present in the right operand.
>> (right shift)The left operand is moved right by the number of bits present in the right operand.


Python Logical Operators:

There are following logical operators supported by Python language. Assume variable 
a = 10 and variable b = 20 then:

OperatorDescription
andIf both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true.
orIf one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true.
notIf an expression a is true then not (a) will be false and vice versa.

Membership Operators:

Python membership operators are used to check the membership of value inside a data. If the value is present in the data, then the resulting value is true otherwise it returns false.


OperatorWorking
inIt give true if the element is present in data (list, tuple, or dictionary).
not inIt give true if the element is not found in the data (list, tuple, or dictionary).


Identity Operators:

OperatorWorking
isIt give true if the reference present at both sides point to the same object.
is notIt give true if the reference present at both side do not point to the same object.

Operator Precedence:

The precedence of the operators is important to find out since it enables us to know which operator should be evaluated first.

OperatorWorking
**The exponent operator is given priority over all the others used in the
expression.
~ , + , -The negation, unary plus and minus.
*, / , % The multiplication, divide, modules and reminder
+ , -Binary plus and minus
>> , <<Left shift and right shift
& And.
^ , |Xor, Or 
<= , < , > , >=Comparison operators (less then, less then equal to, greater then,
greater then equal to).
<> , == , !=Equality operators.
=, %=, /=, -= ,+=
*= , **=
Assignment operators

Post a Comment

0 Comments