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.Arithmetic 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 operator||A+B =300|
|Subtraction(-)||Subtracts A from B||B-A =100|
|Multiplication(*)||Multiplies A with B||A*B = 2000|
|Division(/)||Divides the denominator with numerator||B/A =2|
|Modulo(%)||Performs the division operator and returns the remainder||B%A=0|
|Exponent(**)||Performs exponential calculation on operators||A**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 equal||A ==B returns False|
|!=||If the two operations are not equal this returns true||A!=B returns true|
|If the value of the left operand is greater than right this returns true||A>B returns false|
|<||This operator true if the left operand is less than the right operand||A< B returns true|
|>=||This operator returns true if the left operand is greater than or equal to the right operand||A>=B returns false|
|<=||This operator returns true if the left operand is less than or equal to the right operand||A<=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 operand||C= a+b assigns the value of the sum of A and B to c|
|+= ADD and||It adds right operand to the left operand and assigns the result to the left operand||C+=a is equivalent to c= c+a|
|-=Sub and||It subtracts right operand from the left operand and assigns the results to the left operand||C-=a is equivalent to c = c –a|
|*=Multiply and||It multiplies right operand with left operand and assigns the result to the left operand||C*=a is equivalent to c = c*a|
|/= division and||It divides the left operand with the right operand and assigns the result to the left operand||C/=a is equivalent to c = c/a|
|%= Modulo and||It performs modulus of the two operands and assigns the result to left operand||%=a is equivalent to c= c%a|
|**=Exponent And||It performs an exponential calculation on two operands and assigns the value to the left operand||c**=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.
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 zero||A&B =22 (0001 0110)|
|Or(|)||If both the inputs are 0’s the result is zero else the result is 1||A|B= 31(0001 1111)|
|Xor(^)||If both the bits are same the result is 0 else the result is 1||A^B =9(0000 1001)|
|Compliment(~)||If the input is one the output is zero and vice-versa||A~B =-31(1000 1111)|
|Binary left shift (<<)||The left operands are move left by the number of bits specified by the right operand||A<<2 = 120(0111 1000)|
|Binary right shift(>>)||The right operands are moved right but the number of bits specified by the right operand||A>>2 =(0000 0111)|
Logical operators :Logical operators are used the logic condition ( true (or) false). We have three types of logical operators.
Let a = 5 , b=10, c =20
|And||True only if both the inputs are one||aTrueac returns false|
|Or||True if any one of the input is one||ac returns truea>b and a>c returns false|
|Not||It negates the input||afalse|
Membership operator :
The membership operators are classified into two types.
|Is||It returns true if the value is found in the sequence||5 in A returns True10 in A returns False|
|Not in||True if the variable is not found in the sequence||5 not in A returns False10 not in A returns True|
Identity operators :It compares the objects memory address
A =100 , B =100
|Is||Returns true if the operands identity is same||A is B returns True|
|Is not||Returns true if the operand identity is not the same||A 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.
= -= +=//=%=/=*=*==
Not or and
we can expect a question in Operator precedence in online certificate courses