Calculator Program in Python Using While Loop

Calculator Program in Python Using While Loop we can execute mathematical calculations like addition, subtraction, multiplication, divide using a python programming language. These are python sample codes for beginners to implement python calculator program.

If Else Statement in Python

an if else clause The true or false value of an expression is determined by a Python statement.

The “if” statement is executed if a condition is true. If not, the “else” statement is executed.

Conditional statements enable you to better control the flow of your program.

You can create a chain of if statements using the if/else if statement.

The if statements are evaluated one after the other until one of the if expressions is true or the if/else if chain ends.

If there is no true expression at the end of the if/else if chain, no code blocks are executed.

Types of Operator in Python

In Python, operators are special symbols that perform arithmetic or logical computations.

The operand is the value on which the operator operates. To compare values, there are three logical operators available.

They convert expressions into Boolean values and return True or False.

An operator is a character in mathematics and computer programming that represents an action, such as the arithmetic operator x, which symbolises multiplication.

The Boolean operators, one of the most well-known sets of operators, are used in computer programs to operate with true/false data.

Using this program we can execute Calculator Program in Python Using While Loop.

return x + y
def subtraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
return x / y
print("Select operation")
print("2.subtraction")
print("3.multiplication")
print("4.division")
while True:
choice = input("Enter choice(1/2/3/4)")
if choice in ('1', '2', '3', '4'):
a = float(input("Enter first number"))
b = float(input("Enter second number"))
if choice == '1':
print(a, "+", b, "=", addition(a, b))
elif choice == '2':
print(a, "-", b, "=", subtraction(a, b))
elif choice == '3':
print(a, "*", b, "=", multiplication(a, b))
elif choice == '4':
print(a, "/", b, "=", division(a, b))
next_calculation = input("Any Calculations ")
if next_calculation == "no":
break
else:
print("Invalid Input")

Output:

Select operation