IntroductiongtogComputergProgramminggingPython
AgSunCamgonlinegcontinuinggeducationgcourse
Python Programming for
g g
gEngineers - Part 1: g g g
Expressions, Data Types, Variables,
g g g
Strings and Collections g g
by
Kwabena Ofosu, Ph.D., P.E., PTOE
g g g g
www.SunCam.com Copyrightg2019gKwabenagOfosu,gPh.D.,gP.E.,gPTOE
, 362.pdf
IntroductiongtogComputergProgramminggingPython
AgSunCamgonlinegcontinuinggeducationgcourse
Abstract
Pythongisgagfreegopengsourcegcomputergprogrammingglanguagegthatgdrivesgsomegofgtheginternet’sgm
ostgpopulargwebsitesgsuchgasgGoogle,gYoutubegandgInstagram.gPythongcangbegusedgtogperformgcom
plexgmathematicalgcalculations,ghandlegbiggdata,gbuildgwebgappsgandgdesktopgapplications,gandgma
nagegdatabases.
ThisgcoursegisgthegfirstgofgagseriesgongthegPythongprogrammingglanguage.gThisgcoursegisgtailoredgtogp
racticinggengineers.gThegcoursegbeginsgwithgaggeneralgoverviewgofgcomputersgandgcomputergprogra
mming,gfollowedgbygangintroductiongtogthegPythonglanguage.gThegcoursegthengdelvesgintogthegdetail
sgofgthegIDLEg(PythongGUI)gapplicationgforgdevelopinggandgtestinggPythongscriptsgfollowedgbygtheg
conceptsgofgexpressions,gdatagtypes,gvariables,gstrings,glists,gtuples,gdictionariesgandgsets.gSeveralge
xamplesgofgsituationsgencounteredgbygpracticinggengineersgaregusedgtogdemonstrategthegprogrammi
nggconceptsgandgmethodsgpresentedgingthisgcourse.
Computergprogramminggenablesgpractitionersgtogdrasticallygincreasegtheirgproductivitygandgefficie
ncygbygautomatinggrepetitivegtasksgsuchgasgtediousgdesigngcalculationsgandgprocessingglargegandgco
mplexgdata.gProgramminggpredisposesgengineersgandgscientistsgtogpursuegmoregcreativeg andginnov
ativegsolutionsgingtheirgfieldsgofgspecialty.
www.SunCam.com Copyrightg2019gKwabenagOfosu,gPh.D.,gP.E.,gPTOE ii ofg6
, 362.pdf
IntroductiongtogComputergProgramminggingPython
AgSunCamgonlinegcontinuinggeducationgcourse
TABLEgOFgCONTENTS
Abstract ........................................................................................................................................... ii
ListgofgTables ....................................................................................................................................v
ListgofgFigures................................................................................................................................. vi
1. INTRODUCTION ........................................................................................................................1
1.1 Computers ..............................................................................................................................1
1.2 ComputergProgramming .........................................................................................................2
1.3 RelevancegofgComputergProgramminggtogEngineers ...............................................................4
1.4 PlanninggComputergProgramminggSolutions ..........................................................................4
1.5 Python ....................................................................................................................................5
2. GettinggStarted..............................................................................................................................7
2.1 OpeninggPython ......................................................................................................................7
2.2 AgQuickgTourgofgIDLEg(PythongGUI) ...................................................................................31
3. EXPRESSIONS,gDATAgTYPESgANDgVARIABLES ...............................................................36
3.1 Expressions...........................................................................................................................36
3.2 ArithmeticgOperators ............................................................................................................39
3.3 OrdergofgOperations ..............................................................................................................39
3.4 DatagTypes............................................................................................................................44
3.5 Variables ..............................................................................................................................47
3.6 VariablegNames ....................................................................................................................49
3.7 VariablegNamegConventions .................................................................................................50
4. STRINGS ...................................................................................................................................51
4.1 Definition .............................................................................................................................51
4.2 StringgIndexing .....................................................................................................................51
4.2 StringgSlicing ........................................................................................................................52
4.3 StringgOperators ...................................................................................................................53
4.4 Built-ingStringgFunctions ......................................................................................................54
4.5 Built-ingStringgMethods ........................................................................................................54
4.6 ModifyinggStrings ................................................................................................................58
www.SunCam.com Copyrightg2019gKwabenagOfosu,gPh.D.,gP.E.,gPTOE iii ofg6
, 362.pdf
IntroductiongtogComputergProgramminggingPython
AgSunCamgonlinegcontinuinggeducationgcourse
4.7 FormattedgStringgLiteral ...................................................................................................... 59
5. LISTSgANDgTUPLES ................................................................................................................61
5.1 Lists ......................................................................................................................................61
5.2 ManipulatinggagList.............................................................................................................. 62
5.3 ListgMethods .........................................................................................................................67
5.4 Tuples ...................................................................................................................................69
5.5 ManipulatinggTuples ............................................................................................................69
6. DICTIONARIESgANDgSETS ....................................................................................................71
6.1 Dictionary .............................................................................................................................71
6.2 ManipulatinggDictionaries ................................................................................................... 72
6.3 DictionarygMethods ..............................................................................................................75
6.4 Sets .......................................................................................................................................77
6.5 ManipulatinggSets .................................................................................................................78
6.6 SetgOperationsgandgMethods .................................................................................................78
7. RUNNINGgTHEgPYTHONgFILE...............................................................................................81
7.1 CodegExecution ....................................................................................................................81
7.2 ThegFilegEditor ......................................................................................................................82
7.3 PracticalgExample .................................................................................................................87
7.4 WindowsgCommandgPrompt ..............................................................................................103
8. CONCLUSION ........................................................................................................................115
REFERENCES .............................................................................................................................116
www.SunCam.com Copyrightg2019gKwabenagOfosu,gPh.D.,gP.E.,gPTOE iv ofg6