Python – An Introduction
              Arulalan.T
              arulalant@gmail.com

              Kanchi Linux User Group
Python is a Programming Language
There are so many 
Programming Languages.




     Why Python      ?
Python is simple and beautiful
Python is Easy to Learn
Python is Free Open Source Software
Can Do
●   Text Handling             ●   Games
●   System Administration     ●   NLP
●   GUI programming
●   Web Applicati...
  H i s t o r y
Guido van Rossum 
  Father of Python 
           1991
                 Perl  Java  Python   Ruby    PHP
            1987       1991           1993      1995
What is
Python?
Python is...


             A dynamic,open source
programming language with a focus on
simplicity and productivity.   It h...
Quick and Easy

Intrepreted Scripting Language

Variable declarations are unnecessary

Variables are not typed

Syntax is ...
     Object Oriented Programming
      
      Classes
         Methods

         Inheritance

         Modules

         e...
    Examples!
print    “Hello World”
         No Semicolons !
         Indentation
You have to follow 
the Indentation 
Correctly.

Otherwise,

Python will beat 
you !
 Discipline 

   Makes  

    Good 
          Variables


  colored_index_cards
No Need to Declare Variable Types !




      Python Knows Everything !
value = 10

print value

value = 100.50

print value

value = “This is String”

print      value * 3
Input
name = raw_input(“What   is Your name?”)




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

print ...
  Loop
for  i   in   range(1, 5):
        print    i
else:
        print    'The for loop is over'
number = 23

while True :
        guess = int(raw_input('Enter an integer : '))
        if  guess == number :
            ...
Array
                List = Array


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


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


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


primes.sort()
                Sort List


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


primes.sort()


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

names = [ "Shrini", "Bala", "Suresh",
"Arul"]
names.sort()

>>> ["Arul", "Bala","Shrini","Sures...
                Mixed List

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

names[1]+10
>>> 20


names[2].upper()

>>> ARUL
                Mixed List

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

names[1]+10
>>> 20


names[2].upper()

>>> ARUL
         Append on List


Numbers = [ 1,3,5,7]

numbers.append(9)

>>> [1,3,5,7,9]
    Tuples
                                                             immutable
names = ('Arul','Dhastha','Raj')

name.append('Selva')

Error : Can not modify the tuple

Tuple is immutable type
Dictionary
menu = {
  “idly”        :   2.50,
  “dosai”       :   10.00,
  “coffee”      :   5.00,
  “ice_cream”   :   5.00,
   100  ...
      Function
def sayHello():
        print 'Hello World!' # block belonging of fn
# End of function

sayHello() # call the function
def printMax(a, b):
        if a > b:
                print a, 'is maximum'
        else:
                print b, 'is max...
Using in built Modules
#!/usr/bin/python
# Filename: using_sys.py
import time

print 'The sleep started'
time.sleep(3)
print 'The sleep finished'
Making Our Own Modules
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
        print “Hi, this is mymodule speaking.”
version = '0.1'

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

import mymodule

mymodule.sayhi()
print 'Version', mymodule.version
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:                 
# from...
Class
Classes

class Person:
        pass # An empty block

p = Person()

print p
Classes

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

p = Person()

p.sayHi()
Classes
class Person:
        def __init__(self, name):
                #like contstructor                
               ...
Classes


                            
Inheritance
Classes
class A:
        def  hello(self):
              print  ' I am super class '
class B(A):
         def  bye(self):
...
File Handling
File Writing
poem = ''' Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
'''

f = fil...
File Reading
f= file('poem.txt','r') 
for line in f.readlines():
   print line
f.close() 
           Database Intergration
import psycopg2
   

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

...
import psycopg2
   

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

...
THE END
                                                    of code :­)
How to learn ?
                                     
               
Python – Shell
    Interactive Python
                                                
●


●   Instance Responce
●        ...
bpython
ipython        }    
                       teach you very easily




                                     
      ...
Python can communicate 
                 With
                Other
            Languages
           C
           +
       Python
        Java
           +
       Python
     GUI
        With 
   Python
                 Glade
                    +
                Python
                    +
                 GTK
           ...
GLADE
Using Glade + Python
Web
Web
        Web Frame Work in Python
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Python An Intro
Upcoming SlideShare
Loading in …5
×

Python An Intro

3,561 views

Published on

Published in: Education

