top of page

# How to make a basic calculator

 

# This function adds two numbers

def add(x, y):

    return x + y

 

# This function subtracts two numbers

def subtract(x, y):

    return x - y

 

# This function multiplies two numbers

def multiply(x, y):

    return x * y

 

# This function divides two numbers

def divide(x, y):

    return x / y

 

 

print("Select operation.")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

 

while True:

    # take input from the user

    choice = input("Enter choice(1/2/3/4): ")

 

    # check if choice is one of the four options

    if choice in ('1', '2', '3', '4'):

        num1 = float(input("Enter first number: "))

        num2 = float(input("Enter second number: "))

 

        if choice == '1':

            print(num1, "+", num2, "=", add(num1, num2))

 

        elif choice == '2':

            print(num1, "-", num2, "=", subtract(num1, num2))

 

        elif choice == '3':

            print(num1, "*", num2, "=", multiply(num1, num2))

 

        elif choice == '4':

            print(num1, "/", num2, "=", divide(num1, num2))

       

        # check if user wants another calculation

        # break the while loop if answer is no

        next_calculation = input("Let's do next calculation? (yes/no): ")

        if next_calculation == "no":

          break

   

    else:

        print("Invalid Input")

Output

 

Select operation.

1.Add

2.Subtract

3.Multiply

4.Divide

Enter choice(1/2/3/4): 3

Enter first number: 15

Enter second number: 14

15.0 * 14.0 = 210.0

Let's do next calculation? (yes/no): no

# Python Program to calculate the square root

# https://www.programiz.com/python-programming/examples

# Progrmiz

# Example: For positive numbers

 

 

# Note: change this value for a different result

num = 8

 

# To take the input from the user

#num = float(input('Enter a number: '))

 

num_sqrt = num ** 0.5

print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

 

Output

The square root of 8.000 is 2.828

# Python Program to find the area of triangle

 

If a, b and c are three sides of a triangle. Then,

 

s = (a+b+c)/2

area = √(s(s-a)*(s-b)*(s-c))

Source Code

 

a = 5

b = 6

c = 7

 

# Uncomment below to take inputs from the user

# a = float(input('Enter first side: '))

# b = float(input('Enter second side: '))

# c = float(input('Enter third side: '))

 

# calculate the semi-perimeter

s = (a + b + c) / 2

 

# calculate the area

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)

 

Output

 

The area of the triangle is 14.70

bottom of page