Upload
in4400
View
66
Download
3
Embed Size (px)
DESCRIPTION
Week 2
Citation preview
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
Why Python?
• Easy to learn syntax
Why Python?
• Easy to learn syntax
• Built-in datatypes for lists, strings etc.
Why Python?
• Easy to learn syntax
• Built-in datatypes for lists, strings etc.
• Runs on many types of machines
Why Python?
• Easy to learn syntax
• Built-in datatypes for lists, strings etc.
• Runs on many types of machines
• Many scientific libraries (SciPy, NumPy)
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
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
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
Assignment uses =
First assignment makes the var(not ‘dim’ needed)
Comparison uses ==
Comparison uses ==
“Print” prints all datatypes (no toString() needed)
Logical operators are words
Multi assign
Multi assign
Datatypes
Why?
Why?
Integers are the default
“” and ‘’ are both okay
Whitespace
This whitespace has meaning!
Indentation marks a block
No begin/end or {}
Comments start with #
Understanding
Assignment
What exactly happens is:
What exactly happens is:
Type: Integer
Data: 5
memory
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
Important:
Python determines the type automatically
Decides when to delete too (garbage collection)
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
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
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
What exactly happens is:
Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
What exactly happens is:This is the garbage collector at work
Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
Another example
Another example
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Another example
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Name: x
Ref: <address1>
Another example
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 10Name: x
Ref: <address2>
Lists may contain a mix
Lists may contain a mix
Can even contain other lists
Looking up items
Counting starts at 0
Looking up items
Counting starts at 0
We can also count from the end!
Looking up items
Counting starts at 0
We can also count from the end!
‘index out of range’ is a common error
Slicing
Slicing
Slicing
Slicing
Both arguments are optional
Slicing
Both arguments are optional
Slicing
Both arguments are optional
Slicing
Both arguments are optional
Starting at the end also allowed
Lists are mutable!
Remember this example?
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Remember this example?
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Name: x
Ref: <address1>
Remember this example?
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 10Name: x
Ref: <address2>
Remember this example?
How will this work?
Type: List
Data: [1,2,3]
Name: list
Ref: <address1>
name list memory
How will this work?
Type: List
Data: [1,2,3]
Name: list
Ref: <address1>
name list memory
Name: list2
Ref: <address1>
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Name: x
Ref: <address1>
So far everything is as expected
How will this work?
Type: List
Data: [1,2,3]
Name: list
Ref: <address1>
name list memory
Name: list2
Ref: <address1>
How will this work?
Type: List
Data: [3,2,1]
Name: list
Ref: <address1>
name list memory
Name: list2
Ref: <address1>
How will this work?
Type: List
Data: [3,2,1]
Name: list
Ref: <address1>
name list memory
Name: list2
Ref: <address1>
How will this work?
Type: List
Data: [3,2,1]
Name: list
Ref: <address1>
name list memory
Name: list2
Ref: <address1>
To avoid this, use [:] to copy
To avoid this, use [:] to copy
To avoid this, use [:] to copy
Popquiz!!
What does this do?
Popquiz!!
What does this do?
Variables are case sensitive!
Some names are reserved
Some names are reserved
Some names are reserved
Remember this did not
multiply list items?
For that, we use
list comprehensions
For that, we use
list comprehensions
Popquiz!
What do you think is in list now?
The original list is not modified
The original list is not modified
We can also filter
The original list is not modified
We can also filter
Beware, filter is on x!
This is just a string
This is just a string
but we can put data in to format it