# Add Two Numbers in Python

In this blog, we will learn how to add (sum) two numbers in Python or how to find the addition of two numbers in python with and without user input and display it using the print() function. First, let us begin with the sum of two numbers without user input.

## Python Program to Add Two Numbers Without User Input

# Python Program to Add Two Numbers Without User Input

num1 = 15
num2 = 20

sum = num1 + num2

# Display the sum of two numbers
print('The sum of two number is {2}'.format(num1, num2, sum))

#### Output:

The sum of two number is 35

In the above program to find the addition of two numbers in python without user input, the two numbers (15, 20) are stored in the variables num1 & num2. Then the two variables num1 & num2 are added with the help of the arithmetic operator ‘+’ and the result is stored in the variable sum (sum = num1 + num2). You can use different arithmetic operators to divide (/) multiply (*), subtract ( – ), etc instead of the addition (+) Operator to explore more.

## Python Program to Add(Sum) Two Numbers With User Input

# Python Program to Add Two Numbers With User Input

# Taking a number from user & Store as input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# sum of two numbers
sum = float(num1) + float(num2)

# Display the sum of two number
print('The sum of two number is {2}'.format(num1, num2, sum))

#### Output:

Enter first number: 14.5
Enter second number: 8.3
The sum of two number is 22.8

Here, in the above program, we find the addition of two numbers in Python, i.e., the user tells the program which numbers to add. We take the input from the user with the help of the input() function and then save those values separately into two different variables num1 and num2, respectively.

And since the input() function records input as a String, we use the float() function to convert a string into floating numbers to perform our calculation. Then the result is stored in the variable sum. And in the end, we display the results to the user with the print() function.

Programs, where you use basic Mathematical Operators, are very important for you as a beginner because it builds a logical approach in your mind. And in Python, where 99.99% of the times you will be using this logic, this simple and easy program will remind you how important it is.

It’s like those really simple math problems of what’s 2+2 which you solved as kid, that developed your brain and skills to solve more complex problems like trigonometry, Algebra, etc at later stages of your life. And that’s what we’ll be doing here, solving simple problems of mathematical Operations to have a firm grip over the basic concepts of programming and then we gradually increase the complexity of the problems we solve.

## How to Handle Edge Cases?

Handling edge cases in a Python program, especially when dealing with user input, is crucial to ensure the robustness and reliability of the application. Here’s a discussion on how to handle potential errors or edge cases and suggestions for implementing error-checking mechanisms:

1. Validating User Input: Implementing input validation can address invalid user input, such as non-numeric values or unexpected characters. This involves checking the data type or format before calculations, using methods like isdigit() or try-except blocks to catch exceptions.
2. Handling Division by Zero: Use conditional statements and error-handling techniques like try-except blocks to gracefully handle unexpected errors or exceptions, providing informative error messages to the user.
3. Robust Error-Checking Mechanisms: Use conditional statements and error-handling techniques like try-except blocks to gracefully handle unexpected errors or exceptions, providing informative error messages to the user.
4. Providing User Feedback: Clear and informative feedback is crucial in handling edge cases, guiding users on how to correct input or resolve issues.
5. Testing Edge Cases: Test your program with various edge cases, including non-numeric input, zero or negative input, and very large or small numbers, to ensure its robustness and behavior.

By implementing these strategies, you can enhance the resilience of your Python program and ensure that it handles edge cases gracefully, providing a more user-friendly and reliable experience.

Slow and Steady wins the race!

There’s a lot more in Python Programming, Not just addition but more complex problems where you’ll be dealing with strings, arrays, building more complex functions, and a lot more, and we’ll be covering all of that. But first, you need to build a strong foundation, so keep practicing, and don’t lose hope! We’ll see you in the next one!

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions