Operators and Decision Making
Operators and Decision Making
Operators in Python
“Operators in Python can be defined as symbols that assist us to perform
certain operations. The operations can be between variable and variable,
variable and value, value, and value”.
Operators enable you to do things such as calculate numbers, join strings
together, assign values to a variable, compare one value to another, and more.
Operators that Python Language supports are:
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Identity Operators
• Membership Operators
• Bitwise Operators
Arithmetic Operators
Operator Name Example
+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division a/b
% Modulus a%b
** Exponentiation a ** b
// Floor Division a // b
Example
a = 20
b = 3
print(a + b, a - b, a * b, a / b)
print(a % b, a ** b, a // b)
Output
Assignment Operators
Assignment operators are used to assign values to variables. The basic
assignment operator is the equal-sign (=), which assigns the value of its right
operand/s with its left operand.
Operator Example Same as
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
%= x %= 5 x=x%5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
Example
a, b, c, d, e = 30, 67, 32, 22, 45
print(a, b, c, d, e)
a += 5
b -= 53
c **= 3
d %= 10
e //= 3
print(a, b, c, d, e)
Output
Comparison Operator
They are also known as relational operators. They compare the values on
either side of the operator and decide the relation among them.
• Less than (<) — returns true if the value on the left is less than the value
on the right, otherwise it returns false.
• Greater than (>) — returns true if the value on the left is greater than
the value on the right, otherwise it returns false.
• Less than or equal to (<=) — returns true if the value on the left is less
than or equal to the value on the right, otherwise it returns false.
• Greater than or equal to (>=) — returns true if the value on the left is
greater than or equal to the value on the right, otherwise it returns false.
• Equal to (==) — returns true if the value on the left is equal to the value
on the right, otherwise it returns false.
• Not equal to (!=) — returns true if the value on the left is not equal to
the value on the right, otherwise it returns false.
Operator Name Example
== Equal a == b
!= Not Equal a != b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
Example
a = 45
b = 23
print("a == b is", a == b)
print("a != b is", a != b)
print("a > b is", a > b)
print("a < b is", a < b)
print("a >= b is", a >= b)
print("a <= b is", a <= b)
Output
Logical Operator
Logical operators perform logical AND, OR and NOT, operations. They are
usually used in conditional statements to join multiple conditions. AND, OR and
NOT keywords are used to perform logical operations.
Logical operators return either True or False depending on the value of the
operands. They are used when testing for a value. For example, the and logical
operator can be used to test that both operands have a certain value.
Operator Description Example
and Returns True if both statements are true a<5 and b<10
or Returns True if one of the statements is true a<5 or b<10
not Reverse the result, returns False if result is True not(a<5 and b<10)
Example
a = 45
b = 23
print(a > 33 and b > 33)
print(a > 33 and b < 33)
print(a >= 45 and b <= 23)
print(a < 100 or b > 50)
print(a < 1 or b < 11)
print(a < 1 and b < 11)
print(not (a > 0 and b > 0))
Output
Identity Operator
Identity operator checks if two operands share the same identity or not, which
means that they share the same location in memory or different. “is” and “is
not” are the keywords used for identity operands.
Operator Description Example
is Returns True if both variables are the same object a is b
is not Returns True if both variables are not the same a is not b
object
Membership Operator
Membership operand checks if the value or variable is a part of a sequence or
not. The sequence could be string, list, tuple, etc. “in” and “not in” are
keywords used for membership operands.
Operator Description Example
in Returns True if a sequence with the specified value a in b
is present in the object
not in Returns True if a sequence with the specified value a not in b
is not present in the object
Bitwise Operator
Bitwise operands are used to perform bit by bit operation on binary numbers.
First, we must change the format of the number from decimal to binary and
then compare them using AND, OR, XOR, NOT, etc.
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one both bits are 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left Shift left by pushing zeros in from the right and let the
shift leftmost bits fall off
>> Signed right Shift right by pushing copies of the leftmost bit from the
shift left, and let the rightmost bits fall off
Example
var1 = 5
var2 = 10
if var1 > var2:
print("Var 1 is greater.")
else:
print("Var 2 is greater.")
# --------------------------------
var1 = 5
var2 = 5
if var1 > var2:
print("Var 1 is greater.")
elif var2 > var1:
print("Var 2 is greater.")
else:
print("Both Are Equal")
Output