Friday, March 2, 2012

First Python Program

In this post we will see how to make a small program with the basics that we learnt till now.

As usual we will open up the IDLE.

and lets say we want to make a program, which on execution would ask the user his name to type, and would display a nice little message, and also would calculate addition of two numbers that the user wishes.

On IDLE, click on File>>new and before writing the program i always like to save it, but this is not a hard and fast rule that you have to save the file before writing, you can save it after typing the program also.

Note: "#" symbol used in python, means a comment line, so i will use the # symbol to explain each line in the program

======================================
# Program to ask for username and a basic addition program
x = raw_input("Enter your name here: ")
# Note: In the above line x is a variable, and when we want the user to type a string type data(Characters), we use raw_input
print "Hi "+x
# The above line would print Hi name, name is the data that user entered on the first line
# Also note the "+", this is a concatenate operator, this adds a string to a print line
print "Addition of two numbers"
# The above Line prints a simple message, to let the user know, what it will do
y = input("Enter the first number here: ")
# The above Line y is a variable, and when we want the user to enter a integer type data likes numbers, we use the keyword input, so the number entered is stored in the variable y here
z = input("Enter the second number here: ")
# The above Line z is a variable, and when we want the user to enter a integer type data likes numbers, we use the keyword input, so the number entered is stored in the variable z here
add = y+z
# The above line adds the value of y and z, and stores it in variable add
print "The addition of two numbers entered is: ",add
# This Line prints the addition result of the numbers entered
# Note here we used "," instead of "+", guess why?? "+" is used to add string type data, and "," to add an integer type data
# so we are good to go here, the program i have already saved it as test.py, if you have not saved it, save it as test.py, and run the program. If you are not sure how to run it, check the earlier posts about how to run a program here Executing-python-program

========================================

Output on IDLE is:

>>>
Enter your name here: Subir
Hi Subir
Addition of two numbers
Enter the first number here: 20
Enter the second number here: 30
The addition of two numbers entered is:  50
>>>

=========================================

Cool... so we made our first program in python, lets try to execute the test.py by double clicking the file. Before this we will now close the IDLE and Python's text editor

In My case, they are saved in Python27 folder, under C drive, I hope you know where its getting saved so go to that location and run the test.py file

So lets double click on it, we see that the Python Command Line opens up and displays:

Enter your name here: Subir
Hi Subir
Addition of two numbers
Enter the first number here: 20
Enter the second number here: 30


And the moment we pressed enter here after typing 30, wohoo it flashed away... did we do something wrong?? Why did it not show the result??

Here is the explanation:

The python actually displayed the last line, and then baaamm, it flashed away, vanished, as it doesn't have any other things to run or execute after the last print statement.

So we need to use one more command here, and from here on make it a practice, to enter the command on all programs that we do.

The command is raw_input("Press Enter")

So we need to edit the program, open up IDLE, Click on File>>Open>>select "test.py" and click OK

So our final program will be:
=======================================

# Program to ask for username and a basic addition program
x = raw_input("Enter your name here: ")
print "Hi "+x
print "Addition of two numbers"
y = input("Enter the first number here: ")
z = input("Enter the second number here: ")
add = y+z
print "The addition of two numbers entered is: ",add
raw_input("Press Enter")
=======================================

Save it, and then Close the IDLE, and again try to Double Click on the test.py, and let me know what happened... Paste the result in comment box below.

I Hope You understood this and was informative.

Here is a Task for people who are following the Python Journey..

Write a Program, to Ask for Username, and does subtraction of two numbers that the user inputs, and make sure the Python does not exits until the user press the Enter Key

If you can make it, post the program in the comment box below.

Dont forget to check my next post.

Thank You!

7 comments:

  1. Very Nice...............Good Work Bro...

    ReplyDelete
    Replies
    1. Make Sure You Write the Task Given....a nd post it in Comments here.... if you following.. :)

      Delete
  2. Enter your name here: MaFioZi
    Hi MaFioZi
    Addition of two numbers
    Enter the first number here: 54
    Enter the second number here: 21
    The Addition of two numbers entered is 75
    Press Enter


    Thanks brother
    Good Tut

    ReplyDelete
  3. hey , raw_input and input are methods?????

    ReplyDelete
    Replies
    1. raw_input >> To take a String type data from the user
      input >> To take Int type data from the user.

      They are in-built function

      TIP:
      1.

      To know what a keyword is:
      >>> type(input) <-- Replace input with anything you want to check or know about in future

      2.

      To get an instant help about usage:
      >>> help(input) <-- Replace input with anything you want to check or know about in future

      Thanks

      Delete
  4. Program
    -----------------------------------------------------------------------
    # homework 1
    a=raw_input("Enter your fake name :D >>>") # i wanna ask if there is a way that we can take the input in a new line, i mean like we do in c as "\n"
    print "Hi "+a
    print "Hi",a #gettin correct o/p here too! y so???
    print "Let's add two no"
    b=input("Enter d frst no:")
    c=input("Enter the 2nd mo:")
    c=b+c
    print "The sum is=",c
    raw_input("Press any key to quit(if u r in command shell)")
    -----------------------------------------------------------------------
    -----------------------------------------------------------------------

    Output:
    -----------------------------------------------------------------------
    Enter your fake name :D >>>Lucas Das
    Hi Lucas Das
    Hi Lucas Das
    Let's add two no
    Enter d frst no:23
    Enter the 2nd mo:36
    The sum is= 59
    Press any key to quit(if u r in command shell)
    -----------------------------------------------------------------------
    My questions are
    1>i wanna ask if there is a way that we can take the input in a new line, i mean like we do in c as "\n"
    2>y m gettin correct o/p wid print ["hi",a] too
    BDW thnk u for such a nice endevour :D

    ReplyDelete