programming and problem-solving

104
Departement Informatik Programming and Problem-Solving Introduction to the Course Dennis Komm Spring 2021 – February 25, 2021

Upload: others

Post on 18-Dec-2021

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming and Problem-Solving

Departement Informatik

Programmingand Problem-SolvingIntroduction to the Course

Dennis Komm

Spring 2021 – February 25, 2021

Page 2: Programming and Problem-Solving

Welcome to the Course

Page 3: Programming and Problem-Solving

Material

Lecture website

https://lec.inf.ethz.ch/ppl

Moodle Course

https://moodle-app2.let.ethz.ch/course/view.php?id=14883

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 1 / 36

Page 4: Programming and Problem-Solving

The Team

Lecturer Dennis Komm

Assistants Manuela FischerJonas HeinDavid SommerCathirn ElichLea FritschiSarah KampSafira PiaskoSara Steiner

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 2 / 36

Page 5: Programming and Problem-Solving

Appointments

Lecture Thursday, 16:15 – 18:00

Exercises Monday, 13:15 – 15:00Thursday, 10:15 – 12:00

Exam End of the semester

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 3 / 36

Page 6: Programming and Problem-Solving

Goal of Today’s Lecture

General information about the lecture

The projects, using [code]expert

Introduction to Algorithms

The first Python program

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 4 / 36

Page 7: Programming and Problem-Solving

Introduction to the CourseComputers and Algorithms

Page 8: Programming and Problem-Solving

Computer – Concept

What does a computer have to be able to do to compute?

Does it have to be able to multiply?

Isn’t it sufficient to be able to add?

Turing Machine [Alan Turing, 1936]

Finite number of states

Memory consisting of arbitrarily many cells

Pointer to current cell

Pointer can change cell’s content and move left orright

