Python Basics – A quick python review

I have been working with Python since 2007 on various projects. I have mostly used it on prototyping projects, some web development and some data analysis work. Python has changed a lot. However I still need to sometimes go and review the basics. Here are some useful notes that I have taken over many years. In case you also like to remind yourself of some of the basic operations and techniques in Python these might come in handy. I am also interested to know some of the simple Python principles and techniques that you find useful

Python Variables

For deleting a variable you can use delete

Python style guide uses underscores for specific purposes _ is used for a throw away variable or for referring to the previous results in the interactive Python. Leading __var__ are usually used for reserved variables. Leading single underscore is usually used for internal variables _var.

 

Keys in Dictionaries
Python has a very easy to use dictionary data structure. Dictionaries are highly efficient that provide O(1) insertion and read operations (as opposed to O(n) in a list). However, it is very easy to accidentally degrade your dictionary to a list and dramatically impact its efficiency. Let’s assume you like to check to know whether your dictionary contains a key. There are two ways to do that. The first method is checking key in dict and the second one is key in dict.keys(). Both will return the correct value. However, the second method is dramatically slower than the first since dict.keys() is in fact a list and not a dictionary anymore

IF Statements 

Unlike other programing languages there is no multi branching in Python (no “case” and “switch” statements). Python also uses “elif” for else if

Various Assignment Operations

Zip and Map

Zip and Map are two of the most useful functions in Python. Zip allows you to make tuples of data from lists. Map allows you to apply functions to tuples.

You can define your own function by using lambda. Below is the same sum function


Python Classes

In Python the __init__ method is called automatically at the instantiation stage. Every time Python instantiates an object it will call this method.

List Comprehensions in Python
List comprehensions are one of the most elegant language constructs in Python. Let’s assume we want to produce a list of all numbers between 0 and 100 that are divisible by both 5 and 3. This can be done by the following code

List Slicing in Python
Python has excellent syntaxt for working with its lists. Lists in Python are all zero-indexed so the first element of a list a = [1,2,3] can be found by a[0]. You can also take a range of elements, for example if you like to take the second element all the way to the end you would use a[1:]. Interestingly if you like to take the second element until the end but excluding the last element you would use a[1:-1]. List slicing notation also accepts steps, for example if you like to take the former list but slip every other element, the you would use a[1:-1:2]. You can also use negative steps. See this link for more.

The following code sample allows you to generate bigrams for a sentence

Object Serialization in Python
Python has two modules for serialization cPickle and Marshal. You can compress each serialized object by using the gzip module.

Raising an Exception in Python
If you like to manually raise an arbitrary exception then you can do the following

Python: Reversing the order of words in a string and in place

Question: Say we want to write a function in Python that takes a string and reverses the order of the words in place. How would you do it?

Answer: In python, C# and Java (and many other languages) strings are immutable so this is not possible to do. But let’s say we use the list representation of the string and want to reverse the order of words in place. The following is the shortest code that I can come up with.

Merge Sort in Python

Below is the most compact merge sort function that I can come up with in Python

And the following are the unit tests for it

Quick Sort in Python

Below is the most compact implementation of the quick sort algorithm that I could come up with

And below are its unit tests

atoi() in Python

Sometimes in an interview question you might be asked to implement the atoi() function that exists in C. Below is the most compact version of atoi() that I can come up with. atoi() taks an string and converts that to an integer.

Find the maximum depth of nested parentheses in a string

Pairs of elements that add up to n

Question: find all the elements in a list or array that add up to n

Answer: