Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Add Two Numbers (Sum/Addition Program)
Python is widely used for various applications, ranging from web development to data analysis. One of the fundamental operations in programming is performing arithmetic calculations. Here, we will explore how to write a Python program to add two numbers.
This program serves as an excellent starting point for beginners (like the Hello World program in Python) to grasp the basic syntax and structure of the language. It showcases how Python's simplicity and readability allow for concise yet powerful code.
By understanding the program for the addition of two numbers in Python, you can build a solid foundation to tackle more complex tasks and further explore the capabilities of Python.
Throughout this tutorial, we'll walk you through step-by-step instructions, explaining each part of the addition program in Python, and providing examples to demonstrate its execution.
So let's dive in and explore how this programming language handles arithmetic operations through a simple program to add two numbers in Python!
How to Add Two Numbers in Python?
Code
# Write a Python program to add two numbers
num1 = 10
num2 = 5
# Add two numbers
sum = num1 + num2
# Display the sum
print("The sum of", num1, "and", num2, "is:", sum)
Output
The sum of 10 and 5 is: 15
Explanation
- In this program, we have two variables num1 and num2 initialized with the values 10 and 5, respectively. We then add these two numbers together and store the result in the variable sum.
- Finally, we use the print() function to display the sum on the console, Python compiler or output window.
- You can modify the values of num1 and num2 to add different numbers.
- When you run the program, it will calculate the sum of the two numbers and display the result on the console or output window.
Python Program to Add Two Numbers With User Input
Code
# Program to add two numbers in Python with input from the user
# Get input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Add the two numbers
sum = num1 + num2
# Display the sum
print("The sum of", num1, "and", num2, "is:", sum)
Output
Enter the first number: 5
Enter the second number: 20
The sum of 5 and 20 is: 25
Explanation
- In this program, we use the input() function to prompt the user to enter two numbers. The float() function is used to convert the input into floating-point numbers (decimals) to handle both integer and decimal inputs.
- After obtaining the user's input, we add two numbers in Python together and store the result in the variable sum. Finally, we use the print() function to display the sum on the console or output window.
- When you run this program, it will prompt you to enter the first number and then the second number. After you provide the inputs, it will calculate the sum and display the result.
Addition of Two Numbers in Python Without Additional Variables
Addition of Two Numbers in Python Without Additional Variables
Code
To add two numbers without using any additional variables, you can perform the addition directly within the print() function.
# Sum of two numbers in Python Without Additional Variables
print("The sum is:", float(input("Enter the first number: ")) + float(input("Enter the second number: ")))
Output
Enter the first number: 9
Enter the second number: 9
The sum is: 18.0
Explanation
In this program, the input() function is used to receive user input for the first and second numbers. The float() function converts the input to floating-point numbers (decimals) to handle both integer and decimal inputs.
The addition of the two numbers is performed within the print() function itself, using the + operator. The result of the addition is immediately passed as an argument to print(), which displays the sum on the console or output window.
When you run this program, it will prompt you to enter the first number, followed by the second number. After you provide the inputs, it will calculate the sum and display it directly using the print() statement.
Add Two Numbers Using Class
# Python program to add two numbers using class
Code
# Python program to add two numbers using class
class Calculator:
def add(self, num1, num2):
return num1 + num2
# Create an instance of the Calculator class
calculator = Calculator()
# Get input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Call the add() method of the Calculator instance
sum = calculator.add(num1, num2)
# Display the sum
print("The sum of", num1, "and", num2, "is:", sum)
Output
Enter the first number: 4
Enter the second number: 99
The sum of 4.0 and 99.0 is: 103.0
Explanation
In this program, we define a class called Calculator that has an add() method. The add() method takes two numbers as input, adds them together, and returns the sum.
We create an instance of the Calculator class using the line calculator = Calculator(). This allows us to use the add() method defined within the class.
We then prompt the user to enter two numbers using the input() function and store them in the variables num1 and num2.
Next, we call the add() method of the calculator instance, passing in num1 and num2 as arguments. This performs the addition of the two numbers and returns the sum, which is stored in the variable sum.
Finally, we use the print() function to display the sum on the console or output window.
Python Program to Add Two Numbers in a List
Code
# Addition program in Python for list
def add_numbers(numbers):
if len(numbers) < 2:
print("List must contain at least two numbers.")
return None
return sum(numbers[:2])
# Example usage
numbers = [10, 5, 8, 3]
result = add_numbers(numbers)
if result is not None:
print("The sum of the first two numbers in the list is:", result)
Output
The sum of the first two numbers in the list is: 15
Explanation
- In this program, we define a function add_numbers that takes a list of numbers as input. The function checks if the list contains at least two numbers.
- If not, it prints an error message and returns None. If the list has two or more numbers, it uses the sum() function to add the first two numbers together and returns the sum.
- In the example section, we create a list called numbers with the values [10, 5, 8, 3]. We then call the add_numbers function, passing in the numbers list. The function calculates the sum of the first two numbers in the list, which are 10 and 5, resulting in 15.
- Finally, we print the result if it is not None, displaying "The sum of the first two numbers in the list is: 15".
You can modify the numbers list with different values or create new lists to add different sets of numbers.
Practice More Python Programs:
- Hello World Program in Python
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program