ThePythonIDLE:IntroductionThistutorialassumesyouhaveaPYTHON3versionoftheprogramminglanguageinstalled.(CurrentversionatofthiswritingisPython3.6)
Pythonisavailableat:https://www.python.org/WhyPythonisawesome:https://www.python.org/about/success/
OnaLinuxmachineoraMacyoucanchecktoseeifPython3isinstalledbyopeningaterminalwindowandtypingpython attheprompt.IfPythonisnotinstalled,youcandownloaditatthepython.org website.
After installing Python, you should be able to invoke Python on the commandline in a terminal window by typing the name of the program. This opens thePython Interpreter, where you can run Python code directly in the terminal bytyping ‘python’ and hitting the Enter key:
Pythononthecommandline
YoucantypeinandrunyourveryfirstPythonprograminthePythonInterpreter.ByLawoftheCoders,itmustprint“Helloworld!”
Look,it’sacalculatortoo!
Let’sdosomethingalittlemoreinteresting.This“codesnippet”printsthenumbersfrom5to9.Howwouldyouchangetheloopittoprintnumbersfrom3to101?Tryityourself!
ThePythonInterpreter:Instantfeedback
Thisstatementiscalledaforloop,whichloopsthroughasetofconditionsoneatatimefromthebeginningtotheendanddoessomething.Inthiscase,theforlooploopsthroughalistoffivenumbersandprintseachofthemtothescreen.
Bothrange andprint arepythonfunctions.Therange functioncreatesalistofnumbers,whiletheprint functiondumpsoutputtothescreen.
Noticethattheprint statementiswithintheforloop.Pythonknowsitiswithintheloopbecauseitistab-indented.
OnceyouquitthePythonInterpreter(typeControl-d toquit)allyourworkdisappears.Theinterpreterisnicefortestingoutsimplefunctionsorfordoingcalculations,butyourworkislostafteryouquit.Tosaveyour,programyouneedtowriteitinaseparatefile.BelowwesaveourworkwiththeEmacstexteditor,acommonlyusedtexteditorforprogramming.(Thenano programisaneditorthatoftencomeswithlinux.Typenano attheprompt.)
Python:Savingyourprogrammingcode
Writetheprograminthetexteditorandsaveit.Icalledthispythonprogram‘tmp.py’.
Noticethetabthatputsprintinsidetheforloop.(Removethetabanditwon’tworkproperly.)
Toruntheprogramtypepython andthenameoftheprogramlikethis:
Oneofthemostimportantaspectsofprogrammingespeciallyinbioinformaticsiswritingtoandreadingfromfiles.Dataisstoredinfilesandbiologicaldataareprimarilysavedandstoredintextfiles.HereisanexampleshowinghowonecanusePythontowritesometab-delimitedtextdatatoafile.Tryingmakingthisfileinatexteditorthenrunningit(belowright).
Python:Writingdatatoafile
HereisaPythonscriptforwritingdatatoafilecalled“my_data.txt”
ThestuffinRED afterthe#signarecommentsthatareignoredbyPython,buthelpusknowwhatishappening.
Threevariableswithstringdatatobewrittentothefile
Thefout variableisafileobjectforwritingdata.The‘w’indicateswecanwritedatatothefile.
Thewrite functionwritesthedatatofile.
Thisprogramgiveanexampleofhowtoreadafilefromthecomputer.Theprogramalsohassomethingcalleda‘loop’thatloopsthrougheverylineofthefile.Note:Forthistowork,youmustalreadyhaveafilenamed‘my_data.txt’anditneedstobeinthesamefolder/directoryasthepythonfile.
Python:Readingdatafromafile
Functionsarethekeytoprogramming.Theyarelikelittlemachines:Youputinsomedata,thenyougetsomethingdifferentontheotherside.Pythonhasalotofonesalreadymadecalledbuilt-infunctions,butyoucanalsomakeyourown.Calltheyinterpreter,thentrythecodebelow:
PythonFunctions:Instantfeedback
Thefunctionstype andlen arefunctionsalreadyinPython.Whatdoyouthinktheirpurposesare?
Hereisasimplefunctionthattakesnodata.
Hereweusethefunction.(Callthefunction).
Savethefunctiontoafilecalled’run_functions.py’.Thencallthefunctions.
PythonFunctions:Savingandrunning
Wait,whydidadd_2_numbersnotprintouttheanswer?
Savethefunctiontoafilecalled’run_functions.py’.Thencallthefunctions.
PythonFunctions:Savingandrunning
Usingreturn,youcanassigntheoutcometoavariable!
Thenyoucanprintthevariableseparately.
Nowweseetheansweronthescreen.