Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Python Operators

Operators are functions that perform some logical calculations. In other words, these are the constructs that manipulate the value of the operands. Python supports several kinds of operators. let us discuss one –by – one in detailed.operatorsArithmetic operators :

Arithmetic operators are used for Addition, subtraction, multiplication and division and so on. Let us discuss them with an example.

Let us consider the operator with A =100 , B = 200

Addition(+)Add values on either side of the operatorA+B =300
Subtraction(-)Subtracts A from BB-A =100
Multiplication(*)Multiplies A with BA*B = 2000
Division(/)Divides the denominator with numeratorB/A =2
Modulo(%)Performs the division operator and returns the remainderB%A=0
Exponent(**)Performs exponential calculation on operatorsA**B= 100 power 200

Relational / comparison operator :

These operator compares the operands and displays the result.

Let us assume a = 3 , b =5

==This operation returns true if two values are equalA ==B  returns False
!=If the two operations are not equal  this returns trueA!=B returns true
If the value of the left operand is greater than right this returns trueA>B returns false
<This operator true if the left operand is less than the right operandA< B returns true
>=This operator returns true if the left operand is greater than or equal to the right operandA>=B returns false
<=This operator returns true if the left operand  is less than or equal to the right operandA<=B returns True

Visit Online courses  for more live examples

Assignment operator :

An assignment operator is used to assign a new value to the variable.

Let us assume a =5 , B = 10

=Assigns a value from right operand to left operandC= a+b assigns the value of the sum of A and B to c
+= ADD andIt adds right operand to the left operand and assigns the result to the left operandC+=a  is equivalent to c= c+a
-=Sub andIt subtracts right operand from the left operand and assigns the results to the left operandC-=a is equivalent to  c = c –a
*=Multiply andIt multiplies right operand with left operand and assigns the  result to the left operandC*=a is equivalent to c = c*a
/= division andIt divides the left operand with the right operand and assigns the result to the left  operandC/=a is equivalent to c = c/a
%= Modulo andIt performs modulus of the two operands and assigns the result to left operand%=a  is equivalent to c= c%a
**=Exponent AndIt performs an exponential calculation on two operands and assigns the value to the left operandc**=a is equivalent to c = c**a

Visit Python Training for more code examples

Bit-wise operators :

Every operation that needs to perform must divide into bits. And performs the operation and displays the result in the decimal format.Python Operators

Let us discuss with an example A =30, B= 23

These two numbers written in binary format as

A = 30 = 0001 1110, B = 23 = 0001 0111.

And (&)If both the inputs are 1 ‘s the result is 1 else the result is zeroA&B  =22 (0001 0110)
Or(|)If both the inputs are 0’s the result is zero else the result is 1A|B= 31(0001 1111)
Xor(^)If both the bits are same the result is 0 else the result is 1A^B =9(0000 1001)
Compliment(~)If the input is one the output is zero and vice-versaA~B =-31(1000 1111)
Binary left shift (<<)The left operands are move left by the number of bits specified by the right operandA<<2 = 120(0111 1000)
Binary right shift(>>)The right operands are moved right but the number of bits specified by the right operandA>>2 =(0000 0111)

Logical operators :

Logical operators are used the logic condition ( true (or) false). We have three types of logical operators.Python Operators

Let a = 5 , b=10, c =20

AndTrue  only if both the inputs are oneaTrueac returns false
OrTrue if any one of the input is oneac  returns truea>b and a>c returns false
NotIt negates the inputafalse
Will Python replace java or not let's check here.

Membership operator :

The membership operators are classified into two types.

A={1 ,2,3,4,5}

IsIt returns true if the value is found in the sequence 5 in A returns True10 in A returns False
Not inTrue if the variable is not found in the sequence5 not in A returns False10 not in A returns True
Identity operators :
It compares the objects memory address

A =100 , B =100

IsReturns true if the operands identity is same A is B returns True
Is notReturns true if the operand identity is not the sameA is not B returns false  

Operator precedence :

When there is several operations to be performed, operator precedence determines which operator would be given importance over others. 








<=< >>=


= -= +=//=%=/=*=*==

Is isnot

In innot

Not or and

Note :

we can expect a question in Operator precedence in  online certificate courses