Computer Science Of Engineering

Simple and practical computer science tutorials, programming guides, and engineering concepts for students and beginners.

Breaking

Sunday, April 26, 2026

Add Two Numbers in Python

Add Two Numbers in Python

This is one of the first and easiest Python programs for beginners.

The goal is to take two numbers from the user and display their sum.


Python Code

# Program to add two numbers

# Taking first number from user
num1 = int(input("Enter first number: "))

# Taking second number from user
num2 = int(input("Enter second number: "))

# Adding both numbers
sum = num1 + num2

# Displaying the result
print("The sum is:", sum)

Step-by-Step Explanation

1. input() Function

num1 = int(input("Enter first number: "))

This line asks the user to enter the first number.

  • input() takes input from keyboard

  • By default, input is taken as string

  • int() converts string into integer

Example:

If user enters 10, Python first sees "10" as text, then int() converts it into number 10.

Same for second number:

num2 = int(input("Enter second number: "))

2. Addition Operation

sum = num1 + num2

This adds both numbers.

Example:

If

num1 = 10
num2 = 20

Then

sum = 30

3. Print Output

print("The sum is:", sum)

This displays the final answer on screen.


Sample Output

Enter first number: 10
Enter second number: 20
The sum is: 30




Another Example

Enter first number: 45
Enter second number: 55
The sum is: 100

Short Version

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", a + b)

This is the same program in shorter form.


Important Note

Do not use:

sum

as variable name in big projects because sum() is already a built-in Python function.

Better to use:

total
answer
result

Example:

result = num1 + num2

Viva Question Answer

Q: Why do we use int()?

Because input() takes data as string, and for mathematical operations we need integer values.

Q: What does print() do?

It displays output on the screen.

Q: What is + operator?

It is used for addition.


 

No comments:

Post a Comment