Sum of Two Numbers In Python or Adding Two Numbers

This article is an easy way to Print the Sum of Two Numbers In Python. This code will dynamically sum the 2 given numbers when the user clicks on the calculate button.

How To Print Sum Of Two Numbers using Python: Project Information

Project Name:How To Print Sum Of Two Numbers Python Project
Language/s Used:Python (GUI) Based
Python version (Recommended):2.x or 3.x
Database:None
Type:Python App
Developer:IT SOURCECODE
Updates:0
How To Print Sum Of Two Numbers using Python – Project Information

The code uses the Tkinter module to create a layout and widgets that may call specific Python functions.

About The Project

We will be the usage of Python programming language because it’s widely used as an advanced-degree programming language for a general approach to the developer. This tutorial also includes the downloadable Sum of 2 Numbers In Python source code.

To start creating How To Print Sum Of Two Numbers, make sure that you have Pycharm IDE installed on your computer.

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

How To Add or Sum of Two Numbers In Python With Source Code

How To Print Sum Of Two Numbers With Source Code

  • Step 1: Create a project name.

    First step open Pycharm IDE and click “file.” After that create a project name and then click the “create” button.
    Sum of Two Numbers Create Project

  • Step 2: Create a python file name.

    Second step “right-click” your project name folder and choose new and then click “python file“.
    Sum of Two Numbers Create Python File

  • Step 3: Name your python file.

    Third step name your Python file and click “enter” to start creating on How To Print Sum Of Two Numbers
    Sum of Two Numbers Python File Name

  • Step 4: The actual code.

    The fourth step is the actual coding of How To Print the Sum Of Two Numbers.

Python Program To Add Two Numbers Code Explanations

1. Import Tkinter

The code given below is for importing tkinter modules.

from tkinter import *
from tkinter import messagebox

Importing the tkinter modules.

2. GUI Design

The code given below is for the GUI design.

        def check():



                    self.label1 = Label(self, text="Enter Number 1" ,  fg = 'black', font=("courier-new", 18, 'bold'))
                    self.label1.place(x=280, y=180)
                    self.num1 = Entry(self, textvariable=self.a,bg='light blue', width=45)
                    self.num1.place(x=480, y=190)
                    self.label2 = Label(self, text="Enter Number 2" , fg = 'black', font=("courier-new", 18, 'bold'))
                    self.label2.place(x=280, y=250)
                    self.num2 = Entry(self, textvariable=self.b,bg='light blue', width=45)
                    self.num2.place(x=480, y=255)
                    self.butt = Button(self, text="Calculate",bg ='green', font=10, width=8, command=chex).place(x=580, y=300)
                    self.label3 = Label(self, text="Sum of Two Numbers", fg='black', font=("courier-new", 24, 'bold'))
                    self.label3.place(x=380, y=30)

This module which is the design of this project uses Graphical User Interface (GUI).

Output:

Sum of Two Numbers Output
Sum of Two Numbers Output

3. Calculate Two Numbers

The code given below is for calculating the two numbers.

        def chex():
            if not self.num1.get() and not self.num2.get():
                print("Please enter a number")
            else:
                res = int(self.num1.get()) + int(self.num2.get())
                messagebox.showinfo('Sum of Two Numbers', "The answer is: %d" % (res))
                self.num1.set = ""
                self.num2.set = ""

This module calculates the two input numbers in addition.

Output:

Calculate Two Numbers in Python
Calculate Two Numbers in Python

Complete Source Code

from tkinter import *
from tkinter import messagebox

#creating window
class Lib(Tk):
    def __init__(self):
        super().__init__()
        self.a = StringVar()
        self.b = StringVar()
        self.maxsize(1200, 700)
        self.minsize(1200, 700)
        self.configure(bg="white")
        self.title("Sum of Two Numbers by: IT SOURCECODE")


#verifying input
        def chex():
            if not self.num1.get() and not self.num2.get():
                print("Please enter a number")
            else:
                res = int(self.num1.get()) + int(self.num2.get())
                messagebox.showinfo('Sum of Two Numbers', "The answer is: %d" % (res))
                self.num1.set = ""
                self.num2.set = ""


        def check():



                    self.label1 = Label(self, text="Enter Number 1" ,  fg = 'black', font=("courier-new", 18, 'bold'))
                    self.label1.place(x=280, y=180)
                    self.num1 = Entry(self, textvariable=self.a,bg='light blue', width=45)
                    self.num1.place(x=480, y=190)
                    self.label2 = Label(self, text="Enter Number 2" , fg = 'black', font=("courier-new", 18, 'bold'))
                    self.label2.place(x=280, y=250)
                    self.num2 = Entry(self, textvariable=self.b,bg='light blue', width=45)
                    self.num2.place(x=480, y=255)
                    self.butt = Button(self, text="Calculate",bg ='green', font=10, width=8, command=chex).place(x=580, y=300)
                    self.label3 = Label(self, text="Sum of Two Numbers", fg='black', font=("courier-new", 24, 'bold'))
                    self.label3.place(x=380, y=30)


        check()

Lib().mainloop()

Downloadable Source Code

I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.

Summary

This article How To Print Sum Of Two Numbers In Python is designed in Python programming language, because it’s widely used as an advanced-degree programming language for a general approach to the developer.

This simple project is a way to help beginners or students in declaring modules, variables, and many more, and also help to enhance and develop their logical skills in programming.

Inquiries

If you have any questions or suggestions about How To Print the Sum Of Two Numbers In Python, please feel free to leave a comment below.

Leave a Comment