Chenthill Palanisamy edited this page Jul 11, 2016 · 12 revisions

Python is dynamically typed interpreted object oriented language. Python uses indentation to identify the body of functions, loop and conditions. It is garbage collected.

Uses indentation to denote the body of a loop, function, conditional statements etc. Usually tab space can be set to 4 spaces.

settings for vim,
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab

Basic data types

integer, string, float, complex Boolean - True, False del - to remove the reference of a variable


eg = r"example\n"
r - raw, 

""" to span string
literals across multiple lines \

Python strings are immutable

Arithmetic operators

** - exponent
// - Floor
Rest of the operators are same as C.

Descriptive operators

in, not in - membership operators
is, is not - identity operators
or, and, not - logical operators

Conditional operators

if (expression):
elif expression:
else: statement


for iterating_var in sequence:

for i in range(10)
for i in range(5,10)
range generates arithmetic progressions

while expression:

has else statement which can be tagged along with end of loop. executed unless loop is terminated by break
break and continue statements similar to C


def function_name (parameters):
    """function documentation"""
    return [expression]

All parameters are passed by reference argument types: required, keyword, default, variable length

Variable length args,

def function_name ([formal_args], *var_args_tuple)

Anonymous functions,

lambda [arg1 [,arg2,.....argn]]:expression

return statement with no arguments is same as return None

local and global variables same as C.

global variable_name - to access global variable

pass - statement is a no op. useful for empty api documentation

BuiltIn Data Structures


list =  [ 'abcd', 786 , 2.23, 'john', 70.2 ]

list[start:stop:step] to slice a list

list [::-1] - reverses the list

Looping for i in list: print i

for q, a in zip(questions, answers):
    print (q, a)
zip - pair multiple sequences and loop through,

for i, v in enumerate(['tic', 'tac', 'toe']):
    print(i, v)
fetch index and value of a sequence using enumerate


It is a immutable list

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )


dict = { jack': 4098, 'jones': 4139}
dict['first'] = "Add the first item"
dict[2]     = "This is two"

for k, v in dict.items():
    print(k, v)


set = { 'apple', "orange" }

'apple' in set
- find if apple is a member of set.


A module is a file containing python code. modules provide namespacing.

import module1[, module2[,... moduleN] - to import whole module
from module_name import func_name
from module_name import * - lets you access function_names, variables without specifying module_name

Modules are searched first in current directory, then PYTHONPATH and default installation dependent path - /usr/local/lib/python/


Package is a collection of modules specified in . Directory name being the package name.

__all__ = [module1, module2] (in for supporting from package_name import *

dir(package_name) - outputs all the names defined in a module
globals() - returns global names
locals() - from a function returns local names
reload(module_name) - to execute the top level code of module again.

if __name__ == "__main__":
    functions to run when the module is run as a script

Object Oriented Features


Command Line Arguments

Profiling and Debugging Tools

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.