00 Lecture MD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Fundamentals of Computer

Programming
Lecture slides - Introduction to Programming

© 2020 Mark Dixon


Intro

● This lesson covers


○ What is a program?

○ Types of programming language

○ Overview of Python

○ The development tools

○ Expressions

○ Errors
Overview of the Module

● This module aims to introduce the art of computer programming

● We will be using the Python programming language, although the aim is to


teach and understand the concepts applicable to all programming
● Python is one of the world's most widely used programming languages,
and relatively easy to understand
● The lecture notes are accompanied by a set of step-by-step tutorials, and a
number of exercises for self-testing purposes
● The tutorial, and many of the exercises will be delivered using a command
line program called the python interpreter
What is a Computer Program?

● A computer program is a sequence of instructions that are executed in


order to carry out a specific set of operations
● Programs take input, perform processing, and generate output
● Input and output (I/O) can include many sources (not just human via a
keyboard or mouse, and screen), e.g. I/O can be via files only
● Programmers write the computer programs, which implement algorithms
designed to solve a problem
● Programming languages allow us to write code that is easy to understand,
this is then translated into a form that can be executed by a computer
Generations of Programming Languages

● Programming languages can be divided into “generations”, which have


developed over time to make programming easier
● Languages are usually classified as a 1GL, 2GL, 3GL, 4GL, etc.

● First generation languages involved writing directly in machine code


● This is basically a list of numbers that instruct a CPU to perform operations
● Problem: A CPU has a very specific instruction set, that is not that easy to
understand by human standards
● Machine code therefore takes a long time to write, is hard to read, difficult
to debug and varies for different types of CPU
Second Generation Programming Languages (2GL)

● Assembly is a second generation programming language

● Removes the need to program with numbers and introduces simple


mnemonics that are mapped directly into CPU machine code

● One mnemonic usually equates to one CPU instruction, e.g.

add r1, r1, r2 # gets mapped to a single CPU instruction

● The tool that converts mnemonics to machine code is called an Assembler


● Problems:
● Different CPUs require different assembly languages (code not portable)
● Programming is still fairly difficult and time consuming
Third Generation Programming Languages (3GL)

● Third generation languages are the most commonly used, they include -

○ C, COBOL, Pascal (procedural)

○ C++, Objective C, C#, Java (Object Oriented)

○ Python, JavaScript, PHP (multi-paradigm - scripting type).

● A Compiler translates these into Assembly/Machine code

● Languages such as C# and Java often execute on a virtual machine

● Scripting type languages are often Interpreted, or sometimes use Just in


Time (JIT) compilation
Fourth Generation Programming Languages (4GL)

● Fourth generation languages are more abstract from the underlying


machine, and are based on defining what is to be done, rather than how

● These type of languages tend to be less general purpose, and more


focussed on a particular problem domain, well known 4GLs include,

○ SQL (data manipulation)

○ R (statistics and data-science)

○ SPSS (statistical analysis)

● Although 4GLs are seen as more advanced than 3GLs, they are not a direct
replacement, especially for general purpose problems
Python - A Popular 3GL

● Python is easier to learn than many other languages and is widely used in
education and industry

● It supports the popular procedural, functional and object-oriented (OO)


styles of programming

● It can be used to build anything from simple programs (snippets) to


complex applications

● Although general purpose, it is popular in many specific areas such as


artificial intelligence, data science, forensics, and security

● It is a good starting point before moving on to learn other languages


Command Line Python and IDEs

● Initially we will be using a command-line Interpreter for learning Python

● You will have to learn the basics of how to use the Interpreter, but what you
are really trying to learn is how to program in Python

● Later on within the module you will write programs using a text editor then
execute the program files using the interpreter

● Other environments such as Jupyter Notebook, along with IPython are often
used during teaching (and may be referred to in books etc.)

● Don’t get confused between the various environments and the language
The “Python” Interpreter

● The python interpreter can be used in interactive-mode that allows you to


write and execute Python code
● It is based on a Read–Eval–Print Loop (REPL) interaction model
● The command line interpreter has a number of features that make
experimenting with Python easy, including -
○ An in-built help system (for both itself and Python)

○ Auto-completion while typing

○ Running and debugging

○ Command history
Python Interpreter - example screen

● In the above example several commands have been interactively input


(following the ‘>>>’ input prompt)
● Once <return> is hit the code is Read, Evaluated, and output is Printed
● The ‘up’ and ‘down’ arrow keys can be used to recall and allow editing of
previously input instructions
Integrated Development Environment

● IDEs are popular environments for developing software (support many


other languages as well as Python)
● Typically provide a text editor (with syntax highlighting), debuggers, version
control, help, etc.
● IDEs are much easier than using the Python interpreter when developing
large applications
● IDEs can be a complex to use, given their large amount of functionality
● Used by many software houses, so a useful skill to develop in the future
Software Resources

● All of the software we use on this module is freely available

● These lecture notes (along with the associated tutorials and exercises) will
be made available for download for use away from the University

● Be careful when downloading Python or looking at resources.

● Python 3.x is quite different to version 2.7 and earlier

● The provided notes will assume you are using Python version 3.7 or later.

● Any text editor can be used to develop programs later in the module
Python Libraries

● Python programs rely heavily on libraries (which contain pre-written code)


● They often perform significant tasks with modest amounts of code, and
help avoid “reinventing the wheel”
● The Python Standard Library itself provides lots of rich capabilities, but
many additional libraries are also available
● This module will focus primarily upon the language rather than the
libraries, but we will use parts of the standard library within the module
● Python itself is a fairly small language, so knowing how to find and use 3rd
party libraries is key to development e.g. the Visualization libraries are
extremely powerful and popular with data-scientists
Expressions within Python

● An easy way to start programming, and becoming accustomed with the


tools, is to write expressions
● Expressions are used in just about every program you will ever write
● Expressions consist of operands and operators, e.g.
45 + 20
● In this example the operands are the numbers 45 and 20 and the operator
is the +
● Typing an expression into the python interpreter, causes the result to be
immediately displayed (note: this only occurs in interactive mode)
Common Python Operators

● Other common operators used within Python expressions include -

** (exponentiation), i.e. raise to the power


* (multiplication)
/ (division)
// (floor division)
% (remainder)
- (subtraction)
Operator Precedence

● Expression evaluation is not always performed left to right, there is an


operator precedence at work, e.g. multiplication occurs before addition -
>>> 10 + 5 * 2
20
● The precedence of the common operators we have seen so far is -
parentheses ( )
exponentiation (**)
multiplication (*), division (/), floor division (//) and remainder (%)
addition (+) and subtraction (-)

● Operators at the same level are evaluated left to right


Errors

● When writing computer programs errors are inevitable


● The ability to find and fix errors is a key skill required by any programmer
● Errors are generally categorized as syntax errors or logical errors
● Syntax errors are usually the most common, but also the most easy to fix
○ These occur when the programming language syntax has not been used correctly

● Logical errors occur during run-time, and can be hard to find and fix
○ These occur when a program is syntactically correct, but the underlying algorithm was
incorrectly designed or poorly implemented

○ These type of errors are commonly referred to as "bugs"


Dealing with Errors

● People new to programming spend most of their time fixing syntax errors
● The development tools will report these (often prior to program execution),
so ensure you read the error report and then fix the error
● Logical errors do not always result in an error being reported, and have to
be identified through testing
● Occasionally errors will be reported at run-time, and displayed as a
“traceback” message. Although they may seem cryptic these allow the
problem to be identified and fixed
● A good program will be written in a way that prevents such run-time errors
occuring, or reports them gracefully to the user
Summary

● A program instructs a computer to perform specific operations

● There are many types of programming language, we will be using Python


which is an interpreted 3GL
● We will be initially programming in interactive-mode to allow
experimentation and rapid feedback of results

● Libraries are very useful and save a lot of time, but only start to use these
once you understand the language itself

● Writing expressions and dealing with common errors is a good place to


start - tip: ALWAYS READ ERROR MESSAGES
Useful Resources

● The Python web-site


https://www.python.org/

● The Python 3.x tutorial


https://docs.python.org/3/tutorial/

You might also like