Calculate and Print sum of Two Numbers in Python

How To Print Sum Of Two Numbers in Python | Python For Beginners

Python is a powerful and flexible programming language that makes it easy to perform various arithmetic operations. In this article, we will discuss how to calculate the sum of two numbers in Python.

The input() function

The input() function is a built-in Python function that allows the user to enter input from the keyboard. It takes a string argument as a prompt, which is displayed on the screen, and waits for the user to enter some text. When the user presses Enter, the input is read and returned as a string.

Example code on How to use the input() function to get user input for two numbers and then calculate their sum

a = input(“Enter the first number: “)
b = input(“Enter the second number: “)

c = int(a) + int(b)

print(“The sum of”, a, “and”, b, “is”, c)

  • In this code, we first prompt the user to enter two numbers using the input() function. The prompt message is passed as a string argument to the function. The user enters the numbers as strings, and we store them in variables a and b.
  • Next, we convert the strings to integers using the int() function. This is necessary because we can’t add two strings together to get their sum. We then add the two integers together and store the result in a variable c.
  • Finally, we use the print() function to display the result. We pass a string argument to the function that contains the text we want to display, along with the values of a, b, and c. We separate the different parts of the string with commas, and Python automatically adds spaces between them.

When you run this code and enter two numbers at the prompts, you should see the sum of the numbers displayed on the screen.

Wrap up on input() function

The input() function is a useful way to get user input in Python. It allows your program to be interactive and respond to user input in real-time. By combining the input() function with the int() function and the + operator, you can easily calculate the sum of two numbers entered by the user.

The format() function

The format() function is a convenient way to format strings in Python. It allows you to insert values into a string, making it more readable and easier to understand. Here’s how you can use the format() function to calculate the sum of two numbers:

First, prompt the user to enter two numbers using the input() function and store them in variables a and b:

a = input(“Enter the first number: “)

b = input(“Enter the second number: “)

Convert the values of a and b to integers using the int() function:

a = int(a)
b = int(b)

Calculate the sum of a and b and store it in a variable c:

c = a + b

Use the format() function to insert the values of a, b, and c into a string:

result = “The sum of {} and {} is {}”.format(a, b, c)

In this example, the placeholders {} are used to indicate where the values of a, b, and c should be inserted. The first placeholder will be replaced by the value of a, the second by the value of b, and the third by the value of c.

Finally, print the result using the print() function:

print(result)

The output of the program will be a string that displays the sum of the two numbers entered by the user.

Example code for calculating the sum of two numbers in Python using the format() function

a = input(“Enter the first number: “)
b = input(“Enter the second number: “)

a = int(a)
b = int(b)

c = a + b

result = “The sum of {} and {} is {}”.format(a, b, c)

print(result)

This code prompts the user to enter two numbers, converts them to integers, calculates their sum, and displays the result using the format() function.

OUTPUT:

The output of this code will depend on the input provided by the user. However, assuming the user enters 3 for the first number and 4 for the second number, the output will be:

The sum of 3 and 4 is 7

Wrap up on format() function

In conclusion, calculating the sum of two numbers in Python is a straightforward process. By using the format() function, you can easily format the output to display the result in a readable and understandable way. We hope this article has been helpful in explaining how to calculate the sum of two numbers in Python.

Use the f-string feature of Python to format the string more easily

a = input(“Enter first number: “)
b = input(“Enter second number: “)

c = int(a) + int(b)

print( f”The sum of {a} and {b} is {c}”)

In this code, we have used f-string to format the string directly. We have used the curly braces {} to enclose the variables, and they are replaced with their values during runtime.