Thursday, April 10, 2014

Python Syllabus



Python Syllabus
Core Python

Introduction
  • History
  • Features
  • Setting up path
  • Working with Python
  • Basic Syntax
  • Variable and Data Types
  • Operator
Conditional Statements
  • If
  • If- else
  • Nested if-else

Looping
  • For
  • While
  • Nested loops
Control Statements
  • Break
  • Continue
  • Pass

String Manipulation
  • Accessing Strings
  • Basic Operations
  • String slices
  • Function and Methods

Lists
  • Introduction
  • Accessing list
  • Operations
  • Working with lists
  • Function and Methods
Tuple
  • Introduction
  • Accessing tuples
  • Operations
  • Working
  • Functions and Methods

Dictionaries
  • Introduction
  • Accessing values in dictionaries
  • Working with dictionaries
  • Properties
  • Functions

Functions
  • Defining a function
  • Calling a function
  • Types of functions
  • Function Arguments
  • Anonymous functions
  • Global and local variables

Modules
  • Importing module
  • Math module
  • Random module
  • Packages
  • Composition

Input-Output
  • Printing on screen
  • Reading data from keyboard
  • Opening and closing file
  • Reading and writing files
  • Functions

Exception Handling
  • Exception
  • Exception Handling
  • Except clause
  • Try ??? finally clause
  • User Defined Exceptions
Regular expressions
  • Match function
  • Search function
  • Matching VS Searching
  • Modifiers
  • Patterns

Tuesday, February 14, 2012

How to write a simple CGI script using Python :
step 1: Install apache server in your linux machine:
           for installation : sudo apt-get install --purge apache2 apache2-utils
           if remove :          sudo apt-get remove --purge apache2 apache2-utils
             Now do your typical installation:
             "sudo apt-get install apache2"
step 2:
            First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.

             Second, configure cgi in Apache. There are many ways to run a Python program on a web/http interface. I think CGI is the easiest. Assume you are on your own server and using all default settings. On Ubuntu Linux 9.04, the default configuration file for your default website is /etc/apache2/sites-available/default Open it, find the part for cgi directory, and make it like this

 _________________________________________________________________________________
 ScriptAlias /cgi-bin/ /var/www/cgi-bin/
        <Directory "/var/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
                AddHandler cgi-script .py 
              # tell Apache to handle every file with .py suffix as a cgi program
                AddHandler default-handler .html .htm 
              # tell Apache to handle HTML files in regular way
        </Directory>
______________________________________________________________________________________________
 
 
 
The line
ScriptAlias /cgi-bin/ /var/www/cgi-bin/ specifies the path of cgi codes and the actual directory of your program. So when you type http://127.0.0.1/cgi-bin, the Apache will look into the directory /var/www/cgi-bin/ of your localhost.
Now restart your apache. On Ubuntu Linux, by default installation and configuration, it is

sudo /etc/init.d/apache2 restart
Third, write up a Hello, World! program. Save it as hello.py and give it execution privilege for anyone.

1
2
3
4
5
6
#!/usr/bin/env python
print "Content-Type: text/html"
print print """\ <html>
<head><title>First Python HTTP Programming </title></head>
<body> <h2>Hello World!</h2>
 </body> </html> """
Now open http://127.0.0.1/cgi-bin/hello.py in your web browser and you shall see a hello world in it.

If you have any error, e.g., 500 error, please check your Apache log file. It shall tell you something. You can find errors like what you have seen from a standard Python interpreter.
 

Thursday, November 3, 2011

Python File Operation

Python File Operation:
The below is the script to help to learn about the file read, write operation.
Script will ask operation from the user and do the operation accordingly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import properties
from properties import filePath
# Program is about read and write operation
# Need to write the file and specify the Name of the file and reading the file.

def writeFile(filename,content):
    f=open(filename,"w")
    f.write(content)
    f.close()

def readfile(filename):
    f=open(filename,"r")
    text=f.read()
    f.close()
    print text


def print_menu():
    print '========================'
    print 'Select any one operation'
    print '1. Write the file'
    print '2. Read the file '
    print '4. exit'
    print
