108

3. python intro

  • Upload
    in4400

  • View
    66

  • Download
    3

Embed Size (px)

DESCRIPTION

Week 2

Citation preview

Page 1: 3. python   intro
Page 2: 3. python   intro
Page 3: 3. python   intro
Page 4: 3. python   intro
Page 5: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

• Many scientific libraries (SciPy, NumPy)

• Large community of scientists using it

• #1 programming language in universities

• Shell mode for experimentation

Page 6: 3. python   intro

Why Python?

• Easy to learn syntax

Page 7: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

Page 8: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

Page 9: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

• Many scientific libraries (SciPy, NumPy)

Page 10: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

• Many scientific libraries (SciPy, NumPy)

• Large community of scientists using it

Page 11: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

• Many scientific libraries (SciPy, NumPy)

• Large community of scientists using it

• #1 programming language in universities

Page 12: 3. python   intro

Why Python?

• Easy to learn syntax

• Built-in datatypes for lists, strings etc.

• Runs on many types of machines

• Many scientific libraries (SciPy, NumPy)

• Large community of scientists using it

• #1 programming language in universities

• Shell mode for experimentation

Page 13: 3. python   intro
Page 14: 3. python   intro
Page 15: 3. python   intro
Page 16: 3. python   intro

Assignment uses =

First assignment makes the var(not ‘dim’ needed)

Page 17: 3. python   intro

Comparison uses ==

Page 18: 3. python   intro

Comparison uses ==

“Print” prints all datatypes (no toString() needed)

Page 19: 3. python   intro

Logical operators are words

Page 20: 3. python   intro
Page 21: 3. python   intro

Multi assign

Page 22: 3. python   intro

Multi assign

Page 23: 3. python   intro

Datatypes

Page 24: 3. python   intro

Why?

Page 25: 3. python   intro

Why?

Integers are the default

Page 26: 3. python   intro
Page 27: 3. python   intro
Page 28: 3. python   intro

“” and ‘’ are both okay

Page 29: 3. python   intro

Whitespace

Page 30: 3. python   intro
Page 31: 3. python   intro
Page 32: 3. python   intro

This whitespace has meaning!

Page 33: 3. python   intro
Page 34: 3. python   intro

Indentation marks a block

No begin/end or {}

Page 35: 3. python   intro

Comments start with #

Page 36: 3. python   intro

Understanding

Assignment

Page 37: 3. python   intro

What exactly happens is:

Page 38: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5

memory

Page 39: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5

Name: a

Ref: <address1>

name list memory

Page 40: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5

Name: a

Ref: <address1>

name list memory

Page 41: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5

Name: a

Ref: <address1>

name list memory

Page 42: 3. python   intro

Important:

Python determines the type automatically

Decides when to delete too (garbage collection)

Type: Integer

Data: 5

Name: a

Ref: <address1>

name list memory

Page 43: 3. python   intro

Important:

Python determines the type automatically

Decides when to delete too (garbage collection)

Basic data types (int, float, string) are immutable

Type: Integer

Data: 5

Name: a

Ref: <address1>

name list memory

Page 44: 3. python   intro
Page 45: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5Name: a

Ref: <address1>

name list memory

Page 46: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 6

Page 47: 3. python   intro

What exactly happens is:

Type: Integer

Data: 5Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 6

Page 48: 3. python   intro

What exactly happens is:

Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 6

Page 49: 3. python   intro

What exactly happens is:This is the garbage collector at work

Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 6

Page 50: 3. python   intro

Another example

Page 51: 3. python   intro

Another example

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Page 52: 3. python   intro

Another example

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Name: x

Ref: <address1>

Page 53: 3. python   intro

Another example

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 10Name: x

Ref: <address2>

Page 54: 3. python   intro
Page 55: 3. python   intro
Page 56: 3. python   intro

Lists may contain a mix

Page 57: 3. python   intro

Lists may contain a mix

Can even contain other lists

Page 58: 3. python   intro

Looking up items

Counting starts at 0

Page 59: 3. python   intro

Looking up items

Counting starts at 0

We can also count from the end!

Page 60: 3. python   intro

Looking up items

Counting starts at 0

We can also count from the end!

‘index out of range’ is a common error

Page 61: 3. python   intro

Slicing

Page 62: 3. python   intro

Slicing

Page 63: 3. python   intro

Slicing

Page 64: 3. python   intro

Slicing

Both arguments are optional

Page 65: 3. python   intro

Slicing

Both arguments are optional

Page 66: 3. python   intro

Slicing

Both arguments are optional

Page 67: 3. python   intro

Slicing

Both arguments are optional

Starting at the end also allowed

Page 68: 3. python   intro
Page 69: 3. python   intro

Lists are mutable!

Page 70: 3. python   intro

Remember this example?

Page 71: 3. python   intro

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Remember this example?

Page 72: 3. python   intro

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Name: x

Ref: <address1>

Remember this example?

Page 73: 3. python   intro

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Type: Integer

Data: 10Name: x

Ref: <address2>

Remember this example?

Page 74: 3. python   intro

How will this work?

Type: List

Data: [1,2,3]

Name: list

Ref: <address1>

name list memory

Page 75: 3. python   intro

How will this work?

Type: List

Data: [1,2,3]

Name: list

Ref: <address1>

name list memory

Name: list2

Ref: <address1>

Page 76: 3. python   intro

Type: Integer

Data: 6

Name: a

Ref: <address1>

name list memory

Name: x

Ref: <address1>

So far everything is as expected

Page 77: 3. python   intro

How will this work?

Type: List

Data: [1,2,3]

Name: list

Ref: <address1>

name list memory

Name: list2

Ref: <address1>

Page 78: 3. python   intro

How will this work?

Type: List

Data: [3,2,1]

Name: list

Ref: <address1>

name list memory

Name: list2

Ref: <address1>

Page 79: 3. python   intro

How will this work?

Type: List

Data: [3,2,1]

Name: list

Ref: <address1>

name list memory

Name: list2

Ref: <address1>

Page 80: 3. python   intro

How will this work?

Type: List

Data: [3,2,1]

Name: list

Ref: <address1>

name list memory

Name: list2

Ref: <address1>

Page 81: 3. python   intro

To avoid this, use [:] to copy

Page 82: 3. python   intro

To avoid this, use [:] to copy

Page 83: 3. python   intro

To avoid this, use [:] to copy

Page 84: 3. python   intro
Page 85: 3. python   intro
Page 86: 3. python   intro

Popquiz!!

What does this do?

Page 87: 3. python   intro

Popquiz!!

What does this do?

Page 88: 3. python   intro
Page 89: 3. python   intro
Page 90: 3. python   intro
Page 91: 3. python   intro

Variables are case sensitive!

Page 92: 3. python   intro

Some names are reserved

Page 93: 3. python   intro

Some names are reserved

Page 94: 3. python   intro

Some names are reserved

Page 95: 3. python   intro
Page 96: 3. python   intro

Remember this did not

multiply list items?

Page 97: 3. python   intro

For that, we use

list comprehensions

Page 98: 3. python   intro

For that, we use

list comprehensions

Page 99: 3. python   intro

Popquiz!

What do you think is in list now?

Page 100: 3. python   intro

The original list is not modified

Page 101: 3. python   intro

The original list is not modified

We can also filter

Page 102: 3. python   intro

The original list is not modified

We can also filter

Beware, filter is on x!

Page 103: 3. python   intro
Page 104: 3. python   intro
Page 105: 3. python   intro
Page 106: 3. python   intro
Page 107: 3. python   intro

This is just a string

Page 108: 3. python   intro

This is just a string

but we can put data in to format it