Loading notes...
Loading notes...
BCA • Chapter 2
Chapter 2 covers the fundamental operators in Python, including arithmetic, relational, logical, assignment, and bitwise operators, along with operator precedence.
Programming is all about data manipulation. Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. For example, in the expression `4 + 5`, `4` and `5` are operands and `+` is the operator. This chapter covers the vast array of operators available in Python, from simple math to complex bitwise manipulation.
2.1 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
a = 15
b = 4
print(f"{a} + {b} = {a + b}") # 19
print(f"{a} - {b} = {a - b}") # 11
print(f"{a} * {b} = {a * b}") # 60
print(f"{a} / {b} = {a / b}") # 3.75 (Standard division)
print(f"{a} % {b} = {a % b}") # 3 (Remainder)
print(f"{a} // {b} = {a // b}") # 3 (Floor division, drops the .75)
print(f"{a} ** 2 = {a ** 2}") # 225 (15 to the power of 2)2.2 Relational (Comparison) Operators
Relational operators compare the values on either side of them and decide the relation among them. They are also called comparison operators. The return value of a comparison is always a boolean (`True` or `False`).
x = 10
y = 12
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
# Python supports chained comparisons!
print(5 < x <= 15) # True (Equivalent to: 5 < x and x <= 15)2.3 Logical Operators
Logical operators are used to combine conditional statements. Python uses plain English words for these operators.
Important Concept: Short-Circuit Evaluation. Python evaluates logical expressions from left to right. In an `and` expression, if the first operand is False, Python knows the whole expression must be False, so it skips evaluating the second operand. In an `or` expression, if the first operand is True, Python knows the whole expression is True, so it skips the second.
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
# Short-circuiting example
x = 10
y = 0
# If Python evaluated (y != 0) first, it's fine.
# But if it evaluated (x / y > 2) when y is 0, it would crash (ZeroDivisionError).
# By checking (y != 0) first with 'and', the crash is prevented if y is 0.
if y != 0 and (x / y) > 2:
print("Valid")2.4 Assignment Operators
Assignment operators are used to assign values to variables. The most basic is `=`. Python also provides compound assignment operators which perform an arithmetic operation and an assignment in one step.
c = 10 # Standard assignment
c += 5 # Equivalent to c = c + 5 (c is now 15)
c -= 2 # Equivalent to c = c - 2 (c is now 13)
c *= 2 # Equivalent to c = c * 2 (c is now 26)
c /= 2 # Equivalent to c = c / 2 (c is now 13.0)
c //= 3 # Equivalent to c = c // 3 (c is now 4.0)
c **= 3 # Equivalent to c = c ** 3 (c is now 64.0)2.5 Bitwise Operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name. These are generally used in low-level programming, cryptography, and network programming.
x = 10 # Binary: 1010
y = 4 # Binary: 0100
print(x & y) # 0 (Binary: 0000)
print(x | y) # 14 (Binary: 1110)
print(x ^ y) # 14 (Binary: 1110)
print(~x) # -11 (Inverts the bits, resulting in -(x+1))
print(x << 1) # 20 (Binary: 10100) - Multiplied by 2
print(x >> 1) # 5 (Binary: 0101) - Divided by 22.6 Operator Precedence
When an expression contains more than one operator, the order of evaluation depends on the operator precedence. For example, multiplication has a higher precedence than addition.
General Order of Precedence (Highest to Lowest): 1. Parentheses `()` 2. Exponentiation `**` 3. Unary plus/minus `+x`, `-x`, `~x` 4. Multiplication/Division `*`, `/`, `//`, `%` 5. Addition/Subtraction `+`, `-` 6. Bitwise Shifts `<<`, `>>` 7. Bitwise AND `&` 8. Bitwise XOR `^` 9. Bitwise OR `|` 10. Comparisons `==`, `!=`, `>`, `<`, `>=`, `<=`, `is`, `is not`, `in`, `not in` 11. Logical NOT `not` 12. Logical AND `and` 13. Logical OR `or`