1.
Python Program to Make a Simple Calculator
Simple Calculator by Making Functions
1. # Program make a simple calculator that can add, subtract, multiply and divide using
functions
2.
3. # This function adds two numbers
4. def add(x, y):
5. return x + y
6.
7. # This function subtracts two numbers
8. def subtract(x, y):
9. return x - y
10.
11. # This function multiplies two numbers
12. def multiply(x, y):
13. return x * y
14.
15. # This function divides two numbers
16. def divide(x, y):
17. return x / y
18.
19. print("Select operation.")
20. print("1.Add")
21. print("2.Subtract")
22. print("3.Multiply")
23. print("4.Divide")
24.
25. # Take input from the user
26. choice = input("Enter choice(1/2/3/4):")
27.
28. num1 = int(input("Enter first number: "))
29. num2 = int(input("Enter second number: "))
30. if choice == '1':
31. print(num1,"+",num2,"=", add(num1,num2))
32.
33. elif choice == '2':
34. print(num1,"-",num2,"=", subtract(num1,num2))
35.
36. elif choice == '3':
37. print(num1,"*",num2,"=", multiply(num1,num2))
38.
39. elif choice == '4':
40. print(num1,"/",num2,"=", divide(num1,num2))
41. else:
42. print("Invalid input")
43.
44. Output
45. Select operation.
46. 1.Add
47. 2.Subtract
48. 3.Multiply
49. 4.Divide
50. Enter choice(1/2/3/4): 3
51. Enter first number: 15
52. Enter second number: 14
53. 15 * 14 = 210
2. Program for Celsius To Fahrenheit conversion
Formula for converting Celsius scale to Fahrenheit scale
T(°F) = T(°C) × 9/5 + 32
# Python code to convert Celsius scale
# to Fahrenheit scale
def Cel_To_Fah(n):
# Used the formula
return (n*1.8)+32
# Driver Code
n = 20
print(int(Cel_To_Fah(n)))
3. Python Program to Replace all Occurrences of ‘a’ with $ in
a String
This is a Python Program to replace all occurrences of ‘a’ with ‘$’ in a string.
Problem Description
The program takes a string and replaces all occurrences of ‘a’ with ‘$’.
Problem Solution
1. Take a string and store it in a variable.
2. Using the replace function, replace all occurrences of ‘a’ and ‘A’ with ‘$’ and store it
back in the variable.
3. Print the modified string.
4. Exit.
Program/Source Code
Here is source code of the Python Program to replace all occurrences of ‘a’ with ‘$’ in a
string. The program output is also shown below.
string=input("Enter string:")
string=string.replace('a','$')
string=string.replace('A','$')
print("Modified string:")
print(string)
Program Explanation
1. User must enter the string and store it in a variable.
2. The replace function replaces all occurrences of ‘a’ and ‘A’ with ‘$’ and store it back in
the variable.
3. The modified string is printed
Runtime Test Cases
Case 1:
Enter string:Apple
Modified string:
$pple
Case 2:
Enter string:Asia
Modified string:
$si$