Python Operators: Comparison

These guys compare two operands and tell you if one is greater than or less than the other. They return True or False depending on whether their condition is met or not.

Here’s a table of all the comparison operators in Python:

| Operator | Function | Description | Example in Python Shell |
|———–|——————————-|———————————————-|———————————————|
| == | Equal to | Check if two operands are equal | `2 == 3` returns False |
| != | Not equal to | Check if two operands are not equal | `2 != 3` returns True |
| > | Greater than | Check if the left operand is greater than the right one | `5 > 10` returns False |
| < | Less than | Check if the left operand is less than the right one | `5 < 10` returns True | | >= | Greater than or equal to | Check if the left operand is greater than or equal to the right one | `5 >= 3` returns True |
| <= | Less than or equal to | Check if the left operand is less than or equal to the right one | `5 <= 10` returns True | So, for example, let's say you have a list of numbers and want to find out which ones are greater than 5. You could use the > operator like this:

# Create a list of numbers
numbers = [2, 4, 6, 8]

# Create an empty list to store numbers greater than 5
greater_than_five = []

# Loop through each number in the list
for num in numbers:
    # Check if the number is greater than 5
    if num > 5:
        # If it is, add it to the greater_than_five list
        greater_than_five.append(num)

# Print the list of numbers greater than 5
print(greater_than_five) # Output: [6, 8]

But you could also use a list comprehension to do the same thing more concisely:

# Create a list of numbers
numbers = [2, 4, 6, 8]

# Use list comprehension to create a new list with numbers greater than 5
greater_than_five = [num for num in numbers if num > 5]

# Print the new list
print(greater_than_five) # Output: [6, 8]

In terms of bitwise operators, these are used to manipulate bits at the binary level. They can be useful when working with large data sets or optimizing performance in certain situations. Here’s a table of all the bitwise operators in Python:

| Operator | Function | Description | Example in Python Shell |
|———–|——————————-|———————————————-|———————————————|
| & | Bitwise AND | Returns a new number with bits set to 1 only if both corresponding bits are set to 1 in the operands. | `5 & 3` returns binary 0b101 (decimal 3) |
| | | Bitwise OR | Returns a new number with bits set to 1 if either or both of the corresponding bits are set to 1 in the operands. | `5 | 3` returns binary 0b111 (decimal 7) |
| ^ | Bitwise XOR | Returns a new number with bits set to 1 only if one and only one of the corresponding bits is set to 1 in the operands. | `5 ^ 3` returns binary 0b100 (decimal 4) |
| ~ | Bitwise NOT | Returns a new number where all bits are flipped, i.e., 0 becomes 1 and vice versa. | `~5` returns binary 0b1111111111111111111111111101 (decimal -6) |
| | Bitwise left shift | Shifts the bits in a number to the left by n positions. The rightmost n bits are discarded and new 0s are inserted on the left side. | `5 2` returns binary 0b10100 (decimal 20) |
| | Bitwise right shift | Shifts the bits in a number to the right by n positions. The leftmost n bits are discarded and new 0s or sign bit is inserted on the right side depending on whether the original number was positive or negative. | `5 2` returns binary 0b01 (decimal 1) |

So, for example, let’s say you have a large data set of numbers and want to find out which ones are divisible by 8 using bitwise operators instead of division. You could use the & operator like this:

# This script checks a list of numbers to see which ones are divisible by 8 using bitwise operators instead of division.

numbers = [24, 36, 10, 5] # Creating a list of numbers to check
divisible_by_eight = [] # Creating an empty list to store the numbers that are divisible by 8

for num in numbers: # Looping through each number in the list
    if (num & 0b111) == 0b000: # Using the bitwise AND operator to check if the last three bits are all set to 0, indicating that the number is divisible by 8
        divisible_by_eight.append(num) # If the number is divisible by 8, it is added to the list

print(divisible_by_eight) # Output: [24, 36] - The list of numbers that are divisible by 8

# The original script had a mistake in the if statement, as it was checking for the last three bits to be set to 1 instead of 0. This has been corrected.
# Additionally, the comment explaining the purpose of the if statement has been updated to reflect the correct functionality.

But you could also use a list comprehension to do the same thing more concisely:

# Create a list of numbers
numbers = [24, 36, 10, 5]

# Use list comprehension to filter numbers divisible by 8
divisible_by_eight = [num for num in numbers if (num % 8) == 0]

# Print the resulting list
print(divisible_by_eight) # Output: [24, 36]

# The purpose of this script is to filter a list of numbers and only keep those that are divisible by 8. 
# The list comprehension allows for a more concise and efficient way of filtering the numbers. 
# The modulo operator (%) is used to check if a number is divisible by 8, and the resulting list is stored in the variable "divisible_by_eight". 
# Finally, the resulting list is printed to the console.

In terms of practical scenarios where bitwise operators might be useful, one example is when working with large binary data sets. Bitwise operations can often be faster than traditional arithmetic or logical operations because they operate on individual bits rather than entire numbers. This can lead to significant performance improvements in certain situations. Another scenario where bitwise operators might be useful is when dealing with embedded systems or low-level programming, as these typically involve working directly with binary data and manipulating bits at the hardware level.

SICORPS