Download - 3. python intro
![Page 1: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/1.jpg)
![Page 2: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/2.jpg)
![Page 3: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/3.jpg)
![Page 4: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/4.jpg)
![Page 5: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/6.jpg)
Why Python?
• Easy to learn syntax
![Page 7: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/7.jpg)
Why Python?
• Easy to learn syntax
• Built-in datatypes for lists, strings etc.
![Page 8: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/8.jpg)
Why Python?
• Easy to learn syntax
• Built-in datatypes for lists, strings etc.
• Runs on many types of machines
![Page 9: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/13.jpg)
![Page 14: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/14.jpg)
![Page 15: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/15.jpg)
![Page 16: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/16.jpg)
Assignment uses =
First assignment makes the var(not ‘dim’ needed)
![Page 17: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/17.jpg)
Comparison uses ==
![Page 18: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/18.jpg)
Comparison uses ==
“Print” prints all datatypes (no toString() needed)
![Page 19: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/19.jpg)
Logical operators are words
![Page 20: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/20.jpg)
![Page 21: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/21.jpg)
Multi assign
![Page 22: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/22.jpg)
Multi assign
![Page 23: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/23.jpg)
Datatypes
![Page 24: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/24.jpg)
Why?
![Page 25: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/25.jpg)
Why?
Integers are the default
![Page 26: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/26.jpg)
![Page 27: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/27.jpg)
![Page 28: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/28.jpg)
“” and ‘’ are both okay
![Page 29: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/29.jpg)
Whitespace
![Page 30: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/30.jpg)
![Page 31: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/31.jpg)
![Page 32: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/32.jpg)
This whitespace has meaning!
![Page 33: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/33.jpg)
![Page 34: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/34.jpg)
Indentation marks a block
No begin/end or {}
![Page 35: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/35.jpg)
Comments start with #
![Page 36: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/36.jpg)
Understanding
Assignment
![Page 37: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/37.jpg)
What exactly happens is:
![Page 38: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/38.jpg)
What exactly happens is:
Type: Integer
Data: 5
memory
![Page 39: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/39.jpg)
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
![Page 40: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/40.jpg)
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
![Page 41: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/41.jpg)
What exactly happens is:
Type: Integer
Data: 5
Name: a
Ref: <address1>
name list memory
![Page 42: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/44.jpg)
![Page 45: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/45.jpg)
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
![Page 46: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/46.jpg)
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
![Page 47: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/47.jpg)
What exactly happens is:
Type: Integer
Data: 5Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
![Page 48: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/48.jpg)
What exactly happens is:
Name: a
Ref: <address1>
name list memory
Type: Integer
Data: 6
![Page 49: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/50.jpg)
Another example
![Page 51: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/51.jpg)
Another example
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
![Page 52: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/52.jpg)
Another example
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Name: x
Ref: <address1>
![Page 53: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/54.jpg)
![Page 55: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/55.jpg)
![Page 56: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/56.jpg)
Lists may contain a mix
![Page 57: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/57.jpg)
Lists may contain a mix
Can even contain other lists
![Page 58: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/58.jpg)
Looking up items
Counting starts at 0
![Page 59: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/59.jpg)
Looking up items
Counting starts at 0
We can also count from the end!
![Page 60: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/61.jpg)
Slicing
![Page 62: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/62.jpg)
Slicing
![Page 63: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/63.jpg)
Slicing
![Page 64: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/64.jpg)
Slicing
Both arguments are optional
![Page 65: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/65.jpg)
Slicing
Both arguments are optional
![Page 66: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/66.jpg)
Slicing
Both arguments are optional
![Page 67: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/67.jpg)
Slicing
Both arguments are optional
Starting at the end also allowed
![Page 68: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/68.jpg)
![Page 69: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/69.jpg)
Lists are mutable!
![Page 70: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/70.jpg)
Remember this example?
![Page 71: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/71.jpg)
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Remember this example?
![Page 72: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/72.jpg)
Type: Integer
Data: 6
Name: a
Ref: <address1>
name list memory
Name: x
Ref: <address1>
Remember this example?
![Page 73: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/74.jpg)
How will this work?
Type: List
Data: [1,2,3]
Name: list
Ref: <address1>
name list memory
![Page 75: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/76.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/77.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/78.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/79.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/80.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/81.jpg)
To avoid this, use [:] to copy
![Page 82: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/82.jpg)
To avoid this, use [:] to copy
![Page 83: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/83.jpg)
To avoid this, use [:] to copy
![Page 84: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/84.jpg)
![Page 85: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/85.jpg)
![Page 86: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/86.jpg)
Popquiz!!
What does this do?
![Page 87: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/87.jpg)
Popquiz!!
What does this do?
![Page 88: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/88.jpg)
![Page 89: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/89.jpg)
![Page 90: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/90.jpg)
![Page 91: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/91.jpg)
Variables are case sensitive!
![Page 92: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/92.jpg)
Some names are reserved
![Page 93: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/93.jpg)
Some names are reserved
![Page 94: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/94.jpg)
Some names are reserved
![Page 95: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/95.jpg)
![Page 96: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/96.jpg)
Remember this did not
multiply list items?
![Page 97: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/97.jpg)
For that, we use
list comprehensions
![Page 98: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/98.jpg)
For that, we use
list comprehensions
![Page 99: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/99.jpg)
Popquiz!
What do you think is in list now?
![Page 100: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/100.jpg)
The original list is not modified
![Page 101: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/101.jpg)
The original list is not modified
We can also filter
![Page 102: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/102.jpg)
The original list is not modified
We can also filter
Beware, filter is on x!
![Page 103: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/103.jpg)
![Page 104: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/104.jpg)
![Page 105: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/105.jpg)
![Page 106: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/106.jpg)
![Page 107: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/107.jpg)
This is just a string
![Page 108: 3. python intro](https://reader034.vdocuments.net/reader034/viewer/2022042515/547cf6e3b37959492b8b513a/html5/thumbnails/108.jpg)
This is just a string
but we can put data in to format it