BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Add Two Numbers

Last Updated: January 3, 2018 by Chaitanya Singh | Filed Under: Python Examples

In this post we will see how to add two numbers in Python. In the first program we have the values assigned to the numbers that we are going to add. In the second program we are adding the numbers entered by user.

Example: Adding two numbers in Python

Here we have hardcoded the values of two numbers in the source code.

# two float values
val1 = 100.99
val2 = 76.15

# Adding the two given numbers
sum = float(val1) + float(val2)

# Displaying the addition result
print("The sum of given numbers is: ", sum)

Output:
Python Adding Two numbers

Example 2: Adding the numbers entered by User

Here we are taking the values from user and then performing the addition on the input numbers. The reason we are using the float() function over the input() function is because the input() function receives the value as String, so to convert it into a number we are using the float().

# Getting the values of two numbers from user
val1 = float(input("Enter first number: "))
val2 = float(input("Enter second number: "))

# Adding the numbers
sum = val1 + val2

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

Output:
Python two input numbers Addition

Related Posts:

  1. Java Program to Add two numbers
  2. C Program to add two numbers
  3. C++ Program to add two numbers
  4. Python Program to print Hello World
  5. Get Input from User in Python

Top Related Articles:

  1. Python Program to Convert Decimal to Hexadecimal
  2. Python Program to Convert Celsius To Fahrenheit and Vice Versa
  3. Python Program to Add Digits of a Number
  4. Python Program to Check if a Number is Positive Negative or Zero
  5. Python Program to Add Subtract Multiply and Divide two numbers

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. jyothi prakash reddy says

    September 15, 2019 at 4:00 AM

    #Adding using Python lambda
    x=(lambda a,b:a+b)
    print(x(5,10))

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap