University of Energy and Natural Resources
CIVL/EENG 208: COMPUTER PROGRAMMING FOR ENGINEERS
MR. EBENEZER K. SIABI
EARTH OBSERVATION RESEARCH AND INNOVATION CENTER
(EORIC)
OUTLINE
• INTRODUCTION TO PYTHON
• PHILOSOPHY OF PYTHON
• PYTHON & ENGINEERING
• MODULA PROGRAMMING
• INTEGRATED DEVELOPMENT ENVIRONMENTS (IDEs)
• VARIABLES & DATA TYPES
• OPERATORS
• ARRAYS
• PLOTTING
• FUNCTIONS AND LOOPS
OBJECTIVE
• The course will equip students with knowledge and skills in
applying computer programming methods to solve engineering
problems.
• It is designed for students with no prior programming experience.
This course introduces the fundamental concepts of procedural
programming.
Mode of Delivery
• Lectures, seminars, practical and hands-on exercises in
programming.
Popular tools used in data science CIVL/EENG
208
• Data pre-processing and analysis (Python, R, Microsoft Excel,
SAS, SPSS)
• Data exploration and visualization (Tableau, Qlikview, Microsoft
Excel)
• Parallel and distributed computing incase of big data (Apache
Spark, Apache Hadoop)
CIVL/EENG
208
Philosophy and History of Python CIVL/EENG
208
• What ties the alphabet and the Python computer language together? Yes, both begin with ABC. The programming
language ABC is clearly intended if we are referring to ABC in terms of Python.
• A general-purpose programming language and environment called ABC was created at the CWI in Amsterdam,
Netherlands (Centrum Wiskunde & Informatica). The most notable accomplishment of ABC was its ability to impact
Python's design.
• What do you think of Python?
• Although the logo also features two snakes, which is what most people associate with the word, the name actually
has roots in British mockery. The Python programming language's developer, Guido van Rossum, wrote the following
in 1996 on how the name came to be: I was seeking for a 'hobby' programming project to keep me busy for the week
leading up to Christmas more than six years ago, in December 1989.
• I made the decision to create an interpreter for the new programming language I have been considering lately—an
ABC clone that would be useful to Unix/C hackers. Being in a little irreverent mindset and a big fan of Monty
Python's Flying Circus, I gave the project the working title Python.
CIVL/EENG
208
Evolution of Python CIVL/EENG
208
• Python was developed by Guido van Rossum in the late eighties at
the ‘National Research Institute for Mathematics and Computer
Science’ at Netherlands
• Python Editions
• Python 1.0
• Python 2.0
• Python 3.0
CIVL/EENG
208
Python as a programming language CIVL/EENG
208
• Supports multiple programming paradigm (Functional, Structural,
OOPs, etc.)
• Dynamic typing (Runtime type safety checks)
• Reference counts (Deallocates objects which are not used for long)
• Late binding (Methods are looked up by name during runtime)
• Python’s design is guided by 20 aphorisms as described in Zen of
Python by Tim Peters
CIVL/EENG
208
Python as a programming language Con’t CIVL/EENG
208
• Standard CPython interpreter is managed by “Python Software
Foundation”
• There are other interpreters namely JPython (Java), Iron Python (C#),
Stackless Python (C, used for parallelism), PyPy (Python itself JIT
compilation)
• Standard libraries are written in python itself
• High standards of readability
CIVL/EENG
208
Python as a programming language Con’t CIVL/EENG
208
• Cross-platform (Windows, Linux, Mac)
• Highly supported by a large community group
• Better error handle
CIVL/EENG
208
Python as a programming language Con’t CIVL/EENG
208
Comparison to Java
• Python vs Java
• Java is statically typed i.e. type safety is checked during compilation
(static compilation). Thus in Java the time required to develop the code
is more
• Python which is dynamically typed compensates for huge
compilation time when compared to Java
• Codes which are dynamically typed tend to be less verbose therefore
offering more readability
CIVL/EENG
208
Advantages of using python CIVL/EENG
208
• Python has several features that make it well suited for data science
• Open source and community development
• Developed under Open Source Initiative license making it free to use
and distribute even commercially
• Syntax used is simple to understand and code
• Libraries designed for specific data science tasks
• Combines well with majority of the cloud platform service providers
CIVL/EENG
208
Python support CIVL/EENG
208
• Python 2.x support will be available till 2020
• Python 3.x is an enhanced version of 2.x and will only be maintained from
3.6.x post 2020
• Install basic python version or use the online python console as in
https://www.python.org/
CIVL/EENG
208
Coding environment CIVL/EENG
208
• A software program can be written using a terminal, a command prompt
(cmd), a text editor or through an Integrated Development Environment
(IDE)
• The program needs to be saved in a file with an appropriate extension (.py for
python, .mat for matlab, etc...) and can be executed in corresponding
environment (Python, Matlab, etc…)
• Integrated Development Environment (IDE) is a software product solely
developed to support software development in various or specific
programming language(s)
CIVL/EENG
208
Integrated development environment (IDE) CIVL/EENG
208
• Software application consisting of a cohesive unit of tools required for
development
• Designed to simplify software development
• Utilities provided by IDEs include tools for managing, compiling, deploying
and debugging software
CIVL/EENG
208
Coding environment- IDE CIVL/EENG
208
• An IDE usually comprises of
◦ Source code editor
◦ Compiler
◦ Debugger
◦ Additional features include syntax and error highlighting, code
completion
• Offers supports in building and executing the program along with debugging
the code from within the environment
CIVL/EENG
208
Coding environment- IDE CIVL/EENG
208
• The Python programming language's interactive shell is sometimes known as
REPL (Read-Evaluate-Print-Loops) since it
• reads what a user types
• evaluates what it reads
• prints out the return value after evaluation, and
• loops back and does it all over again.
CIVL/EENG
208
Coding environment- IDE CIVL/EENG
208
• Best IDEs provide version control features
• Eclipse+PyDev, SublimeText, Atom, GNU Emacs, Vi/Vim, Visual Studio,
Visual Studio Code are general IDEs with python support
• Apart from these some of the python specific editors include
• Pycharm, Jupyter, Spyder, Thonny
CIVL/EENG
208
Spyder CIVL/EENG
208
• Supported across Linux, Mac OS X and Windows platforms
• Available as open source version
• Can be installed separately or through Anaconda distribution
• Developed for Python and specifically data science
• Features include
◦ Code editor with robust syntax and error highlighting
◦ Code completion and navigation
◦ Debugger
◦ Integrated document
CIVL/EENG
208 • Interface similar to MATLAB and RStudio
Coding environment- IDE CIVL/EENG
208
• This type of interactive workspace is extremely beneficial for facilitating the
prototyping of a problem where each step's outcome can be seen in real time.
• Python has the following features in addition to making it simple and rapid to
evaluate statements:
• A searchable history
• Tab completion
• Many helpful key bindings
• Help and documentation
CIVL/EENG
208
How to choose the best IDE? CIVL/EENG
208
• Requirements
• Working with different IDEs helps us understand our own
• requirement
CIVL/EENG
208
High-Level Programming CIVL/EENG
208
• A programming language that is decoupled from the specifics of a given
computer is known as a high-level language. A low-level language is one that
allows users to submit machine code directly to a processor to receive results.
• A high-level language offers tools, such as library functions, for carrying out
low-level operations. It also offers a mechanism to specify the code in a human-
readable format, which is subsequently translated into machine language and
supplied to a processor.
CIVL/EENG
208
Low Vs High level programming CIVL/EENG
208
• High-level languages are easier to interpret and
• Low-level languages are also known as machine-
compile compared to low-level languages.
level languages.
• They are user-friendly and easy to comprehend.
• They are easily understood by machines.
• These languages are designed with the programmer in
• Low-level languages are designed to be
mind.
machine-friendly.
• Debugging in high-level languages is relatively
• Debugging low-level languages can be
straightforward.
challenging.
• They are easy to maintain, making them simple and
• They can be difficult for humans to understand.
manageable.
• Maintaining these languages can be complex.
• High-level languages can be executed on various
• Low-level languages are not portable.
platforms.
• Low-level languages require assemblers to
• They require a compiler or interpreter to translate into
translate instructions.
machine code.
CIVL/EENG
208
Interpreter Vs Compiler
• We generally write a computer program using a high-level language. A high-level
language is one that is understandable by us, humans. This is called source code.
However, a computer does not understand high-level language. It only understands
the program written in 0's and 1's in binary, called the machine code.
• To convert source code into machine code, we use either a compiler or an interpreter.
• Both compilers and interpreters are used to convert a program written in a high-level
language into machine code understood by computers. However, there are
differences between how an interpreter and a compiler works.
CIVL/EENG
208
Interpreter Vs Compiler CIVL/EENG
208
• Translates program one statement at a • Scans the entire program and translates
time. it as a whole into machine code.
• Interpreters usually take less amount of • Compilers usually take a large amount of
time to analyze the source code. time to analyze the source code.
However, the overall execution time is However, the overall execution time is
comparatively slower than compilers. comparatively faster than interpreters.
• No Object Code is generated, hence are • Object Code which further requires
memory efficient. Generates linking, hence requires more memory.
• Programming languages like JavaScript, • Programming languages like C, C++,
Python, Ruby use interpreters. Java use compilers.
CIVL/EENG
208
Object-oriented programming CIVL/EENG
208
• The majority of early programming languages were procedural. In other words,
to compute a problem, a set of procedures was established, and the information
flow was managed inside these procedures to obtain an intended result.
• On the other hand, data is treated as an object in object-oriented programming
(OOP) languages, where several methods interact with the object to generate
the intended outcome.
• The ability to create custom objects and employ programming principles in a
variety of contexts is made feasible by objects' abstract nature.
CIVL/EENG
208
Multipurpose Nature CIVL/EENG
208
• OOP-based architecture enables programmers from all backgrounds to utilize and
improve the language in their specialized domains.
• Due to the frequent creation of new modules, it is impossible to include all of the modules
for a certain application.
• Modules are being used and developed by programmers from all over the world at an
incredibly fast rate.
• Python is the programming language with the widest range of applications since it can be
utilized in so many different computing disciplines.
CIVL/EENG
208
Multipurpose Nature Con’t CIVL/EENG
208
Field of Study Name of Python Module
scientific Computation scipy, numpy, sympy
statistics pandas
Networking Networkx
Cryptography pyOpenSSL
Game Development PyGame
Graphic User interface pyQT
Machine learning scikit-learn, tensorflow, keras
image processing scikit-image
plotting Matplotlib
CIVL/EENG
208
Modular Programming CIVL/EENG
208
• The modular nature of Python programming incorporates the complex tasks being divided
into small modules that seamlessly interact with each other,
• Modules make both development and debugging easier, and they can be simply imported
to enable the use of various functions. Python comes with thousands of modules to perform
various tasks.
• Below are a few basic necessary modules,
Package Name Meaning Purpose
numpy numerical python numerical computation
scipy scientific python scientific computations
sympy symbolic python symbolic computing
matplotlib Mathematical plotting library for plotting graphs
CIVL/EENG
208
Minimalistic Design CIVL/EENG
208
• The minimalistic design philosophy of Python means that it emphasizes code readability.
• It also provides a syntax structure that allows programmers to express concepts in fewer lines of code than in
languages such as C++ and Java.
• Python features a dynamic type system where a Python interpreter guesses the type of an object by its
definition, which avoids the user’s need to define the same.
• The core philosophy of the language is summarized by Tim Peters in the document The Zen of Python (PEP
20) which includes the following aphorisms:
• Beautiful is better than ugly.
• Explicit is better than implicit.
• Simple is better than complex.
• Complex is better than complicated.
• Flat is better than nested.
CIVL/EENG
208
Portability CIVL/EENG
208
• Since Python belongs to an open source community, it has been ported (that is, adapted to
work on) to many platforms so that Python code written on one platform can run without
modification on others (except system-dependent features).
• Python has been posted for popular operating systems including Linux, Windows,
Macintosh, Solaris, and Sony PlayStation
CIVL/EENG
208
Extensibility CIVL/EENG
208
• Rather than providing all functionalities in its core program, Python’s creators designed it to be highly
extensible.
• Users can thus choose to have functionality as per their requirements. For example, if a user needs to work on
differential equations, then that user can use a module for differential equations rather than all users having
that functionality. Python can also be embedded in existing applications that need a programmable interface.
• Python programs can be embedded into other programs written in programming languages such as Julia, C,
C++, and so on. Furthermore, other programming language codes can be embedded into Python.
• Hence, Python has been embraced with open arms by industry and academia alike.
CIVL/EENG
208
Python and Engineering CIVL/EENG
208
• Engineering problems employ numerical computations both on a small scale and on a large
scale. Thus, engineering applications require a programming language to fit well in both
these regimes.
• There are very few languages that can boast this quality, so Python is definitely a winner
here. While running large computational tasks on bigger computational architectures,
memory management, speed, and reliability are the key parameters.
• Python, being an interpretative language, is generally considered to be a slower option in
this regard, but its ability to use faster codes written in C, Java, and Fortran using the
interlinking packages cython, jython, and f2p allows speed-intensive tasks to be run in their
native language within a Python code.
CIVL/EENG
208
Python and Engineering CIVL/EENG
208
• Another engineering concern is the ability of a programming language to communicate
with physical devices efficiently. Electronic devices are connected via wires and Bluetooth
wireless technology to the Internet.
• Using an appropriate Python module, users can connect to a compatible device to derive
data from it and then visualize it in the desired platform.
CIVL/EENG
208
Python and Engineering Con’t CIVL/EENG
208
• A variety of microcontrollers allow Python to run its hardware with ease.
• MicroPython is a lean and efficient implementation of the Python 3 programming language,
which includes a small subset of the Python standard library and is optimized to run on
microcontrollers and in constrained environments.
• Even microcomputers like Raspberry Pi allow the running of Python programs accessing
the input-output devices. This enables cost-effective prototyping of an engineering
problem.
CIVL/EENG
208
Python and Engineering Con’t CIVL/EENG
208
• Users of MATLAB argue that Simulink is one of the easiest ways of prototyping and
simulating an engineering problem because they don’t need to code. Instead, users just
stitch together pieces of codes represented by graphical blocks on a graphics terminal.
(Scilab also provides a similar platform called Xcos.)
• Python still lacks this ability and budding programmers can take this up as a challenge. A
large community of developers is eagerly waiting for such a solution, but most engineers
won’t mind investing a day or two to learn a new programming language that can enhance
their productivity that a ready-made tool cannot provide
CIVL/EENG
208