Programming in geoinformatics
Autumn 2017
# Hi, I am a comment!
## assigning values
students = 15 # int
lat = 49.204186 # float
lon = 16.598004 # float
address = "Kotlářská 2" # string
VARIABLE ASSIGNMENT
# Hi, I am a comment!
## assigning values
students = 15 # int
lat = 49.204186 # float
lon = 16.598004 # float
address = "Kotlářská 2" # string
## mixing data types
bad1 = 15students # expects int/float
bad2 = 'Kotlářská'2
## error
bad3 =
bad4 = 'Kotlářská
bad5 = 10 000
## will be stored as a wrong type!
bad6 = 49,204186
VARIABLE ASSIGNMENT
# Hi, I am a comment!
## assigning values
students = 15 # int
lat = 49.204186 # float
lon = 16.598004 # float
address = "Kotlářská 2" # string
## mixing data types
bad1 = 15students # expects int/float
bad2 = 'Kotlářská'2
## error
bad3 =
bad4 = 'Kotlářská
bad5 = 10 000
## will be stored as a wrong type!
bad6 = 49,204186
point1 = Brno
the_answer = 42
theAnswer = 42
VARIABLE ASSIGNMENT
# Hi, I am a comment!
## assigning values
students = 15 # int
lat = 49.204186 # float
lon = 16.598004 # float
address = "Kotlářská 2" # string
## mixing data types
bad1 = 15students # expects int/float
bad2 = 'Kotlářská'2
## error
bad3 =
bad4 = 'Kotlářská
bad5 = 10 000
## will be stored as a wrong type!
bad6 = 49,204186
point1 = Brno
the_answer = 42
theAnswer = 42
12apes = 'movie' # beggining with a number
the-answer = 42 # special characters
the answer = 42 # spaces
round = 1 # built-in names and keywords
VARIABLE ASSIGNMENT
latitude = 49.2041869
longitude = 16.5980044
address = 'Kotlářská 2'
print 'Hi there!'
print 'Kotlářská 267/2, Brno, 602 00'
## using variables
print address
print 'I am at: ' + address
print 'I am at %s, N%s E%s' % (address, latitude, longitude)
city = raw_input('Where are you? ')
print city + ' is the best!'
USER INPUT
city = raw_input('Where are you? ')
print city + ' is the best!'
USER INPUT
COMMENTS
# comments begin with a hash sign
notAComment # comment at the end of the line
city = raw_input('Where are you? ')
print city + ' is the best!'
USER INPUT
COMMENTS
# comments begin with a hash sign
notAComment # comment at the end of the line
use print to make a pyramid in the console
# example
*
***
*****
# example
Tomato: 5
Cucumber: 15
Eggplant: 42
INPUT
# example
Tomato: 5
Cucumber: 15
Eggplant: 42
BOOLEAN VALUES
True
False
BOOLEAN VALUES
True
False
INT AND FLOAT
students = 15
lat = 49.2041869
lon = 16.5980044
BOOLEAN VALUES
True
False
INT AND FLOAT
students = 15
lat = 49.2041869
lon = 16.5980044
STRINGS
address = 'Kotlářská 2'
BOOLEAN VALUES
True
False
INT AND FLOAT
students = 15
lat = 49.2041869
lon = 16.5980044
STRINGS
address = 'Kotlářská 2'
TESTING TYPES
type(lat)
type(address)
OPERATIONS
3 + 5
"Hello " + user # but also this!
5 - 3
"bad" - "b" # this throws error! what did you expect …
10 * 3
"-" * 5 # "-----" this also works! (can be very useful)
3 / 2 # = 1 ! careful!
3.0 / 2 # either like this
3 / float(2) # or like this
5 % 4 # = 1; modulo
3 ** 2 # power
## comparing booleans
True or False
True and 1
True and 5
not False
MODIFYING VARIABLES
a = 5
a = a + 3 # a = 8
a += 1 # a = 9; easier, right?
a ++ # a = 10; this is even better
a -= 10 # also *= /=
OPERATIONS
3 + 5
"Hello " + user # but also this!
5 - 3
"bad" - "b" # this throws error! what did you expect …
10 * 3
"-" * 5 # "-----" this also works! (can be very useful)
3 / 2 # = 1 ! careful!
3.0 / 2 # either like this
3 / float(2) # or like this
5 % 4 # = 1; modulo
3 ** 2 # power
## comparing booleans
True or False
True and 1
True and 5
not False
MODIFYING VARIABLES
a = 5
a = a + 3 # a = 8
a += 1 # a = 9; easier, right?
a ++ # a = 10; this is even better
a -= 10 # also *= /=
OPERATIONS
3 + 5
"Hello " + user # but also this!
5 - 3
"bad" - "b" # this throws error! what did you expect …
10 * 3
"-" * 5 # "-----" this also works! (can be very useful)
3 / 2 # = 1 ! careful!
3.0 / 2 # either like this
3 / float(2) # or like this
5 % 4 # = 1; modulo
3 ** 2 # power
## comparing booleans
True or False
True and 1
True and 5
not False
COMPARISONS
"A" < "B" # less than (alphabet order)
lat <= 90 # less than or equal to
5 > 3 # greater than
city == "Brno" # equal to
city != "Praha" # not equal to
SUBSTRINGS
message = "hello everyone"
message[0] # 'h' → character at index 0
message[-1] # 'e' → character at the last index
message[0:5] # 'hello' → from index 0 to (not including!) index 5
message[6:] # from index 6 to the end
STRING OPERATIONS
"a" in message # False
"hello" in message # True
"hello " + user # merging strings, we know this already
"-" * 5 # string repetition, we know this too
STRING METHODS
message.find("l") # 2
message.find("a") # -1
message.count("l") # 2
len(message) # 14
message.replace("hello", "bye") # "bye everyone"
STRING METHODS
message.find("l") # 2
message.find("a") # -1 (e.g. there is no "a")
message.count("l") # 2
len(message) # 14
message.replace("hello", "bye") # "bye everyone"
STRING METHODS
message.find("l") # 2
message.find("a") # -1 (e.g. there is no "a")
message.count("l") # 2
len(message) # 14
message.replace("hello", "bye") # "bye everyone"
CONVERTING DATA TYPES
students = 5
print "Number of students: " + students # ERROR
print "Number of students: " + str(students) # correct
MATH
use exercise 3 and calculate the total cost where:
vegetable1_amount = 10
vegetable2_amount = 35
vegetable3_amount = 7
MATH
ROUNDING
ROUNDING
round to two decimal places:
lat = 49.2041869
lon = 16.5980044
STRINGS
variables raster1 .. 3 are rows of an aerial image after surface classification (1 – forest, 2 – water, 3 – field)
raster1 = "133322232111"
raster2 = "1132111223223"
raster3 = "22111332211122"
print the following:
NAME RECOGNISER
FORTUNE TELLER
hint: use str.count() and len()
PARTY CALCULATOR
DISTANCES
brnoX = -598112
brnoY = -1160187
ostravaX = -470650
ostravaY = -1101660