SANKALP STUDY SUCCESS
Way to Learn and Key to Success
PYTHON OPERATORS CODING PROGRAMS
Q1. Perform addition, subtraction, multiplication, and division of two
numbers.
x, y = 10, 5
print("Addition:", x + y)
print("Subtraction:", x - y)
print("Multiplication:", x * y)
print("Division:", x / y)
Q2. Find remainder and exponent of two numbers.
x, y = 10, 3
print("Remainder:", x % y)
print("Exponent:", x ** y)
Q3. Use assignment operators (+=, -=, *=) with a variable.
x = 10
x += 5
print("After += :", x)
x -= 3
print("After -= :", x)
x *= 2
print("After *= :", x)
Q4. Use floor division (//) assignment operator.
x = 17
x //= 3
print("After //= :", x)
Q5. Compare two numbers using comparison operators.
a, b = 15, 20
print("a == b:", a == b)
print("a != b:", a != b)
print("a > b:", a > b)
print("a < b:", a < b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)
Q6. Use logical operators (and, or, not).
x, y = 10, 20
print("x < 15 and y > 15:", x < 15 and y > 15)
print("x < 5 or y > 15:", x < 5 or y > 15)
print("not(x < 15 and y > 15):", not(x < 15 and y > 15))
Q7. Check identity operators with is and is not.
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print("a is b:", a is b) # Same object
print("a is not c:", a is not c) # Different objects
Q8. Use membership operators (in, not in).
fruits = ["apple", "banana", "mango"]
print("'apple' in fruits:", "apple" in fruits)
print("'orange' not in fruits:", "orange" not in fruits)
Q9. Demonstrate bitwise AND, OR, and XOR.
x, y = 6, 3 # (6 = 110, 3 = 011 in binary)
print("Bitwise AND:", x & y)
print("Bitwise OR :", x | y)
print("Bitwise XOR:", x ^ y)
Q10. Demonstrate left shift and right shift operators.
x = 8 # 1000 in binary
print("Left shift by 2:", x << 2) # 100000 = 32
print("Right shift by 2:", x >> 2) # 10 = 2
Q11. Use combined assignment with bitwise operator (&=).
x = 7 # 111 in binary
x &= 3 # 011 in binary
print("After &= :", x)
Q12. Use combined assignment with bitwise OR (|=).
x = 4 # 100 in binary
x |= 1 # 001 in binary
print("After |= :", x)
Q13. Use combined assignment with XOR (^=).
x = 5 # 101 in binary
x ^= 3 # 011 in binary
print("After ^= :", x)
Q14. Combine multiple operators in an expression.
x, y, z = 5, 10, 15
result = (x + y) * z / 5
print("Result:", result)
Q15. Write a program to check if a number is even using modulus and
comparison operator.
num = 12
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")