Python - Operators
The modulus operator (%) in Python returns the remainder of the division of one number by another. It is often used to check if a number is even or odd or to wrap values around a certain range.
Example: Understanding the concept of modulus in Python.
# Modulus operator example
number = 15
divisor = 7
# Calculate remainder using modulus
remainder = number % divisor
print(f"The remainder of {number} divided by {divisor} is: {remainder}")
The remainder of 15 divided by 7 is: 1
In this example, the modulus operator is used to find the remainder when 15 is divided by 7. The result is 1, as 15 = 7 * 2 + 1.
== and is operators in Python.
In Python, the == operator is used to compare the values of two objects, while the is operator is used to check if two objects refer to the same memory location.
Example: Differentiating between == and is operators.
# Example with == operator
list1 = [1, 2, 3]
list2 = [1, 2, 3]
# == compares values
result1 = list1 == list2
# Example with is operator
list3 = [1, 2, 3]
list4 = [1, 2, 3]
# is checks if objects refer to the same memory location
result2 = list3 is list4
print("Using == Operator:", result1)
print("Using is Operator:", result2)
Using == Operator: True Using is Operator: False
In this example, == compares the values of list1 and list2, resulting in True because they have the same values. However, the is operator checks if list3 and list4 refer to the same memory location, and it returns False because they are different objects with the same values.
and, or, and not
logical operators in Python?
In Python, the and, or, and not operators are used for logical operations.
Example: Using and, or, and not operators.
# Example with and operator
num1 = 5
num2 = 10
# and returns True if both conditions are True
result1 = (num1 > 0) and (num2 > 0)
# Example with or operator
num3 = -5
num4 = 10
# or returns True if at least one condition is True
result2 = (num3 > 0) or (num4 > 0)
# Example with not operator
flag = True
# not returns the opposite of the given condition
result3 = not flag
print("Using and Operator:", result1)
print("Using or Operator:", result2)
print("Using not Operator:", result3)
Using and Operator: True Using or Operator: True Using not Operator: False
In this example, the and operator returns True if both num1 and num2 are greater than zero. The or operator returns True if at least one of num3 and num4 is greater than zero. The not operator returns the opposite of the given condition, so not flag returns False because flag is True.
&, |, ^, <<, >>) in Python.
In Python, bitwise operators are used to perform operations at the bit level. The bitwise operators include & (AND), | (OR), ^ (XOR), << (left shift), and >> (right shift).
Example: Using bitwise operators in Python.
# Example with bitwise AND (&) operator
num1 = 5 # 0101 in binary
num2 = 3 # 0011 in binary
result_and = num1 & num2 # 0101 & 0011 = 0001 (1 in decimal)
# Example with bitwise OR (|) operator
result_or = num1 | num2 # 0101 | 0011 = 0111 (7 in decimal)
# Example with bitwise XOR (^) operator
result_xor = num1 ^ num2 # 0101 ^ 0011 = 0110 (6 in decimal)
# Example with left shift (<<) operator
result_left_shift = num1 << 1 # 0101 << 1 = 1010 (10 in decimal)
# Example with right shift (>>) operator
result_right_shift = num1 >> 1 # 0101 >> 1 = 0010 (2 in decimal)
print("Bitwise AND:", result_and)
print("Bitwise OR:", result_or)
print("Bitwise XOR:", result_xor)
print("Left Shift:", result_left_shift)
print("Right Shift:", result_right_shift)
Bitwise AND: 1 Bitwise OR: 7 Bitwise XOR: 6 Left Shift: 10 Right Shift: 2
In this example, the bitwise AND, OR, XOR, left shift, and right shift operations are performed on binary representations of numbers. The outputs demonstrate the results of these bitwise operations.