by BehindJava
Write a program to design a calculator using Python
In this tutorial we are learn about designing a calculator using Python.
Calculator program
Method 1:
num_1 = int(input('Enter your first number: '))
num_2 = int(input('Enter your second number: '))
# Addition
print('{} + {} = '.format(num_1, num_2))
print(num_1 + num_2)
# Subtraction
print('{} - {} = '.format(num_1, num_2))
print(num_1 - num_2)
# Multiplication
print('{} * {} = '.format(num_1, num_2))
print(num_1 * num_2)
# Division
print('{} / {} = '.format(num_1, num_2))
print(num_1 / num_2)
# The format() will help out output look descent and formatted.
Including condition statement to make the program as User’s choice
choice = input('''
Please select the type of operation you want to perform:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
num_1 = int(input('Enter your first number: '))
num_2 = int(input('Enter your second number: '))
if choice == '+':
print('{} + {} = '.format(num_1, num_2))
print(num_1 + num_2)
elif choice == '-':
print('{} - {} = '.format(num_1, num_2))
print(num_1 - num_2)
elif choice == '*':
print('{} * {} = '.format(num_1, num_2))
print(num_1 * num_2)
elif choice == '/':
print('{} / {} = '.format(num_1, num_2))
print(num_1 / num_2)
else:
print('Enter a valid operator, please run the program again.')
To display Calender
Here,you need to import the calendar module which comes with Python.
import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))