🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment