Program to Find Sum of 2 Numbers in Python: Easy Code Example

Python, a popular high-level programming language, is renowned for its simplicity and readability. One of the most fundamental tasks in any programming language is mathematical operations, such as finding the sum of two numbers. Let's explore how to accomplish this in Python.

Sum of Two Numbers in Python or Adding Two Numbers
Sum of Two Numbers in Python or Adding Two Numbers

Python provides a straightforward way to perform this task, using its arithmetic operators. We'll discuss two popular methods: using variables and input() function, and using the format() method.

DAY 4 | Python Loops: for Loop & while Loop
DAY 4 | Python Loops: for Loop & while Loop

Using Variables and input() Function

The first method involves storing the numbers in variables and using the input() function to get user input.

the sum all numbers in a list python
the sum all numbers in a list python

Here's a simple Python program demonstrating this method:

```python num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) sum = num1 + num2 print("The sum of the numbers is", sum) ```

Variable Initialization

Sieve of Eratosthenes in Python (Efficient Prime Algorithm)
Sieve of Eratosthenes in Python (Efficient Prime Algorithm)

In the code above, the input() function reads a line from input (usually user) and returns it as a string. The int() function converts this string to an integer. These numbers are then stored in the variables num1 and num2.

To demonstrate, you can run this code and input the numbers 5 and 10. The output will be "The sum of the numbers is 15".

Performing the Summation

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

The sum of these numbers is calculated using the addition operator (+) and stored in the variable sum. This sum is then printed to the console using the print() function.

Using the format() Method

The second method uses the formatted string literal (f-string) introduced in Python 3.6. f-strings offer a concise way to embed expressions inside string literals, using curly braces {}.

an image of a programming program with the text, python program to find square root of a number
an image of a programming program with the text, python program to find square root of a number

Here's how to use this method:

```python num1 = 5 num2 = 10 print(f"The sum of {num1} and {num2} is {num1 + num2}") ```

Embedding Expressions

a table with numbers and symbols on it that says, number patterns programs in python
a table with numbers and symbols on it that says, number patterns programs in python
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
Important Methods in Python Part-II
Important Methods in Python Part-II
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Python Programming And Numerical Methods: A Guide For Engineers And Scientists
Python Programming And Numerical Methods: A Guide For Engineers And Scientists
the top 10 python tricks every beginer should know
the top 10 python tricks every beginer should know
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
an image of a computer screen with the text python program to check whether a given number is even odd using recursion
an image of a computer screen with the text python program to check whether a given number is even odd using recursion
Python Numbers Types with Examples
Python Numbers Types with Examples
Day 3 – Operators | 10 Days Python Challenge
Day 3 – Operators | 10 Days Python Challenge
Python if Example Even Odd - EasyCodeBook.com
Python if Example Even Odd - EasyCodeBook.com
Simple, Quick, and Fun Number Guessing Python Game for Beginners
Simple, Quick, and Fun Number Guessing Python Game for Beginners
Python Grade Calculator for Students 🎓
Python Grade Calculator for Students 🎓
an info sheet with the words 10 python one - liners that will blow your mind
an info sheet with the words 10 python one - liners that will blow your mind
Python Lists Explained: Complete Beginner Cheat Sheet
Python Lists Explained: Complete Beginner Cheat Sheet
Smart Home Gadgets You Must Try
Smart Home Gadgets You Must Try
Python Program to output Calendar for a month - EasyCodeBook.com
Python Program to output Calendar for a month - EasyCodeBook.com
Fundamentals of Python Programming - Free download book (pdf, epub)
Fundamentals of Python Programming - Free download book (pdf, epub)
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
| Pythonista Planet
| Pythonista Planet

In the code above, the variables num1 and num2 are embedded directly into the string using {}. The sum of these numbers is also calculated within the string literal and displayed.

When you run this code, it will output "The sum of 5 and 10 is 15".

Advantages of f-strings

f-strings are more readable and easier to format than the older string formatting techniques. They also automatically convert the type to string when you embed expressions, avoiding type errors.

Python's simplicity makes it an excellent choice for learning programming and performing mathematical tasks like finding the sum of two numbers. Understanding these methods will help you create efficient, readable code in any context.

Embark on your learning journey today and explore the endless possibilities that Python offers.

Related Articles

Python Dump Json To File Pretty Send Python Code In Email Python Json Save Pretty Speech Recognition Python Raspberry Pi Python Code To Call Rest Api With Basic Authentication Python Excel Example Code Xgboost Python Code Example Sample Json File For Python Simple Python Game Code Without Pygame How Python Is Used In Machine Learning