introduction to programming (lt2111) lecture 1: introduction · -20pt course literature i main...

37

Upload: others

Post on 24-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

Introduction to programming (LT2111)Lecture 1: Introduction

Richard Johansson

September 3, 2013

Page 2: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Introduction & Administration

I The main goal of the course is that you will learn how to

program using the programming language Python.

I Teachers:

Richard Johansson Johan RoxendalCourse coordinator Course assistant

[email protected] [email protected]

Page 3: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Schedule

I Course homepage:

http://spraakbanken.gu.se/personal/richard/itp2013

I We will meet on Tuesdays and Fridays:I lectureI assignment supervision

I 45 min + 15 min break + 45 min

Page 4: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Course literature

I Main course book: Python for software design, how to think

like a computer scientist, Allen B. Downey

I Natural Language Processing with Python, Steven Bird et al.

(we will only use the �rst chapters, but it is the main book in

the 'Programming in NLP' course)

I The books are available online for free (linked from the

homepage).

I Paperbacks cost around 25 euro each.

I Python documentation at the Python website:

http://docs.python.org/.

Page 5: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Lectures

I Tuesdays, 10.15-12.00

I The main goal of the lectures is to help you grasp the

theoretical content of the course.

I Please mail me about parts of the course that you �nd

especially di�cult, and I will try to include more material

about it in the coming lectures.

I The slides are put on the course homepage after the lecture

(as quickly as I can manage).

Page 6: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Assignments

I Assignment supervision:

Tuesdays, 13.15-15.00

Fridays, 10.15-12.00

I 3 obligatory practical assignments, 1 optional, but

recommended (this week).

I The assignments are done in groups of two.

I Do not make the mistake of being a passive member of a

group! Switch control of the keyboard frequently!

Page 7: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Assignment 0: description

I Not obligatory, but highly recommended.

I A hands-on assignment, where you will be familiarized with

both programming in Python and Language Technology.

I Do not except to understand everything! Just work your way

through the examples.

I Chapter 1 of the NLTK book.

Page 8: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Exercises

I Not mandatory

I Paper-and-pen programming (except for the �rst week, which

is practical)

I Why no computers?

Page 9: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Exam

I Date: one of October 21 � November 3 (exact day yet to be

decided)

I Grade: Pass with distinction, Pass, or Fail

Page 10: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Computer science crash course

I Computers are machines that solve computational problems�mechanically�

I To do anything useful, they need detailed instructions.

I Algorithm: a detailed step-by-step account of how to solve a

problem.

I Programming language: a formal language to express

algorithms.

Page 11: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example of an algorithm

Serves 4

I 100g slightly stale crusty white bread, soaked in cold water for 20 mins

I 1kg very ripe tomatoes, diced

I 1 ripe red pepper and 1 green pepper, deseeded and diced

I 1 medium cucumber, peeled and diced

I 2 cloves of garlic, peeled and crushed

I 150ml extra virgin olive oil

I 2tbsp sherry vinegar

I Salt, to taste

I Garnishes � see below

1. Mix the diced tomatoes, peppers and cucumber with the crushed garlic and olive oil in the bowlof a food processor or blender. Squeeze out the bread, tear it roughly into chunks, and add tothe mixture.

2. Blend until smooth, then add the salt and vinegar to taste and stir well.

3. Pass the mixture through a �ne sieve, then cover and refrigerate until well chilled.

4. Serve with garnishes of your choice: I liked diced black olives, hard-boiled egg and small pieces ofcucumber and pepper; mint or parsley also works well, and many people add spring onion, cubesof Spanish ham and so on.

Page 12: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example: long division

71648

0

0235,4...

16

14

2421

3835

3028...

Page 13: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example: Swedish personal identity check digit

For instance: 640823-323

1. Multiply every second digit by 2, leave the rest unchanged

12 4 0 8 4 3 6 2 6

2. Sum all the digits (note that 12 becomes 1 + 2)

1 + 2 + 4 + 0 + 8 + 4 + 3 + 6 + 2 + 6 = 36

3. The check digit is equal to 10 - d , where d is the last digit of

the sum. (If this is 10, then 0).

640823-3234

Page 14: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example: �nding a name in the telephone book

1. Open the book roughly where you think the name would be

(or in the middle)

2. Found the name?I If yes, then we're doneI If the name would be on this page but isn't, then it is not listed

3. Select a new place to open the book: before or after the

current position

4. Repeat from 2.

Binary search more e�cient than linear search!

(logarithmic complexity faster than linear)

Page 15: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Discussion

How do we �nd the most frequent word in today's Göteborgsposten?

Page 16: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

What does a computer do?

(the von Neumann model)

Page 17: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Programming in a nutshell

What are those �instructions�?

I Input (keyboard, �le, other devices)

I Output (screen, �le, other device)

I Math (addition, multiplication)

I Conditional execution (select what to execute based on a

condition)

I Repetition (usually combined with conditionals)

I That's it!

I Or is it?

Page 18: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example

�multiply the two numbers 7 and 8 and print the result�

1. put 7 on the stack

2. put 8 on the stack

3. move the top stack item into register %eax

