Python Operators
✔ In computer programming, “An operator is a symbol that usually
represents an action or process.”
✔ These symbols were adapted from mathematics and logic.
✔ An operator is capable of manipulating a certain value or operand.
✔ Operators are the backbone of any program and they are used for
everything from very simple functions like counting to complex
algorithms like security encryption.
✔ The variables on which operator works are known as Operands.
✔ E.g. a+b Here a and b are operands and + is operator.
Operator's Categories
❖ Unary : A unary operator is an operator, which operates on one operand.
E.g. -a
❖ Binary : A binary operator is an operator, which operates on two
operands.
E.g. a + b
❖ Ternary : A ternary operator is an operator, which operates on three
operands.
TRUE
Condition ? Expr 1 : Expr 2
FALSE
Types of Operator in Python
✔ There are several classifications of operators and each of them can have
one or more operands, a specific data that is to be manipulated.
1)Arithmetic operators
2)Assignment Operators
3)Comparison operators
4)Logical Operators
5)Bitwise Operators
6)Membership Operators
7)Identity Operators
8)Unary Operator
Arithmetic operators
✔ Arithmetic operators are used to perform arithmetic operations
between two operands.
1) + (Addition)
✔ It is used to add two operands.
✔ For example, if a = 20, b = 10 => a + b = 30
2) - (Subtraction)
✔ It is used to subtract the second operand from the first operand.
✔ If the first operand is less than the second operand, the value result
negative.
✔ For example, if a = 20, b = 10 => a - b = 10
3) / (divide)
✔ It returns the quotient after dividing the first operand by the
second operand.
✔ For example, if a = 20, b = 10 => a/b = 2
4) * (Multiplication)
✔ It is used to multiply one operand with the other.
✔ For example, if a = 20, b = 10 => a * b = 200
5) % (reminder)
✔ It returns the reminder after dividing the first operand by the
second operand.
✔ For example, if a = 20, b = 10 => a % b = 0
6) ** (Exponent)
✔ It is an exponent operator represented as it
calculates the first operand power to second
operand.
✔ For example, if a = 2, b = 3 => a**b = 2**3=8
7) // (Floor division)
✔ It gives the floor value of the quotient in division
operation.
✔ For example, if a = 5, b = 2 => a//b = 2
Assignment Operators
• The assignment operators are used to assign the value of the right expression
to the left operand. The assignment operators are described in the following
table.
Operator Description
= It assigns the the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and assign the
modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a =
a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assign the
modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a =
a- b and therefore, a = 10.
Operator Description
*= It multiplies the value of the left operand by the value of the right operand
and assign the modified value back to left operand. For example, if a = 10, b =
20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and
assign the reminder back to left operand. For example, if a = 20, b = 10 => a %
= b will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign
4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign
4//3 = 1 to a.
Multiple Assignment
✔ Python allows us to assign a value to multiple
variables in a single statement which is also
known as multiple assignment.
✔ Python apply multiple assignments in two ways
either
(i)by assigning a single value to multiple variables
(ii)or assigning multiple values to multiple
variables.
✔ Lets see given examples.
(1) Assigning single value to (2)Assigning multiple values
multiple variables to multiple variables
✔ Eg: ✔ Eg:
x=y=z=50 x,y,z=5,10,15
print (“x”,x) print (“x”,x)
print (“y”,y) print (“y”,y)
print (“z”,z) print (“z”,z)
✔ Output: ✔ Output:
X=50 X=50
Y=50 Y=50
Z=50 Z=50
Unary Operator
✔ A unary operator is an operator which works on a single
operand.
✔ Python support unary minus operator(-).
✔ When an operand is preceded by a minus sign, then the unary
operator negates its value.
✔ For Example:
x=100
y=-x
print(y) Output= -100
Comparison/Relational operator
✔ Comparison operators are used to comparing the value of the
two operands and returns boolean true or false accordingly. The
comparison operators are described in the following table.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition becomes
true.
>= If the first operand is greater than or equal to the second operand, then the condition becomes
true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.
Logical Operators
✔ The logical operators are used primarily in the expression
evaluation to make a decision. Python supports the following
logical operators.
Operator Description
and If both the expression are true, then the condition will be true. If a
and b are the two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a
and b are the two expressions, a → true, b → false => a or b → true.
not If an expression a is true then not (a) will be false and vice versa.
Bitwise operator
• The bitwise operators perform bit by bit operation
on the values of the two operands.
• For example: a = 7; then, binary (a) = 0111
• b = 6; binary (b) = 0110
• hence, a & b = 0110
• a | b = 0111
• a ^ b = 0100
• ~ a = 1000
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is
copied to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero otherwise the
resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different otherwise the
resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is
0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in
the right operand.
>> (right shift) The left operand is moved right by the number of bits present in the
right operand.
Membership Operators
✔ Python membership operators are used to check the
membership of value inside a Python data structure. If the
value is present in the data structure, then the resulting
value is true otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the
second operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the
second operand (list, tuple, or dictionary).
Identity Operators
✔Identity operators compare the memory locations of
two objects.
Operator Description
is Evaluates to true if the variables on either side of the
operator point to the same object and false otherwise.
is not Evaluates to true if the variables on either side of the
operator point to the same object and false otherwise.
✔
Operator Precedence
The precedence of the operators is important to find out since it enables us to know which operator
should be evaluated first. The precedence table of the operators in python is given below.
Operator Description
** The exponent operator is given priority over all the others used in
the expression.
~+- The negation, unary plus and minus.
* / % // The multiplication, divide, modules, reminder, and floor division.
+- Binary plus and minus
>> << Left shift and right shift
& Binary and.
^| Binary xor and or
<= < > >= Comparison operators (less then, less then equal to, greater then,
greater then equal to).
<> == != Equality operators.
= %= /= //= -= += Assignment operators
*= **=
is , is not Identity operators
in, not in Membership operators
Not, or, and Logical operators
THANK
YOU