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

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

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 *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap