Skip to main content Link Search Menu Expand Document (external link)

Python Refresher

Table of contents

  1. Python Data Types
  2. High-Performance Data Types
    1. Collections
    2. Priority Queues or Binary Heap
    3. Sets
  3. Python Functions
    1. String Tricks
    2. Using map function
    3. Using sort and sorted functions
    4. Generating Random Numbers
    5. Custom Comparators
  4. Python is call by value or call by reference?
  5. Classes
    1. Inheritance in Classes
  6. References vs. Copying
  7. Important Functions
  8. Miscellaneous

Statistical vs Dynamic Type: Python exhibits dynamic typing, i.e., no need to indicate the type of the object.

Python Data Types

  • Built-in atomic data types: int, float, and bool
  • Built-in collection data types: list, string, and tuple
    • List: Sequentially ordered collection, mutable and heterogeneous myList = [1, 3, True, 6.5]
    • String: Sequential collection of characters, immutable myString = "Mohit"
    • Tuple: Heterogeneous sequences of data, immutable myTuple = (2, True, 4.96)
    • Set: Unordered collection of zero or more immutable data items, heterogeneous mySet = {3, "Ca", 4.5, False} or myEmptySet = set()
    • Dictionary: Unordered structure with pair of items of key and value capitals = {'Iowa':'DesMoines','Wisconsin':'Madison'}

Variable names can start from letter and underscore(_), and are case-sensitive. A reference pointer is created to the data type when an assignment operator is used (e.g., theSum = 0)

Mutability: The property to get modified. e.g., In the list, an item can be modified using indexing and assignment. However, in the string, the item can not be modified

myName = "Mohit", myTuple = (2, True, 4.96)
myName[0] = 'R' # Not allowed
myTuple[1] = False # Not allowed

High-Performance Data Types

Collections

  • Counter: Dictionary to count number of occurrences
  • Deque: Queue w/ both ends, operations: deque([iterable]), append(), appendleft(), pop(), popleft(). All append and pop operations are O(1)
  • DefaultDict: Does not raise a key error
  • OrderedDict: Maintains and remembers the insertion order
    import collections
    count = collections . Counter (<list >)
    deque = collections . deque ()
    def_dict = collections . defeaultdict () # attributes: list, int, set
    ord_dict = collections . OrderedDict ()
    

Priority Queues or Binary Heap

  • import heapq
  • heapq.heapify(iterable): Make a new heap [TC: O(n)]
  • heapq.heappush(heap, elem): Push the value item onto the heap. [TC: O(logn)]
  • heapq.heappop(heap): Pop and return the smallest item from the heap. [TC: O(logn)]
  • heap[-1]: Peek the smallest element [TC: O(1)]
  • In python, it is implemented as List.
  • For maxheap, multiple all elements with -1

Sets

Use sets to convert a list into O(1) lookup (instead of converting it into a dictionary)

set(<list >)

Python Functions

String Tricks

  • Joining an iterable with a separator: separator.join(iterable)
    s = " 123456 "
    "_".join(s) #1_2_3_4_5_6
    
  • Splitting an string: string.split(separator, maxsplit)
    s = "12 _34_56_78 "
    s. split ("_") #["12" , "34" , "56" , "78"]
    
  • String Manipulations: cases
    a = " ABcdEF "
    a_lower = a. lower () # " abcdef "
    a_upper = a. upper () # "ABCDEF"
    a_sent = a. capitalize () # "Abcdef"
    
  • Check if character is alphanumeric: s.isalnum(): returns True/False

Using map function

map(func, iter): Returns a map object (an iterator) of the results after applying the given function to each item of a given iterable.

  • Example: x = map(str, nums) returns all numbers in nums array as strings

Using sort and sorted functions

  • x = sorted(nums) returns the sorted array
  • Returns the sorted with custom comparator
    • x = sorted(nums, key=newKey) class newKey(str) def __lt__(x, y): return x+y > y+x
<list >. sort(key= lambda x:x[0] , reverse =True) #returns Nothing
sorted (<list >, key= lambda x:x[0] , reverse =True)

Generating Random Numbers

random.randint(a,b): give random integer including a and b

Custom Comparators

Add custom comparators in python for any operations like sort, etc. In cases like heapq, the best way to add custom comparator is to make it a tuple with the first element being one that accepts normal python comparator. [Ref]

def compare (item1 , item2):
	if len(item1) > len(item2): return 1
	elif len(item1) < len(item2): return 1
	return 0
words.sort(cmp = compare)

Python is call by value or call by reference?

Python is neither call by value or call by reference. We can say that python is call by object reference. [Ref1] [Ref2] [Ref3] [Ref4]

How does variables work in python? In Python, variables work as tags. When an assignment is performed in python, it tags the value with the variable name. When a variable is updated, it just changes the tag to the new value in memory. (If a value is in the memory without any tags, the python garbage collector will automatically free the memory). The idea of tagging is also called binding, where an object is binded to a name. Consider the following code:

a = 1 
a = 2 

The first line creates a value 1 in memory and tags it with a. The second line creates a value 2 in memory and tags it with a. Since, the tag for value 1 is not available anymore, python garbage collector frees the memory.

a = 10 
b = a

This code will assign two tags b and a to the value 10. To finally understand, consider the following code:

def spam(eggs):
  eggs.append(1)
  eggs = [2, 3]
ham = [0]
spam(ham)
print(ham)

The output of the program would be $[0,1]$. The idea of binding can be tested as follows,

a, b = 10, 10
print id(a), id(b) #same

Note on id(x) behavior: The behavior of id(x) might not be consistent for large number depending upon the large numbers of large strings.


Classes

  • Constructors are called using __init__(self, params)
  • Class object can be converted into string as (used by print function) __str__(self)
  • A list of objects when printed uses function __repr__(self)
  • Arithmetic operators __add__(self, other)
  • Equality __eq__(self, other)

Inheritance in Classes

IS-A relationship

  • In the parent classes, we can define functions which will be implemented in Child classes
  • In child class, the constructor needs to call the parent class constructor
    • parent_class.__**init__**(self, parent_class_params)
    • super(child_class, self).__**init__**(parent_class_params)

References vs. Copying

Cloning a list: newList = oldList[:]

Important Functions

  • range
  • input(String) : Prompts a String message to ask user for an input, and returns the String

Miscellaneous

  • Reversing a string: s[::-1]