Mike Trienis bio photo

Mike Trienis

All about data product and services that scale; from design to implementation

Email Twitter LinkedIn Github Stackoverflow

Preparing for a technical interview with Python means that you should have a decent understanding of the following concepts.

Iterating over iterators

Lists, dictionaries, generators and strings are all example of iterators. Each one of these constructs support for statements.

# looping through a list
>>> for i in [1,2,3]:
...   print i

# looping through a dictionary
>>> for i in {"key":"value"}:
...   print i

# looping through a string
>>> for i in "hi":
...     print i

Generator functions

Generators are used to produce iterators with minimum memory consumption.

# Using the generator pattern (an iterable)
class firstn(object):
    def __init__(self, n):
        self.n = n
        self.num, self.nums = 0, []

    def __iter__(self):
        return self

    # Python 3 compatibility
    def __next__(self):
        return self.next()

    def next(self):
        if self.num < self.n:
            cur, self.num = self.num, self.num+1
            return cur
            raise StopIteration()

sum_of_first_n = sum(firstn(1000000))

Anonymous (lambda) functions

The next snippet of code defines a lambda function which is essentially a function that is not bounded to a name.

>>> print lambda x : i * x
<function <lambda> at 0x109ae6938>

List comprehensions

A list comprehension a way to generate lists using a natural syntax. If we build on the previous example, we can generate a list of lambda functions by:

>>> print [lambda x : i * x for i in range(4)]
[<function <lambda> at 0x109ae6938>, <function <lambda> at 0x109ae6758>, <function <lambda> at 0x109ae69b0>, <function <lambda> at 0x109ae6a28>]

Dynamic arguments

It’s possible to pass a dynamic number of arguments. Either a sequence of values or a sequence of key-values.

>>> def dynamicArguments(*arg, **kwargs):
...     print arg
...     print kwargs
>>> dynamicArguments(1,2,3, first=4, second=5, third=6)
(1, 2, 3)
{'second': 5, 'third': 6, 'first': 4}

Linked-list implementation in python

A node in a linked list can represented as a class with a storage variable and another variable that points to the next node in the list. If the next pointer is None then it’s the last element of the list.

class Node:
  def __init__(self, item=None, next=None):
    self.item = cargo
    self.next  = next
  def __str__(self):
    return str(self.item)

nodeA = Node("A")
nodeB = Node("B")
nodeC = Node("C")

nodeA.next = nodeB
nodeB.next = nodeC

Private members in class

A member variable in a class can be set to private by prefixing with __.

>>> class Foo:
...     myPublicVar = "a" 
...     __myPrivateVar = "b" 
>>> foo = Foo()
>>> print foo.myPublicVar
>>> print foo.__myPrivateVar

Class variable and instance variables

Class variables:


  path = "something/"
  children = [AController, BController]

  def action(request):

Instance variables:


  def __init__(self):
    self.path = "something/"
    self.children = [AController, BController]

  def action(self, request):

Command-line arguments (sys.argv)

Passing command line arguments to a python script is as simple as importing sys module and using the sys.argv for retrieving the arguments.

import sys
print sys.argv

The arguments are constructed as a list when executing the python script.

Mikes-MacBook-Pro-3:code miketrienis$ python example1_4.py a b
['example1_4.py', 'a', 'b']

Pass statement

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:

>>> def initlog(*args):
...     pass   # Remember to implement this!