Tuesday, November 24, 2009

Python Tutorials.

Work on Python began in late 1989 by Guido van Rossum, then at CWI (Centrum voor Wiskunde en Informatica, the National Research Institute for Mathematics and Computer Science) in the Netherlands. It was eventually released for public distribution in early 1991.
At the time, van Rossum was a researcher with considerable language design experience with the interpreted language ABC, also developed at CWI, but he was unsatisfied with its ability to be developed into something more. Having used and partially developed a higher-level language like ABC, falling back to C was not an attractive possibility. Some of the tools he envisioned were for performing general system administration tasks, so he also wanted access to the power of system calls that were available through the Amoeba distributed operating system. Although van Rossum gave some thought to an Amoeba-specific language, a generalized language made more sense, and late in 1989, the seeds of Python were sown.

Python buzzword –
High level
Object oriented
Scalable
Extensible
Portable
Easy to learn
Easy to read
Easy to maintain
Robust
Effective as rapid prototype tool
A memory Manger
Interpreted and (byte) compiled

Command-Line Options
When starting Python from the command-line, additional options may be provided to the interpreter. Here are some of the options to choose from:

-d Provide debug output
-O Generate optimized bytecode (resulting in .pyo files)
-S Do not run importsite to look for Python paths on startup
-v Verbose output (detailed trace on import statements)
-m mod run (library) module as a script
-Q opt division options (see documentation)
-c cmd Run Python script sent in as cmd string
file Run Python script from given file (see later)

>>>(primary prompt)
……(secondary prompt)
Other Implementation

1.Cpython
2.jpython
3.IronPython
4.Stackless


Functions that do not explicitly return a value by the programmer automatically return None, Python's equivalent to NULL


You will notice two primary ways that Python "does things" for you: statements and expressions (functions, equations, etc.). Most of you already know the difference between the two, but in case you need to review, a statement is a body of control which involves using keywords. It is similar to issuing a command to the interpreter. You ask Python to do something for you, and it will do it. Statements may or may not lead to a result or output. Let us use the print statement for the programmer's perennial first example, Hello World:
>>> print 'Hello World!'
Hello World!
Expressions, on the other hand, do not use keywords. They can be simple equations that you use with mathematical operators, or can be functions which are called with parentheses. They may or may not take input, and they may or may not return a (meaningful) value. (Functions that do not explicitly return a value by the programmer automatically return None, Python's equivalent to NULL.) An example of a function that takes input and has a return value is the abs() function, which takes a number and returns its absolute value is:
>>> abs(4)
4
>>> abs(-4)
4

The underscore (_) also has special meaning in the interactive interpreter: the last evaluated expression. So after the code above has executed, _ will contain the string:
>>> _
Hello World!

Python's print statement, paired with the string format operator ( % ), supports string substitution, much like the printf() function in C:
>>> print "%s is number %d!" % ("Python", 1)
Python is number 1!

The print statement also allows its output directed to a file. This feature was added way back in Python 2.0. The >> symbols are used to redirect the output, as in this example with standard error:
The print statement also allows its output directed to a file. This feature was added way back in Python 2.0. The >> symbols are used to redirect the output, as in this example with standard error:

import sys
print >> sys.stderr, 'Fatal error: invalid input!'

Here is the same example with a logfile:
logfile = open('/tmp/mylog.txt', 'a')
print >> logfile, 'Fatal error: invalid input!'
logfile.close()

Program Input and the raw_input()Built-in Function

The easiest way to obtain user input from the command line is with the raw_input() built-in function. It reads from standard input and assigns the string value to the variable you designate. You can use the int() built-in function to convert any numeric input string to an integer representation.
>>> user = raw_input('Enter login name: ')
Enter login name: root
>>> print 'Your login is:', user
Your login is: root

Comments
As with most scripting and Unix-shell languages, the hash or pound ( # ) sign signals that a comment begins from the # and continues until the end of the line.
>>> # one comment
... print 'Hello World!' # another comment
Hello World!

There are special comments called documentation strings, or "doc strings" for short. You can add a "comment" at the beginning of a module, class, or function string that serves as a doc string, a feature familiar to Java programmers:
def foo():
"This is a doc string."
return True

Unlike regular comments, however, doc strings can be accessed at runtime and be used to automatically generate documentation.

No comments:

Post a Comment