Class 10 String Comparison ,Bitwise Operators,Chaining Comparisons
String Comparison with Relational Operators in Python ๐ฌโ๏ธ
In Python, you can compare strings using relational operators (<, <=, >, >=, ==, !=). These comparisons are based on lexicographical (dictionary) order, which uses the Unicode code points of the characters. ๐
How String Comparison Works ๐ค
- Character-by-character comparison: Python compares strings character by character from left to right. โก๏ธ
- Unicode values: Each character is compared based on its Unicode code point value. ๐ข
- Case sensitivity: Uppercase letters have lower Unicode values than lowercase letters (‘A’ is 65, ‘a’ is 97). ๐ก
- Length matters: If all characters are equal, the shorter string is considered smaller. ๐
Examples ๐ก
Python
# Equality comparisons
print("hello" == "hello") # True โ
print("Hello" == "hello") # False (case matters) โ
print("hello" != "world") # True โ
# Lexicographical comparisons
print("apple" < "banana") # True ('a' comes before 'b') โ
print("apple" > "banana") # False โ
print("apple" <= "apricot") # True ('p' < 'r') โ
# Case sensitivity
print("Apple" < "apple") # True (uppercase comes before lowercase) โ
print("Zebra" < "apple") # True (all uppercase before lowercase) โ
# Different length strings
print("hi" < "hello") # False ('i' > 'e') โ
print("hi" < "high") # True (shorter string when prefixes match) โ
Important Notes ๐
- ASCII/Unicode Order:
- Digits (0-9) < Uppercase letters (A-Z) < Lowercase letters (a-z) ๐ข๐ ฐ๏ธ๐ก
- Special characters have their own positions in the Unicode table. โจ
- Case-insensitive comparison: To compare strings ignoring case, convert both to the same case first: โฌ๏ธPython
print("Hello".lower() == "hello".lower()) # True โ - Locale-aware comparison: For language-specific sorting, use the
localemodule or special string comparison functions. ๐ - Performance: String comparison is O(n) where n is the length of the shorter string. โฑ๏ธ
String comparison is fundamental for sorting algorithms, searching operations, and many other common programming tasks in Python. ๐
Bitwise Operators in Python ๐งฎ
Bitwise operators perform operations on integers at the binary level by manipulating individual bits. Here are all the bitwise operators available in Python with examples:
1. Bitwise AND (&) ๐ค
Performs a logical AND on each pair of corresponding bits.
Python
a = 10 # 1010 in binary
b = 4 # 0100 in binary
print(a & b) # 0 (0000 in binary)
2. Bitwise OR (|) โ
Performs a logical OR on each pair of corresponding bits.
Python
a = 10 # 1010
b = 4 # 0100
print(a | b) # 14 (1110)
3. Bitwise XOR (^) ๐
Performs a logical exclusive OR on each pair of corresponding bits.
Python
a = 10 # 1010
b = 4 # 0100
print(a ^ b) # 14 (1110)
4. Bitwise NOT (~) ๐
Inverts all the bits (unary operator). Note that this gives the two’s complement.
Python
a = 10 # 1010
print(~a) # -11 (inverts bits and adds 1 for two's complement)
5. Left Shift (<<) โฌ
๏ธ
Shifts bits to the left, filling with 0s on the right.
Python
a = 10 # 1010
print(a << 1) # 20 (10100)
print(a << 2) # 40 (101000)
6. Right Shift (>>) โก๏ธ
Shifts bits to the right, filling with 0s on the left (for positive numbers).
Python
a = 10 # 1010
print(a >> 1) # 5 (0101)
print(a >> 2) # 2 (0010)
Practical Examples ๐ก
- Check if a number is even or odd:Python
num = 15 if num & 1: print("Odd") else: print("Even")โ - Swap two numbers without temporary variable:Python
a = 5 b = 7 a = a ^ b b = a ^ b a = a ^ b print(a, b) # 7 5๐ - Check if two numbers have opposite signs:Python
x = 10 y = -5 if (x ^ y) < 0: print("Opposite signs") else: print("Same sign")โ๏ธ - Multiply/Divide by powers of 2:Python
num = 20 print(num << 1) # Multiply by 2 (40) print(num >> 1) # Divide by 2 (10)โ๏ธโ
Important Notes ๐
- Bitwise operators work on integers only. ๐ข
- The results depend on how numbers are represented in binary (two’s complement for negative numbers). ๐ป
- Left shifting is equivalent to multiplying by 2n. ๐
- Right shifting is equivalent to integer division by 2n. ๐
- Bitwise operations are generally faster than arithmetic operations. โก
Bitwise operators are commonly used in:
- Low-level programming ๐ฅ๏ธ
- Embedded systems ๐ค
- Cryptography ๐
- Compression algorithms ๐ค
- Network protocols ๐
- Performance-critical code sections ๐
Chaining Comparisons in Python ๐โ๏ธ
Python allows you to chain multiple comparison operations in a single expression, making your code more concise and readable. This feature is sometimes called “comparison operator chaining” or “range comparisons.” ๐
How Chained Comparisons Work ๐ค
When you chain comparisons, Python automatically combines them with logical and operators. The expression a < b < c is equivalent to a < b and b < c, but more efficient because b is only evaluated once. โก
Basic Syntax:
Python
x < y < z
This is equivalent to:
Python
x < y and y < z
Examples ๐ก
Numeric Range Checking ๐ข
Python
age = 25
# Traditional way
if 18 <= age and age <= 65:
print("Eligible")
# With chained comparisons
if 18 <= age <= 65:
print("Eligible") # Output: Eligible
Multiple Comparisons ๐งฉ
Python
x = 5
# Check if x is between 1 and 10 but not 7
if 1 <= x <= 10 and x != 7:
print("Valid number") # Output: Valid number
# Can be written as (though less readable and subtly different if x is modified within an expression):
# if 1 <= x != 7 <= 10:
# print("Valid number") # Works but less readable
String Comparisons ๐ฌ
Python
name = "Bob"
if "A" <= name <= "M":
print("Name is in first half of alphabet") # Output: Name is in first half of alphabet
Combining Different Operators ๐
Python
temperature = 22.5
if 20 <= temperature <= 25:
print("Comfortable room temperature") # Output: Comfortable room temperature
Important Notes ๐
- Evaluation Order: Comparisons are evaluated from left to right. โก๏ธPython
a < b < c # Evaluates as (a < b) and (b < c) - Short-Circuiting: Like regular boolean expressions, chained comparisons short-circuit. โกPython
# If the first comparison fails, the second isn't evaluated result = 1 > 2 > some_function() # some_function() never gets called if 1 > 2 is False - All Operators Don’t Need to Be the Same: You can mix and match.Python
if 1 < x <= 10: pass if a == b == c: pass if x != y != z: pass - Performance Benefit: In
a < b < c,bis only evaluated once, whereas ina < b and b < c,bmight be evaluated twice ifa < bis true. ๐
Advanced Examples ๐ก
Multiple Variables ๐
Python
a, b, c = 1, 2, 3
if a < b < c:
print("Increasing sequence") # Output: Increasing sequence
With Functions โ๏ธ
Python
def is_positive(x):
return x > 0
if is_positive(5) < is_positive(10): # Evaluates to (True < True) which is (1 < 1) โ False
print("This won't print")
In While Loops ๐
Python
x = 0
while 0 <= x <= 3: # Changed limit for shorter output
print(x)
x += 1
# Output:
# 0
# 1
# 2
# 3
Chained comparisons are particularly useful for range checking and making mathematical conditions more readable. They’re a Pythonic way to write clean, expressive conditions. โ