Arithmetic - Conditional - Python Programming Exercise

This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or division (/). Based on the user's input, the program executes the corresponding mathematical operation and displays the result. For example, if the user enters "5" for the first number, "+" for the operation, and "7" for the second number, the program will calculate "5+7=12." This program uses an if statement to determine which operation to perform, ensuring the correct calculation is executed based on the user's choice. This task highlights the importance of conditional statements in Python, specifically using an if structure to handle different operations. By asking the user for both the numbers and the operation, the program allows for dynamic calculations, providing a simple yet effective example of basic mathematical operations and user interaction. This exercise also showcases how Python handles different arithmetic operations and emphasizes the use of control flow to implement simple calculators.

 Category

Python Data Types

 Exercise

Arithmetic - Conditional

 Objective

Develop a Python program that prompts the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in this example:

Enter the first number: 5
Enter operation: +
Enter the second number: 7
5+7=12

Note: You MUST use "if", not "switch".

 Example Python Exercise

 Copy Python Code
# Prompt the user for the first number, the operation, and the second number
num1 = float(input("Enter the first number: "))
operation = input("Enter operation (+, -, *, x, /): ")
num2 = float(input("Enter the second number: "))

# Perform the operation based on the input
if operation == "+":
    result = num1 + num2
    print(f"{num1} + {num2} = {result}")
elif operation == "-":
    result = num1 - num2
    print(f"{num1} - {num2} = {result}")
elif operation == "*":
    result = num1 * num2
    print(f"{num1} * {num2} = {result}")
elif operation == "x":
    result = num1 * num2
    print(f"{num1} x {num2} = {result}")
elif operation == "/":
    if num2 != 0:
        result = num1 / num2
        print(f"{num1} / {num2} = {result}")
    else:
        print("Error: Cannot divide by zero.")
else:
    print("Invalid operation. Please enter one of +, -, *, x, or /.")

 Output

Case 1:
Enter the first number: 5
Enter operation (+, -, *, x, /): +
Enter the second number: 7
5.0 + 7.0 = 12.0

Case 2:
Enter the first number: 10
Enter operation (+, -, *, x, /): x
Enter the second number: 3
10.0 x 3.0 = 30.0

Case 3:
Enter the first number: 8
Enter operation (+, -, *, x, /): /
Enter the second number: 2
8.0 / 2.0 = 4.0

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Double Value

    This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...

  •  Evaluate Function Outputs

    This Python program displays the values of the function y = x² - 2x + 1 for integer values of x ranging from -10 to +10. The function is a quadratic equation, ...

  •  Show Function

    This Python program "draws" the graph of the function y = (x-4)² by displaying a series of asterisks on the screen. For each x value ranging from -1 to 8, the ...

  •  Floating Point, Speed Units

    This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units:...

  •  Floating Point Sphere

    This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and v...

  •  Vowel Check - Conditional

    This Python program prompts the user to input a symbol and categorizes it as a vowel (if it’s a lowercase vowel), a digit, or any other symbol. T...