Python Operators Example

Python Operators in Python (Article) Operators in Python (Program)

215

Program:

# Arithmetic Operators
x = 10
y = 5
sum_result = x + y
diff_result = x - y
mul_result = x * y
div_result = x / y
mod_result = x % y
exp_result = x ** y
floordiv_result = x // y

# Comparison Operators
is_equal = (x == y)
is_not_equal = (x != y)
is_greater = (x > y)
is_less = (x < y)
is_greater_or_equal = (x >= y)
is_less_or_equal = (x <= y)

# Logical Operators
and_result = (x > y) and (y > 0)
or_result = (x > y) or (y < 0)
not_result = not (x > y)

# Assignment Operators
z = 10
z += 5  # z = z + 5
z -= 3  # z = z - 3
z *= 2  # z = z * 2
z /= 2  # z = z / 2

# Identity Operators
a = [1, 2, 3]
b = [1, 2, 3]
is_same = (a is b)
is_not_same = (a is not b)

# Membership Operators
list_example = [1, 2, 3, 4, 5]
is_member = (3 in list_example)
is_not_member = (6 not in list_example)

# Output Results
print("Arithmetic Operations:")
print(f"Sum: {sum_result}, Difference: {diff_result}, Product: {mul_result}")
print(f"Division: {div_result}, Modulus: {mod_result}, Exponent: {exp_result}, Floor Division: {floordiv_result}\n")

print("Comparison Operations:")
print(f"Equal: {is_equal}, Not Equal: {is_not_equal}, Greater: {is_greater}")
print(f"Less: {is_less}, Greater or Equal: {is_greater_or_equal}, Less or Equal: {is_less_or_equal}\n")

print("Logical Operations:")
print(f"AND: {and_result}, OR: {or_result}, NOT: {not_result}\n")

print("Assignment Operations:")
print(f"Final value of z: {z}\n")

print("Identity Operators:")
print(f"Is a same as b: {is_same}, Is a not same as b: {is_not_same}\n")

print("Membership Operators:")
print(f"Is 3 a member of the list: {is_member}, Is 6 not a member: {is_not_member}")

Output:

Arithmetic Operations:
Sum: 15, Difference: 5, Product: 50
Division: 2.0, Modulus: 0, Exponent: 100000, Floor Division: 2

Comparison Operations:
Equal: False, Not Equal: True, Greater: True
Less: False, Greater or Equal: True, Less or Equal: False

Logical Operations:
AND: True, OR: True, NOT: False

Assignment Operations:
Final value of z: 12.0

Identity Operators:
Is a same as b: False, Is a not same as b: True

Membership Operators:
Is 3 a member of the list: True, Is 6 not a member: True

Explanation:

  • Arithmetic Operators: Used for basic mathematical operations like addition, subtraction, multiplication, division, modulus, exponentiation, and floor division.
  • Comparison Operators: Used to compare two values (e.g., equality, greater than, less than).
  • Logical Operators: Used to combine conditional statements (AND, OR, NOT).
  • Assignment Operators: Used to assign values to variables with shorthand notation (+=, -=, etc.).
  • Identity Operators: Check if two variables point to the same object in memory (is, is not).
  • Membership Operators: Check if a value is present in a collection (in, not in).

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.