lecture 1 – Basics

Programming in geoinformatics

Autumn 2017

BASICS

# Hi, I am a comment!

## assigning values
students = 15           # int
lat = 49.204186         # float
lon = 16.598004         # float
address = "Kotlářská 2" # string

DO

VARIABLE ASSIGNMENT

BASICS

# Hi, I am a comment!

## assigning values
students = 15           # int
lat = 49.204186         # float
lon = 16.598004         # float
address = "Kotlářská 2" # string

DO

## 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

DON'T

VARIABLE ASSIGNMENT

BASICS

# Hi, I am a comment!

## assigning values
students = 15           # int
lat = 49.204186         # float
lon = 16.598004         # float
address = "Kotlářská 2" # string

DO

## 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

DON'T

point1 = Brno
the_answer = 42
theAnswer = 42

VARIABLE ASSIGNMENT

BASICS

# Hi, I am a comment!

## assigning values
students = 15           # int
lat = 49.204186         # float
lon = 16.598004         # float
address = "Kotlářská 2" # string

DO

## 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

DON'T

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

BASICS

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)

PRINT

BASICS

city = raw_input('Where are you? ')
print city + ' is the best!'

USER INPUT

BASICS

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

BASICS

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
  • comments shouldn't be neccessary to understand the code
  • use self-explanatory variable names
    • bad names: var1, var2, aaa, a, ab
    • good names: cities, latDD, lonDMS

EXERCISE 1

PRINT

use print to make a pyramid in the console

# example

  *  
 *** 
*****

EXERCISE 2

PRINT

  • create variables name and age with your name and age
  • print "Hi, my name is name, and I'm age"

EXERCISE 3

PRINT

  • create variables vegetable1, vegetable2, vegetable3 with any vegetable names
  • create variables vegetable1_price, vegetable2_price, vegetable3_price with any prices
  • print available vegetables with their prices 
# example

Tomato: 5
Cucumber: 15
Eggplant: 42

EXERCISE 4

INPUT

  • do the same as in the previous exercise, but get the vegetable prices from the user
  • use raw_input()
# example

Tomato: 5
Cucumber: 15
Eggplant: 42

BASICS

BOOLEAN VALUES

True
False

BASICS

BOOLEAN VALUES

True
False

INT AND FLOAT

students = 15
lat = 49.2041869
lon = 16.5980044

BASICS

BOOLEAN VALUES

True
False

INT AND FLOAT

students = 15
lat = 49.2041869
lon = 16.5980044

STRINGS

address = 'Kotlářská 2'

BASICS

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)

BASICS

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 *= /=

BASICS

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 *= /=

BASICS

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

BASICS

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

BASICS

STRING METHODS

message.find("l")     # 2
message.find("a")     # -1
message.count("l")    # 2
len(message)          # 14
message.replace("hello", "bye") # "bye everyone"

BASICS

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"

BASICS

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

EXERCISE 5

MATH

use exercise 3 and calculate the total cost where:

vegetable1_amount = 10
vegetable2_amount = 35
vegetable3_amount = 7

EXERCISE 6

MATH

  • get variable values circle_radius and square_side from the user
  • calculate circle and square areas
  • print which one has the larger area

EXERCISE 7

ROUNDING

  • one shop rounds their prices and the other doesn't
  • print the difference between the total cost, where:
    • 4 x item1 (15.29)
    • 16 x item2 (1.99)
    • 2 x item3 (18.49)

EXERCISE 8

ROUNDING

round to two decimal places:

lat = 49.2041869
lon = 16.5980044

EXERCISE 9

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:

  • which row contains the most forest cells
  • which row contains a water cell that has a forest to the left
  • which value is the most frequent if we count only the third up to sixth column

HOMEWORK 1

NAME RECOGNISER

  • let the user input his name and save it to variable name
    • the name should consist of the first and the last name separated by space (e.g. "Santa Claus")
  • create two new variables (first and last) and save the first and last names to them
  • print them to console
  • hint: use str.find() and str[]
  • hint: use more than three variables in total

HOMEWORK 2

FORTUNE TELLER

  • create a guessing application that estimates how long will a person live based on his name
  1. multiply the number of characters in persons name by 4
  2. add 5 years for every "a" and "e" character
  3. subtract 5 years for every "i" and "o" character
  4. print the estimated age and the persons name

 

hint: use str.count() and len()

HOMEWORK 3

PARTY CALCULATOR

  • create an application that will calculate the cost of a party
  • ask the user for the number of adults, children and entry fee
  • icecream price is 19.99 and beer price is 12.99
  • children eat two icecreams each
  • adults eat only one icecream but drink 4 beers

  • the result will be the cost of tickets minus the total cost of icecreams and beers

HOMEWORK 4

DISTANCES

  • calculate the distance between Brno and Ostrava with their coordinates in S-JTSK
  • use Pythagorean theorem (a² + b² = c²)
brnoX = -598112
brnoY = -1160187
ostravaX = -470650
ostravaY = -1101660

Lecture 1 - Basics

By Šimon Leitgeb

Lecture 1 - Basics

  • 233