-
This lecture could also be named containers.
-
Because containers are used to store things.
- Programming is all about:
- Storing data
- Processing data
- Storing processed data
-
List
-
Dictionary
-
Tuple
-
String
-
Set
-
Frozenset
-
Order matters everywhere except in dictionaries.
-
“Dictionaries are order agnostic.”
-
Two types (dimensions) of mutability:
- Can we add/delete elements from collection (box)
- Can we change/update existing elements from collection (box)
-
Mutable objects
- List
- Dictionary
- Set
- Bytearray
-
Immutable objects
- Integer
- Float
- String
- Tuple
- Frozen set
- Complex
- Bytes
-
What can we do with list?
-
What concepts are related to list? In which way?
-
Computer can do only 3 things:
- Perform some calculation
- Remember what is calculated
- Repeat process large number of time
-
List is collection of elements.
-
Those elements can be of different type.
-
List indexing
- We can access elements of list by using corresponding indices.
-
Index starts from 0 !!!
-
List slicing
- We can get several elements of list using slicing operator.
-
Two rules:
- Including first
- Excluding last.
- .copy
- .count
- .append
- .index
- .extend
- .clear
- .insert
- .pop
- .remove
- .reverse
- .sort
- ._ len _
-
List is just box which stores sequence of elements.
-
Each element has its own unique index.
-
It is done using loops.
-
Nested lists count as single element.
-
Skip for now.
-
“+” operator concatenates two lists.
-
“ * ” operator repeats a list given number of times.
- my_list [x:y] - all elements from Xth (including) up to but not including Yth element.
- If you omit first element, it starts from beginning and goes up to Y-th element
- If you omit last element, it starts from X-th element (including) and goes towards end.
- Append
- Extend
- Sort
- …
-
Very important concepts!
-
Skip for now. When we learn loops, we will come back.
- .pop
- Delete and return n-th element in list
- If n is not specified, then removes last element in list
- .remove
- If you know element, but not its index
- Deletes first element it sees
- del
-
Equivalent: two objects have same values
-
Identical: if two objects are same i.e. two variables are pointing to same object.
-
Objects are used for storing values.
-
Objects can be equivalent, but not identical.
-
Every identical object is also equivalent.
-
Association of variable with object is called reference.
-
An object with mor ethan one reference has more than one name. This object is aliased.
-
If aliased object is mutable, changes made with obe object affect other object.
-
Aliasing is useful, but it is error prone.
-
When working with mutable objects, it is best to avoid aliasing at all.
-
Rule of thumb:
- If you have return value, then modification is not performed in place.
- If you have return value, then modification is done of ‘pass by value’ type.
-
List
-
Element (aka item)
-
Nested list
-
Accumulator (variable i)
-
Augmented assignment
-
Reduce
-
Map
-
Filter
-
Object
-
Equivalent
-
Identical
-
Reference
-
Aliasing
-
Collection of key-value pairs.
-
Key must be unique, value does not have to be.
-
Unordered collection of key-value pairs (aka items).
-
List has index, dictionary has keys !!!
-
Dictionary methods:
- .copy (shallow copy)
- .clear
- .items
- .keys
- .values
- .pop
- .popitem
- .update
- ._ len _
- key:value pairs
-
Mapping
-
Dictionary
-
Key-value pairs
-
Item
-
Key
-
Value
-
Implementation
-
Hashtable
-
Hash function
-
Hashtable
-
Immutable data type
-
Collection of elements
-
Tuple methods:
- .index - returns index of first occurrence of value
- .count - count how many particular elements exist in list
-
Tuple operations:
- Slicing
- Multiplication
- Relational operators (performed on element by element basis)
-
Tuple creation:
- A = 1,2,3,4
- A = (1,2,3,4)
-
Typical usage:
- Multiple return values in function !!!
- Iterators are collections similar to lists.
- You can iterate through.
- But, you can’t use index to access elements of iterator.
- Special and very useful functions.
- Tuple
- Tuple assignment
- Zip object
- Iterator
- Data structure
- String methods:
- .count
- .index
- .format
- .capitalize
- .casefold
- .endswith
- .expandtabs
- .find
- .join
- .lower
- .partition
- .replace
- .startswith
- .strip
- Object
- Sequence
- Item
- Index
- Slice
- Empty string
- Immutable
-
Mutable object i.e. can be changed.
-
Data structure (collection) that can contains unique/distinct elements.
- .add
- .clear
- .copy
- .difference
- .intersection
- …
-
Immutable object i.e. cannot be changed.
-
Can contain only unique/distinct elements.
-
.copy
-
.difference
-
.intersection
-
.isdisjoint
-
.issubset
-
.issuperset
-
.symmetric_difference
-
.union
- Thank you for your most valuable resource. Your time.