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,
aandb, and performs arithmetic operations on them.pythona = 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:
- Addition:
- Adds two numbers.
- Example:
x + y
- Subtraction:
- Subtracts one number from another.
- Example:
x - y
- Multiplication:
- Multiplies two numbers.
- Example:
x * y
- Division:
- Divides one number by another.
- Example:
x / y
- Modulus:
- Returns the remainder when one number is divided by another.
- Example:
x % y
- Exponentiation:
- Raises one number to the power of another.
- Example:
x ** y
- Floor Division:
- Divides one number by another and returns the integer part of the division.
- Example:
x // y
Exercises:
- 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))
- 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!