A Brief Overview of Operators In Python

Posted By : Sidharth Sagar | 30-Oct-2022

python Django

Loading...

Operators In Python

In Python, operators are the special symbols that are responsible for the operation between two operands. We can also say that it is like a pillar of any program on which the logic is built in any programming language.

OPERATORS- Operators are special symbols like '-', '+' , '*' , '/', '=' etc.
OPERAND- Operand is a value on which the operator is applied.

The following are the operators available in python:

1). Arithmetic Operators
These operators are used to perform mathematical operations between two operands. This type of operator is used with numeric values
to perform mathematical operations. In Python version 3.x, the division result is a floating-point number while in Python version
2.x, the division result of 2 integers was also an integer and if you want to obtain a result in integer type in Python version 3.x
floored (// integer) is used. The following are the arithmetic operators:

a). Addition Operator (+): This operator is used to add two operands.
Syntax: X + Y
Example: a=5;
b=7;
print(a+b);
Output: 12

b). Subtraction Operator (-): This operator is used to subtract one operand from another.
Syntax: X - Y
Example: a=7;
b=5;
print(a-b);
Output: 2

c). Multiplication Operator (*): This operator is used to multiply one operand with the other operand.
Syntax: X * Y
Example: a=7;
b=5;
print(a*b);
Output: 35

d). Division Operator (/): This operator is used to divide the first operand by the second operand.
Syntax: X / Y
Example: a=7;
b=5;
print(a/b);
Output: 1.4

e). Reminder Operator (%): This operator returns the remainder when dividing the first operand by the second operand.
Syntax: X % Y
Example: a=7;
b=5;
print(a%b);
Output: 2

f). Exponent Operator (**): This operator calculates the first operand power to the second operand.
Syntax: X ** Y
Example: a=3;
b=2;
print(3**2);
Output: 2

g). Floor Division Operator (//): This operator gives the floor value of the quotient generated by dividing the two operands.
Syntax: X // Y
Example: a=7;
b=5;
print(a//b);
Output: 1

2). Comparison Operators
These operators are used to compare the first operand value to the second operand value. It returns a boolean value such as true or
false. It is also known as Relational operators. Following are the Comparison operators:

a). Equal Operator (==): This operator returns true if the values of two operands are equal otherwise it returns false.
Syntax: X == Y
Example: a=7;
b=5;
print(a==b);
Output: False

b). Not Equal Operator (!=): This operator returns true if the values of two operands are not equal otherwise it returns false.
Syntax: X != Y
Example: a=7;
b=5;
print(a!=b);
Output: True

c). Less Than Operator (<): This operator returns true if the values of the first operand are less than the second
the operand, otherwise it returns false.
Syntax: X < Y
Example: a=7;
b=5;
print(a<b);
Output: false

d). Greater Than Operator (>): This operator returns true if the values of the first operand are greater than the second
the operand, otherwise it returns false.
Syntax: X > Y
Example: a=7;
b=5;
print(a>b);
Output: True

e). Less Than or Equal to Operator (<=): This operator returns true if the values of the first operand are less than or equal to the
the second operand, otherwise it returns false.
Syntax: X <= Y
Example: a=7;
b=5;
print(a<=b);
Output: false

f). Greater Than or Equal to Operator (>=): This operator returns true if the values of the first operand are greater than or equal to
the second operand, otherwise it returns false.
Syntax: X >= Y
Example: a=7;
b=5;
print(a>=b);
Output: True


3). Assignment Operators
This type of operator is used to assign values to the variables. The following are the Assignment operators:

a). Equal To Operator (=): This operator assigns the value of the right side of the expression to the left side operand.
Syntax: X = Y + Z
Example: a=7;
b=5;
c= a+b;
print(c);
Output: 12

b). Add AND (+=): This operator adds the right operand to the left operand and assigns the result to the left operand.
Syntax: X = X + Y or X+=Y
Example: a=7;
b=5;
a= a+b;
print(a);
Output: 12

c). Subtract AND (-=): This operator subtracts the right operand from the left operand and assigns the result to the left operand.
Syntax: X = X - Y or X-=Y
Example: a=7;
b=5;
a= a-b;
print(a);
Output: 2

d). Multiply AND (*=): This operator multiplies the right operand with the left operand and assigns the result to the left operand.
Syntax: X = X * Y or X*=Y
Example: a=7;
b=5;
a= a*b;
print(a);
Output: 35

e). Divide AND (/=): This operator divides the left operand with the right operand and assigns the result to the left operand.
Syntax: X = X / Y or X/=Y
Example: a=7;
b=5;
a= a/b;
print(a);
Output: 1.4

f). Modulus AND (%=): This operator divides the left operand by the right operand and assigns the remainder back to the left operand.
Syntax: X = X % Y or X%=Y
Example: a=7;
b=5;
a= a%b;
print(a);
Output: 2

g). Exponent AND (**=): This operator calculates the exponent value using operands and assigns the value to the left operand.
Syntax: X = X ** Y or X**=Y
Example: a=3;
b=2;
a= a**b;
print(a);
Output: 9

h). Floor Division AND (//=): This operator divides the left operand by the right operand and then assigns the floor value to left
operand.
Syntax: X = X // Y or X//=Y
Example: a=7;
b=5;
a= a//b;
print(a);
Output: 1

4). Bitwise Operators
This type of operator is generally used to work on bits and performs bit-by-bit operations on the values of the two operands.
These operators are commonly used to operate on binary numbers. Following are the Bitwise operators:



a). Binary AND (&): If the bits of the left operand and right operand at the same place are 1, then 1 is copied to the result. Otherwise,
0 is copied.
Syntax: X & Y
Example: a=10;
b=4;
print(a&b);
Output: 0



b). Binary OR (|): If both the bits are zero then the resulting bit will be 0 otherwise, the resulting bit will be 1.
Syntax: X | Y
Example: a=10;
b=4;
print(a|b);
Output: 14



c). Binary Ones Complement (~): This operator calculates the negation of each bit of the operand means if the bit is 0 then the
the resulting bit is 1 or vice versa.
Syntax: ~X
Example: a=10;
print(~a);
Output: -11



d). Binary XOR (^): If both the bits are different then the resulting bit will be 1 otherwise, the resulting bit will be 0.
Syntax: X ^ Y
Example: a=10;
b=4;
print(a^b);
Output: 14



e). Binary Left Shift (<<): The left operand value is moved left by the number of bits specified by the right operand.
Syntax: X << Y
Example: a=10;
b=2;
print(a<<b);
Output: 40



f). Binary Right Shift (>>): The right operand value is moved right by the number of bits specified by the right operand.
Syntax: X >> Y
Example: a=10;
b=2;
print(a>>b);
Output: 2



5). Logical Operators
These types of operators are used to combine conditional statements. The following are the Logical operators:

a). Logical AND (and): If both the operands are true, then the condition will be true otherwise false.
Syntax: X < 2 and X < 4
Example: a=1;
print(a<2 and a<4);
Output: true



b). Logical OR (or): If any of the two operands are true, then the condition will be true otherwise false.
Syntax: X > 2 or X > 4
Example: a=3;
print(a>2 or a>4);
Output: true



c). Logical NOT (not): This operator reverses the result, and returns true if the result is false.
Syntax: not(X < 2 and X < 4)
Example: a=1;
print(not(a<2 and a<4));
Output: false

At Oodles ERP, weprovide full-scaleEnterprise app development servicesto solve complex business problems and strengthen your brand's online presence. Our development team specializes in using Python along with web frameworks like Django and Flask to build custom web applications that are easy to scale. Contact us at [email protected] to learn more about our enterprise web app development services.