Arithmetic Operators

Arithmetic Operators

Introduction:

  • Arithmetic operators in Python are used to perform common mathematical operations on numeric values.

Python Program:

  • Below is a Python program that takes two numbers, a and b, and performs arithmetic operations on them.

    python
    a = 5
    b = 2
    print('a + b  :', (a + b))  # Arithmetic Addition
    print('a - b  :', (a - b))  # Arithmetic Subtraction
    print('a * b  :', (a * b))  # Arithmetic Multiplication
    print('a / b  :', (a / b))  # Arithmetic Division
    print('a % b  :', (a % b))  # Arithmetic Modulus
    print('a // b :', (a // b)) # Arithmetic Floor Division
    print('a ** b :', (a ** b)) # Arithmetic Exponentiation
    

Output:

a + b  : 7
a - b  : 3
a * b  : 10
a / b  : 2.5
a % b  : 1
a // b : 2
a ** b : 25

Detailed Tutorials:

  • Here are detailed tutorials covering each of the arithmetic operators with examples:
  1. Addition:
    • Adds two numbers.
    • Example: x + y
  2. Subtraction:
    • Subtracts one number from another.
    • Example: x - y
  3. Multiplication:
    • Multiplies two numbers.
    • Example: x * y
  4. Division:
    • Divides one number by another.
    • Example: x / y
  5. Modulus:
    • Returns the remainder when one number is divided by another.
    • Example: x % y
  6. Exponentiation:
    • Raises one number to the power of another.
    • Example: x ** y
  7. Floor Division:
    • Divides one number by another and returns the integer part of the division.
    • Example: x // y

Exercises:

  1. Basic Arithmetic Operations:
    • Write a program to perform basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers entered by the user.

      python
      # Read two numbers from user
      a = int(input("Enter first number: "))
      b = int(input("Enter second number: "))
      # Perform arithmetic operations
      print('a + b  :', (a + b))
      print('a - b  :', (a - b))
      print('a * b  :', (a * b))
      print('a / b  :', (a / b))
      
  2. Modulus and Exponentiation:
    • Write a program to calculate the modulus and exponentiation of two numbers entered by the user.

      python
      # Read two numbers from user
      a = int(input("Enter first number: "))
      b = int(input("Enter second number: "))
      # Calculate modulus and exponentiation
      print('a % b  :', (a % b))
      print('a ** b :', (a ** b))
      

Summary:

  • Arithmetic operators in Python are fundamental for performing mathematical calculations. Practice using these operators to become proficient in handling arithmetic operations in Python!