4. multiply the top stack item with register %eax and keep the

result in %eax

5. move the result from %eax to %esi (prepare for printing)

6. jump to the library function printf

Page 19: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Low-level language: Assembler

.LC0:

.string "%f\n"

main:

pushq %rbp

movq %rsp, %rbp

subq $32, %rsp

movl %edi, -20(%rbp)

movq %rsi, -32(%rbp)

movl $7, -4(%rbp)

movl $8, -8(%rbp)

movl -4(%rbp), %eax

imull -8(%rbp), %eax

movl %eax, %esi

movl $.LC0, %edi

movl $0, %eax

call printf

Page 20: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

The same program written in Python

print 7*8

Page 21: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

High-level language

I The di�erence is in the level of abstraction � low-level

machine details are hidden in a high-level language.

I A high-level language allows us to be much more productive.

I It also separates us from the machine, which makes our

programs portable.

I However, for every new programming language you need tolearn the abstraction of that language.

I Python, C, Java, Scala, Perl, Prolog, Matlab, Haskell, . . .I Each language is a way of thinkingI Python is one of the easiest!

Page 22: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example: most frequent word in GP

f = file("göteborgsposten.txt")

table = {}

for line in f:

for word in line.split():

if table.has_key(word):

table[word] += 1

else:

table[word] = 1

print max([(f, w) for (w, f) in statistics.items()])

Page 23: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Editing Python program code

I Write the Python program in a text �le, e.g. hello.pyI Specialized editors assist you (e.g. coloring)

I The text that you write is called the source code

I The text �le (conventionally ending in .py) is called a

program �le or a script

Page 24: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Running a Python program

I Your code is executed (run) by a tool called the Python

interpreter:

python hello.py

Page 25: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

The Python interpreter interactively

I The interpreter can also be run interactively:

Page 26: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Python interpreters online

I http://www.pythontutor.com/visualize.html

I also shows state of execution

I http://www.compileonline.com/execute_python_online.php

I https://www.pythonanywhere.com/try-ipython/

I interactive interpreter

Page 27: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Modular programming

I To be productive, we often need to build on what others have

done.

I Instead of trying to reinvent the wheel, we often use code

de�ned by others that helps us solve a particular problem.

I Modularity: breaking down a program into reusable parts.

I Python terminology: a library consists of packages that

consist of modules. A module is a �le containing code. (More

about this later)

I Python standard library is always available with the Python

program.

I However, we will actually many times reinvent the wheel just

for the practice.

Page 28: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Some Python terminology

I values: basic things a program works with, like letters and

numbers.

I expression: denotes a value, possibly after some computation

(5+5 denotes 10).

I types: every value has a type, e.g., 2 is an integer, "Hello

world!" is a string.

I variables: gives a name to a value. A variable has the same

type as the value.

I statement: performs an action, such as printing a string or

assigning a name to a value.

Page 29: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example

x = 5

y = x*x + 1

print x + y

Page 30: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Example

$ python

...

>>> type(12)

<type 'int'>

>>> type(12+12)

<type 'int'>

>>> name = 12+12

>>> name

24

>> type(name)

<type 'int'>

Page 31: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

When things go wrong: �bugs�

Syntax errors are formal errors that may be lexical or syntactical.

Runtime errors are errors, also referred to as exceptions, occurring

while running a program.

Semantic errors are errors where the program actually runs, but

fails to do what we want.

Page 32: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Last year's FAQ: Typing some symbols on a Mac

shift +8 = (

shift +9 = )

alt+8 = [

alt+9 = ]

shift+alt+8 = {

shift+alt+9 = }

The logic: parentheses-like symbols on the same keys.

Page 33: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Last year's FAQ: What is NLTK?

I NLTK (Natural Language ToolKit) is not a part of standard

Python, it is a Python package that requires separate

installation.

I NLTK covers a wide range of Language Technology subjects

and methods.

I NLTK also provides many Language Technology resources,

e.g., WordNet that we will work with in assignment 1.

Page 34: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Last year's FAQ: How do I install NLTK on my owncomputer?

I Instructions are found here:

http://www.nltk.org/download

Page 35: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Last year's FAQ: �oating point division

>>> 4/10

0

>>> from __future__ import division

>>> 4/10

0.4

I What is __future__?

I First: changing how something such as 'division' works, even if

it is a good idea, must be made conservatively, to avoid

breaking existing code.

I But programmers are allowed to use the new division, if they

explicitly declare that, hence:

from __future__ import division

I Why the strange name __future__? Python's built-in things

have names with surrounding double underscore to avoid that

you by accident would use that name.

Page 36: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Last year's FAQ: �oating point division (cont.)

>>> 1.0/6

0.16666666666666666

>>> x = 1

>>> y = 6

>>> float(x)/y

0.16666666666666666

Page 37: Introduction to programming (LT2111) Lecture 1: Introduction · -20pt Course literature I Main course book: Python for software design, how to think like a computer scientist, Allen

-20pt

Next lecture

Writing and running simple programs.

I Values, expressions, variables, types.

I Numbers, strings, lists.

I Conditions: if . . .

I Functions.