About Lesson
Arithmetic Operator
In Python, arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, etc. Here’s a list of the arithmetic operators in Python:
1. Addition (+)
The + operator adds two numbers together.
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#Addition
total=n1+n2
print(f"The sum is {n1} and {n2} is {total}")2. Subtraction (-)
The - operator subtracts the second number from the first.
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#Subtraction
sub=n1-n2
print(f"The difference is {n1} and {n2} is {sub}")3.Multiplication (*)
The * operator multiplies two numbers.
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#Product
product=n1*n2
print(f"The product is {n1} and {n2} is {product}")4. Division (/)
The / operator divides the first number by the second. It returns a float (even if the result is a whole number).
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#division
div=n1/n2
print(f"The result is {n1} and {n2} is {div}")5. Floor Division (//)
The // operator performs division but returns the largest integer that is less than or equal to the result (essentially discarding the fractional part).
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#floor division
intdiv=n1//n2
print(f"The result is {n1} and {n2} is {intdiv}")5. Modulus (%)
The % operator returns the remainder of the division between two numbers.
Python
n1=int(input("enter 1st number"))
n2=int(input("enter 2nd number"))
#Modulus
mod=n1%n2
print(f"The result is {n1} and {n2} is {mod}")