9
The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of this writing is Python 3.6) Python is available at: https://www.python.org/ Why Python is awesome: https://www.python.org/about/success/ On a Linux machine or a Mac you can check to see if Python 3 is installed by opening a terminal window and typing python at the prompt. If Python is not installed, you can download it at the python.org website.

The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

ThePythonIDLE:IntroductionThistutorialassumesyouhaveaPYTHON3versionoftheprogramminglanguageinstalled.(CurrentversionatofthiswritingisPython3.6)

Pythonisavailableat:https://www.python.org/WhyPythonisawesome:https://www.python.org/about/success/

OnaLinuxmachineoraMacyoucanchecktoseeifPython3isinstalledbyopeningaterminalwindowandtypingpython attheprompt.IfPythonisnotinstalled,youcandownloaditatthepython.org website.

Page 2: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

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!

Page 3: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Let’sdosomethingalittlemoreinteresting.This“codesnippet”printsthenumbersfrom5to9.Howwouldyouchangetheloopittoprintnumbersfrom3to101?Tryityourself!

ThePythonInterpreter:Instantfeedback

Thisstatementiscalledaforloop,whichloopsthroughasetofconditionsoneatatimefromthebeginningtotheendanddoessomething.Inthiscase,theforlooploopsthroughalistoffivenumbersandprintseachofthemtothescreen.

Bothrange andprint arepythonfunctions.Therange functioncreatesalistofnumbers,whiletheprint functiondumpsoutputtothescreen.

Noticethattheprint statementiswithintheforloop.Pythonknowsitiswithintheloopbecauseitistab-indented.

Page 4: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

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:

Page 5: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Oneofthemostimportantaspectsofprogrammingespeciallyinbioinformaticsiswritingtoandreadingfromfiles.Dataisstoredinfilesandbiologicaldataareprimarilysavedandstoredintextfiles.HereisanexampleshowinghowonecanusePythontowritesometab-delimitedtextdatatoafile.Tryingmakingthisfileinatexteditorthenrunningit(belowright).

Python:Writingdatatoafile

HereisaPythonscriptforwritingdatatoafilecalled“my_data.txt”

ThestuffinRED afterthe#signarecommentsthatareignoredbyPython,buthelpusknowwhatishappening.

Threevariableswithstringdatatobewrittentothefile

Thefout variableisafileobjectforwritingdata.The‘w’indicateswecanwritedatatothefile.

Thewrite functionwritesthedatatofile.

Page 6: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Thisprogramgiveanexampleofhowtoreadafilefromthecomputer.Theprogramalsohassomethingcalleda‘loop’thatloopsthrougheverylineofthefile.Note:Forthistowork,youmustalreadyhaveafilenamed‘my_data.txt’anditneedstobeinthesamefolder/directoryasthepythonfile.

Python:Readingdatafromafile

Page 7: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Functionsarethekeytoprogramming.Theyarelikelittlemachines:Youputinsomedata,thenyougetsomethingdifferentontheotherside.Pythonhasalotofonesalreadymadecalledbuilt-infunctions,butyoucanalsomakeyourown.Calltheyinterpreter,thentrythecodebelow:

PythonFunctions:Instantfeedback

Thefunctionstype andlen arefunctionsalreadyinPython.Whatdoyouthinktheirpurposesare?

Hereisasimplefunctionthattakesnodata.

Hereweusethefunction.(Callthefunction).

Page 8: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Savethefunctiontoafilecalled’run_functions.py’.Thencallthefunctions.

PythonFunctions:Savingandrunning

Wait,whydidadd_2_numbersnotprintouttheanswer?

Page 9: The Python IDLE - kelleybioinfo.org · The Python IDLE: Introduction This tutorial assumes you have a PYTHON 3 version of the programming language installed. (Current version at of

Savethefunctiontoafilecalled’run_functions.py’.Thencallthefunctions.

PythonFunctions:Savingandrunning

Usingreturn,youcanassigntheoutcometoavariable!

Thenyoucanprintthevariableseparately.

Nowweseetheansweronthescreen.