7 Replies - 3024 Views - Last Post: 23 June 2011 - 10:03 PM Rate Topic: -----

#1 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Problem understanding the hello world program

Posted 14 June 2011 - 02:56 PM

I'm using Lubuntu.

I'm officially new to programming as of today (please be nice)

#!/usr/bin/python
# File : helloworld.py
print 'Hello World'


Am I correct in saying that the first two lines are just comments and python can work without them? The book I'm using stated the function of a shebang line but fails to explain what this particular one means. What does this shebang mean or do?

Lastly, when I type in the terminal 'python helloworld.py' the program appears. However, I move the program from the home folder to another folder and I can't open it. How do I tell the terminal to open this program when it's located in a different folder?

Thank you so much

This post has been edited by monster92: 14 June 2011 - 02:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem understanding the hello world program

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Problem understanding the hello world program

Posted 14 June 2011 - 03:01 PM

The first line tells the computer where the interpreter is located. This is useful if you have multiple versions of python installed. It will work fine without the comment, it'll just use your default version of python.

The second line is just a comment. Python will work fine without it.

The reason it only works in the 1 folder is because you're not navigating to the file in your terminal. if your python file is in ~/programming, then do this:

cd ~/programming
python helloworld.py

or you can just use the full address of the file:

python ~/programming/helloworld.py

I prefer the second method.

By the way, welcome to DIC :D

This post has been edited by atraub: 14 June 2011 - 03:25 PM

Was This Post Helpful? 2
  • +
  • -

#3 danicloud  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 27-May 11

Re: Problem understanding the hello world program

Posted 14 June 2011 - 10:04 PM

http://en.wikipedia..../Shebang_(Unix)

good information

This post has been edited by danicloud: 14 June 2011 - 10:04 PM

Was This Post Helpful? 1
  • +
  • -

#4 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

Re: Problem understanding the hello world program

Posted 15 June 2011 - 05:07 AM

View Postmonster92, on 14 June 2011 - 02:56 PM, said:

I'm using Lubuntu.

I'm officially new to programming as of today (please be nice)

#!/usr/bin/python
# File : helloworld.py
print 'Hello World'


Am I correct in saying that the first two lines are just comments and python can work without them? The book I'm using stated the function of a shebang line but fails to explain what this particular one means. What does this shebang mean or do?

Lastly, when I type in the terminal 'python helloworld.py' the program appears. However, I move the program from the home folder to another folder and I can't open it. How do I tell the terminal to open this program when it's located in a different folder?

Thank you so much


You have it right. Everything after # is commented out and will not be read by the interpreter.

The print function evaluates a string expression enclosed in " ", or ' '. print 'hello world' tells the built in print function to output any expression enclosed in " " or ' '.

This means your program will work without those two comments above. Furthermore you could even do something like
print 'hello' + 'goodbye'

or

raw_input("Enter something: ")

raw_input will evaluate anything you enter, and then print it in the terminal. If you are going to learn how to print to the screen (output function), why not learn how to capture user input (input function)?
Was This Post Helpful? 0
  • +
  • -

#5 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

Re: Problem understanding the hello world program

Posted 15 June 2011 - 05:28 AM

To make it an executable, which may resolve your problem try this in the terminal.
$chmod +x helloworld.py



Then you need to make a bash script. http://www.dreaminco...nux-executable/
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Problem understanding the hello world program

Posted 15 June 2011 - 06:52 AM

Writing a bash script to execute python code? That's completely unnecessary. Reread that post more carefully E-Thug.
Was This Post Helpful? 1
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Problem understanding the hello world program

Posted 15 June 2011 - 09:43 AM

I don't know if there's any merit doing it one way or the other: python myScript.py will run valid python code whether or not the "shebang line" is present, given the path is set up and working correctly.

If you give yourself permission to execute the file i.e.: chmod +x myScript.py you can run a python file using ./myScript.py without invoking python explicitly, only if the "shebang" line is there, this is because the python path is already present in the "shebang" line. I personally like to run my scripts using python where available but this method has it's merits for example, if you wanted to easily run two scripts with different versions of Python.


With a shebang line:
#!/usr/bin/python
#File: helloWorld.py
print "Hello World!"



Unix terminal
$ python helloWorld.py
...
Hello World!
$ chmod +x helloWorld.py
$ ./helloWorld.py
...
Hello World!



Without a shebang line:
#File: helloWorld.py
print "Hello World!"



Unix terminal
$ python helloWorld.py
...
Hello World!
$ chmod +x helloWorld.py
$ ./helloWorld.py
...
Warning: unknown mime-type for "Hello World!" -- using "application/octet-stream"
Error: no such file "Hello World!"



To reiterate what atraub said, a executable bash script is not needed to run python scripts.

This post has been edited by Simown: 15 June 2011 - 09:43 AM

Was This Post Helpful? 1
  • +
  • -

#8 milawynsrealm  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 23
  • Joined: 12-May 11

Re: Problem understanding the hello world program

Posted 23 June 2011 - 10:03 PM

Just for future reference, if you use Python 3 or later, the syntax is different and is not backwards compatible (I don't know all the details as I know only the basics of the language). For example:

#Python 2 Example
print 'Hello World'

#Python 3 Example
print("Hello World")



If you try to use the first example in Python 3, you will get a syntax error pointing to the quotes you made. And apparently there are other changes to the syntax as well, but that is beyond my current level of experience as I am still learning Python.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1