Home arrow Python arrow Page 4 - Python 101 (part 1): Snake Eyes

Dissecting A Python...Program - Python

Known for its power, flexibility and elegance - much like its namesake - Python is one of the most interesting programming languages around. In this introductory tutorial, find out how learning Python will earn you respect, affection and bags of money. Oh yeah...you might also learn a little bit about the origins, features and syntactical rules of the language.

TABLE OF CONTENTS:
  1. Python 101 (part 1): Snake Eyes
  2. The Jedi Master Speaks
  3. Start It Up
  4. Dissecting A Python...Program
  5. Milk And Toast And Honey
  6. Adding Things Up
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 15
May 29, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 >>>


 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: