• Like
Lesson1 python an introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Lesson1 python an introduction

  • 953 views
Published

Document under Creative Common

Document under Creative Common

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
953
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
69
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Python – An Introduction Arulalan.T arulalant@gmail.com Centre for Atmospheric Sciences  Indian Institute of Technology Delhi
  • 2. Python is a Programming Language
  • 3. There are so many Programming Languages. Why Python ?
  • 4. Python is simple and beautiful
  • 5. Python is Easy to Learn
  • 6. Python is Free Open Source Software
  • 7. Can Do● Text Handling ● Games● System Administration ● NLP● GUI programming● Web Applications ● ...● Database Apps● Scientific Applications
  • 8.   H i s t o r y
  • 9. Guido van Rossum   Father of Python            1991
  • 10.                  Perl  Java  Python   Ruby    PHP            1987       1991           1993      1995
  • 11. What isPython?
  • 12. Python is... A dynamic,open sourceprogramming language with a focus onsimplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  • 13. Quick and EasyIntrepreted Scripting LanguageVariable declarations are unnecessaryVariables are not typedSyntax is simple and consistentMemory management is automatic
  • 14.      Object Oriented Programming            Classes         Methods         Inheritance         Modules         etc.,  
  • 15.     Examples!
  • 16. print    “Hello World”
  • 17.          No Semicolons !
  • 18.          Indentation
  • 19. You have to follow the Indentation Correctly.Otherwise,Python will beat you !
  • 20.  Discipline    Makes      Good 
  • 21.           Variables  colored_index_cards
  • 22. No Need to Declare Variable Types !      Python Knows Everything !
  • 23. value = 10print valuevalue = 100.50print valuevalue = “This is String”print      value * 3
  • 24. Input
  • 25. name = raw_input(“What is Your name?”)print "Hello" , name , "Welcome"
  • 26. Flow
  • 27. if score >= 5000 : print “You win!”elif score <= 0 : print “Game over.”else: print “Current score:”,scoreprint “Donen”
  • 28.   Loop
  • 29. for  i   in   range(1, 5):        print    ielse:        print    The for loop is over
  • 30. number = 23while 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. Array
  • 32.                 List = Arraynumbers = [ "zero", "one", "two", "three", "FOUR" ]  
  • 33.                 List = Arraynumbers = [ "zero", "one", "two", "three", "FOUR" ]numbers[0]>>> zero numbers[4]                                 numbers[­1]>>> FOUR                                  >>> FOUR                         numbers[­2]           >>> three
  • 34.   Multi Dimension Listnumbers = [ ["zero", "one"],["two", "three", "FOUR" ]]numbers[0]>>> ["zero", "one"] numbers[0][0]                       numbers[­1][­1]>>> zero                                  >>> FOUR                         len(numbers)           >>> 2
  • 35.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]
  • 36.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort()
  • 37.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort()>>> [2, 3, 5, 7, 11, 13]
  • 38.                 Sort Listnames = [ "Shrini", "Bala", "Suresh","Arul"]names.sort()>>> ["Arul", "Bala","Shrini","Suresh"]names.reverse()>>> ["Suresh","Shrini","Bala","Arul"]
  • 39.                 Mixed Listnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL
  • 40.                 Mixed Listnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL
  • 41.          Append on Listnumbers = [ 1,3,5,7]numbers.append(9)>>> [1,3,5,7,9]
  • 42.     Tuples                                                             immutable
  • 43. names = (Arul,Dhastha,Raj)name.append(Selva)Error : Can not modify the tupleTuple is immutable type
  • 44.     String
  • 45. name = Arulname[0]>>>Amyname = Arul + alan>>> Arulalan
  • 46. splitname = This is python stringname.split( )>>>[This,is,python,string]comma = Shrini,Arul,Sureshcomma.split(,)>>> [Shrini,Arul,Suresh]
  • 47. joinli = [a,b,c,d]s = ­new = s.join(li)>>> a­b­c­dnew.split(­)>>>[a,b,c,d]
  • 48. small.upper()>>>SMALLBIG.lower()>>> bigmIxEd.swapcase()>>>MiXwD
  • 49. Dictionary
  • 50. menu = { “idly” : 2.50, “dosai” : 10.00, “coffee” : 5.00, “ice_cream” : 5.00, 100 : “Hundred”}menu[“idly”]2.50menu[100]Hundred
  • 51.       Function
  • 52. def sayHello():        print Hello World! # block belonging of fn# End of functionsayHello() # call the function
  • 53. def printMax(a, b):        if a > b:                print a, is maximum        else:                print b, is maximumprintMax(3, 4) 
  • 54. Using in built Modules
  • 55. #!/usr/bin/python# Filename: using_sys.pyimport timeprint The sleep startedtime.sleep(3)print The sleep finished
  • 56. #!/usr/bin/pythonimport osos.listdir(/home/arulalan)os.walk(/home/arulalan)
  • 57. Making Our Own Modules
  • 58. #!/usr/bin/python# Filename: mymodule.pydef sayhi():        print “Hi, this is mymodule speaking.”version = 0.1# End of mymodule.py
  • 59. #!/usr/bin/python# Filename: mymodule_demo.pyimport mymodulemymodule.sayhi()print Version, mymodule.version
  • 60. #!/usr/bin/python# Filename: mymodule_demo2.pyfrom mymodule import sayhi, version# Alternative:                 # from mymodule import *sayhi()print Version, version
  • 61. Class
  • 62. Classesclass Person:        pass # An empty blockp = Person()print p
  • 63. Classesclass Person:        def sayHi(self):                print Hello, how are you?p = Person()p.sayHi()
  • 64. Classesclass Person:        def __init__(self, name): #like contstructor                                self.name = name        def sayHi(self):                print Hello, my name is, self.namep = Person(Arulalan.T)p.sayHi()
  • 65. Classes                            Inheritance
  • 66. Classesclass 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()
  • 67. Classesclass A: Var = 10        def  __init__(self): self.public = 100 self._protected_ = protected self.__private__ = privateClass B(A): passp = B()p.__protected__
  • 68. File Handling
  • 69. File Writing
  • 70. poem =  Programming is funWhen the work is doneif you wanna make your work also fun:        use Python!f = file(poem.txt, w) # open for writingf.write(poem) # write text to filef.close() 
  • 71. File Reading
  • 72. f= file(poem.txt,r) for line in f.readlines(): print linef.close() 
  • 73.            Database Intergration
  • 74. 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 rowscur.close()conn.close()
  • 75. 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()
  • 76. THE END                                                    of code :­)
  • 77. How to learn ?                                                    
  • 78. Python – Shell Interactive Python                                      ●● Instance Responce●                 Learn as you type
  • 79. bpythonipython }   teach you very easily                                                    
  • 80. Python can communicate                  With                Other            Languages
  • 81.            C           +       Python
  • 82.         Java           +       Python
  • 83.      GUI        With    Python
  • 84.                  Glade                    +                Python                    +                 GTK                    =              GUI APP
  • 85. GLADE
  • 86. Using Glade + Python
  • 87. WebWeb
  • 88.         Web Frame Work in Python