[pythonvis] Re: print"Hello world."

  • From: James Scholes <james@xxxxxxxxxxxxx>
  • To: pythonvis@xxxxxxxxxxxxx
  • Date: Mon, 28 Apr 2014 23:23:45 +0100

To answer your questions about Python and how it interacts with
Terminal: Python is what we call an interpreted language, which means
that instead of compiling your entire program into machine code, i.e. an
executable, the Python interpreter runs your program one instruction at
a time.  So, if you tell it to run a file called hello.py, and that file
contains these lines:

print "Hello world!"
print "Goodbye!"

The Python interpreter will run those lines, one after the other until
it reaches the end of the file.  So you will, of course, see:

Hello world!
Goodbye world!

If there are any errors while doing this, the interpreter will pick up
on them and give you some useful information, called a traceback, to
tell you what your program was trying to do when it failed and what
exactly went wrong.  So if we make a typo on the second line and try to
run a program containing:

pritn "Goodbye world!"

You will see:

Hello world!
  File "hello.py", line 2
    pritn "Goodbye world!"
                         ^
SyntaxError: invalid syntax

First, our correctly typed statement is run, so we see our "Hello
world!" message.  But then, because we typed the N and the T in the word
"print" the wrong way round, Python tells us we've made a syntax error.
 It also gives us the line number, the line of code being interpreted
for convenience, and if you can see or be bothered counting the spaces,
a visual representation of exactly where the syntax error was found on
the line of code.  We're also told that the offending code is contained
in hello.py, which is invaluable information when you are writing
programs with many different code files.

The Python interpreter, on most systems, is invoked simply by typing
"python" at the command line (or Terminal).  This is a program, just
like any other, so on Windows it must be installed.  On Mac OS X and
most flavours of Linux, a version of the interpreter is preinstalled
which will be sufficient for your learning.  When following the
exercises in Learn Python the Hard Way, most of the time you will run
Python with one argument; the name of the file you want to run.  For
example, "python hello.py".  If you invoke the interpreter with no
arguments, i.e. by simply typing "python", you will get an interactive
prompt which allows you to run lines of Python code instantaniously.
For example:

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello world!"
Hello world!
>>> 10 + 10
20
>>> 14 / 7
2
>>> exit()

Hope that helps.
-- 
James Scholes
http://twitter.com/JamesScholes

Other related posts: