python an intro

91
Python – An Introduction Arulalan.T [email protected] Kanchi Linux User Group

Upload: arulalan-t

Post on 19-May-2015

2.664 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Python An Intro

Python – An [email protected]

Kanchi Linux User Group

Page 2: Python An Intro

Python is a Programming Language

Page 3: Python An Intro

There are so many 

Programming Languages.

Why Python?

Page 4: Python An Intro
Page 5: Python An Intro
Page 6: Python An Intro

Python is simple and beautiful

Page 7: Python An Intro

Python is Easy to Learn

Page 8: Python An Intro

Python is Free Open Source Software

Page 9: Python An Intro

Can Do

● Text Handling● System Administration● GUI programming● Web Applications● Database Apps● Scientific Applications

● Games● NLP

● ...

Page 10: Python An Intro

  H i s t o r y

Page 11: Python An Intro

Guido van Rossum   Father of Python            1991

Page 12: Python An Intro

                 Perl  Java  Python   Ruby    PHP            1987       1991           1993      1995

Page 13: Python An Intro

What is

Python?

Page 14: Python An Intro

Python is...

A dynamic,open source programming language with a focus onsimplicity and productivity. It has anelegant syntax that is natural to

read and easy to write.

Page 15: Python An Intro

Quick and Easy

Intrepreted Scripting Language

Variable declarations are unnecessary

Variables are not typed

Syntax is simple and consistent

Memory management is automatic

Page 16: Python An Intro

     Object Oriented Programming            Classes

         Methods

         Inheritance

         Modules

         etc.,  

Page 17: Python An Intro

    Examples!

Page 18: Python An Intro
Page 19: Python An Intro

print    “Hello World”

Page 20: Python An Intro

         No Semicolons !

Page 21: Python An Intro

         Indentation

Page 22: Python An Intro

You have to follow the Indentation Correctly.

Otherwise,

Python will beat you !

Page 23: Python An Intro
Page 24: Python An Intro

 Discipline 

   Makes  

    Good 

Page 25: Python An Intro

          Variables

  colored_index_cards

Page 26: Python An Intro

No Need to Declare Variable Types !

      Python Knows Everything !

Page 27: Python An Intro

value = 10

print value

value = 100.50

print value

value = “This is String”

print      value * 3

Page 28: Python An Intro

Input

Page 29: Python An Intro

name = raw_input(“What is Your name?”)

print "Hello" , name , "Welcome"

Page 30: Python An Intro

Flow

Page 31: Python An Intro

if score >= 5000 : print “You win!”elif score <= 0 : print “Game over.”else: print “Current score:”,score

print “Done\n”

Page 32: Python An Intro

  Loop

Page 33: Python An Intro

for  i   in   range(1, 5):        print    ielse:        print    'The for loop is over'

Page 34: Python An Intro

number = 23

while True :        guess = int(raw_input('Enter an integer : '))        if  guess == number :                print 'Congratulations, you guessed it.'                running = False         elif  guess < number :                print 'No, it is a little higher than that.'        else:                print 'No, it is a little lower than that.'

print  'Done'

Page 35: Python An Intro

Array

Page 36: Python An Intro

                List = Array

numbers = [ "zero", "one", "two", "three", "FOUR" ]  

Page 37: Python An Intro

                Sort List

primes = [ 11, 5, 7, 2, 13, 3 ]

Page 38: Python An Intro

                Sort List

primes = [ 11, 5, 7, 2, 13, 3 ]

primes.sort()

Page 39: Python An Intro

                Sort List

primes = [ 11, 5, 7, 2, 13, 3 ]

primes.sort()

>>> [2, 3, 5, 7, 11, 13]

Page 40: Python An Intro

                Sort List

names = [ "Shrini", "Bala", "Suresh", "Arul"]

names.sort()

>>> ["Arul", "Bala","Shrini","Suresh"]

names.reverse()

>>> ["Suresh","Shrini","Bala","Arul"]

Page 41: Python An Intro

                Mixed List

names = [ "Shrini", 10, "Arul", 75.54]

names[1]+10>>> 20

names[2].upper()

>>> ARUL

Page 42: Python An Intro

                Mixed List

names = [ "Shrini", 10, "Arul", 75.54]

names[1]+10>>> 20

names[2].upper()

>>> ARUL

Page 43: Python An Intro

         Append on List

Numbers = [ 1,3,5,7]

numbers.append(9)

>>> [1,3,5,7,9]

Page 44: Python An Intro

    Tuples                                                             immutable

Page 45: Python An Intro

