Python an-intro - odp

download Python an-intro - odp

If you can't read please download the document

description

This is python introduction which is created by me.

Transcript of Python an-intro - odp

  • 1. PythonAnIntroduction Arulalan.T [email protected] CentreforAtmosphericScience IndianInstituteofTechnologyDelhi

2. PythonisaProgrammingLanguage 3. TherearesomanyProgrammingLanguages. WhyPython? 4. Pythonissimpleandbeautiful 5. PythonisEasytoLearn 6. PythonisFreeOpenSourceSoftware 7. Can DoTextHandling GamesSystemAdministration NLPGUIprogrammingWebApplications...DatabaseAppsScientificApplications 8. Hi s t o r y 9. GuidovanRossumFatherofPython1991 10. PerlJavaPythonRubyPHP1987199119931995 11. What isPython? 12. Python is... A dynamic,open sourceprogramming language with a focus onsimplicity and productivity. It has anelegant syntax that is natural to read and easy to write. 13. QuickandEasyIntrepretedScriptingLanguageVariabledeclarationsareunnecessaryVariablesarenottypedSyntaxissimpleandconsistentMemorymanagementisautomatic 14. ObjectOrientedProgrammingClassesMethodsInheritanceModulesetc., 15. Examples! 16. printHelloWorld 17. NoSemicolons! 18. Indentation 19. YouhavetofollowtheIndentationCorrectly.Otherwise,Pythonwillbeatyou! 20. DisciplineMakesGood 21. Variablescolored_index_cards 22. NoNeedtoDeclareVariableTypes!PythonKnowsEverything! 23. value=10printvaluevalue=100.50printvaluevalue=ThisisStringprintvalue*3 24. Input 25. name = raw_input(What is Your name?)print "Hello" , name , "Welcome" 26. Flow 27. ifscore >= 5000 :print You win!elif score >zeronumbers[4]numbers[1]>>>FOUR>>>FOURnumbers[2]>>>three 34. MultiDimensionListnumbers=[["zero","one"],["two","three","FOUR"]]numbers[0]>>>["zero","one"]numbers[0][0]numbers[1][1]>>>zero>>>FOURlen(numbers)>>>2 35. SortListprimes = [ 11, 5, 7, 2, 13, 3 ] 36. SortListprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort() 37. SortListprimes = [ 11, 5, 7, 2, 13, 3 ]primes.sort()>>> [2, 3, 5, 7, 11, 13] 38. SortListnames = [ "Shrini", "Bala", "Suresh","Arul"]names.sort()>>> ["Arul", "Bala","Shrini","Suresh"]names.reverse()>>> ["Suresh","Shrini","Bala","Arul"] 39. MixedListnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL 40. MixedListnames = [ "Shrini", 10, "Arul", 75.54]names[1]+10>>> 20names[2].upper()>>> ARUL 41. AppendonListnumbers=[1,3,5,7]numbers.append(9)>>>[1,3,5,7,9] 42. Tuplesimmutable 43. names=(Arul,Dhastha,Raj)name.append(Selva)Error:CannotmodifythetupleTupleisimmutabletype 44. String 45. name=Arulname[0]>>>Amyname=Arul+alan>>>Arulalan 46. splitname=Thisispythonstringname.split()>>>[This,is,python,string]comma=Shrini,Arul,Sureshcomma.split(,)>>>[Shrini,Arul,Suresh] 47. joinli=[a,b,c,d]s=new=s.join(li)>>>abcdnew.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. defsayHello():printHelloWorld!#blockbelongingoffn#EndoffunctionsayHello()#callthefunction 53. defprintMax(a,b):ifa>b:printa,ismaximumelse:printb,ismaximumprintMax(3,4) 54. UsinginbuiltModules 55. #!/usr/bin/python#Filename:using_sys.pyimporttimeprintThesleepstartedtime.sleep(3)printThesleepfinished 56. #!/usr/bin/pythonimportosos.listdir(/home/arulalan)os.walk(/home/arulalan) 57. MakingOurOwnModules 58. #!/usr/bin/python#Filename:mymodule.pydefsayhi():printHi,thisismymodulespeaking.version=0.1#Endofmymodule.py 59. #!/usr/bin/python#Filename:mymodule_demo.pyimportmymodulemymodule.sayhi()printVersion,mymodule.version 60. #!/usr/bin/python#Filename:mymodule_demo2.pyfrommymoduleimportsayhi,version#Alternative:#frommymoduleimport*sayhi()printVersion,version 61. Class 62. ClassesclassPerson:pass#Anemptyblockp=Person()printp 63. ClassesclassPerson:defsayHi(self):printHello,howareyou?p=Person()p.sayHi() 64. ClassesclassPerson:def__init__(self,name):#likecontstructorself.name=namedefsayHi(self):printHello,mynameis,self.namep=Person(Arulalan.T)p.sayHi() 65. ClassesInheritance 66. ClassesclassA:def hello(self):printIamsuperclassclassB(A):defbye(self):printIamsubclassp=B()p.hello()p.bye() 67. ClassesclassA:Var=10def __init__(self): self.public=100 self._protected_=protected self.__private__=privateClassB(A): passp=B()p.__protected__ 68. FileHandling 69. FileWriting 70. poem=ProgrammingisfunWhentheworkisdoneifyouwannamakeyourworkalsofun:usePython!f=file(poem.txt,w)#openforwritingf.write(poem)#writetexttofilef.close() 71. FileReading 72. f=file(poem.txt,r)forlineinf.readlines(): printlinef.close() 73. DatabaseIntergration 74. importpsycopg2conn=psycopg2.connect("dbname=pg_databaseuser=dbuserhost=localhostpassword=dbpass")cur=conn.cursor()cur.execute("""SELECT*frompg_table""")rows=cur.fetchall()printrowscur.close()conn.close() 75. importpsycopg2conn=psycopg2.connect("dbname=pg_databaseuser=dbuserhost=localhostpassword=dbpass")cur=conn.cursor()cur.execute("insertintopg_tablevalues(1,python)")conn.commit()cur.close()conn.close() 76. THEENDofcode:) 77. Howtolearn? 78. PythonShellInteractivePython InstanceResponce Learnasyoutype 79. bpythonipython} teachyouveryeasily 80. PythoncancommunicateWithOtherLanguages 81. C+Python 82. Java+Python 83. GUIWithPython 84. Glade+Python+GTK=GUIAPP 85. GLADE 86. UsingGlade+Python 87. WebWeb 88. WebFrameWorkinPython