Wednesday, March 30, 2016

Hello World! (console printing and executable)

Of course the first post of a blog about programming in Python have to be hello world in Python :)
This is the file helloworld.py
#!/usr/bin/env python
print "Hello World"
to execute this file
$python helloworld.py 
Two elements of the helloworld.py script. First, the command print, this command write to the standard output the parameters given. These parameters can be variables, objects (as we are going to discuss in a later post), or as in this case constants. The second element is the first line starting with the charcarter '#'. In Python every character in a line after the symbol '#' is considered as a comment (python interpreter will ignore this). Particularly in our example we have the following comment
#!/usr/bin/env python
This comment is not mandatory, however, it is used if we want to run helloworld.py as an standalone executable. First, we can give execution rights to the file helloworld.py using chmod
$chmod +x helloworld.py 
From now on, we only have to type,
$./helloworld.py 
to execute the helloworld.py.

No comments:

Post a Comment