Upload
others
View
15
Download
0
Embed Size (px)
Citation preview
IntroductiontoProgrammingLanguage
Lecture1
*Somematerialsadaptedfromocw.mit.edu 6.0001
Today’sAgenda• CourseLayout
• Whatiscomputation
• Python
IntroductiontoProgrammingLanguage 2
CourseLayout
IntroductiontoProgrammingLanguage 3
AboutthisCourse• Instructor• SeongjinLee
• Email:[email protected]
• Office:407-314
• OfficeHour:EveryThursday11:00-12:00orMakeappointment
• Class• Time:Thursday16:00-19:00
• Place:407-202
• Coursewebpage• http://open.gnu.ac.kr컴퓨터프로그래밍기초
IntroductiontoProgrammingLanguage 4
AboutthisCourse• Python• 모두의파이썬 20일만에배우는프로그래밍기초 |이승찬지음 |길벗
• AllenB.Downey,"ThinkPython:HowtoThinkLikeaComputerScientist",2015
• C• K.N.King,"CProgramming:AModernApproach,"W.W.Norton&Company,2ndEdition,2008
• BrianW.Kernighan,DennisM.Ritchie,"CProgrammingLanguage"PrenticeHall,1988(2ndEdition)(Kernighan의 C언어프로그래밍)
• StephenPrata."CPrimerPlus".Sams,2004(C기초플러스)
• 어서와 C언어는처음이지!초보자를위한 C프로그래밍완벽가이드 |그레그페
리 ,딘밀러지음 |천인국옮김 |인피니티북스
IntroductiontoProgrammingLanguage 5
AboutthisCourse• Goal– Concepts,programmingskills,problemsolving
• Evaluation• Attendance– 10%
• Assignments– 20%
• Exam1– 25%
• Exam2- 25%
• Exam3- 30%
• Closedbookandnotes
• Requestforregradewithinoneweekuponreturn;describereasonsinwriting
• whatandwhythescoreisincorrectorunfair
• Thewrittenargumentmustbeself-contained
IntroductiontoProgrammingLanguage 6
AboutthisCourse• ReadingAssignments– Duebeforefollowingclassperiod
• Attendance• Participationisimportantpartofthiscourse
• 3absenceswithoutpriorarrangementwillloweryourgradebyoneletter(eachsubsequent1absenceswillloweragradebyoneletter)
• FixedSittings• Ifyouhavetroublehearingmeorseeingthescreen,nowisthetimetochangeyoursits
• Eachstudentswillbeassignedanumber,usethatnumberoneverypieceofworkyouhandin
IntroductiontoProgrammingLanguage 7
AboutthisCourse• AcademicHonesty• Assignments,quizzes,andexamsdoneindividually
• Nolying,cheating,copying
• Iffound,nogradeforthatparticularassessment
• Suspiciousworkwillbequestionedthoroughly
IntroductiontoProgrammingLanguage 8
AboutthisCourse• Noclasseson추석
• Exams• Closebookandnotes
• Exam1onOct.10th (inclass)– 407-101
• Exam2onNov.11th (inclass)– 407-101
• Exam3onDec.12th (inclass)– 407-101
• Survey• Togivefeedbackonyourunderstandingofmaterialaswellashelpwithmaterial
IntroductiontoProgrammingLanguage 9
Assignmentsforeveryweek• Chooseanythreeconcepts• Writetheconceptonthetopofthepage
• Explaintheconceptwithyourwords(makesureanybodycanunderstandtheconcept)
• Giveanexampleoftheconcept
• 1pageforeachconcept
• YouaretohanditoneveryTuesdaymorning
• Nohandwrittenpapers
IntroductiontoProgrammingLanguage 10
Howtosucceedinthiscourse• Readcodeandthemanual
• Makemistakesandlearnwhy
• Keepitsimple
IntroductiontoProgrammingLanguage 11
Don’t practice until you get it right
Practice until you can’t get it wrong
Whatiscomputation
IntroductiontoProgrammingLanguage 12
WhatDoesAComputerDo• Fundamentally:• performscalculations
• abillioncalculationspersecond!
• remembersresults
• 100sofgigabytesofstorage!
• Whatkindsofcalculations?• built-intothelanguage
• onesthatyoudefineastheprogrammer
• Computersonlyknowwhatyoutellthem
IntroductiontoProgrammingLanguage 13
TypesOfKnowledge• declarativeknowledgeisstatementsoffact.
• imperativeknowledgeisarecipeor“how-to”.
IntroductiontoProgrammingLanguage 14
AnumericalExample• squarerootofanumberx isysuchthaty*y=x
• recipefordeducingsquarerootofanumberx(16)1. Startwithaguess,g
2. Ifg*giscloseenoughtox,stopandsaygistheanswer
3. Otherwisemakeanewguessbyaveraginggandx/g
4. Usingthenewguess,repeatprocessuntilcloseenough
IntroductiontoProgrammingLanguage 15
g g*g x/g (g+x/g)/2
3 9 16/3 4.17
4.17 17.36 3.837 4.0035
4.0035 16.0277 3.997 4.000002
Whatisarecipe1. Sequenceofsimplesteps
2. flowofcontrolprocessthatspecifieswheneachstepisexecuted
3. ameansofdeterminingwhentostop
1+2+3=analgorithm!
IntroductiontoProgrammingLanguage 16
Computersaremachines• Howtocapturearecipeinamechanicalprocess
1. fixedprogramcomputer:Calculator
2. StoredProgramcomputer:machinestoresandexecutesinstructions
IntroductiontoProgrammingLanguage 17
BasicMachineArchitecture
IntroductiontoProgrammingLanguage 18
MEMORY
ControlUnit ArithmeticLogicUnit
INPUT OUTPUT
programcounter primitiveoperations
StoredProgramComputer• Sequenceofinstructionsstoredinsidecomputer• builtfrompredefinedsetofprimitiveinstructions
1. arithmeticandlogic
2. simpletests
3. movingdata
• specialprogram(interpreter)executeseachinstructioninorder• useteststochangeflowofcontrolthroughsequence
• stopwhendone
IntroductiontoProgrammingLanguage 19
BasicPrimitives• Turingshowedthatyoucancomputeanythingusing6primitives• Moveleft,Moveright,Print,Scan,Erase,Donothing
• ifyouareinterestedtolearnmoreaboutit:reference orsmallvideo
• modernprogramminglanguageshavemoreconvenientsetofprimitives
• canabstractmethodstocreatenewprimitives
• anythingcomputableinonelanguageiscomputableinanyotherprogramminglanguage
IntroductiontoProgrammingLanguage 20
CreatingRecipes• aprogramminglanguageprovidesasetofprimitiveoperations
• expressionsarecomplexbutlegalcombinationsofprimitivesinaprogramminglanguage
• expressionsandcomputationshavevaluesandmeaningsinaprogramminglanguage
IntroductiontoProgrammingLanguage 21
AspectsofLanguages• Primitiveconstructs:SyntaxandSemantic• Syntax:Definesthegrammar
• Semantic:isthemeaningassociatedwithsyntacticallycorrectsymbolswithnosemanticerrors
• English/Korean:Words
• 아버지가방에들어가신다 – syntacticallyvalidbutsemanticallynotcorrect
• 아기고기다리 – notsyntacticallyvalid
• ProgrammingLanguage:Numbers,Strings,Simpleoperators
• 3.14*8– syntacticallyvalid
• “hi”5– notsyntacticallyvalid
IntroductiontoProgrammingLanguage 22
AspectsofLanguages
IntroductiontoProgrammingLanguage 23
• Naturallanguageshavemanymeanings
• ProgrammingLanguageshaveonlyonemeaningbutmaynotbewhatprogrammerintended
Wherethingsgowrong• syntacticerrors• commonandeasilycaught
• staticsemanticerrors• somelanguagescheckforthesebeforerunningprogram
• cancauseunpredictablebehavior
• nosemanticerrorsbutdifferentmeaningthanwhatprogrammerintended• programcrashes,stopsrunning
• programrunsforever
• programgivesananswerbutdifferentthanexpected
IntroductiontoProgrammingLanguage 24
Python
IntroductiontoProgrammingLanguage 25
PythonPrograms• aprogramisasequenceofdefinitionsandcommands• definitionsevaluated
• commandsexecutedbyPythoninterpreterinashell
• commands(statements)instructinterpretertodosomething
• canbetypeddirectlyinashellorstoredinafilethatisreadintotheshellandevaluated
IntroductiontoProgrammingLanguage 26
Objects• Pythonprogrammanipulatesdataobjects
• Objectshaveatypethatdefinesthekindsofthingsprogramcandotothem
• objectsare• scalar(cannotbesubdivided)
• non-scalar
IntroductiontoProgrammingLanguage 27
Scalarobjects• int – representintegers,ex.1,2,3,4,etc.
• float– representrealnumbers,ex.3.14,48.12
• bool– representBooleanvaluesTrueandFalse
• canusetype()toseethetypeofanobject
IntroductiontoProgrammingLanguage 28
>>>type(5)int>>>type(3.14)float
Typeconversion(cast)• canconvertobjectofonetypetoanother
• example:• float(3)convertsinteger3tofloat(3.0)
• int(3.9)truncatesfloat3.9tointeger3
IntroductiontoProgrammingLanguage 29
Printingtoconsole• toshowoutputfromcodetoauser,useprintcommand
IntroductiontoProgrammingLanguage 30
In[30]:3+8Out[30]:11
In[31]:print(3+8)11
“out”tellsyouit’saninteractionwithintheshellonly
No“out”meansitisactuallyshowntoauserwhenyourunafile
Expressions• combinesobjectsandoperatorstoformexpressions
• anexpressionhasavalue,whichhasatype
• syntaxforasimpleexpression• <object><operator><object>
IntroductiontoProgrammingLanguage 31
Operatorsforint andfloattypes• i+jà sum,int->int,float->float
• i-jà difference ,int->int,float->float
• i*jà product ,int->int,float->float
• i/jà division,resultisalwaysfloat
• i%jà themodularoperator,itgivesremainderwhenIisdividedbyj
• i **jà Itothepowerofj
IntroductiontoProgrammingLanguage 32
Bindingvariablesandvalues• equalsignisanassignmentofavaluetoavariablename
• valuestoredincomputermemory
• anassignmentbindsnametovalue
• retrievevalueassociatedwithnameorvariablebyinvokingthename,bytypingpi
IntroductiontoProgrammingLanguage 33
pi=3.141592pi_approx =22/7
Abstractingexpressions• Whygivenamestovaluesofexpressions?• toreusenamesinsteadofvalues
• easiertochangethecodelater
IntroductiontoProgrammingLanguage 34
pi=3.141592radious =2.2area=pi*(radious**2)
ToDo
IntroductiontoProgrammingLanguage 35
Whenyougobackhome• Makesureyoureadthetextandunderstandthemeaning
• Chooseanythreeconceptsandwriteareportoneachconcepts
• InstallLinuxusingaVirtualmachineornatively.• installfollowingprograms
• sudo apt-getinstallpythonviemacs spyder
IntroductiontoProgrammingLanguage 36