Python Programming for Geomatics
(GEO1000)
Chapter 1: The way of the program
1.1 What is a program?
A program is a sequence of instructions that specifies how to perform a computation. The
details look different in different languages, but a few basic instructions appear in just about
every language:
Input: get data from the keyboard, a file, the network, or some other device.
Output: display data on the screen, save it in a file, send it over the network, etc.
Math: perform basic mathematical operations like addition and multiplication.
Conditional execution: check for certain conditions and run the appropriate code.
Repetition: perform some action repeatedly, usually with some variation.
1.5 Values and types
A value is one of the basic things a program works with, like a letter or a number.
Name Example Type
Integer 2 int
Floating-point number 42.0 float
String ‘Hello, World!’ str
1.6 Formal and natural languages
Natural languages: are the languages people speak, such as English, Spanish and
French. They were not designed by people they evolved naturally.
Formal languages: are languages that are designed by people for specific applications.
For example, the notation that mathematicians use in a formal language that is
particularly good at denoting relations among numbers and symbols. Formal languages
tend to have strict end to have strict syntax rules that govern the structure of statements.
Programming languages are formal languages that have been designed to express
computations.
1.7 Debugging
Programmers make mistakes. For whimsical reasons, programming errors are called bugs
and the process of tracking them down is called debugging.
1.8 Glossary
Problem solving: The process of formulating a problem, finding a solution, and
expressing it.
High-level language: A programming language like Python that is designed to be easy for
humans to read and write.
Low-level language: A programming language that is designed to be easy for a computer
to run: also called “machine language” or “assembly language”.
Portability: A property of a program that can run on more than one kind of computer.
Interpreter: A program that reads another program and executes it.
, Prompt: Characters displayed by the interpreter to indicate that it is ready to take input
from the user.
Program: A set of instructions that specifies a computation.
Print statement: An instruction that causes the Python interpreter to display a value on
the screen.
Operator: A special symbol that represents a simple computation like addition,
multiplication, or string concatenation.
Value: One of the basic units of data, like a number or string, that a program manipulates.
Type: A category of values. The types we have seen so far are integers (type int),
floating-point numbers (type float), and strings (type str).
Integer: A type that represents whole numbers.
Floating-point: A type that represents numbers with fractional parts.
String: A type that represents sequences of characters.
Natural language: Any one of the languages that people speak that evolved naturally.
Formal language: Any one of the languages that people have designed for specific
purposes, such as representing mathematical ideas or computer programs; all
programming languages are formal languages.
Token: One of the basic elements of the syntactic structure of a program, analogous to a
word in a natural language.
Parse: To examine a program and analyze the syntactic structure.
Bug: An error in a program.
Debugging: The process of finding and correcting bugs.
Chapter 2: Variables, expressions and statements
2.1 Assignment statements
A common way to represent variables on paper is to write the name with an arrow pointing to
its value. This is called a state diagram.
2.2 Variable names
Variable names can’t begin with a
number. Python has special keywords
reserved which cannot function as variable names.
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
2.3 Expressions and statements
An expression is a combination of values, variables and operators. A value all by itself is
considered an expression and so is a variable. A statement is a unit of code that has an
effect, like creating a variable or displaying a value.
, 2.5 Order of operations
When an expression contains more than one operator, the order of evaluation depends on
the order of operations. For mathematical operators, Python follows mathematical convention
(acronym: PEMDAS):
- Parentheses
- Exponentiation
- Multiplication, division, addition and subtraction.
2.6 String operations
The + operator performs string concatenation, which means it joins the strings by linking
them end-to-end. For example:
first = ‘throat’
second = ‘warbler’
first + second
throatwarbler
The ∗ operator also works on strings; it performs repetition. For example: ‘Spam’ ∗3 is
‘SpamSpamSpam’.
2.7 Comments
As programs get bigger and more complicated, they get more difficult to read. For this
reason, it is a good idea to add notes to your programs to explain in natural language what
the program is doing. These notes are called comments and they start with the # symbol.
2.8 Debugging
Three kinds of errors can occur in a program: syntax errors, runtime errors and semantic
errors.
Syntax error: ‘Syntax’ refers to the structure of a program and the rules about that
structure. For example: 1 + 2) is a syntax error.
Runtime error: A runtime error does not appear until after the program has started
running. These errors are also called exceptions because they indicate that something
exceptional (and bad) has happened.
Semantic error: If there is a semantic error your program will run without generating error
messages, but it will not do the right thing.
2.9 Glossary
Variable: A name that refers to a value.
Assignment: A statement that assigns a value to a variable.
State diagram: A graphical representation of a set of variables and the values they refer
to.
Keyword: A reserved word that is used to parse a program; you cannot use keywords like
if, def and while as variable names.
Operand: One of the values on which an operator operates.
Expression: A combination of variables, operators and values that represents a single
result.
Evaluate: To simplify an expression by performing the operations in order to yield a single
value.
Statement: A section of code that represents a command or action.
Execute: To run a statement and do what it says.
Interactive mode: A way of using the Python interpreter by typing code at the prompt.