Python Programming - Operators
Python Programming - Operators
Python Programming - Operators
5. >>> 45/10
4.5
6. >>> 45//10.0
4.0
7. >>> 2025%10
5
8. >>> 2025//10
202
1. >>> x = 5
2. >>> x = x + 1
3. >>> x
6
>>> x = x + 1
>>> x += 1
If you try to update a variable which doesn’t contain any value, you get an error.
1. >>> z = z + 1
NameError: name 'z' is not defined
Trying to update variable z which doesn’t contain any value results in an error because
Python evaluates the right side before it assigns a value to z ➀.
1. >>> z = 0
2. >>> x = z + 1
Parts of Python Programming Language 41
For example,
1. >>> p = 10
2. >>> q = 12
3. >>> q += p
4. >>> q
22
5. >>> q *= p
6. >>> q
220
7. >>> q /= p
8. >>> q
22.0
9. >>> q %= p
10. >>> q
2.0
11. >>> q **= p
12. >>> q
1024.0
13. >>> q //= p
14. >>> q
102.0
42 Introduction to Python Programming
TABLE 2.4
List of Comparison Operators
Operator Operator Name Description Example
== Equal to If the values of two operands are equal, then the (p == q) is not
condition becomes True. True.
!= Not Equal to If values of two operands are not equal, then the (p != q) is True
condition becomes True.
> Greater than If the value of left operand is greater than the value of (p > q) is not True.
right operand, then condition becomes True.
< Lesser than If the value of left operand is less than the value of right (p < q) is True.
operand, then condition becomes True.
>= Greater than or If the value of left operand is greater than or equal to the (p >= q) is not
equal to value of right operand, then condition becomes True. True.
<= Lesser than or If the value of left operand is less than or equal to the (p <= q) is True.
equal to value of right operand, then condition becomes True.
Note: The value of p is 10 and q is 20.
For example,
1. >>>10 == 12
False
2. >>>10 != 12
True
3. >>>10 < 12
True
4. >>>10 > 12
False
5. >>>10 <= 12
True
Parts of Python Programming Language 43
6. >>>10 >= 12
False
7. >>> "P" < "Q"
True
8. >>> "Aston" > "Asher"
True
9. >>> True == True
True
TABLE 2.5
List of Logical Operators
Operator Operator Name Description Example
and Logical AND Performs AND operation and the result is p and q results in False
True when both operands are True
or Logical OR Performs OR operation and the result is True p or q results in True
when any one of both operand is True
not Logical NOT Reverses the operand state not p results in False
Note: The Boolean value of p is True and q is False.
TABLE 2.6
Boolean Logic Truth Table
P Q P and Q P or Q Not P
For example,
The rules of logic guarantee that these evaluations are always correct. The some_expression
part of the above expressions is not evaluated, so any side effects of doing so do not take
effect. For example,
In ➀ the expression 1 > 2 is evaluated to False. Since and operator is used in the statement
the expression is evaluated to False and the remaining expression 9 > 6 is not evaluated.
In ➁ the expression 3 > 2 is evaluated to True. As or operator is used in the statement the
expression is evaluated to True while the remaining expression 8 < 4 is ignored.
of 1010. Bitwise operators perform their operations on such binary representations, but
they return standard Python numerical values. TABLE 2.7 shows all the bitwise operators.
TABLE 2.7
List of Bitwise Operators
Operator Operator Name Description Example
& Binary AND Result is one in each bit position for which p & q = 12
the corresponding bits of both operands (means 0000 1100)
are 1s.
| Binary OR Result is one in each bit position for which p | q = 61
the corresponding bits of either or both (means 0011 1101)
operands are 1s.
^ Binary XOR Result is one in each bit position for which (p ^ q) = 49 (means 0011 0001)
the corresponding bits of either but not
both operands are 1s.
~ Binary Ones Inverts the bits of its operand. (~p) = −61 (means 1100 0011 in
Complement 2’s complement form due to
a signed binary number.
<< Binary Left Shift The left operands value is moved left by p << 2 = 240
the number of bits specified by the right (means 1111 0000)
operand.
>> Binary Right Shift The left operands value is moved right by p >> 2 = 15
the number of bits specified by the right (means 0000 1111)
operand.
Note: The value of p is 60 and q is 13.
TABLE 2.8
Bitwise Truth Table
P Q P&Q P|Q P^Q ~P
0 0 0 0 0 1
0 1 0 1 1
1 0 0 1 1 0
1 1 1 1 0
FIGURE 2.1 shows examples for bitwise logical operations. The value of operand a is
60 and value of operand b is 13.