Monday, May 23, 2011

Hello World!!!!

I want to start this post with a funny thought. I don’t know why all teachers use “Hello World!” example as the beginning to any programming languages. Even the author of “Think Python” Allen B. Downey uses the same. Can they just change the words? I think this program has been accepted world wide as the first program of any language.

“Hello World!” In Python looks like this:
print 'Hello World!'

The output will be
Hello world

In Python 3.0, print is a function, not a statement, so the syntax is 
print(’Hello, World!’)

We will discuss about functions soon. Now it’s time for discuss an example given in “Think Python” implementing the use of print statement and mathematical operations. The example is

If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a mile).

It’s a simple program and logic is also simple. Isn’t it?

Logic is, we have to first find distance he ran in one minute in terms of mile (i.e., miles/minute) and then we have to multiply it with 60 (for converting into miles/hr).

I have done this program in python and its screenshot is given below.


Notice that I have to use a comma after the print'The average speed in miles per hour is' statement; otherwise you will get an error like this.


That’s it! My first program is over. Now I want to study more about variables, expressions and statements and will be back soon.

Thanks

AJAY

No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.