Sunday, June 14, 2015

Addition Program in Python - Python Tutorial

Addition Program in PythonAddition Program in Python


In this tutorial, we are about build a very simple addition program in Python. We will show how to build a simple program that will add two integer numbers that a user gives us via the terminal or command prompt and then we will return the sum of those two numbers to the user. The program will be accessible to download at the end of the tutorial. Just try to follow along the more you practice writing programs in Python the more you will learn.


Building the Addition Python Program


We will be using sublime text as our text editor to build this program. We will be writing our code using Python 3 syntax.  We will run the code via our terminal or command prompt.


Step 1 - Open Text Editor


Open your text editor so we can write the code for our program.


Step 2 - Save your program


Save your program to your desktop as addition.py


Step 3 - Write Welcome Message For Our Program


Here we will use a simple print statement to welcome our users to our program.


#Write Welcome Message For Our Program

print("Welcome to our addition program")

Step 4 - Get Users First Number


In this step, we will get the users first number using input() and then convert the number to an integer using int() and assign a variable to the object that holds the users first number.


#Get Users First Number

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))

Step 5 – Get Users Second Number


In this step, we do the same for the previous step above but we give this user input a different variable.


#Get Users Second Number

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))

Step 6 – Perform The Equation


In this step, we will add the users two numbers together and then assign the sum a variable.


#Perform The Equation

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))
thesum = firstnum + secondnum

Step 7 – Return The Sum To The User


We will return the sum of the user. We will print a string to the user and format the string using a format() method to add our sum into the string.


#Return The Sum To The User

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))
thesum = firstnum + secondnum
print("Your sum is ".format(thesum))

Step 8 – Run The Program


Open your terminal or command prompt and change into your desktop. To run the program, simply type python3 addition.py and press return or enter. The program will ask you to input a number then press return. The program will again prompt you to add your second number again input your number then press return. Now the program will return your sum to you. This is a very simple program in Python.


If you have any questions about Addition Program in Python please leave a comment below and we will help you out.


Get Source Code From Git

 



11 comments:

  1. Hiii....Thanks for sharing Great information...Nice post....Keep move on....
    Python Training in Hyderabad

    ReplyDelete
  2. advantages of lifi,benefits of lifi, working of lifi,
    lifi technology, lifi in hindi,lifi in urdu, lifi internet,
    light fidelity,lifi speed,
    LiFi Technology ?
    What Is LiFi ? How Does Lifi Work?

    ReplyDelete
  3. Really i found this article more informative, thanks for sharing this article! Also Check here

    Download and install Vidmate App which is the best HD video downloader software available for Android. Get free latest HD movies, songs, and your favorite TV shows

    Vidmate App Download

    Vidmate apk for Android devices

    Vidmate App

    download Vidmate for Windows PC

    download Vidmate for Windows PC Free

    Vidmate Download for Windows 10

    Download Vidmate for iOS

    Download Vidmate for Blackberry

    Vidmate For IOS and Blackberry OS

    ReplyDelete
  4. Tally Course | Tally Institute


    Excellent blog with lots of information. I have to thank for this. Do share more.

    ReplyDelete
  5. Trade Capital Options: Trade245 Offers Customers The Opportunity To Trade A Wide Array Of Assets And Financial Instruments With A 30p Per Lot Commission.

    ReplyDelete
  6. If You Are Looking For Forex Broker? Read This Review And Find Out How Much I've Enjoyed My Experience With ETRADE LOG ON

    ReplyDelete