23
Programming Environments There are several ways of crea/ng a computer program Using an Integrated Development Environment (IDE) Using a text editor You should use the method you are most comfortable with. I’ll PyCharm for all my in-class examples 9/2/16 27

Programming Environments - Westmont Collegedjp3.westmont.edu › classes › 2016_09_CS010 › Lectures › Lecture_0… · ch01.pptx Author: Donald Patterson Created Date: 9/2/2016

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

  • Programming Environments •  Thereareseveralwaysofcrea/ngacomputerprogram

    •  UsinganIntegratedDevelopmentEnvironment(IDE)•  Usingatexteditor

    •  Youshouldusethemethodyouaremostcomfortablewith.•  I’llPyCharmforallmyin-classexamples

    9/2/16 27

  • IDE components •  Thesourcecodeeditorcanhelpprogrammingby:

    •  Lis/nglinenumbersofcode•  Colorlinesofcode(comments,text…)•  Auto-indentsourcecode

    •  Outputwindow

    •  Debugger

    9/2/16 28

  • The PyCharm IDE

    Output

    Editor

    9/2/16 29

    Files

  • Your first program •  Tradi/onal‘HelloWorld’programinPython

    9/2/16 30

    •  Wewillexaminethisprograminthenextsec/on•  TypingtheprogramintoyourIDEwouldbegoodprac/ce!•  Becarefulofspellinge.g.,‘print’vs.‘primt’•  PyTHoniSCaSeSeNsI/Ve.

  • Text editor programming •  Youcanalsouseasimpletexteditortowriteyoursourcecode•  OncesavedasHello.py,youcanuseaconsolewindowto:

    •  Compiletheprogram•  Runtheprogram

    Compile/executeOutput

    9/2/16 31

  • Organize your work •  Your‘sourcecode’isstoredin.pyfiles•  Createafolderforthiscourse

    •  Createonefolderperprograminsidethecoursefolder•  Aprogramcanconsistofseveral.pyfiles

    •  BesureyouknowwhereyourIDEstoresyourfiles•  Youneedtobeabletofindyoufiles

    •  Backupyourfiles:•  ToaUSBflashdrive•  Toanetworkdrive

    9/2/16 32

  • Python interactive mode •  Likeotherlanguagesyoucanwrite/saveacompletePythonprograminafileandlettheinterpreterexecutetheinstruc/onsallatonce.

    •  Alterna/velyyoucanruninstruc/onsoneata/meusinginterac/vemode.•  Itallowsquick‘testprograms’tobewriben.•  Interac/vemodeallowsyoutowritepythonstatementsdirectlyintheconsolewindow

    9/2/16 33

  • Source Code to a Running Program •  Thecompilerreadsyourprogramandgeneratesbytecodeinstruc/ons(simpleinstruc/onsforthePythonVirtualmachine)•  ThePythonVirtualmachineisaprogramthatissimilartotheCPUofyourcomputer

    •  Anynecessarylibraries(e.g.fordrawinggraphics)areautoma/callylocatedandincludedbythevirtualmachine

    9/2/16 34

  • Let’s Get Started! •  OpenthePyCharmonyoulabcomputer•  Wearegoingtostartsimple,andaswelearnmoreaboutPython,we’lluseaddi/onalfeaturesinPyCharm

    9/2/16 35

  • “Hello World” •  TypethefollowingintotheEditor:# My first Python program

    print(“Hello World!”)

    •  Saveyourfileas“hello.py”

    •  Thisis“StepTwoWriteasimpleprogram”frompage7inyourtext.•  Remember–Pythoniscasesensi(ve

    •  Youhavetoentertheupperandlowercaselebersexactlyasthisappearabove

    9/2/16 36

  • Analyzing Your First Program •  APythonprogramcontainsoneormorelinesofinstruc/ons(statements)thatwillbetranslatedandexecutedbytheinterpreter# My first Python program

    print(“Hello World!”)

    •  Thefirstlineisacomment(astatementthatprovidesdescrip/veinforma/onabouttheprogramtoprogrammers).

    •  Thesecondlinecontainsastatementthatprintsalineoftextonscreen“Hello,World!”

    9/2/16 37

  • Basic Python Syntax: Print •  UsingthePython‘print()’func/on.

    •  Afunc/onisacollec/onofprogramminginstruc/onsthatcarryoutapar/culartask(inthiscasetoprintavalueonscreen).

    •  It’scodethatsomebodyelsewroteforyou!

    9/2/16 38

  • Syntax for Python Functions •  Touse,orcall,afunc/oninPythonyouneedtospecify:

    •  Thenameofthefunc/onthatyouwanttouse(inthepreviousexamplethenamewasprint)

    •  Anyvalues(arguments)neededbythefunc/ontocarryoutitstask(inthiscase,“HelloWorld!”).

    •  Argumentsareenclosedinparenthesesandmul/pleargumentsareseparatedwithcommas.

    •  Asequenceofcharactersenclosedinquota/onsmarksarecalledastring

    9/2/16 39

  • More Examples of the print Function •  Prin/ngnumericalvalues

    •  print(3+4)•  Evaluatestheexpression3+4anddisplays7

    •  Passingmul/plevaluestothefunc/on•  print(“theansweris”,6*7)•  DisplaysTheansweris42•  Eachvaluepassedtothefunc/onisdisplayed,oneajeranother,withablank

    spaceajereachvalue

    •  Bydefaulttheprintfunc/onstartsanewlineajeritsargumentsareprinted•  print(“Hello”)•  print(“World!”)•  Printstwolinesoftext•  Hello•  World!

    9/2/16 40

  • Our Second Program (Page 12, printtest.py) ## # Sample Program that demonstrates the print function # # Prints 7 print(3 + 4) # Print Hello World! on two lines print(“Hello”) print(“World!”) # Print multiple values with a single print function call print(“My favorite number are”, 3 + 4, “and” 3 + 10) # Print Hello World! on two lines print(“Goodbye”) print() print(“Hope to see you again”)

    9/2/16 41

  • Errors •  TherearetwoCategoriesofErrors:

    •  Compile-/meErrors•  akaSyntaxErrors

    •  Spelling,capitaliza/on,punctua/on•  Orderingofstatements,matchingofparenthesis,quotes…

    •  Noexecutableprogramiscreatedbythecompiler•  Correctfirsterrorlisted,thencompileagain.

    •  Repeatun/lallerrorsarefixed•  Run-/meErrors

    •  akaLogicErrors•  Theprogramruns,butproducesunintendedresults•  Theprogrammay‘crash’

    9/2/16 42

  • Syntax Errors •  Syntaxerrorarecaughtbythecompiler•  Whathappensifyou

    •  Miss-capitalizeaword: Print("HelloWorld!")•  Leaveoutquotes print(HelloWorld!)•  Mismatchquotes print("HelloWorld!')•  Don’tmatchbrackets print('Hello'

    •  TypeeachexampleaboveintheWingPythonShellwindow•  Whaterrormessagesaregenerated?

    9/2/16 43

  • Logic Errors •  Whathappensifyou

    •  Dividebyzero print(1/0)•  Misspelloutput print("Hello,Word!")•  Forgettooutput Removeline2

    •  Programswillcompileandrun•  Theoutputmaynotbeasexpected

    •  TypeeachexampleaboveinthePyCharmPythonShellwindow•  Whaterrormessagesaregenerated?

    9/2/16 44

  • Summary: Computer Basics •  Computersrapidlyexecuteverysimpleinstruc/ons•  AProgramisasequenceofinstruc/onsanddecisions

    •  Programmingistheart(andscience)ofdesigning,implemen/ng,andtes/ngcomputerprograms

    •  TheCentralProcessingUnit(CPU)performsprogramcontrolanddataprocessing

    •  Storagedevicesincludememoryandsecondarystorage(e.g.,aUSBFlashDrive)

    9/2/16 45

  • Summary: Python •  PythonwasdesignedinawaythatmakesiteasiertolearnthanotherprogramminglanguagessuchasJava,CandC++.

    •  ThedesignersgoalwastogivePythonsimplerandcleanersyntax.•  Setasidesome/metobecomefamiliarwiththeprogrammingenvironmentthatyouwilluseforyourclasswork.•  Itisimportanttoprac/cewiththetoolsoyoucanfocusonlearningPython

    •  Aneditorisaprogramforenteringandmodifyingtext,suchasaPythonprogram.

    9/2/16 46

  • Summary: Python •  Pythoniscasesensi/ve.

    •  Youmustbecarefulaboutdis/nguishingbetweenupperandlowercaselebers.

    •  ThePythoncompilertranslatessourcecodeintobytecodeinstruc/onsthatareexecutedbytheVirtualmachine.

    •  Afunc/oniscalledbyspecifyingthefunc/on’snameanditsparameters.

    •  Astringisasequenceofcharactersenclosedinquota/onmarks.

    9/2/16 47

  • Summary: Errors and pseudo code •  Acompile-/meerrorisaviola/onoftheprogramminglanguagerulesthatisdetectedbythecompiler.

    •  Arun-/meerrorcausesaprogramtotakeanac/onthattheprogrammerdidnotintend.

    •  Pseudocodeisaninformaldescrip/onofasequenceofstepsforsolvingaproblem.

    •  Analgorithmforsolvingaproblemisasequenceofstepsthatisunambiguous,executable,andtermina/ng.

    9/2/16 48

  • Poll Everywhere •  PollEv.com/donpaberson223

    9/2/16 49