Python An Intro

  1. 1. Python – An Introduction Arulalan.T arulalant@gmail.com Kanchi Linux User Group
  2. 2. Python is a Programming Language
  3. 3. There are so many  Programming Languages. Why Python ?
  4. 4. Python is simple and beautiful
  5. 5. Python is Easy to Learn
  6. 6. Python is Free Open Source Software
  7. 7. Can Do ● Text Handling ● Games ● System Administration ● NLP ● GUI programming ● Web Applications ● ... ● Database Apps ● Scientific Applications
  8. 8.   H i s t o r y
  9. 9. Guido van Rossum    Father of Python             1991
  10. 10.                  Perl  Java  Python   Ruby    PHP             1987       1991           1993      1995
  11. 11. What is Python?
  12. 12. Python is... A dynamic,open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  13. 13. Quick and Easy Intrepreted Scripting Language Variable declarations are unnecessary Variables are not typed Syntax is simple and consistent Memory management is automatic
  14. 14.      Object Oriented Programming              Classes          Methods          Inheritance          Modules          etc.,   
  15. 15.     Examples!
  16. 16. print    “Hello World”
  17. 17.          No Semicolons !
  18. 18.          Indentation
  19. 19. You have to follow  the Indentation  Correctly. Otherwise, Python will beat  you !
  20. 20.  Discipline     Makes       Good 
  21. 21.           Variables   colored_index_cards
  22. 22. No Need to Declare Variable Types !       Python Knows Everything !
  23. 23. value = 10 print value value = 100.50 print value value = “This is String” print      value * 3
  24. 24. Input
  25. 25. name = raw_input(“What is Your name?”) print "Hello" , name , "Welcome"
  26. 26. Flow
  27. 27. if score >= 5000 : print “You win!” elif score <= 0 : print “Game over.” else: print “Current score:”,score print “Donen”
  28. 28.   Loop
  29. 29. for  i   in   range(1, 5):         print    i else:         print    'The for loop is over'
  30. 30. 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'
  31. 31. Array
  32. 32.                 List = Array numbers = [ "zero", "one", "two", "three",  "FOUR" ]  
  33. 33.                 Sort List primes = [ 11, 5, 7, 2, 13, 3 ]
  34. 34.                 Sort List primes = [ 11, 5, 7, 2, 13, 3 ] primes.sort()
  35. 35.                 Sort List primes = [ 11, 5, 7, 2, 13, 3 ] primes.sort() >>> [2, 3, 5, 7, 11, 13]
  36. 36.                 Sort List names = [ "Shrini", "Bala", "Suresh", "Arul"] names.sort() >>> ["Arul", "Bala","Shrini","Suresh"] names.reverse() >>> ["Suresh","Shrini","Bala","Arul"]
  37. 37.                 Mixed List names = [ "Shrini", 10, "Arul", 75.54] names[1]+10 >>> 20 names[2].upper() >>> ARUL
  38. 38.                 Mixed List names = [ "Shrini", 10, "Arul", 75.54] names[1]+10 >>> 20 names[2].upper() >>> ARUL
  39. 39.          Append on List Numbers = [ 1,3,5,7] numbers.append(9) >>> [1,3,5,7,9]
  40. 40.     Tuples                                                              immutable
  41. 41. names = ('Arul','Dhastha','Raj') name.append('Selva') Error : Can not modify the tuple Tuple is immutable type
  42. 42. Dictionary
  43. 43. menu = { “idly” : 2.50, “dosai” : 10.00, “coffee” : 5.00, “ice_cream” : 5.00, 100 : “Hundred” } menu[“idly”] 2.50 menu[100] Hundred
  44. 44.       Function
  45. 45. def sayHello():         print 'Hello World!' # block belonging of fn # End of function sayHello() # call the function
  46. 46. def printMax(a, b):         if a > b:                 print a, 'is maximum'         else:                 print b, 'is maximum' printMax(3, 4) 
  47. 47. Using in built Modules
  48. 48. #!/usr/bin/python # Filename: using_sys.py import time print 'The sleep started' time.sleep(3) print 'The sleep finished'
  49. 49. Making Our Own Modules
  50. 50. #!/usr/bin/python # Filename: mymodule.py def sayhi():         print “Hi, this is mymodule speaking.” version = '0.1' # End of mymodule.py
  51. 51. #!/usr/bin/python # Filename: mymodule_demo.py import mymodule mymodule.sayhi() print 'Version', mymodule.version
  52. 52. #!/usr/bin/python # Filename: mymodule_demo2.py from mymodule import sayhi, version # Alternative:                  # from mymodule import * sayhi() print 'Version', version
  53. 53. Class
  54. 54. Classes class Person:         pass # An empty block p = Person() print p
  55. 55. Classes class Person:         def sayHi(self):                 print 'Hello, how are you?' p = Person() p.sayHi()
  56. 56. Classes 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()
  57. 57. Classes                              Inheritance
  58. 58. Classes 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()
  59. 59. File Handling
  60. 60. File Writing
  61. 61. poem = ''' Programming is fun When the work is done if you wanna make your work also fun:         use Python! ''' f = file('poem.txt', 'w') # open for 'w'riting f.write(poem) # write text to file f.close() 
  62. 62. File Reading
  63. 63. f= file('poem.txt','r')  for line in f.readlines(): print line f.close() 
  64. 64.            Database Intergration
  65. 65. 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()
  66. 66. 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()
  67. 67. THE END                                                     of code :­)
  68. 68. How to learn ?                                                      
  69. 69. Python – Shell Interactive Python                                       ● ● Instance Responce ●                 Learn as you type
  70. 70. bpython ipython }   teach you very easily                                                      
  71. 71. Python can communicate                   With                 Other             Languages
  72. 72.            C            +        Python
  73. 73.         Java            +        Python
  74. 74.      GUI         With     Python
  75. 75.                  Glade                     +                 Python                     +                  GTK                     =               GUI APP
  76. 76. GLADE
  77. 77. Using Glade + Python
  78. 78. Web Web
  79. 79.         Web Frame Work in Python

×