Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Get this slide deck:

slides.com/cheukting_ho/python-list-dict

Recap

Beginner topics:

Python objects, Control flows,

Functions, modeuls, classes and decorators

strings operations and regex with re

Testing:

pytest with fixtures and mock

property-based testing

python linters & auto-formatters

TDD, type hinting

Intermedite topics:

Iterators, generators, async

Packaging:

generating docs, pypi

Others:
Functional Programming

List (revisit)

 

  • mutable - can change the elements
     
  • hence not hashable (what does that mean)
     
  • A non-mutable, hashable cusion is Tuple - e.g (1, 2)
     
  • str is also a sequence that is hashable
     
  • list object elements are by reference so changes will populate

List (revisit)

 

  • sequencial object
     
  • has __getitem__()
     
  • iterable
     
  • ordered (can be sorted)

List comprehension

https://www.programiz.com/python-programming/list-comprehension

 

A short cut to create a list in a "Pythonic way"

h_letters = []

for letter in 'human':
    h_letters.append(letter)
h_letters = [ letter for letter in 'human' ]

Sorting Dicts

 

  • Dictionaries on the other hands are not ordered
  • We cannot sort a dicitonary
  • But we can have a ordered dictionary form the collections module

 

https://docs.python.org/3/library/collections.html#collections.OrderedDict

from collections import OrderedDict
d = {'banana': 3,
     'apple':4,
     'pear': 1,
     'orange': 2}
new_d = OrderedDict(sorted(d.items()))
new_d
c = Counter()
c = Counter('gallahad')
c = Counter({'red': 4, 'blue': 2})
c = Counter(cats=4, dogs=8)

Next week:
TBC

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm

More Python List and Dict

By Cheuk Ting Ho

More Python List and Dict

  • 322
Loading comments...

More from Cheuk Ting Ho