PYTHON PROGRAMMING (2010)

Pythons Basic Syntax

Python sample program-

$ Python

Python 2.4.3 (#1, Nov 11 2010, 13:34:43)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2

Type"help","copyright","credits" or"license" for more information

≫≫

Following should be written on the command prompt and the enter key should be pressed-

>>> print "Hello, Python!";

Output

Hello, Python!

Identifiers

Here are following identifiers conventions declared for python.

·        All the identifiers are in lower case except the class name which would start with an upper case.

·        Identifiers are used for privacy, starting with a single leading underscore.

·        If two leading underscore are contained then it contains a strong privacy.

·        If it ends with two different underscores then the language is defined special name.

Lines and Indentation

Various spaces in indentation are variables, and almost whole statement must be inside the block must be indented the similar amount.

Example

if True:

print "True"

else:

print "False"

Second block that would produce an error is as follows.

if True:

print "Answer"

print "True"

else:

print "Answer"

print "False"

Following is an example for blocks with various statements.

#!/usr/bin/python

import sys

try:

# open file stream

file = open(file_name, "w")

except IOError:

print "There was an error writing to", file_name

sys.exit()

print "Enter '", file_finish,

print "' When finished"

while file_text != file_finish:

file_text = raw_input("Enter text: ")

if file_text == file_finish:

# close the file

file.close

break

file.write(file_text)

file.write("\n")

file.close

file_name = raw_input("Enter filename: ")

if len(file_name) == 0:

print "Next time please enter something"

sys.exit()

try:

file = open(file_name, "r")

except IOError:

print "There was an error reading file"

sys.exit()

file_text = file.read()

file.close()

print file_text

Command line argument

Basic information’s are provided through this so that one can know how to run these programs.

Example

$ python -h

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...

Options and arguments (and corresponding environment variables):

-c cmd : program passed in as string (terminates option list)

-d : debug output from parser (also PYTHONDEBUG=x)

-E : ignore environment variables (such as PYTHONPATH)

-h : print this help message and exit.

Accessing command line argument

It provides a getopt module for accessing a command line.

Syntax

$ python test.py arg1 arg2 arg3

There are two purpose of it which is as follow.

·        List of command line arguments are provided through sys.argv.

·        And the total number of command line is Len (sys.argv).

Example

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'

print 'Argument List:', str(sys.argv)

Next step is to run above script as follows.

$ python test.py arg1 arg2 arg3

Output

Number of arguments: 4 arguments.

Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

Image