OPERATORS
An operator is used to perform specific mathematical or logical operation on values are called
operands. For example, in the expression “10 + num”, the value 10, and the variable num are
operands and the + (plus) sign is an operator. Python supports several types of operators as
discussed below.
Arithmetic Operators
Python supports arithmetic operators (+, -, *, /) that are used to perform the four basic arithmetic
operations as well as modulus division (%), floor division (//) and exponentiation (**).
Addition operator (+) – Adds the two numeric values on either side of the operator. This
operator can also be used to concatenate two strings on either side of the operator.
Subtraction operator (–) - Subtracts the operand on the right from the operand on the
left.
Multiplication operator (*) - Multiplies the two values on both side of the operator.
Repeats the item on left of the operator if first operand is a string and second operand is
an integer value.
Division operator (/) - Divides the operand on the left by the operand on the right and
returns the quotient.
Modulus operator (%) - Divides the operand on the left by the operand on the right and
returns the remainder.
Exponent operator (**) - Performs exponential (power) calculation on operands. That is,
raise the operand on the left to the power of the operand on the right.
Relational Operators
Relational operator compares the values of the operands on its either side and determines the
relationship among them.
Equals to (==) - If the values of two operands are equal, then the condition is
True, otherwise it is False.
Not equal to (!=) - If values of two operands are not equal, then condition is
True, otherwise it is False.
Greater than (>) - If the value of the left-side operand is greater than the value of
the right- side operand, then condition is True, otherwise it is False.
Less than (<) - If the value of the left-side operand is less than the value of the
right- side operand, then condition is True, otherwise it is False operand, then
condition is True, otherwise it is False.
Greater than or equal to (>=) - If the value of the left-side operand is greater than or
equal to the value of the right-side.
Less than or equal to (<=) - If the value of the left operand is less than or equal to
the value of the right operand, then is True otherwise it is False.
Assume the Python variables num1 = 10, num2 = 20, num3 = 10, str1 = "Hello", str2 = "Students"
and str3 = ―Hello‖.
Assignment operators.
= Assigns value from right-side operand to left-side operand.
+= It adds the value of right-side operand to the left-side operand and assigns the result
to the left-side operand.
In other words, x += y is same as x = x + y.
-= It subtracts the value of right-side operand from the left-side operand and assigns the
result to left-side operand.
In other words, x-=y is same as x=x-y.
*= It subtracts the value of right-side operand from the left-side operand and assigns the
result to left-side operand.
In other words, x-=y is same as x=x-y.
/= It multiplies the value of right-side operand with the value of left-side operand and
assigns the result to left-side operand.
In other words, x*=y is same as x=x*y.
%= It performs modulus operation using two operands and assigns the results to left -side
operand.
In other words, x % =y is same as x = x % y.
**= It performs exponential (power) calculation on operators and assigns value to the left-
side operand.
In other words, x **= y is same as x = x ** y.
Logical Operators
There are three logical operators ―and, or, not‖ supported by Python. These are to be written in
lower case only. The logical operator evaluates to either True or False based on the logical
operands on either side. Every value is logically either True or False. By default, all values are
True except None, False, 0 (zero), empty collections "", (), [], {}, and few other special values. So, if
we say num1 = 10, num2 = -5, then both num1 and num2 are logically True. If we have num3 =
0, than num3 is logically False.
Logical AND (and) - If both the conditions are True, then expression becomes True.
Logical OR (or) - If any of the two condition is True, then expression becomes True.
Logical NOT (not) - It is used to reverse the logical state of its operand.
The following code as shown in figure 10.19 demonstrates the use of Logical Operators in
Python.
Identity Operators
Identity operators are used to determine whether the value of a variable is of a certain type or not.
Identity operators can also be used to determine whether two variables are referring to the same
object or not.
Membership Operators
Membership operators are used to check if a value is a member of the given sequence or not.
Bitwise Operators
Bitwise operators are the only operators which works on equivalent binary value of the integer
operands. First of all, integer operands are converted into binary then respected operator works
on bit by bit, hence the name is bitwise operators. The result is also converted into decimal
format.