Way 1 – Adding two Hard-coded numbers
number1 = 5
number2 = 10
#adding two numbers
sum = number1 + number2
#printing sum
print("Sum =",sum)
In this program, we declare two variables number1 and number2 and assign them the hard-coded values of 5 and 10. Then, we add the two numbers together and store the result in a sum variable. At last, we print out the sum using the print function.
You can modify the hard-coded values of number1 and number2 to add any other numbers you desire.
Way 2 – Adding two using input function
number1 = input("Please enter first number : ")
number2 = input("Please enter second number : ")
#adding two numbers
sum = float(number1) + float(number2)
#printing sum
print("Sum =",sum)
In this program, we use the input function to prompt the user to enter the first number and the second number. Values got saved in number1 and number2 variables.
The float function is used to convert the input values to floating-point numbers, allowing for decimal inputs as well.
Then, we add the two numbers and store the result in the variable sum. But before that, we converted numbers into floats because, by default, input receives a string value.
At last, we printed the sum using the print function.