TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Program to Find Sum of Two Numbers
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Python Program to Find Sum of Two Numbers

Last updated: Nov 05, 2023 12:06 am
By Meenakshi Agarwal
Share
1 Min Read
Python program - sum of two numbers
Python program - sum of two numbers
SHARE

In this sample program, you will learn to find the sum of two numbers in Python and show it using the print() function.

Contents
Sample Code: Sum of Two Numbers Entered by The UserOther ways to find the sum of two numbersUsing the built-in sum() functionUsing a lambda function

Finding the sum of two numbers is one of the most basic operations in computer programming. It is used in a wide variety of applications, such as arithmetic calculations, financial analysis, and scientific computing.

Introduction

In this extended tutorial, we’ll not only learn how to write a Python program to find the sum of two numbers but also explore various concepts and techniques that can enhance your Python programming skills.

Before we begin, ensure you should have the basic Python programming knowledge:

  • Python Data Types
  • Python Operators

To understand the demo programs, you must have a basic understanding of the Python fundamentals. And, you can grasp it in no time by following the above tutorials.

Firstly, let’s start with the most simple approach.

Sample Code: Sum of Two Numbers Entered by The User

In Python, there are several ways to find the sum of two numbers. The most straightforward way is to use the + operator. The + operator adds two numbers and returns the result.

For example, the following code adds the numbers int1 and int2 and prints the result to the console:

# Find sum of two integer numbers
int1 = input('Enter first integer: ')
int2 = input('Enter second integer: ')

# Add two integer numbers
sum = int(int1) + int(int2)

# Show the sum
print('The sum of {0} and {1} is {2}'.format(int1, int2, sum))

The output of the above code is as follows:

Enter first integer: 11
Enter second integer: 22
The sum of 11 and 22 is 33

You can change the (+) operator with the subtraction (-), multiplication (*), division (/), or the floor division (//) operator to perform different operations on the two numbers.

Other ways to find the sum of two numbers

In addition to the + operator, there are a few other ways in Python. Let’s find out.

Using the built-in sum() function

The sum() function takes an iterable of numbers as input and returns the sum of all the elements in the iterable. For example, the following code finds the sum of the numbers in the list [10, 20, 30] and prints the result to the console:

listofnumbers = [11, 22, 33]

sum = sum(listofnumbers)

print(sum)

# 67

Using a lambda function

A lambda function is a single-line anonymous function in Python. We can have it to perform simple operations, such as finding the sum of two numbers.

For example, the following code defines a lambda function that takes two numbers as input and returns their sum:

sum_of_two_numbers = lambda num1, num2: num1 + num2

This lambda function can then be called in the following manner:

sum = sum_of_two_numbers(10, 20)

print(sum)

# 30

Try executing the above Python programs yourself. However, in order to dive deeper, you must practice with our 40 Python exercises for beginners.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article SDLC V Model SDLC V Model Guide for Beginners
Next Article Python program to swap two numbers Swap Two Numbers Without a Temp in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x