Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Input and Output

Input two numbers and print their sum - Python Program

Example 1:
# Prompt the user to enter the first number a = int(input("Enter first number: ")) # Prompt the user to enter the second number b = int(input("Enter second number: ")) # Calculate the sum print("Sum =", a + b)

Output

 
OUTPUT  :
Enter first number: 10
Enter second number: 20
Sum = 30
Example 2:
# Prompt the user to enter the first number num1_str = input("Enter the first number: ") # Prompt the user to enter the second number num2_str = input("Enter the second number: ") # Convert the input strings to integers (or floats if decimal numbers are expected) # Using int() for integers, float() for decimal numbers try: num1 = float(num1_str) # Use float for broader compatibility with numbers num2 = float(num2_str) except ValueError: print("Invalid input. Please enter valid numbers.") else: # Calculate the sum sum_of_numbers = num1 + num2 # Print the sum print(f"The sum of {num1} and {num2} is: {sum_of_numbers}")

Output

 
OUTPUT  :
Enter the first number: 12
Enter the second number: 11
The sum of 12.0 and 11.0 is: 23.0