0% found this document useful (0 votes)
3 views3 pages

3.python Operators

The document provides a series of Python coding exercises focused on various operators including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Each question includes example code demonstrating the use of these operators with explanations of their functionality. It serves as a practical guide for learning and applying Python operators in coding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

3.python Operators

The document provides a series of Python coding exercises focused on various operators including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Each question includes example code demonstrating the use of these operators with explanations of their functionality. It serves as a practical guide for learning and applying Python operators in coding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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")

You might also like