📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In the realm of programming, one of the fundamental tasks often given to beginners is the addition of two numbers. This task, although basic, provides a foundation to understand variables, data types, user input, and basic output operations.
In this blog post, we will learn how to write a Python program to add two numbers.
2. Program Overview
The Python program that we'll develop will carry out the following steps:
1. Prompt the user to input two numbers.
2. Convert the entered values to integers.
3. Sum the two integers.
4. Display the result to the user.
3. Code Program
# Taking input from the user num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) # Adding the numbers sum = num1 + num2 # Displaying the result print(f"The sum of {num1} and {num2} is {sum}")
Output:
Enter first number: 5.5 Enter second number: 6.5 The sum of 5.5 and 6.5 is 12.0
4. Step By Step Explanation
1. We start by taking input from the user. The input() function returns a string, so we convert this string into a float which can handle both integers and decimal numbers.
2. Next, we add the two numbers using the + operator.
3. Finally, we display the result using Python's formatted string, commonly known as f-string. The f-string provides an easy way to embed expressions inside string literals.
Comments
Post a Comment
Leave Comment