Python 101 (part 1): Snake Eyes - Dissecting A Python...Program (
Page 4 of 6 )
Let's take
a closer look at the script above. The first line
#!/usr/bin/python
is used to indicate the location of the Python binary. As I've just explained,
this line must be included in each and every Python script, and its omission is
a common cause of heartache for novice programmers. Make it a habit to include
it, and you'll live a healthier, happier life.
Next up, we have a comment.
# eyes.py - print a line of output
Comments in Python are preceded by a hash (#) symbol. If you're planning to make
your code publicly available on the Internet, a comment is a great way to tell
members of the opposite sex all about yourself - try including your phone number
for optimum results. And, of course, it makes it easier for other developers (not
to mention you, circa 2010) to read and understand your code.
And finally, the meat of the script:
print "Snake Eyes"
In Python, a line of code like the one above is called a "statement". Every Python
program is a collection of statements, and a statement usually contains instructions
to be carried out by the Python interpreter. In this particular statement, the
print() function has been used to send a line of text to the screen. Like all
programming languages, Python comes with a set of built-in functions - the print()
function is one you'll be seeing a lot of in the future. The text to be printed
is included within single or double quotes.
Just as you print text, you can also print numbers...
Python 1.5.2 (#1, Aug 25 2000, 09:33:37) [GCC 2.96 20000731
(experimental)]
on
linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>
print 4
4
>>> print 3443634
3443634
>>>
...or the results of numeric calculations.
>>> print 24+1
25
>>> print 12*10
120
>>>
Although every Python statement usually begins on a new line, a single statement
can be split across lines with a backslash (). It is not necessary to end each
statement with a semicolon (;), as in Perl and PHP.
>>> print
... "Snake Ey
... es"
Snake Eyes
>>>