Top Banner
Introduction to Programming Language Lecture 1 * Some materials adapted from ocw.mit.edu 6.0001
36

Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Jun 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

IntroductiontoProgrammingLanguage

Lecture1

*Somematerialsadaptedfromocw.mit.edu 6.0001

Page 2: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Today’sAgenda• CourseLayout

• Whatiscomputation

• Python

IntroductiontoProgrammingLanguage 2

Page 3: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

CourseLayout

IntroductiontoProgrammingLanguage 3

Page 4: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 5: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 6: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

AboutthisCourse• Goal– Concepts,programmingskills,problemsolving

• Evaluation• Attendance– 10%

• Assignments– 20%

• Exam1– 25%

• Exam2- 25%

• Exam3- 30%

• Closedbookandnotes

• Requestforregradewithinoneweekuponreturn;describereasonsinwriting

• whatandwhythescoreisincorrectorunfair

• Thewrittenargumentmustbeself-contained

IntroductiontoProgrammingLanguage 6

Page 7: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

AboutthisCourse• ReadingAssignments– Duebeforefollowingclassperiod

• Attendance• Participationisimportantpartofthiscourse

• 3absenceswithoutpriorarrangementwillloweryourgradebyoneletter(eachsubsequent1absenceswillloweragradebyoneletter)

• FixedSittings• Ifyouhavetroublehearingmeorseeingthescreen,nowisthetimetochangeyoursits

• Eachstudentswillbeassignedanumber,usethatnumberoneverypieceofworkyouhandin

IntroductiontoProgrammingLanguage 7

Page 8: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

AboutthisCourse• AcademicHonesty• Assignments,quizzes,andexamsdoneindividually

• Nolying,cheating,copying

• Iffound,nogradeforthatparticularassessment

• Suspiciousworkwillbequestionedthoroughly

IntroductiontoProgrammingLanguage 8

Page 9: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

AboutthisCourse• Noclasseson추석

• Exams• Closebookandnotes

• Exam1onOct.10th (inclass)– 407-101

• Exam2onNov.11th (inclass)– 407-101

• Exam3onDec.12th (inclass)– 407-101

• Survey• Togivefeedbackonyourunderstandingofmaterialaswellashelpwithmaterial

IntroductiontoProgrammingLanguage 9

Page 10: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Assignmentsforeveryweek• Chooseanythreeconcepts• Writetheconceptonthetopofthepage

• Explaintheconceptwithyourwords(makesureanybodycanunderstandtheconcept)

• Giveanexampleoftheconcept

• 1pageforeachconcept

• YouaretohanditoneveryTuesdaymorning

• Nohandwrittenpapers

IntroductiontoProgrammingLanguage 10

Page 11: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Howtosucceedinthiscourse• Readcodeandthemanual

• Makemistakesandlearnwhy

• Keepitsimple

IntroductiontoProgrammingLanguage 11

Don’t practice until you get it right

Practice until you can’t get it wrong

Page 12: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Whatiscomputation

IntroductiontoProgrammingLanguage 12

Page 13: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

WhatDoesAComputerDo• Fundamentally:• performscalculations

• abillioncalculationspersecond!

• remembersresults

• 100sofgigabytesofstorage!

• Whatkindsofcalculations?• built-intothelanguage

• onesthatyoudefineastheprogrammer

• Computersonlyknowwhatyoutellthem

IntroductiontoProgrammingLanguage 13

Page 14: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

TypesOfKnowledge• declarativeknowledgeisstatementsoffact.

• imperativeknowledgeisarecipeor“how-to”.

IntroductiontoProgrammingLanguage 14

Page 15: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 16: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Whatisarecipe1. Sequenceofsimplesteps

2. flowofcontrolprocessthatspecifieswheneachstepisexecuted

3. ameansofdeterminingwhentostop

1+2+3=analgorithm!

IntroductiontoProgrammingLanguage 16

Page 17: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Computersaremachines• Howtocapturearecipeinamechanicalprocess

1. fixedprogramcomputer:Calculator

2. StoredProgramcomputer:machinestoresandexecutesinstructions

IntroductiontoProgrammingLanguage 17

Page 18: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

BasicMachineArchitecture

IntroductiontoProgrammingLanguage 18

MEMORY

ControlUnit ArithmeticLogicUnit

INPUT OUTPUT

programcounter primitiveoperations

Page 19: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

StoredProgramComputer• Sequenceofinstructionsstoredinsidecomputer• builtfrompredefinedsetofprimitiveinstructions

1. arithmeticandlogic

