Showing posts with label output. Show all posts
Showing posts with label output. Show all posts

Monday, June 20, 2016

Printing without newline

Print automatically adds a new line after printing its input variables and/or constants. To avoid this new line in Python2.6+ you can add a colon "," (without quotes) after the printing command, here is an example
print 'Hello',
print 'World'
In Python3, you can add the option end="". Example:
print('Hello',end="")
print('World')
Remember that from Python2.6+ you also can use the Python3 print command, adding
from __future__ import print_function

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.