Alan Turing [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 5 / 36

Page 9: Programming and Problem-Solving

Computer – Concept

What does a computer have to be able to do to compute?

Does it have to be able to multiply?

Isn’t it sufficient to be able to add?

Turing Machine [Alan Turing, 1936]

Finite number of states

Memory consisting of arbitrarily many cells

Pointer to current cell

Pointer can change cell’s content and move left orright

Alan Turing [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 5 / 36

Page 10: Programming and Problem-Solving

Computer – Implementation

Analytical Engine – Charles Babbage (1837)

Z1 – Konrad Zuse (1938)

ENIAC – John von Neumann (1945)

Charles Babbage [Wikimedia] Konrad Zuse [Wikimedia] John von Neumann [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 6 / 36

Page 11: Programming and Problem-Solving

Algorithm: Central Notion of Computer Science

Algorithm

Method for step-by-step solution of a problem

Execution does not require intellect, only accuracy

after Muhammad al-Chwarizmi;author of a arabicmath book (around 825)

"‘Dixit algorizmi. . . "’ Latin translation [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 7 / 36

Page 12: Programming and Problem-Solving

Algorithm: Central Notion of Computer Science

Algorithm

Method for step-by-step solution of a problem

Execution does not require intellect, only accuracy

after Muhammad al-Chwarizmi;author of a arabicmath book (around 825)

"‘Dixit algorizmi. . . "’ Latin translation [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 7 / 36

Page 13: Programming and Problem-Solving

Algorithm: Central Notion of Computer Science

Algorithm

Method for step-by-step solution of a problem

Execution does not require intellect, only accuracy

after Muhammad al-Chwarizmi;author of a arabicmath book (around 825)

"‘Dixit algorizmi. . . "’ Latin translation [Wikimedia]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 7 / 36

Page 14: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b 6= 0if a > b then

a = a− b

elseb = b− a

Output: a

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 15: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b 6= 0if a > b then

a = a− b

elseb = b− a

Output: a

a b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 16: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b 6= 0if a > b then

a = a− b

elseb = b− a

Output: a

a b a b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 17: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b 6= 0if a > b then

a = a− b

elseb = b− a

Output: a

a b a b a b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 18: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b 6= 0if a > b then

a = a− b

elseb = b− a

Output: a

a b a b a b a b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 19: Programming and Problem-Solving

“The Oldest (Known) Non-Trivial Algorithm”

Euclid’s Algorithmfrom Euclid’s Elements, 300 BC

Input: integers a > 0, b > 0Output: gcd of a and b

Input: a and b

while b != 0:if a > b:

a = a− b

else:b = b− a

Output: a

a b a b a b a b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 8 / 36

Page 20: Programming and Problem-Solving

Introduction to the CourseGoals

Page 21: Programming and Problem-Solving

1. Computer Science in the Natural Sciences

problem from praxis

computer scienceproblem

solution to comp.science problem

solution to problemfrom practice

ACTGCATGGC

ACGCTAAGCACTGCATGGCCAA

ACGCTAAGCACTGCATGGCCAA

modeling

interpretation

communication between

practice and comp. science

needs basic knowledge

?

algorithmics,concepts ofprogramming

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 9 / 36

Page 22: Programming and Problem-Solving

1. Computer Science in the Natural Sciences

problem from praxiscomputer science

problem

solution to comp.science problem

solution to problemfrom practice

ACTGCATGGC

ACGCTAAGCACTGCATGGCCAA

ACGCTAAGCACTGCATGGCCAA

modeling

interpretation

communication between

practice and comp. science

needs basic knowledge

?algorithmics,concepts ofprogramming

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 9 / 36

Page 23: Programming and Problem-Solving

1. Computer Science in the Natural Sciences

problem from praxiscomputer science

problem

solution to comp.science problem

solution to problemfrom practice

ACTGCATGGC

ACGCTAAGCACTGCATGGCCAA

ACGCTAAGCACTGCATGGCCAA

modeling

interpretation

communication between

practice and comp. science

needs basic knowledge

?algorithmics,concepts ofprogramming

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 9 / 36

Page 24: Programming and Problem-Solving

1. Computer Science in the Natural Sciences

problem from praxiscomputer science

problem

solution to comp.science problem

solution to problemfrom practice

ACTGCATGGC

ACGCTAAGCACTGCATGGCCAA

ACGCTAAGCACTGCATGGCCAA

modeling

interpretation

communication between

practice and comp. science

needs basic knowledge

?algorithmics,concepts ofprogramming

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 9 / 36

Page 25: Programming and Problem-Solving

2. Computational Thinking

Systematic solving of given problems

This implies creativity, abstraction skills etc.

Formulation of solution as algorithm

Solution can be “understood” by a computer

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 10 / 36

Page 26: Programming and Problem-Solving

2. Computational Thinking

Jeannette Wing

"‘Computational thinking is a way humans solveproblems; it is not trying to get humans to thinklike computers. Computers are dull and boring;humans are clever and imaginative. We humansmake computers exciting."’

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 11 / 36

Page 27: Programming and Problem-Solving

3. Algorithms Design Techniques

Most practically relevant problems have easy solutions

Easy to implement

Are based on trying out possibly many possibilities (“solution candidates”)

This means impractically large time to spend

Many problem allow for “faster” solutions

Needs a little more skill

Different techniques: greedy algorithms, divide and conquer, dynamicprogramming etc.

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 12 / 36

Page 28: Programming and Problem-Solving

3. Algorithms Design Techniques

Most practically relevant problems have easy solutions

Easy to implement

Are based on trying out possibly many possibilities (“solution candidates”)

This means impractically large time to spend

Many problem allow for “faster” solutions

Needs a little more skill

Different techniques: greedy algorithms, divide and conquer, dynamicprogramming etc.

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 12 / 36

Page 29: Programming and Problem-Solving

3. Algorithms Design Techniques

Most practically relevant problems have easy solutions

Easy to implement

Are based on trying out possibly many possibilities (“solution candidates”)

This means impractically large time to spend

Many problem allow for “faster” solutions

Needs a little more skill

Different techniques: greedy algorithms, divide and conquer, dynamicprogramming etc.

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 12 / 36

Page 30: Programming and Problem-Solving

3. Algorithms Design Techniques

Most practically relevant problems have easy solutions

Easy to implement

Are based on trying out possibly many possibilities (“solution candidates”)

This means impractically large time to spend

Many problem allow for “faster” solutions

Needs a little more skill

Different techniques: greedy algorithms, divide and conquer, dynamicprogramming etc.

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 12 / 36

Page 31: Programming and Problem-Solving

Introduction to the CourseProjects

Page 32: Programming and Problem-Solving

Projects

During the semester, you work on a few small projects

The project tasks will be published via [code]expert

https://expert.ethz.ch

You work on the tasks on your own

The exercise hours are meant for answering your questions

Presentation of the solutions via Zoom

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 13 / 36

Page 33: Programming and Problem-Solving

Projects

During the semester, you work on a few small projects

The project tasks will be published via [code]expert

https://expert.ethz.ch

You work on the tasks on your own

The exercise hours are meant for answering your questions

Presentation of the solutions via Zoom

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 13 / 36

Page 34: Programming and Problem-Solving

Projects

During the semester, you work on a few small projects

The project tasks will be published via [code]expert

https://expert.ethz.ch

You work on the tasks on your own

The exercise hours are meant for answering your questions

Presentation of the solutions via Zoom

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 13 / 36

Page 35: Programming and Problem-Solving

Projects

The projects will be presented in the exercise hours

Presentation and discussion with assistants

Teams of 2 students each

Grading by assistants, feedback by students

Presentation is mandatory

but without effect on the grade

[code]expert allows you to test your solution before handing it in

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 14 / 36

Page 36: Programming and Problem-Solving

Projects

The projects will be presented in the exercise hours

Presentation and discussion with assistants

Teams of 2 students each

Grading by assistants, feedback by students

Presentation is mandatory

but without effect on the grade

[code]expert allows you to test your solution before handing it in

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 14 / 36

Page 37: Programming and Problem-Solving

Projects

The projects will be presented in the exercise hours

Presentation and discussion with assistants

Teams of 2 students each

Grading by assistants, feedback by students

Presentation is mandatory

but without effect on the grade

[code]expert allows you to test your solution before handing it in

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 14 / 36

Page 38: Programming and Problem-Solving

Projects

The projects will be presented in the exercise hours

Presentation and discussion with assistants

Teams of 2 students each

Grading by assistants, feedback by students

Presentation is mandatory

but without effect on the grade

[code]expert allows you to test your solution before handing it in

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 14 / 36

Page 39: Programming and Problem-Solving

Introduction to Python

Page 40: Programming and Problem-Solving

Programming Tools

Editor: Program to modify, edit and store Python program texts

Compiler: Program to translate a program text into machine language(intermediate code, respectively)

Computer: Machine to execute machine language programs

Operating System: Program to organize all procedures such as file handling,editing, compiling, and program execution

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 15 / 36

Page 41: Programming and Problem-Solving

Programming Tools

Editor: Program to modify, edit and store Python program texts

Compiler: Program to translate a program text into machine language(intermediate code, respectively)

Computer: Machine to execute machine language programs

Operating System: Program to organize all procedures such as file handling,editing, compiling, and program execution

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 15 / 36

Page 42: Programming and Problem-Solving

Programming Tools

Editor: Program to modify, edit and store Python program texts

Compiler: Program to translate a program text into machine language(intermediate code, respectively)

Computer: Machine to execute machine language programs

Operating System: Program to organize all procedures such as file handling,editing, compiling, and program execution

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 15 / 36

Page 43: Programming and Problem-Solving

Programming Tools

Editor: Program to modify, edit and store Python program texts

Compiler: Program to translate a program text into machine language(intermediate code, respectively)

Computer: Machine to execute machine language programs

Operating System: Program to organize all procedures such as file handling,editing, compiling, and program execution

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 15 / 36

Page 44: Programming and Problem-Solving

English vs. Programming Language

English

“Science is what we understand well enoughto explain to a computer.Art is everything else we do.”

DONALD KNUTH

Python

# computationb = a * a # b = a**2b = b * b # b = a**4

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 16 / 36

Page 45: Programming and Problem-Solving

English vs. Programming Language

English

“Science is what we understand well enoughto explain to a computer.Art is everything else we do.”

DONALD KNUTH

Python

# computationb = a * a # b = a**2b = b * b # b = a**4

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 16 / 36

Page 46: Programming and Problem-Solving

Syntax and Semantics

Like our language, programs have to be formed according to certain rulesSyntax: Connection rules for elementary symbols (characters)Semantics: Interpretation rules for connected symbols

Corresponding rules for a computer program are simpler, but also more strictbecause computers are relatively stupid

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 17 / 36

Page 47: Programming and Problem-Solving

Syntax and Semantics

Like our language, programs have to be formed according to certain rulesSyntax: Connection rules for elementary symbols (characters)Semantics: Interpretation rules for connected symbols

Corresponding rules for a computer program are simpler, but also more strictbecause computers are relatively stupid

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 17 / 36

Page 48: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 49: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 50: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 51: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 52: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 53: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 54: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 55: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cooking,my dog, and my cat.

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 56: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 57: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 58: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 59: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 60: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 61: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 62: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 63: Programming and Problem-Solving

Kinds of Errors Illustrated with English Language

The car drove too fast.

Thecar drove too fsat.

Red the car is.

I find inspiration in cookingmy dog and my cat

She is not tall and red-haired.

I own an red car.

The bike gallops fast.

We saw her duck.

Syntactically and semantically correct

Syntax error: word building

Syntax error: word order

Syntax error: missing punctuation marks

Syntactically correct, but ambiguous [no analogon]

Syntactically correct, but gramatically and semanti-cally wrong: wrong article [type error]

Syntactically and gramatically correct, but semanti-cally wrong [run-time error]

Syntactically and sematically correct, but ambiguous[no analogon]

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 18 / 36

Page 64: Programming and Problem-Solving

Introduction to PythonUsed Software

Page 65: Programming and Problem-Solving

Used Software

There are numerous Python development environments (IDEs)

These contain an editor and several tools

We use [code]expert

https://expert.ethz.ch/enroll/SS21/ppl

Also recommended (offline): PyCharm Education

https://www.jetbrains.com/pycharm-educational/download/

Download the Community Edition

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 19 / 36

Page 66: Programming and Problem-Solving

Used Software

There are numerous Python development environments (IDEs)

These contain an editor and several tools

We use [code]expert

https://expert.ethz.ch/enroll/SS21/ppl

Also recommended (offline): PyCharm Education

https://www.jetbrains.com/pycharm-educational/download/

Download the Community Edition

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 19 / 36

Page 67: Programming and Problem-Solving

Used Software

There are numerous Python development environments (IDEs)

These contain an editor and several tools

We use [code]expert

https://expert.ethz.ch/enroll/SS21/ppl

Also recommended (offline): PyCharm Education

https://www.jetbrains.com/pycharm-educational/download/

Download the Community Edition

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 19 / 36

Page 68: Programming and Problem-Solving

Introduction to PythonA First Python Program

Page 69: Programming and Problem-Solving

A First Python Program

print("This is a Python program")

x = 20print("The value of x is", x)y = x * x # y is the square of xprint("The value of y is", y)z = y * y # z is the square of yprint("The value of z is", x * x * x * x)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 20 / 36

Page 70: Programming and Problem-Solving

Behavior of a Program

At compile time

Program accepted by the compiler (syntactically correct)

Compiler error

During runtime

correct result

incorrect result

program crashes

program does not terminate (endless loop)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 21 / 36

Page 71: Programming and Problem-Solving

Behavior of a Program

At compile time

Program accepted by the compiler (syntactically correct)

Compiler error

During runtime

correct result

incorrect result

program crashes

program does not terminate (endless loop)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 21 / 36

Page 72: Programming and Problem-Solving

Comments

print("This is a Python program")

x = 20print("The value of x is", x)y = x * x # y is the square of xprint("The value of y is", y)z = y * y # z is the square of yprint("The value of z is", x * x * x * x)

Comments

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 22 / 36

Page 73: Programming and Problem-Solving

Comments

print("This is a Python program")

x = 20print("The value of x is", x)y = x * x # y is the square of xprint("The value of y is", y)z = y * y # z is the square of yprint("The value of z is", x * x * x * x)

Comments

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 22 / 36

Page 74: Programming and Problem-Solving

Comments and Layout

Comments

are contained in every good program

document, what and how a program does something and how it should beused

are ignored by the compiler

Syntax: # until the line end

Please note

empty lines are ignored

Python dictates indentations that reflect the program logic

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 23 / 36

Page 75: Programming and Problem-Solving

Comments and Layout

Comments

are contained in every good program

document, what and how a program does something and how it should beused

are ignored by the compiler

Syntax: # until the line end

Please note

empty lines are ignored

Python dictates indentations that reflect the program logic

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 23 / 36

Page 76: Programming and Problem-Solving

Introduction to PythonStatements

Page 77: Programming and Problem-Solving

Statements

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 24 / 36

Page 78: Programming and Problem-Solving

Statements

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

statements

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 24 / 36

Page 79: Programming and Problem-Solving

Statements

Statements

are building blocks of a Python program

are executed (sequentially)

are given in one line

Any statement (potentially) provides an effect

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 25 / 36

Page 80: Programming and Problem-Solving

Statements

Statements

are building blocks of a Python program

are executed (sequentially)

are given in one line

Any statement (potentially) provides an effect

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 25 / 36

Page 81: Programming and Problem-Solving

Statements – Values and Effects

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Effect: Output of the string This is...

Effect: Variable x is created and assigned value 20

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 26 / 36

Page 82: Programming and Problem-Solving

Statements – Values and Effects

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Effect: Output of the string This is...

Effect: Variable x is created and assigned value 20

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 26 / 36

Page 83: Programming and Problem-Solving

Introduction to PythonVariables

Page 84: Programming and Problem-Solving

Fundamental Types

Variables represent (varying) values

integers

real numbers (float)

strings

. . .

In contrast to, for example, Java or C, the type is not explicitly stated when avariable is declared (used for the first time)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 27 / 36

Page 85: Programming and Problem-Solving

Fundamental Types

Variables represent (varying) values

integers

real numbers (float)

strings

. . .

In contrast to, for example, Java or C, the type is not explicitly stated when avariable is declared (used for the first time)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 27 / 36

Page 86: Programming and Problem-Solving

Introduction to PythonExpressions

Page 87: Programming and Problem-Solving

Expressions

Expressions

represent computations

are either primary (x)

or composed (x * x)

. . . from different expressions by operators

. . . and parentheses

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 28 / 36

Page 88: Programming and Problem-Solving

Expressions

Expressions

represent computations

are either primary (x)

or composed (x * x)

. . . from different expressions by operators

. . . and parentheses

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 28 / 36

Page 89: Programming and Problem-Solving

Expressions

Expressions

represent computations

are either primary (x)

or composed (x * x)

. . . from different expressions by operators

. . . and parentheses

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 28 / 36

Page 90: Programming and Problem-Solving

Expressions

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x )

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 29 / 36

Page 91: Programming and Problem-Solving

Expressions

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x )

Variable name, primary expression

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 29 / 36

Page 92: Programming and Problem-Solving

Expressions

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x )

Composite expression

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 29 / 36

Page 93: Programming and Problem-Solving

Expressions

represent computations

are primary or composite(by other expressions and operations)

Example

a * a is composed ofvariable name, operator symbol, variable namevariable name: primary expression

can be put into parentheses

a * a can be written as (a * a)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 30 / 36

Page 94: Programming and Problem-Solving

Introduction to PythonOperators and Operands

Page 95: Programming and Problem-Solving

Operators and Operands

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 31 / 36

Page 96: Programming and Problem-Solving

Operators and Operands

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Left operand (variable)Right operand (expression)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 31 / 36

Page 97: Programming and Problem-Solving

Operators and Operands

print("This is a Python program")

x = 20print("The value of x is", x)y = x * xprint("The value of y is", y)z = y * yprint("The value of z is", x * x * x * x)

Assignment operatorMultiplication operator

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 31 / 36

Page 98: Programming and Problem-Solving

Operators

Operators

make expressions (operands) into new composed expressions

have an arity

Example (Multiplication) a * a

Operand a, Operator * , Operand a

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 32 / 36

Page 99: Programming and Problem-Solving

Multiplication Operator *

Multiplication operator

expects two R-values of the same type as operands (arity 2)

“returns the product as value of the same type,” that means formally:

The composite expression is value of the product of the value of the twooperands

Examples

a * a

b * b

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 33 / 36

Page 100: Programming and Problem-Solving

Assignment Operator =

Assigns to the left operand the value of the right operand and returns the leftoperand

Examples

b = b * b

a = b

Attention

The operator "‘="’ corresponds to the assignment operator of mathematics(:=), not to the comparison operator (=)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 34 / 36

Page 101: Programming and Problem-Solving

Assignment Operator =

Assigns to the left operand the value of the right operand and returns the leftoperand

Examples

b = b * b

a = b

Attention

The operator "‘="’ corresponds to the assignment operator of mathematics(:=), not to the comparison operator (=)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 34 / 36

Page 102: Programming and Problem-Solving

Exercise – Celsius to Fahrenheit Calculator

Write a program that

interprets a number (like, e. g., 31)as a temperature in degree Celsius

outputs the same temperature indegree Fahrenheit

uses the formula

fahrenheit = 9 · celsius5 + 32

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 35 / 36

Page 103: Programming and Problem-Solving

Exercise – Celsius to Fahrenheit Calculator

celsius = 31fahrenheit = 9 * celsius / 5 + 32print(fahrenheit)

Programming and Problem-Solving – Introduction to the Course Spring 2021 Dennis Komm 36 / 36

Page 104: Programming and Problem-Solving

Thanks for yourattention