Arithmwtic Operations

Arithmetic Operations

# This script demonstrates addition in Python

# Addition of integers
a = 5
b = 3
result1 = a + b
print("Addition of", a, "and", b, "is:", result1)

# Addition of floats
x = 2.5
y = 1.75
result2 = x + y
print("Addition of", x, "and", y, "is:", result2)

# Addition of integers and floats
p = 10
q = 2.5
result3 = p + q
print("Addition of", p, "and", q, "is:", result3)

# Addition of strings (concatenation)
message1 = "Hello"
message2 = " world!"
result4 = message1 + message2
print("Concatenation of strings:", result4)
# This script demonstrates subtraction in Python

# Subtraction of integers
a = 10
b = 4
result1 = a - b
print("Subtraction of", a, "and", b, "is:", result1)

# Subtraction of floats
x = 3.5
y = 1.25
result2 = x - y
print("Subtraction of", x, "and", y, "is:", result2)
# This script demonstrates multiplication in Python

# Multiplication of integers
a = 3
b = 5
result1 = a * b
print("Multiplication of", a, "and", b, "is:", result1)

# Multiplication of floats
x = 2.5
y = 1.5
result2 = x * y
print("Multiplication of", x, "and", y, "is:", result2)
# This script demonstrates division in Python

# Division of integers
a = 10
b = 3
result1 = a / b
print("Division of", a, "and", b, "is:", result1)

# Division of floats
x = 5.5
y = 2.0
result2 = x / y
print("Division of", x, "and", y, "is:", result2)
# This script demonstrates the modulo operation in Python

# Modulo of integers
a = 10
b = 3
result1 = a % b
print("Modulo of", a, "and", b, "is:", result1)

# Modulo of floats
x = 5.5
y = 2.0
result2 = x % y
print("Modulo of", x, "and", y, "is:", result2)

Shorthand

# Shorthand addition (+=)
x = 5
x += 3  # Equivalent to x = x + 3
print("x:", x)  # Output: 8

# Shorthand addition with strings
message = "Hello"
message += " world!"  # Equivalent to message = message + " world!"
print("Message:", message)  # Output: "Hello world!"
# Shorthand subtraction (-=)
x = 10
x -= 4  # Equivalent to x = x - 4
print("x:", x)  # Output: 6
# Shorthand multiplication (*=)
x = 3
x *= 5  # Equivalent to x = x * 5
print("x:", x)  # Output: 15
# Shorthand division (/=)
x = 10
x /= 2  # Equivalent to x = x / 2
print("x:", x)  # Output: 5.0
# Shorthand modulo (%=)
x = 10
x %= 3  # Equivalent to x = x % 3
print("x:", x)  # Output: 1
# Shorthand exponentiation (**=)
x = 2
x **= 3  # Equivalent to x = x ** 3
print("x:", x)  # Output: 8