Python : Summary
I like books that explain things visually and this book falls in that category. I am reading this book after reading and understanding the basics of Python from “Think Python” and “Learn Python the Hard Way”. This book serves a nice visual recap of Python 101. I have listed down some of the points in various chapters mainly to ruminate over the learning’s from the previous two books.
Chapter 1- Getting Started
-
Python environment variables
-
PYTHONPATH : Has a role similar to PATH. This variable tells the Python interpreter where to locate the module files you import into a program. PYTHONPATH should include the Python source library directory and the directories containing your Python source code.
-
PYTHONSTARTUP : Contains the path of an initialization file containing Python source code that is executed every time you start the interpreter
-
PYTHONCASEOK : Used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
-
PYTHONHOME : An alternative module search path. It’s usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy
-
IDLE (Integrated DeveLopment Environment) is similar to RGUI. You can use it to type in programs, code modules, debug them
-
-
use import sys , sys.argv to access the arguments passed to Python script
-
Like R, you can programs in interactive mode or script mode
Chapter 2 - Expressions and Statements
-
The dir() function prints a list of currently defined names
-
del deletes only a variable, not the object to which it points, because another variable may be pointing to the same object. Python keeps track of objects internally and deletes them automatically when they’re no longer in use.
-
Every object has a unique identity, which is the address of the object in memory, expressed as an integer or long integer. An object’s identity doesn’t change; it is constant during the object’s lifetime. Use the id() function to determine an object’s identity.
-
In assignment of one variable to another, there is a crucial point that needs to be noted. If x is a string and you assign x to another variable y. Now y is also pointing to the same string. Now if you make changes to y, the value that x is pointing to remains and a new value is created and the name y gets binded to this new value. So, there is no problem when you assign one variable to another and then go and make changes to variables. Instead let’s say x is a list variable and you assign it to y. You make changes to y, it gets reflected in x. Why? List is mutable and hence both x and y point to the same data.
-
From highest to lowest priority, the operators are not, and, and or. The expression x and not y or z, for example, is equivalent to (x and (not y)) or z.
-
Python doesn’t have an exclusive-OR (XOR) Boolean operator or a built-in Boolean type
-
An is comparison matters only for mutable objects such as lists and dictionaries
-
You can chain comparisons to create expressions such as this: x<=y<z .Only adjacent operands are compared
Chapter 3 - Working With Numbers
-
Python has four numeric types , Integer, Long, Float and Complex Number. Python rounds floats to 17 significant digits.
-
print sys.maxint gives the maxint allowed on your machine
-
You can use the coerce() function to see how Python promotes numbers
-
int(x) to convert x to a plain integer , long(x) to convert x to a long integer, float(x) to convert x to a floating-point number,str() function to convert a number to a string
-
Useful functions from random module - seed, randrange, choice, shuffle, random, uniform
Chapter 4 - Working With Strings
-
Python implicitly constrains indexes that are too small or too large to sensible values. Reversed indexes return empty strings
-
s.isalpha() to test whether all the characters in s are letters. s.isdigit() to test whether all the characters in s are digits.
-
string.maketrans(from, to) creates a transition table and then one can use s.translate(tablename) to get all the translation done
-
int(s) , long(s),float(s),complex(s),list(s),tuple(s) are all useful for converting string in to different datatypes
-
repr(expr) converts object to string
-
the string returned by repr() can be passed to eval() to (re)create an object with the same value
-
There are a ton of functions related to strings. Probably this is the reason why Python is used in a lot of bioinformatics research
Chapter 5 - Working With Lists and Tuples
-
Why both Lists and Tuples ? Tuples are faster to access and consume less memory than lists. Python also requires tuples in certain situations, such as string formatting, dictionary keys, and multiple function arguments. Tuples also are handy when you want to be sure that your data can’t be modified by an alias
-
To convert list L to tuple T , use (L,)
-
The reverse of tuple packing is sequence unpacking: x, y, z = a
-
s[i][j] used to index tuple or list
-
If a is a mutable object such as a list, however, a = b doesn’t create a copy of a but instead creates an alias. a and b will refer to the same object, so changes in one are reflected in the other
-
[0] * n is an efficient way to initialize a list with n zeroes
-
Repetition creates shallow copies.
-
in, count, index can be used to test membership
-
s[:0] = t to add at the beginning of list, s[len(s):] = t to add at the end of list, s[i:j] = [] to remove elements from the list, s[i:i] = t to insert elements
-
reverse() modifies the original list. Reverse a copy if you want to maintain the original.sort() modifies the original list. Sort a copy if you want to maintain the original.
-
The bisect module contains functions for manipulating sorted lists. The insort() function inserts an item into a list, and maintains the sort order
-
The value returned by bisect() can be used to partition a list
-
cmp(x, y) returns -1 if x < y, 0 if x == y, and 1 if x > y.
Chapter 6 - Working With Dictionaries
-
d.popitem() returns a random item from dictionary
-
You can embed a dictionary in a dictionary by using the key as id(dict_object)
-
d.keys() , d.values(), d.items() to retrieve stuff from a dictionary
-
clear() method to delete all key-value pairs from a dictionary
-
d1.update(d2) to update d1 with all the key-value pairs of d2. If d1 and d2 contain matching keys, the d2 items prevail.
Chapter 7 - Control Flow Statements
-
In an expression surrounded by (), [], or , press Enter, and continue the expression on the next line
-
You can use the -t and -tt command-line options to detect inconsistent use of tabs and spaces in your programs
-
Equivalent to seq function in R – range([start,] stop [,step]) to return a list of integers from start to, but not including, stop.
-
One common use of range() is to assign consecutive values to a group of variables to create an enumeration
Chapter 8 - Functions
-
Difference between parameters and arguments
-
In R, you type the function and you get to see the entire code. In Python, one can use print lassname.__doc__ to see the documentation of the function
-
Parameters with default values must come after parameters without default values
-
When you call a function, Python passes the object references of the arguments from the caller to the function. For immutable objects (numbers, strings, and tuples), this behavior effectively creates a local, temporary copy of the argument inside the called function; the function cannot change the original object. The story is different for mutable objects (lists and dictionaries). Passing a reference creates an alias of the original object inside the called function. aliases are variables that share references to the same object. The original object (in the caller) will change if you modify the passed-in object in place (in the function). On the other hand, if you reassign the aliased parameter a value, the reference to the original object is lost, and changes in the function won’t be reflected in the caller.
-
To avoid modifying a shared mutable object in a function, pass in a copy of the object
-
It’s good practice to make local copies of mutable arguments in a function to ensure absolutely that there are no side effects. It should be the responsibility of the function, and not its caller, to prevent side effects.
-
Functional Programming tools
-
Lamda :Creates small, anonymous functions
-
apply() : Indirectly calls a function and passes it positional and keyword arguments . The apply() function takes a function, a tuple, and a dictionary as arguments. It calls the function by using the tuple items as positional arguments and the dictionary items as keyword arguments, and returns the result of the function call.
-
map() :Does the same thing as apply family in R.Applies a function to sequence items and returns a list containing the results.However in R, there are many more variations of apply. I hope that someone writes a plyr , reshape equivalent modules in Python. In R, I use them so often that i have put them in the startup script .
-
zip() : Takes a variable number of sequences and returns a list of tuples, where the n-th tuple contains the n-th item of each sequence
-
filter() : Returns a list containing the items of a sequence that satisfy a given condition
-
reduce() : Applies a function to sequence items, starting with the first two items and successively using the result of a function call together with the next item, ultimately reducing the sequence to a single value
-
-
Differences between lambda and def
-
lambda is an expression, whereas def is a statement. You can use a lambda expression anywhere you’d use a normal expression: as an argument, as a list item, as a dictionary value, and so on.
-
The def block permits multiple statements, whereas the lambda body must be a single expression.
-
Nonexpression statements such as if, while, for, and print are forbidden in the body of a lambda expression, which restricts the number of operations you can cram into a lambda function.
-
You can use a lambda expression without assigning it a name.
-
-
A list comprehension is a concise and often clearer alternative to creating lists by using lambda, map(), and filter(). [expr for var in seq if cond] This appears like a very interesting way to cut down unnecessary code. This is something I have not seen much in the previous two books on Python that I have read. So , I guess this is one new learning for me after slogging through 250 pages of this book is list comprehension. Yet to use list comprehension in a code fragment
Chapter 9 - Modules
-
Flow of content is the module is as follows : documentation string, import statements, global variable definition, module’s classes, module’s functions, and finally if statement that checks whether the module is being imported in to another program or is it run on a standalone basis
-
Because Python doesn’t rerun module code after the first import, a variable created in a module won’t revert to its initial value on subsequent imports
-
dir(object) is similar to str function in R. It shows the list of important attributes of object
-
from module import doesn’t import names that begin with an underscore, which are termed private names
-
Python executes module code only once, during the first import. You can force Python to reload and rerun a module’s code by using the built-in reload() function. This function is useful when you want to see module changes without stopping Python.
-
You can append a directory in the sys path using the function sys.path.append(dir)
-
You can determine how a module has been loaded by inspecting its built-in attribute
-
sys.modules.keys() gives the list of all modules loaded
-
There are three types of namespaces
-
Local namespaces. Python creates a new local namespace whenever a function is called. The local namespace contains a key-value pair for each function parameter and each local variable defined in the function’s body. Variables in functions that are declared global are not in this namespace (they’re in a global namespace). Each function has its own local namespace, which Python deletes when the function returns a value or raises an exception.
-
Global namespaces. Python creates a new global namespace when a module is first loaded via import. The global namespace contains a key-value pair for each top-level (global) variable, function, class and imported module. Each module has its own global namespace, which Python deletes when the interpreter halts.
-
Built-in namespace. Python creates the built-in namespace when the interpreter starts. The built-in namespace contains a key-value pair for each of Python’s built-in functions (len, str, and so on) and exception (TypeError, SyntaxError, and so on) names. There’s only one built-in namespace, which Python deletes when the interpreter halts.
-
-
locals() to return a dictionary corresponding to the current local namespace
-
globals() to return a dictionary corresponding to the current global namespace.
-
vars(object) to return a dictionary corresponding to the local namespace of object.
-
compile() is used to compile a string into a code object. eval() returns the result of the evaluated expression. exec used to run a file, object or code object
Chapter 10 - Files
This chapter contains a list of functions that one would typically use to work with files stored on the disk. The note on pickle and cpickle module was something that I haven’t tried till date. Have to work on it soon.
**
Chapter 12 - Classes
**
The following gives a schematic diagram of Python in-built types
-
In the above diagram , Slice objects represent extended slices, which are used in Numerical Python (NumPy). Extended slices don’t work with standard sequences (lists, strings, and tuples) .May be once I go over NumPy, I will learn about Slice objects in greater detail.
-
Python provides several predefined class attributes. The class’s attribute holds the class’s attribute names and values in a dictionary
-
__class__ gives the name of instance class
-
A class variable exists independently of class instances. The variable is available no matter how many class instances are created, even if none is created.
-
If you change a class variable’s value at run time by qualifying it with the class object (class.attr = value), Python dynamically changes the value of attr to value in all existing (and future) class instances
-
You can access the attribute of a class before instantiating the class. This is so different from the other languages I have coded.
-
You can do functional overloading, Operator overloading by writing __XYZ__ functions where XYZ is the relevant operator or function that you are trying to overload
-
pt = Point(3, 9) is equivalent to Point.__init__(pt, 3, 9)
-
set the truth value of an instance by writing your custom your custom __nonzero__()
-
To make instance callable , use instance callable, use the special method __call__(self [,args])
-
Names in a class’s namespace (__dict__ attribute) aren’t visible in the class’s methods, so you must
fully qualify names that refer to class attributes outside a method -
Private names begin __ but don’t end__ with a double underscore
-
isinstance() ,issubclass() functions used to check class membership
Takeaway :
With abundant screenshots scattered through out , this book indeed offers a quick recap of all the basic elements of Python programming.