Python Operators with examples

Spread the love

Operators are the constructs that are used to manipulate the value of operands. Some basic operators include +, -, *, and / . In an expression, an operator is used on operand(s) (values to be manipulated). For example, in the expression sum = a + b, a and b are operands and + is the operator.

Python Supports different types of operators which are as follows –

  1. Arithmetic Operators
  2. Comparison Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

1 . Arithmetic Operators –

Some basic arithmetic operators are +, -, *, /, %, ** and //. You can apply these operators on numbers as well as on variables to perform corresponding operations. For example if a=100 and b=200, then look at the below table to see the result of operations.

OperatorDescriptionExampleOutput
+Addition – Add the operandsa + b300
Subtraction – subtracts operand on the right from the operand on the left of the operatora – b -100
*Multiplication – Multiplies the Operandsa * b20000
/Division – Divides operand on the left side of the operator with the operand on its right. The division operator returns the quotient.b / a2.0
%Modulus – Divides operand on the left side of the operator with the operand on its right. The modulus operator returns the remainder.b % a0
//Floor Division – Divides the operands and returns the quotient. It also removes the digits after the decimal point. If one of the operands is negative, the result is floored (i.e. rounded away from zero towards negative infinity)12 // 5
2
**Exponent – Performs exponential calculation that is raises operand on the right side to the operand on the left of the operator.a**b100**200

2 . Comparison Operators –

Comparison operators also known as relational operators are used to compare the values on its either sides and determines the relation between them. For example assuming a = 100 and b = 200, we can use the comparison operators on them as specified in the below table.

OperatorDescriptionExampleOutput
==Returns True if the two values are exactly equal.a == bFalse
!=Returns True if the two values are not equal.a != bTrue
>Returns True if the value at the operand on the left side of the operator is greater than the value on its right side.a > bFalse
<Returns True if the value at the operand on the right side of the operator is greater than the value on its left side.a < b True
>=Return True if the value of the operand on the left side of the operator is either greater than or equal to the value on its right side.a >= bFalse
<=Returns True if the value at the operand on the right side of the operator is either greater than or equal to the value on its left side.a <= bTrue

3 . Assignment Operators –

Assignment operator as the name suggests assigns value to the operand. The in-place operators also known as shortcut operators that includes +=, -=, *=, /=, %=, //= and **= allow you to write code like num = num + 10 more concisely as num += 10. The different types of assignment and in-place operators are given in the table below.

OperatorDescriptionExample
=Assign value of the operand on the right side of the operator to the operand on the leftc = a, assigns value of a to the variable c
+=Add and assign – Adds the operands on the left and right side of the operator and assigns the result to the operand on the left.a += b is same as a = a + b
-=Subtract and assign. Subtracts operand on the right from the operand on the left of the operator and assigns the result to the operand on the lefta -= b is same as a = a – b
*=Multiply and assign – Multiplies the operands and assigns result to the operand on the left side of the operator.a *= b is same as a = a * b
/=Divide and assign – Divides operand on the left side of the operator with the operand on its right. The division operator returns the quotient. The result is assigned to the operand to the left of the division operator.a /= b is same as a = a / b
%=Modulus and assign. Divides operand on the left side of the operator with the operand on its right. The modulus operator returns the remainder which is then assigned to the operand on the left of the operator.a %= b is same as a = a % b
//=Floor division and assign – Divides the operands and returns the quotient. It also removes the digits after the decimal point. If one of the operands is negative, the result is floored (rounded away from zero towards negative infinity) the result is assigned to the operand on the left of the operator.a //= b is same as a = a // b
**=Exponent and assign – Performs exponential calculation that is raises operand on the right side of the operand on the left of the operator and assigns the result in the left operand.a **= b is same as a = a**b

4. Bitwise Operators –

As the name suggests, bitwise operators performs operations at the bit level. Bitwise operators expect their operands to be of integers and treat them as a sequence of bits.

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

5 . Logical Operators –

Logical operators are used to combine conditional statements.

Operator DescriptionExample
and True if both the operands are true.x and y
orTrue if either of the operands is truex or y
not True if operand is false (complements the operand)not x

6. Membership Operators –

Membership operators are used to test if a sequence is presented in an object.

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y

7. Identity Operators –

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

OperatorDescriptionExample
is Returns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y

Operators Precedence and Associativity –

The below table lists all operators from highest precedence to lowest. When an expression has more than one operator then it is the relative priorities of the operators with respect to each other that determine the order in which the expression will be evaluated.

OperatorDescription
**Exponentiation
~, +, – Complement, unary plus (positive) and minus (negative)
*, /, %, // Multiply, divide, modulo, and floor division
+, – Addition and subtraction
>>, <<Right and left bitwise shift
&Bitwise AND
^, |Bitwise exclusive OR and regular OR
<=, <, >, >=comparison operators
<>, ==, !=Equality operators
=, %=, /=, //=, -=, +=, *=, **=Assignment operators
is, is notIdentity operators
in, not in Membership operators
not, or , and Logical Operators

Rating: 1 out of 5.

Leave a Reply