names = ('Arul','Dhastha','Raj')

name.append('Selva')

Error : Can not modify the tuple

Tuple is immutable type

Page 46: Python An Intro

Dictionary

Page 47: Python An Intro

menu = { “idly” : 2.50, “dosai” : 10.00, “coffee” : 5.00, “ice_cream” : 5.00, 100 : “Hundred”}

menu[“idly”]2.50

menu[100]Hundred

Page 48: Python An Intro
Page 49: Python An Intro

      Function

Page 50: Python An Intro

def sayHello():        print 'Hello World!' # block belonging of fn

# End of function

sayHello() # call the function

Page 51: Python An Intro

def printMax(a, b):        if a > b:                print a, 'is maximum'        else:                print b, 'is maximum'printMax(3, 4) 

Page 52: Python An Intro

Using in built Modules

Page 53: Python An Intro

#!/usr/bin/python# Filename: using_sys.pyimport time

print 'The sleep started'time.sleep(3)print 'The sleep finished'

Page 54: Python An Intro

Making Our Own Modules

Page 55: Python An Intro

#!/usr/bin/python# Filename: mymodule.pydef sayhi():        print “Hi, this is mymodule speaking.”

version = '0.1'

# End of mymodule.py

Page 56: Python An Intro

#!/usr/bin/python# Filename: mymodule_demo.py

import mymodule

mymodule.sayhi()print 'Version', mymodule.version

Page 57: Python An Intro

#!/usr/bin/python# Filename: mymodule_demo2.pyfrom mymodule import sayhi, version# Alternative:                 # from mymodule import *

sayhi()print 'Version', version

Page 58: Python An Intro

Class

Page 59: Python An Intro

class Person:        pass # An empty block

p = Person()

print p

Classes

Page 60: Python An Intro

class Person:        def sayHi(self):                print 'Hello, how are you?'

p = Person()

p.sayHi()

Classes

Page 61: Python An Intro

class Person:        def __init__(self, name):

#like contstructor                                self.name = name        def sayHi(self):                print 'Hello, my name is', self.name

p = Person('Arulalan.T')

p.sayHi()

Classes

Page 62: Python An Intro

                            Inheritance

Classes

Page 63: Python An Intro

class A:        def  hello(self):

print  ' I am super class 'class B(A):

 def  bye(self):print  ' I am sub class '

p = B()p.hello()p.bye()

Classes

Page 64: Python An Intro

File Handling

Page 65: Python An Intro

File Writing

Page 66: Python An Intro

poem = ''' Programming is funWhen the work is doneif you wanna make your work also fun:        use Python!'''

f = file('poem.txt', 'w') # open for 'w'ritingf.write(poem) # write text to filef.close() 

Page 67: Python An Intro

File Reading

Page 68: Python An Intro

f= file('poem.txt','r') for line in f.readlines():

print linef.close() 

Page 69: Python An Intro

           Database Intergration

Page 70: Python An Intro

import psycopg2   

conn = psycopg2.connect(" dbname='pg_database' user='dbuser' host='localhost' password='dbpass' ")

cur = conn.cursor()cur.execute("""SELECT * from pg_table""")rows = cur.fetchall()print rows

cur.close()conn.close()

Page 71: Python An Intro

import psycopg2   

conn = psycopg2.connect(" dbname='pg_database' user='dbuser' host='localhost' password='dbpass' ")

cur = conn.cursor()cur.execute("'insert into pg_table values(1,'python')"')conn.commit()

cur.close()conn.close()

Page 72: Python An Intro

THE END                                                    of code :­)

Page 73: Python An Intro

How to learn ?                                                    

Page 74: Python An Intro

Python – Shell

                                                    

● Interactive Python● Instance Responce● Learn as you type

Page 75: Python An Intro

bpythonipython

                                                    

} teach you very easily

Page 76: Python An Intro

Python can communicate                  With                Other            Languages

Page 77: Python An Intro

           C           +       Python

Page 78: Python An Intro
Page 79: Python An Intro

        Java           +       Python

Page 80: Python An Intro
Page 81: Python An Intro

     GUI        With    Python

Page 82: Python An Intro
Page 83: Python An Intro

                 Glade                    +                Python                    +                 GTK                    =              GUI APP

Page 84: Python An Intro

GLADE

Page 85: Python An Intro

Using Glade + Python

Page 86: Python An Intro

Web Web

Page 87: Python An Intro

        Web Frame Work in Python

Page 88: Python An Intro
Page 89: Python An Intro
Page 90: Python An Intro
Page 91: Python An Intro