Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used...
Transcript of Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used...
Scriptinglanguage
FromWikipedia:Ascriptinglanguage orscriptlanguage isaprogramminglanguagethatsupportsscripts,programswrittenforaspecialrun-timeenvironmentthatcaninterpret(ratherthancompile)andautomatetheexecutionoftasksthatcouldalternativelybeexecutedone-by-onebyahumanoperator*....Theterm"scriptinglanguage"isalsousedlooselytorefertodynamichigh-levelgeneral-purposelanguage,suchasPerl,Tcl,andPython,withtheterm"script"oftenusedforsmallprograms(uptoafewthousandlinesofcode)insuchlanguages ...
*Pythonhasinteractiveshell
ShellScripting(bashetc.)vsPythonShellScripting(bashetc.):+ShellscriptinghassimplernotationsforI/Oredirection.+Itissimplertocreatepipelinesoutofexistingprogramsinshell.
+/- Uglysyntaxofcontrolflowstatements
- Mostoftasks(anydataprocessing)aredelegatedtoexternalprograms(awk,sed,perl,pythonetc.)- Extensiveuseofprocessforking- Compatibilityproblemsbetweenplatforms
Python:+Generalpurposeprogramminglanguage+Easy (elegant) syntax+Completetoolsetinamonolithpackage+Cross-platform+Powerful
+/- Blockindentation
- I/Oredirection,creatingpipelinesetc.notaseasyaswithShellScripting- Notallsystemshavepythoninstalled(it’salsotruewithbash)
BashvsPython
#!/bin/bash#commentlineecho"whatafineday:"date• Output,whencalledby
“./test.sh”:whatafineday:ThuOct2823:37:39EEST2004
#!/usr/bin/env python3#commentlineprint("whatafineday:")importosos.system("date")• Output,whencalledby
«./test.py»:whatafineday:ThuOct2823:37:39EEST2004
Eachtaskhasit’sowntool
ShellScripting:• programexecution,input/outputcapturingandchaining(with
pipes).
Python:• filecontent/dataprocessing(fromperspectiveofshell
scripting)• anyothertask...• canbeusedasscriptinglanguage(unlikeC/C++,Javaetc.)
IngeneralPythondoesn’tcomparewithShellScripting.
Pythonisgoodfor
• Conceptchecking• Fastprototyping• Anyperformancenon-essentialprogrammingtask
• Stringprocessing
• Areadable,dynamic,pleasant,• flexible,fastandpowerfullanguage
WhatisPython
• Multi-purpose(Web,GUI,Scripting,etc.)• ObjectOriented• Interpreted• StronglytypedandDynamicallytyped• Focusonreadabilityandproductivity
Features
• BatteriesIncluded• EverythingisanObject• InteractiveShell• StrongIntrospection• CrossPlatform• CPython,Jython,IronPython,PyPy
Releases
• Createdin1989byGuidoVanRossum• Python1.0releasedin1994• Python2.0releasedin2000• Python3.0releasedin2008• Python2.7isthe lastversionofPython2• Python3.4 isthelatestversion
Indentation
• Mostlanguagesdon’tcareaboutindentation
• Mosthumansdo• Wetendtogroupsimilarthingstogether
Python’sWay
• Nointerfaces• Norealprivateattributes/functions• Privateattributesstart(butdonotend)withdoubleunderscores.
• Specialclassmethodsstartandendwithdoubleunderscores.– __init__,__doc__,__cmp__,__str__
Imports
• Allowscodeisolationandre-use• Addsreferencestovariables/classes/functions/etc.intocurrentnamespace
• IPython(http://ipython.org)– betterinteractiveshellandmore
• ContinuumAnalyticsAnacondaPythondistribution (http://www.continuum.io)
• CPython• Jython• IronPython• PyPy• Cython
OtherScriptingoptions
• Ruby• Perl• JavaScript(Node.js)• TypeScript(compilestoJavaScript)• CoffeeScript(compilestoJavaScript)• PHP• ...