Skip to content

Latest commit

 

History

History
415 lines (239 loc) · 6.19 KB

5. Collections.md

File metadata and controls

415 lines (239 loc) · 6.19 KB

Python collections

Containers

  • This lecture could also be named containers.

  • Because containers are used to store things.

Programming - Big picture

  • Programming is all about:
    • Storing data
    • Processing data
    • Storing processed data

Python collections

  • List

  • Dictionary

  • Tuple

  • String

  • Set

  • Frozenset

Collections hack :)

  • Order matters everywhere except in dictionaries.

  • “Dictionaries are order agnostic.”

Mutable/immutable

  • 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

1. Lists

  • 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.

Lists intro

  • 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.

Lists intro - list methods

  • .copy
  • .count
  • .append
  • .index
  • .extend
  • .clear
  • .insert
  • .pop
  • .remove
  • .reverse
  • .sort
  • ._ len _

List is a sequence

  • List is just box which stores sequence of elements.

  • Each element has its own unique index.

Lists are mutable

Traversing a list

  • It is done using loops.

  • Nested lists count as single element.

  • Skip for now.

List operations

  • “+” operator concatenates two lists.

  • “ * ” operator repeats a list given number of times.

List slices

  • 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.

List methods

  • Append
  • Extend
  • Sort

Map, filter, reduce

  • Very important concepts!

  • Skip for now. When we learn loops, we will come back.

Deleting elements

  • .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

List and strings

Objects and values

  • 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.

Aliasing (nickname)

  • 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.

Glossary

  • List

  • Element (aka item)

  • Nested list

  • Accumulator (variable i)

  • Augmented assignment

  • Reduce

  • Map

  • Filter

  • Object

  • Equivalent

  • Identical

  • Reference

  • Aliasing

2. Dictionaries

  • 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 _

A dictionary is a mapping

  • key:value pairs

Looping and dictionaries

Dictionaries VS lists

Glossary

  • Mapping

  • Dictionary

  • Key-value pairs

  • Item

  • Key

  • Value

  • Implementation

  • Hashtable

  • Hash function

  • Hashtable

3. Tuples

  • 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

  • Iterators are collections similar to lists.
  • You can iterate through.
  • But, you can’t use index to access elements of iterator.

Zip and Enumerate

  • Special and very useful functions.

Glossary

  • Tuple
  • Tuple assignment
  • Zip object
  • Iterator
  • Data structure

4. Strings

  • String methods:
    • .count
    • .index
    • .format
    • .capitalize
    • .casefold
    • .endswith
    • .expandtabs
    • .find
    • .join
    • .lower
    • .partition
    • .replace
    • .startswith
    • .strip

A string is a sequence

Traversal with a for loop

String slices

Strings are immutable

“in” operator

String comparison

Glossary

  • Object
  • Sequence
  • Item
  • Index
  • Slice
  • Empty string
  • Immutable

5. Sets

  • Mutable object i.e. can be changed.

  • Data structure (collection) that can contains unique/distinct elements.

Set methods

  • .add
  • .clear
  • .copy
  • .difference
  • .intersection

6. Frozenset

  • Immutable object i.e. cannot be changed.

  • Can contain only unique/distinct elements.

Frozenset methods

  • .copy

  • .difference

  • .intersection

  • .isdisjoint

  • .issubset

  • .issuperset

  • .symmetric_difference

  • .union

The end

  • Thank you for your most valuable resource. Your time.