menu_choice = 0
print_menu()
while True:
    menu_choice = input("Type in a number (1-3): ")
    if menu_choice==1:
        print 'Enter the text do you want to write in to the file:'
        filename = raw_input('Enter the name of the file:')
        print 'Next Step ..'
        cont = raw_input('Enter the text')
        writeFile(filePath+filename.rstrip('\n\r '),cont)
    elif menu_choice==2:
        print 'The content of the file is :'
        readfile(filePath+filename.rstrip('\n\r '))
    elif menu_choice==3:
        select = raw_input('You selected to exit yes or no ?')
        try:
            if select.rstrip('\n\r ') == "yes":
                print 'Good bye'
                break
            else:
                print_menu()
        except ValueError:
            print "That was not a Charector ."

Python List Operation

Python List Operation:
List : Its a sequential collection of object or any element/data type is called List.
The below is the script will help you to lean about the list operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#This program will do the list operation in doing the def functions, using the user defining function its very easy to debug the program and undestandable.


def lookup_ele(eleSerch,list):
    if eleSerch in list:
        print "element has been in the list "
    else:
        print " element",eleSerch,"is not in the list"

def print_list(list):
    i =0

    if len(list)>0:
        print "List as give below \n"
        while i<len(list):
            print i,":",list[i]
            i =i+1
    else:
        print "Empty list "


def add_list(ele,list):
    list.append(ele)
#    return list

def remove_ele(ele,list):
    if ele in list:
        print "Element is found"
        list_num = list.index(ele)
        del list[list_num]
        print ele+" elments has been deleetd from the list "
    else:
        print "element is not found"

def print_menu():
    print '================================='
    print '        List operations          '
    print ' ================================'
    print '1. Display list '
    print '2. Add element to the list '
    print '3. Remove element '
    print '4. Lookup a elements '
    print '5. Quit'
    print ' Please enter the selected value :'
    print '----------------------------------'
list = []
menu_choice = 0
print_menu()
while True:
    menu_choice = input("Type in a number (1-5): ")
    if menu_choice == 1:
        print_list(list)
    elif menu_choice == 2:
        print "Add elements "
        ele = raw_input("element: ")
        add_list(ele,list)
    elif menu_choice == 3:
        print "Remove element"
        ele = raw_input("element: ")
        remove_ele(ele,list)
    elif menu_choice == 4:
        print "Lookup element"
        eleSerch = raw_input("ele: ")
        print lookup_ele(eleSerch,list)
    elif menu_choice == 5:
        break
    else:
        print_menu()
print "Goodbye"

Wednesday, November 2, 2011

Python Basic Operation

Python: Arithmetic Operation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#this program you have to create one file called properties file in that you have to define a and b variable
#need to import those variable in this program by using from <properties file> import <varible>
import properties

from properties import a
from properties import b
sum =  a + b
diff = a-b
div = a/b
product = a*b
print 'Sum of this ',a,'and',b,'value is :',sum
print 'Diffence of this ',a,'and',b,'value is :',diff
print 'Product of this ',a,'and',b,'value is :',product
print 'Division of ',a,'and',b,'value is :',div
Program2: String Operation program :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#this program you have to create one file called properties file in that you have to define a and b variable
#need to import those variable in this program by using from <properties file> import <varible>

import properties

from properties import str1
from properties import str2

newStr = str1 + str2
newRama = str2*3
firstIndex = str2[0]
last2ndIndex = str2[-1]
newstring3 = str2[1:4]
lengthStr = len(str2)
comp = str1 < str2
find = "e" in str1
strUpper = str2.upper()
strLower = str2.lower()


print 'The concatinated String',str1,'+',str2,'is:',newStr
print 'Multiple of ',str2,'is:',newRama
print 'First index of ',str2,'is :',firstIndex
print 'Last 2nd index of ',str2,'is:',last2ndIndex
print '1st Inddex to 2nd Index of ',str2,'is:',newstring3
print 'Lenth of the string',str2,'is:',lengthStr
if find:
    print 'The \'e\' is prasent in the',str1,'is found'
else :
     print 'The \'e\' is prasent in the',str1,'is not found'

if comp:
    print str1,'is less then',str2
else:
    print str1, 'is not less then',str2
print 'Upper case of ',str2,'is:',strUpper
print 'Lowere case of ',str2,'is :',strLower
program 3: List program
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#this program you have to create one file called properties file in that you have to define a and b variable
#need to import those variable in this program by using from <properties file> import <varible>
import properties
from properties import list1
from properties import list2


print "The existing list is ",list1
print " 2nd list is :",list2

newList = list1+list2

list3=list1*3

print "Concatinated new list is:",newList
print "multiple of list:",list1,"is:",list3

list1[0]= "pattar"
print "The list added pattar ",list1
print "2nd to 3rd element of this array",list1[1:2]