Python Program to Add Two Numbers
In Python, adding two numbers is very simple using the '+' operator. You can either assign values directly or take input from the user.
This program demonstrates how to take user input and calculate the sum of two numbers.
Method: Using User Input
1. Take input from the user using input() function.
2. Convert the input values into integers or floats.
3. Use the '+' operator to add the numbers.
4. Display the result using print() function.
Python Program to Add Two Numbers
# Take input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Add two numbers
sum = num1 + num2
# Display the result
print("Sum =", sum)
Sample Input: Enter first number: 10 Enter second number: 15 Output: Sum = 25.0
Explanation
1. The input() function is used to take user input.
2. The float() function converts the input string into a number.
3. The '+' operator performs the addition.
4. The result is printed using the print() function.
Advantages
Simple and beginner-friendly example.
Demonstrates use of input, type conversion, and arithmetic operators.
Helps understand basic Python syntax.
Conclusion
Adding two numbers in Python is easy using the '+' operator.
By taking user input and converting it to numeric type, we can perform arithmetic operations efficiently.
Codecrown