Python an-intro - odp

1,367 views

Published on

This is python introduction which is created by me.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,367
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python an-intro - odp

  1. 1. Python – An Introduction Arulalan.T arulalant@gmail.com Centre for Atmospheric Science  Indian Institute of Technology Delhi
  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 DoText Handling GamesSystem Administration NLPGUI programmingWeb Applications ...Database AppsScientific 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 isPython?
  12. 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. 13. Quick and EasyIntrepreted Scripting LanguageVariable declarations are unnecessaryVariables are not typedSyntax is simple and consistentMemory 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 = 10print valuevalue = 100.50print valuevalue = “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:”,scoreprint “Donen”
  28. 28.   Loop
  29. 29. for  i   in   range(1, 5):        print    ielse:        print    The for loop is over
  30. 30. number = 23running = Truewhile running :        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 = Arraynumbers = [ "zero", "one", "two", "three", "FOUR" ]  
  33. 33.                 List = Arraynumbers = [ "zero", "one", "two", "three", "FOUR" ]numbers[0]>>> zero numbers[4]                                 numbers[­1]>>> FOUR                                  >>> FOUR                         numbers[­2]           >>> three
  34. 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. 35.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]
  36. 36.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort()
  37. 37.                 Sort Listprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort()>>> [2, 3, 5, 7, 11, 13]
  38. 38.                 Sort Listnames = [ "Shrini", "Bala", "Suresh","Arul"]names.sort()>>> ["Arul", "Bala","Shrini","Suresh"]names.reverse()>>> ["Suresh","Shrini","Bala","Arul"]
  39. 39.                 Mixed Listnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL
  40. 40.                 Mixed Listnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL
  41. 41.          Append on Listnumbers = [ 1,3,5,7]numbers.append(9)>>> [1,3,5,7,9]
  42. 42.     Tuples                                                             immutable
  43. 43. names = (Arul,Dhastha,Raj)name.append(Selva)Error : Can not modify the tupleTuple is immutable type
  44. 44.     String
  45. 45. name = Arulname[0]>>>Amyname = Arul + alan>>> Arulalan
  46. 46. splitname = This is python stringname.split( )>>>[This,is,python,string]comma = Shrini,Arul,Sureshcomma.split(,)>>> [Shrini,Arul,Suresh]
  47. 47. joinli = [a,b,c,d]s = ­new = s.join(li)>>> a­b­c­dnew.split(­)>>>[a,b,c,d]
  48. 48. small.upper()>>>SMALLBIG.lower()>>> bigmIxEd.swapcase()>>>MiXwD
  49. 49. Dictionary
  50. 50. menu = { “idly” : 2.50, “dosai” : 10.00, “coffee” : 5.00, “ice_cream” : 5.00, 100 : “Hundred”}menu[“idly”]2.50menu[100]Hundred
  51. 51.       Function
  52. 52. def sayHello():        print Hello World! # block belonging of fn# End of functionsayHello() # call the function
  53. 53. def printMax(a, b):        if a > b:                print a, is maximum        else:                print b, is maximumprintMax(3, 4) 
  54. 54. Using in built Modules
  55. 55. #!/usr/bin/python# Filename: using_sys.pyimport timeprint The sleep startedtime.sleep(3)print The sleep finished
  56. 56. #!/usr/bin/pythonimport osos.listdir(/home/arulalan)os.walk(/home/arulalan)
  57. 57. Making Our Own Modules
  58. 58. #!/usr/bin/python# Filename: mymodule.pydef sayhi():        print “Hi, this is mymodule speaking.”version = 0.1# End of mymodule.py
  59. 59. #!/usr/bin/python# Filename: mymodule_demo.pyimport mymodulemymodule.sayhi()print Version, mymodule.version
  60. 60. #!/usr/bin/python# Filename: mymodule_demo2.pyfrom mymodule import sayhi, version# Alternative:                 # from mymodule import *sayhi()print Version, version
  61. 61. Class
  62. 62. Classesclass Person:        pass # An empty blockp = Person()print p
  63. 63. Classesclass Person:        def sayHi(self):                print Hello, how are you?p = Person()p.sayHi()
  64. 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. 65. Classes                            Inheritance
  66. 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. 67. Classesclass A: Var = 10        def  __init__(self): self.public = 100 self._protected_ = protected self.__private__ = privateClass B(A): passp = B()p.__protected__
  68. 68. File Handling
  69. 69. File Writing
  70. 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. 71. File Reading
  72. 72. f= file(poem.txt,r) for line in f.readlines(): print linef.close() 
  73. 73.            Database Intergration
  74. 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. 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. 76. THE END                                                    of code :­)
  77. 77. How to learn ?                                                    
  78. 78. Python – ShellInteractive Python                                      Instance Responce                Learn as you type
  79. 79. bpythonipython }   teach you very easily                                                    
  80. 80. Python can communicate                  With                Other            Languages
  81. 81.            C           +       Python
  82. 82.         Java           +       Python
  83. 83.      GUI        With    Python
  84. 84.                  Glade                    +                Python                    +                 GTK                    =              GUI APP
  85. 85. GLADE
  86. 86. Using Glade + Python
  87. 87. WebWeb
  88. 88.         Web Frame Work in Python

×