Sunday, 3 January 2016

Hello World!!!

If you are from a programming background, you already have assumed what is going to be in this segment and if you are not, let me tell you a basic truth, these two words have kicked trillions of neurons. It may seem just two words but it is more powerful than anything in any programming.

You will understand the reason very soon...

So, let's get our hands dirty with python.
  1. As usual, open your favourite terminal (Powershell, Console, KSH, Bash etc.).
  2. If you have Vi editor installed on your system, open it otherwise, check this post Vi Editor.
  3. You should provide a name like hello_world.py while opening the editor. Please check the post on Vi Editor to know more about it.
  4. Type the following line in the editor,
    print "Hello World!!!"
    
  5. Now save the file by pressing Esc + ZZ 
  6. The very next thing you are going to do is to run the program
  7. Once you are back in terminal, type the following,
    python hellow_world.py
    
  8. You are expected to see the following output in terminal,
    Hello World!!!
    
You are amazed at your creativity, right ???
Yes, you are creative and you want to take it to the next level and I am here to help you.

Now check the first paragraph of this article and realize why I said so about 'Hello World'.

So, you have printed some line on the terminal, but you are thinking, how can I print my name on it.
Right  ?

Well, just try around, may be you can get some idea on how to do it. I am leaving this as an readers' exercise.
The output should match with the following,
Hello Palash!!!

You know, Palash is my name, so you should change accordingly or it is good if you want to see your system greeting me !!!
:)





Well with greeting yourself with a message printed on terminal ?
Now, let's try something interactive.

What if, you have thrown your first program to your friends in school and they run it in their machine. May be they won't like it every time showing your name only. They may want to see their name as well.

So, what are you going to do ?
Well, you can do two things,
  1. Create one file for each of your friends and ask them to run their own file.
  2. You can ask your friend for his name in the program itself and you should take input of their name and print a greeting message.
Many of you are thinking of the first way, I know and it is good. It will give you lot of exposure to vi editor and also some typing experience which you'll cherish later. Try it out and come back once you are done (and bored, may be).







Done ???
Now, let's see how we deal with the same thing in the second way...

Open vi editor with a file name hello_user.py and paste the following lines in it,
person = raw_input('Enter your name: ')
print 'Hello', person, '!!!'

And try to run this file using python command, as follows,
python hello_user.py

Now let's see how it works,

With only a single program, you can amaze many of your friends.

Cool !!!
Isn't it.

Well, many of you might have thought, why the file extension is set as .py.

Well, you can give it any extension you want as long as the content is something that python understands or in that condition even you can omit the extension but as a convention, we love to give it .py extension.

Following is a snap from my system with different extension of same python program,

You should notice that, in each case the same output is shown. Still, we'll be using .py extension throughout the course and may be later on.

I've recorded all the screen-cast sessions to make it more convenient for you while going through this post. You can check out if you want.



Well, that was it, I know your curiosity level has grown and you want to do more. But before going any further, you must do other alternatives and also should come up with other variations of these two programs. Practice this very basic concept, get hands dirty on vi editor and python interpreter, the print statement. We'll come back with new concepts.

2 comments:

  1. Replies
    1. All programs are run and tested properly with all the poasible way before posting on blog. Please point me what error you are getting.

      Delete