# Basic Calculations
# Calculator Program in Python by using input() and format()
functions
#Promting input from the user
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))
#addition
print("{} + {} = ".format(n1, n2))
print(n1 + n2)
#subtraction
print("{} - {} = ".format(n1, n2))
print(n1 - n2)
#multiplication
print("{} * {} = ".format(n1, n2))
print(n1 * n2)
#division
print("{} / {} = ".format(n1, n2))
print(n1 / n2)
Copy code
Output
Explanation: In the above Program, we take two numeric values (here, we
are taking float data type), and we get all the outputs (addition, subtraction,
multiplication, and division) once we input both numbers.
Also Read: String Formatting in Python
Also Read: Python Input Function
From nerd to expert ninja! Unlock Python on a deeper level and explore
the best Python programmes from top colleges and online Python
courses with our detailed guide.
Method 2: Calculator Program in Python using function
#Calculator Program in python by defining operations
# Define Operators or Functions: Addition, Subtraction,
Multiplication, and Division
# Addition
def addition(n1, n2):
return n1 + n2
# Subtraction
def subtraction(n1, n2):
return n1 - n2
# Multiplication
def multiplication(n1, n2):
return n1 * n2
# Division
def division(n1, n2):
return n1 / n2
print("Select Operations")
print(
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
# Giving the option to the user to choose the operation
operation = int(input("Enter choice of operation 1/2/3/4: "))
#Taking Input from the Users
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))
# Apply Conditional Statements: To make operation as-per-user
choices
if operation == 1:
print (n1, "+", n2, "=", addition(n1, n2))
elif operation == 2:
print (n1, "-", n2, "=", subtraction(n1, n2))
elif operation == 3:
print (n1, "*", n2, "=", multiplication(n1, n2))
elif operation == 4:
print (n1, "/", n2, "=", division(n1, n2))
else:
print("Invalid Input")
Copy code
Output
Explanation:
The above Program is created in three steps:
Step -1: Define Functions: Addition, Subtraction, Multiplication, and Division
Step-2: Promoting Input from the user
(i). Choosing which operation to perform
(ii). Enter the first and second numbers (float data type)
Step-3: Apply Conditional Statements: To make operation as-per-user
choices
Must Read: def keyword in Python
Must Read: Data Type in Python
This Python program calculates the area
and perimeter (circumference) of the
rectangle. The length and breadth of a
rectangle are given by user.
Following formula are used to clculate area and
perimeter of rectangle:
Area of Rectangle = length * breadth
Perimeter of Rectangle = 2 (length + breadth)
Python Source Code: Area &
Perimeter of Rectangle
# Area & Perimeter of Rectangle
# Reading length from user
length = float(input("Enter length of the
rectangle: "))
# Reading breadth from user
breadth = float(input("Enter breadth of the
rectangle: "))
# Calculating area
area = length * breadth
# Calculating perimeter
perimeter = 2 * (length * breadth)
# Displaying resultsprint("Area of rectangle = ",
area)print("Perimeter of rectangle = ", perimeter)
Output
Enter length of the rectangle: 23
Enter breadth of the rectangle: 17
Area of rectangle = 391.0
Perimeter of rectangle = 782.0
Write Python code to compute square
and cube of give number
# WAP to Find Square and and Cube of Given Number
n = input("Enter the value :--> ")
n = int(n)
square = n ** 2
cube = n ** 3
print("\nSquare of the number :-->", square)
print("Cube of the number :-->", cube
Output:
Enter the value :--> 3
Square of the number :--> 9
Cube of the number :--> 27
NEXT STORY Python Program To Format The Number
3. Python Program to find the Square
and Cube of a given number.
How does this program work?
In this program you will learn about how to do Mathematical functions using
Python.
This program will helpful to calculate square and cube of a number
And then by using the given formula you will get output.
Here is the code
//To find Square and Cube of a number
def square (num):
return num*num #Square of a number
def cube (num):
return num*num*num #Cube of a number
number = int(input("Enter an integer number: "))
print("Square of ",number," is ", square(number))
print("Cube of ",number," is ", cube(number))