Python Tricks: A Buffet of Awesome Python book notes


I started reading a book Name: Python Tricks: A Buffet of Awesome Python

I thought to create online notes of this book so that I can share it with everyone, I’ll keep updating this topic as I progress in the book. Let me know if you have any questions. :blush::blush:

Topic: Python Functions are first class ( 21 Jan 2019)

  • Everything in python is an object, including Functions. You can assign them to variables, store them in Data Structures, and pass or return them to and from other functions.
    some examples:
#function greet
def greet(name):
    return 'Hello {}'.format(name)

#assign function to variable
name = greet('Anand Singh')

#add function to a list
functions = [greet('Anand'), greet('Singh'), greet('Baghel')]
  • Functions can be passed to other function. First class functions allow you to abstract away and pass around behavior in your programmes.
def greet(func):
    greetings = func.format('Anand Singh')

def text():
    return "Hello {}"
  • Functions can be nested.

Topic: Dictionaries, Maps, and Hashtables (23 Jan, 2019):

  • Dictionaries store an arbitrary number of objects, each identified by a unique dictionary key.
    Dictionaries are also often called maps, hashmaps, lookup tables, or associative arrays. They allow for efficient lookup, insertion, and deletion of any object associated with the given key.

  • Python’s dictionaries are indexed by keys that can be any hashable type. Immutable types like string and numbers are hashable and work well as dictionary keys.

  • Curly braces, the dictionary expression allows us to define new dictionary objects
    For Example, Phone Book, Allows us to quickly retrieve the contact information (Value) associated with a given name (Key) The dict data type:

phonebook = {
    'anand' : '1234',
    'singh' : '3456',
    'baghel' : '88808'

another example, Square of number in range n where n = 10:

squares = {x: x * x for x in range(10)}
  • Python dictionaries are based on hash table implementation. O(1) time complexity for lookup, insert, update, and delete operations in the average case.

Topic: Array Data Structures (24 Jan 2019):

  • Arrays consist of fixed-size data records that allow each element to be efficiently located based on its index.

  • Performance-wise, It’s very fast to look up an element contained in an array given the element’s index. A proper array implementation guarantees a constant O(1) access time for this case.

  • list - Mutable Dynamic Array

    • List allows elements to be added or removed, and the list automatically adjusts the backing store that holds these elements by allocating or releasing memory.

    • Python list can hold arbitrary elements - “everything” is an object in Python, including functions.

      data = [1, 2, 3, True, 'Anand', 'Singh', 4.50]
      #List are Mutable
      data[2] = 5
      del data[3]
  • tuple - Immutable Containers

    • in a tuple, elements can’t be added or removed dynamically, all elements must be defined at creation time.

    • It can hold elements of arbitrary data types.

      data = 1, 2, 3, True, "Anand", 'Singh', 4.50
  • array.array - Basic Typed Arrays

    • Arrays created with the array.array class are mutable and behave similarly to lists, except for one important difference, they are “typed arrays” constrained to a single data type.
      import array
      dataInt = array.array('i', [1, 2, 3, 4, 5, 6])
      dataFloat = array.array('f', (1.2, 2.5, 3.9))


hashable description from python documentation link: Python Docs Glossary

An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().