2. simpletests

3. movingdata

• specialprogram(interpreter)executeseachinstructioninorder• useteststochangeflowofcontrolthroughsequence

• stopwhendone

IntroductiontoProgrammingLanguage 19

Page 20: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

BasicPrimitives• Turingshowedthatyoucancomputeanythingusing6primitives• Moveleft,Moveright,Print,Scan,Erase,Donothing

• ifyouareinterestedtolearnmoreaboutit:reference orsmallvideo

• modernprogramminglanguageshavemoreconvenientsetofprimitives

• canabstractmethodstocreatenewprimitives

• anythingcomputableinonelanguageiscomputableinanyotherprogramminglanguage

IntroductiontoProgrammingLanguage 20

Page 21: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

CreatingRecipes• aprogramminglanguageprovidesasetofprimitiveoperations

• expressionsarecomplexbutlegalcombinationsofprimitivesinaprogramminglanguage

• expressionsandcomputationshavevaluesandmeaningsinaprogramminglanguage

IntroductiontoProgrammingLanguage 21

Page 22: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 23: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

AspectsofLanguages

IntroductiontoProgrammingLanguage 23

• Naturallanguageshavemanymeanings

• ProgrammingLanguageshaveonlyonemeaningbutmaynotbewhatprogrammerintended

Page 24: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Wherethingsgowrong• syntacticerrors• commonandeasilycaught

• staticsemanticerrors• somelanguagescheckforthesebeforerunningprogram

• cancauseunpredictablebehavior

• nosemanticerrorsbutdifferentmeaningthanwhatprogrammerintended• programcrashes,stopsrunning

• programrunsforever

• programgivesananswerbutdifferentthanexpected

IntroductiontoProgrammingLanguage 24

Page 25: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Python

IntroductiontoProgrammingLanguage 25

Page 26: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

PythonPrograms• aprogramisasequenceofdefinitionsandcommands• definitionsevaluated

• commandsexecutedbyPythoninterpreterinashell

• commands(statements)instructinterpretertodosomething

• canbetypeddirectlyinashellorstoredinafilethatisreadintotheshellandevaluated

IntroductiontoProgrammingLanguage 26

Page 27: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Objects• Pythonprogrammanipulatesdataobjects

• Objectshaveatypethatdefinesthekindsofthingsprogramcandotothem

• objectsare• scalar(cannotbesubdivided)

• non-scalar

IntroductiontoProgrammingLanguage 27

Page 28: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 29: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Typeconversion(cast)• canconvertobjectofonetypetoanother

• example:• float(3)convertsinteger3tofloat(3.0)

• int(3.9)truncatesfloat3.9tointeger3

IntroductiontoProgrammingLanguage 29

Page 30: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Printingtoconsole• toshowoutputfromcodetoauser,useprintcommand

IntroductiontoProgrammingLanguage 30

In[30]:3+8Out[30]:11

In[31]:print(3+8)11

“out”tellsyouit’saninteractionwithintheshellonly

No“out”meansitisactuallyshowntoauserwhenyourunafile

Page 31: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Expressions• combinesobjectsandoperatorstoformexpressions

• anexpressionhasavalue,whichhasatype

• syntaxforasimpleexpression• <object><operator><object>

IntroductiontoProgrammingLanguage 31

Page 32: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

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

Page 33: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Bindingvariablesandvalues• equalsignisanassignmentofavaluetoavariablename

• valuestoredincomputermemory

• anassignmentbindsnametovalue

• retrievevalueassociatedwithnameorvariablebyinvokingthename,bytypingpi

IntroductiontoProgrammingLanguage 33

pi=3.141592pi_approx =22/7

Page 34: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Abstractingexpressions• Whygivenamestovaluesofexpressions?• toreusenamesinsteadofvalues

• easiertochangethecodelater

IntroductiontoProgrammingLanguage 34

pi=3.141592radious =2.2area=pi*(radious**2)

Page 35: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

ToDo

IntroductiontoProgrammingLanguage 35

Page 36: Introduction to Programming Languageopen.gnu.ac.kr/lecslides/2017-2-introProg/IPL-python-01.pdf · 2017-08-28 · Introduction to Programming Language 7. About this Course •Academic

Whenyougobackhome• Makesureyoureadthetextandunderstandthemeaning

• Chooseanythreeconceptsandwriteareportoneachconcepts

• InstallLinuxusingaVirtualmachineornatively.• installfollowingprograms

• sudo apt-getinstallpythonviemacs spyder

IntroductiontoProgrammingLanguage 36