Operators

Every business requirement, you will convert in the code will require some operation; it could be a mathematical operation, logical operation, etc. Mathematical operations mean addition, subtraction, division, multiplication or any other combination of mathematical operations. The logical operation means either this or that; these can be implemented using relational operators and logical operators.

Before getting into specific details of operators let us have a look at a statement and understand what all components are present in a statement.

b+c

In this statement, "b" and "c" are operands, and "+" is the operator. Every operator has two operands.

Arithmetic operators

Whatever arithmetical operation you can think of is present in Python, whether it is basic BODMAS operations or power or percentage operations

OperatorOperationDescriptionExample
+AdditionSum the value of both operands on either side of "+" operatorx+y gives 13
-SubtractionSubtract the value of right operands from left side operandsx-y gives 7
/DivisionDivide the left side operand by right side operandx/y gives 3
*MultiplicationMultiply the left side operand by right side operandx*y gives 30
%ModulusGives the remainder after dividing the left side operand by right side operandx%y gives 1
**Exponentreturn value of left operand to the power of right operandx**y gives 1000
//Floor DivisionGives the rounded off quotient towards negative infinity after dividing the left side operand by right side operandx//y gives 3

Assignment Operator

In Assignment operator, Python gives you the facility to use less keystroke to perform arithmetic operation and almost every programming language support this kind of assignment operator

OperatorOperationDescriptionExample
=Basis assignmentAssign the value of right operand to left operandx=3 assignes 3 to x
+=Addition assignmentAssign the sum of Left and the Right operand to left operandx+=y assigns x with 13 ≈ x=x+y
-=Subtraction assignmentSubtract the value of right operand from left operand and assign it to left operandx-=y assigns x with 7 ≈ x=x-y
/=Division assignmentdivide the value of left operand with right operand and assign the quotient to left operandx/=y assigns x with 3 ≈ x=x/y
*=Multiplication assignmentMultiplies the value of left operand with right operand and assign the multiplication to left operandx/=y assigns x with 30 ≈ x=x*y
%=Modulus assignmentDivides the value of left operand from right operand and assigns the remainder to left operandx%=y assigns x with 1 ≈ x=x%y
**=Exponent assignmentcalculate the value of left operand to the power right operand and assign the result to left operandx**=y assigns x with 1000 ≈ x=x**y
//=Floor Divisionassignment divide the value of left operand from right operand and assign the rounded off quotient towards negative infinity to left operandx//=y assigns x with 3 ≈ x=x//y

Relational Operators

Python provides you the functionality to handle relation kind of operation e.g. a is larger than b or b is smaller than a.

OperatorOperationDescriptionExample
>greater thanReturn true if the left operand is greater than the right operandx > y gives true
<less thanReturn true if the left operand is lesser than the right operandx < y gives false
>=greater than equal toReturn true if the left operand is greater than or equal to the right operandx >= y gives true
<=less than equal toReturn true if the left operand is lesser than or equal to the right operandx <= y gives false
==equal toReturn true if the left operand is equal to the right operandx == y gives false
!=not equal toReturn true if the left operand is not equal to the right operandx != y gives true

In all above example assume x=10 and y=3

Before moving further practice these operators as much as you can to increase the understanding with these. Apart from these operators, Python has logical operators, bitwise operators which we will see in the next article. 