Table of Contents

Python

The Python language has many similarities to Perl, C and Java. However, there are some definite differences between the languages. This chapter is designed to quickly get you up to speed on the syntax that is expected in Python.

Install Python

Convert Python Script to execute file on windows

Install PyInstaller

  1. Step1: Install PyInstaller
    easy_install.exe dis3
    easy_install.exe pyinstaller
    easy_install.exe --upgrade pyinstaller
  2. Step2: Check PyInstaller was installed:
    pyinstaller --version

Create execute file exe to run on windows

Steps to create exe file from python script:

  1. Step1: Create Hello.py
    print "Hello, Python!";
  2. Step2: convert Hello.py to exe file:
    pyinstaller --onefile Hello.py

    or

    pyinstaller --onedir Hello.py

    ⇒ The exe file will startup more fastly

Error can't run exe python file:

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth__tkinter.py", line 30,
in <module>
IOError: Tcl data directory "C:\Users\anh\AppData\Local\Temp\_MEI83~1\tcl" not found.
[6720] Failed to execute script pyi_rth__tkinter

Fix

easy_install --upgrade pyinstaller
pyinstaller --add-data d:\tools\python27\tcl\tcl8.5;tcl\tcl8.5  --add-data d:\tools\python27\tcl\tk8.5;tk\tk8.5 --onefile tktonkho.py

Package Manager for Python

Pip

Install Pip

  1. Step1: Install
    wget https://bootstrap.pypa.io/get-pip.py
    python2.7 get-pip.py
  2. Step2: Update pip
    python -m pip install --upgrade pip

Using pip

conda

refer: http://conda.pydata.org/docs/intro.html

Conda is an open source package management system and environment management system for installing multiple versions of software packages and their dependencies and switching easily between them. It works on Linux, OS X and Windows, and was created for Python programs but can package and distribute any software.

If you’ve used pip and virtualenv in the past, you can use conda to perform all of the same operations. Pip is a package manager, and Virtualenv is an environment manager. Conda is both.

TaskConda package and environment manager commandPip package manager commandVirtualenv environment manager command
Install a packageconda install $PACKAGE_NAMEpip install $PACKAGE_NAMEX
Update a packageconda update –name $ENVIRONMENT_NAME $PACKAGE_NAMEpip install –upgrade $PACKAGE_NAMEX
Update package managerconda update condaLinux/OSX: pip install -U pip Win: python -m pip install -U pipX
Uninstall a packageconda remove –name $ENVIRONMENT_NAME $PACKAGE_NAMEpip uninstall $PACKAGE_NAMEX
Create an environmentconda create –name $ENVIRONMENT_NAME pythonXcd $ENV_BASE_DIR; virtualenv $ENVIRONMENT_NAME
Activate an environmentsource activate $ENVIRONMENT_NAMEXsource $ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
Deactivate an environmentsource deactivateXdeactivate
Search available packagesconda search $SEARCH_TERMpip search $SEARCH_TERMX
Install package from specific sourceconda install –channel $URL $PACKAGE_NAMEpip install –index-url $URL $PACKAGE_NAMEX
List installed packagesconda list –name $ENVIRONMENT_NAMEpip listX
Create requirements fileconda list –exportpip freezeX
List all environmentsconda info –envsXInstall virtualenv wrapper, then lsvirtualenv
Install other package managerconda install pippip install condaX
Install Pythonconda install python=x.xXX
Update Pythonconda update python *XX

conda update python updates to the most recent in the series, so Python 2 to latest 2.x, Python 3 to latest 3.x, and so on.

Install Conda

Linux

pip install conda

Windows: download and install it from http://conda.pydata.org/miniconda.html(Win32, not 64bits)

After install, change the PATH of python to PATH of conda

Basic Create virtual enviornment with conda

Create virtual environment for zipline app and using it

We will install zipline which requirement package numpy (Because we can found numpy in packages of conda on https://anaconda.org/anaconda/repo):

  1. Step1: Create new virtual environment with name zipline which contain package numpy in conda
    conda create -n zipline numpy

    ⇒ (option -n → –name of environment)You click yes to install these packages for new virtual environment. You can check new environment created in directory

    D:\tools\miniconda2\envs\
  2. Step2: Go to virtual environment zipline:
    source activate zipline

    in Windows, go to directory d:\tools\Miniconda2\condabin\ and run command below

    conda.bat activate zipline
  3. Step3: Install zipline in environment:
    conda install -c Quantopian zipline

    ⇒install package zipline from channel Quantopian on https://anaconda.org/anaconda/repo

Other commands of conda

Anaconda

install anaconda:

conda install anaconda

Build python modules from source

  1. Step1: Download source and go to source directory and run
    pip install -r requirements.txt
    python setup.py build
    python setup.py install
  2. Step2: Check modules installed using python -c, for example:
    python -c "from PIL import Image, ImageFont, ImageDraw"

First Python Program

Interative mode programming

Invoking the interpreter without passing a script file as a parameter brings up the following prompt:

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Type the following text to the right of the Python prompt and press the Enter key:

>>> print "Hello, Python!";

If you are running new version of Python, then you would need to use print statement with parenthesis like

print ("Hello, Python!"); 

. However at Python version 2.4.3, this will produce following result:

Hello, Python!

Script mode programming

Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Let us write a simple Python program in a script. All python files will have extension .py.

Let's try another way to execute a Python script.

Python Command line and environment variables

refer: https://docs.python.org/2/using/cmdline.html

Python Command line

Python Environment Variables

refer: https://docs.python.org/2/using/cmdline.html#environment-variables

PYTHONHOME

Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

PYTHONPATH

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

Basic Syntax

Python Identifiers

A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $ and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.

Here are following identifier naming convention for Python:

Reserved Words

The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.

andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

Lines and Indentation

One of the first caveats programmers encounter when learning Python is the fact that there are no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Both blocks in this example are fine:

if True:
    print "True"
else:
  print "False"

However, the second block in this example will generate an error:

if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"

Thus, in Python all the continous lines indented with similar number of spaces would form a block. Following is the example having various statement blocks:

Note: Don't try to understand logic or different functions used. Just make sure you undertood various blocks even if they are without braces.

#!/usr/bin/python
 
import sys
 
try:
  # open file stream
  file = open(file_name, "w")
except IOError:
  print "There was an error writing to", file_name
  sys.exit()
print "Enter '", file_finish,
print "' When finished"
while file_text != file_finish:
  file_text = raw_input("Enter text: ")
  if file_text == file_finish:
    # close the file
    file.close
    break
  file.write(file_text)
  file.write("\n")
file.close()
file_name = raw_input("Enter filename: ")
if len(file_name) == 0:
  print "Next time please enter something"
  sys.exit()
try:
  file = open(file_name, "r")
except IOError:
  print "There was an error reading file"
  sys.exit()
file_text = file.read()
file.close()
print file_text

Multi-Line Statements

Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example:

total = item_one + \
        item_two + \
        item_three

Statements contained within the [], {} or () brackets do not need to use the line continuation character. For example:

days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']

Quotation in Python

Python accepts single ('), double (“) and triple (''' or ”“”) quotes to denote string literals, as long as the same type of quote starts and ends the string.

The triple quotes can be used to span the string across multiple lines. For example, all the following are legal:

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Comments in Python

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line end are part of the comment and the Python interpreter ignores them.

#!/usr/bin/python
 
# First comment
print "Hello, Python!";  # second comment

This will produce the following result:

Hello, Python!

A comment may be on the same line after a statement or expression:

name = "Madisetti" # This is again comment

You can comment multiple lines as follows:

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

Using Blank Lines

A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.

In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.

Waiting for the User

The following line of the program displays the prompt, Press the enter key to exit and waits for the user to press the Enter key:

#!/usr/bin/python
 
raw_input("\n\nPress the enter key to exit.")

Here, “\n\n” are being used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.

Multiple Statements on a Single Line

The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon:

import sys; x = 'foo'; sys.stdout.write(x + '\n')

Multiple Statement Groups as Suites

A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class, are those which require a header line and a suite.

Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite. For example:

if expression : 
   suite
elif expression : 
   suite 
else : 
   suite

Command Line Arguments

You may have seen, for instance, that many programs can be run so that they provide you with some basic information about how they should be run. Python enables you to do this with -h:

python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser (also PYTHONDEBUG=x)
-E     : ignore environment variables (such as PYTHONPATH)
-h     : print this help message and exit
[ etc. ]

You can also program your script in such a way that it should accept various options. Command Line Arguments is an advanced topic and should be studied a bit later once you have gone through rest of the Python concepts.

Python Data Types

Python has five standard data types:

Assigning Values to Variables

Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. For example:

#!/usr/bin/python
counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string
 
print counter
print miles
print name

Output:

100
1000.0
John

Python Numbers

Python has five standard data types:

Python Strings

Strings in Python are identified as a contiguous set of characters in between quotation marks. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus ( +) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example:

#!/usr/bin/python
 
str = 'Hello World!'
 
print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string

This will produce the following result:

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

Python Lists

refer: http://www.tutorialspoint.com/python/python_lists.htm
A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example:

#!/usr/bin/python
 
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
 
print list          # Prints complete list
print list[0]       # Prints first element of the list
print list[1:3]     # Prints elements starting from 2nd till 3rd 
print list[2:]      # Prints elements starting from 3rd element
print tinylist * 2  # Prints list two times
print list + tinylist # Prints concatenated lists

This will produce the following result:

['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and size cannot be updated. Tuples can be thought of as read-only lists. For example:

#!/usr/bin/python
 
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
tinytuple = (123, 'john')
 
print tuple           # Prints complete list
print tuple[0]        # Prints first element of the list
print tuple[1:3]      # Prints elements starting from 2nd till 3rd 
print tuple[2:]       # Prints elements starting from 3rd element
print tinytuple * 2   # Prints list two times
print tuple + tinytuple # Prints concatenated lists

This will produce the following result:

('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')

Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists:

#!/usr/bin/python
 
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]
tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Python Dictionary

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ).

Data Type Conversion

FunctionDescription
int(x [,base])Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )Converts x to a long integer. base specifies the base if x is a string.
float(x)Converts x to a floating-point number.
complex(real [,imag])Creates a complex number.
str(x)Converts object x to a string representation.
repr(x)Converts object x to an expression string.
eval(str)Evaluates a string and returns an object.
tuple(s)Converts s to a tuple.
list(s)Converts s to a list.
set(s)Converts s to a set.
dict(d)Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)Converts s to a frozen set.
chr(x)Converts an integer to a character.
unichr(x)Converts an integer to a Unicode character.
ord(x)Converts a single character to its integer value.
hex(x)Converts an integer to a hexadecimal string.
oct(x)Converts an integer to an octal string.

Python Operators

Arithmetic Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
+Addition - Adds values on either side of the operatora + b will give 30
-Subtraction - Subtracts right hand operand from left hand operanda - b will give -10
*Multiplication - Multiplies values on either side of the operatora * b will give 200
/Division - Divides left hand operand by right hand operandb / a will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainderb % a will give 0
**Exponent - Performs exponential (power) calculation on operatorsa**b will give 10 to the power 20
//Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.9//2 is equal to 4 and 9.0//2.0 is equal to 4.0

Compare Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
==Checks if the value of two operands are equal or not, if yes then condition becomes true.(a == b) is not true.
!=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(a != b) is true.
<>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(a <> b) is true. This is similar to != operator.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(a > b) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(a < b) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(a >= b) is not true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(a ⇐ b) is true.

Python Assignment Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
=Simple assignment operator, Assigns values from right side operands to left side operand c = a + b will assigne value of a + b into c
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**=Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a<nowiki>| |<nowiki>//=<nowiki>|Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand|<nowiki> c //= a is equivalent to c = c // a

Python Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows:

a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011

There are following Bitwise operators supported by Python language

Operator Description Example
&Binary AND Operator copies a bit to the result if it exists in both operands.(a & b) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in eather operand.(a | b) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(a ^ b) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.(~a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
«Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.a « 2 will give 240 which is 1111 0000
»Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.a » 2 will give 15 which is 0000 1111

Python Logical Operators

There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example
andCalled Logical AND operator. If both the operands are true then then condition becomes true.(a and b) is true.
orCalled Logical OR Operator. If any of the two operands are non zero then then condition becomes true.(a or b) is true.
notCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.not(a and b) is false.

Python Membership Operators

In addition to the operators discussed previously, Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators explained below:

Operator Description Example
inEvaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y.
not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise.x not in y, here not in results in a 1 if x is not a member of sequence y.

Try following example to understand all the membership operators available in Python programming language:

#!/usr/bin/python
 
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
 
if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"
 
if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"
 
a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"

When you execute the above program it produces the following result:

Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list

Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator Description Example
isEvaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).
is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwise. x is not y, here is not results in 1 if id(x) is not equal to id(y).

Python Decision Making

Statement Description
if expression:
  statement(s)
An if statement consists of a boolean expression followed by one or more statements.
if expression:
   statement(s)
else:
   statement(s)
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   else
      statement(s)
elif expression4:
   statement(s)
else:
   statement(s)
You can use one if or else if statement inside another if or else if statement(s).

Python loops

Loop TypeDescription
while expression:
   statement(s)
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for iterating_var in sequence:
   statements(s)
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops: with for:
for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)

with while:

while expression:
   while expression:
      statement(s)
   statement(s)
You can use one or more loop inside any another while, for or do..while loop.

With Loop Control Statements:

Control Statement Description
break statementTerminates the loop statement and transfers execution to the statement immediately following the loop.
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

Examples:

Python Functions

Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

Syntax and examples

Pass by reference vs value

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example:

Global vs. Local variables

#!/usr/bin/python
 
total = 0; # This is global variable.
# Function definition is here
def sumlocal( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print "Inside the function local total : ", total
   return total;
def sumglobal( arg1, arg2 ):
   # Add both the parameters and return them."
   global total
   total = arg1 + arg2; # Here total is local variable.
   return total; 
# Now you can call sum function
sumlocal( 10, 20 );
print "Outside total after run sumlocal : ", total
sumglobal( 10, 20 );
print "Outside total after run sumglobal : ", total

Output:

Inside the function local total :  30
Outside total after run sumlocal :  0
Outside total after run sumglobal :  30

Built-in Functions

https://docs.python.org/2/library/functions.html

Define Function inside Function

Define functions inside other functions:

def greet(name):
    def get_message():
        return "Hello "
 
    result = get_message()+name
    return result
 
print greet("John")
 
# Outputs: Hello John

Functions can be passed as parameters to other functions

def greet(name):
   return "Hello " + name 
 
def call_func(func):
    other_name = "John"
    return func(other_name)  
 
print call_func(greet)
 
# Outputs: Hello John

Functions can return other functions

In other words, functions generating other functions.

def compose_greet_func():
    def get_message():
        return "Hello there!"
 
    return get_message
 
greet = compose_greet_func()
print greet()
 
# Outputs: Hello there!

Python Modules

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.

Create simple module

Create module support.py

def print_func( par ):
   print "Hello : ", par
   return

import and using module

Basic syntax:

from file1 import *

⇒ import all objects and methods in file1

Excuting modules as script

To run modules as script, you add below code at the end your module for running functions in module:

if __name__ == "__main__":
    import sys
    print_func(sys.argv[1])

⇒ This code only run when your module is main. That mean It only implement when you run module as script Therefore simple module support.py will update to:

def print_func( par ):
   print "Hello : ", par
   return
if __name__ == "__main__":
    import sys
    print_func(sys.argv[1])

And you can test the module with command below:

python support.py "zara"

Output:

Hello :  zara

Packages in Python

Packages are a way of structuring Python’s module namespace by using “dotted module names”.

Python Exceptions

Used to replace “Try Catch” with “Try Except”

Syntax

try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 

Simple code:

#!/usr/bin/python
 
try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
   fh.close()

The try-except clause

The try-finally clause

Python with Statement

With the “With” statement, you get better syntax and exceptions handling:

Without with statement, we use:

import sys
 
try:
  # open file stream
  file = open("welcome.txt")
  data = file.read()
  print data
  file.close()
  # It's important to close the file when you're done with it  
except IOError:
  print "There was an error reading welcome.txt"
  sys.exit()

With Statement Usage

Notice, that we didn't have to write “file.close()”. That will automatically be called.

Python Classes/Objects

Creating Classes, Instant Objects and Using

Class Inheritance and Overriding methods

Private and public variables

Default variable in python are public. For private variable, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders Example:

#!/usr/bin/python
 
class JustCounter:
   __secretCount = 0
 
   def count(self):
      self.__secretCount += 1
      print self.__secretCount
 
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount

Output:

1
2
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'

Python static variable

refer: http://radek.io/2011/07/21/static-variables-and-methods-in-python/

All variables defined on the class level in Python are considered static. See this example:

class Example:
    staticVariable = 5 # Access through class
 
print Example.staticVariable # prints 5
 
# Access through an instance
instance = Example()
print instance.staticVariable # still 5
 
# Change within an instance
instance.staticVariable = 6
print instance.staticVariable # 6
print Example.staticVariable # 5
 
# Change through
class Example.staticVariable = 7
print instance.staticVariable # still 6
print Example.staticVariable # now 7

If variable:

Python static functions

With static methods it gets a little more complex. In Python, there are two ways of defining static methods within a class.

Python Interface and Multiple Inheritance

Interfaces is not necessary in Python. This is because Python has proper multiple inheritance, and also ducktyping, which means that the places where you must have interfaces in Java, you don't have to have them in Python.For example:

class SomeAbstraction( object ):
    pass # lots of stuff - but missing something
 
class Mixin1( object ):
    def something( self ):
        pass # one implementation
 
class Mixin2( object ):
    def something( self ):
        pass # another
 
class Concrete1( SomeAbstraction, Mixin1 ):
    pass
 
class Concrete2( SomeAbstraction, Mixin2 ):
    pass

Decorators for Functions and Methods

A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.
refer: http://thecodeship.com/patterns/guide-to-python-function-decorators/
https://wiki.python.org/moin/PythonDecoratorLibrary

Basic Syntax

The current syntax for function decorators as implemented in Python 2.4a2 is:

@dec2
@dec1
def func(arg1, arg2, ...):
    pass

⇒ function func will automatically call func = dec2(dec1(func)) after declare This is equivalent to:

def func(arg1, arg2, ...):
    pass
func = dec2(dec1(func))

Composition of Decorators

refer: Python Functions
Function decorators are simply wrappers to existing functions. Putting the ideas mentioned above together, we can build a decorator. In this example let's consider a function that wraps the string output of another function by p tags.

def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)
 
def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper
 
my_get_text = p_decorate(get_text)
 
print my_get_text("John")
 
# <p>Outputs lorem ipsum, John dolor sit amet</p>

⇒ my_get_text = p_decorate(get_text) generate a new function with name func_wrapper

   def func_wrapper(name):
       return "<p>{0}</p>".format(get_text(name))

Example with Python's Decorator Syntax

Python makes creating and using decorators a bit cleaner and nicer for the programmer through some syntactic sugar To decorate get_text we don't have to get_text = p_decorator(get_text) There is a neat shortcut for that, which is to mention the name of the decorating function before the function to be decorated. The name of the decorator should be perpended with an @ symbol.

def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper
 
@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)
 
print get_text("John")
 
# Outputs <p>lorem ipsum, John dolor sit amet</p>

Generators functions and Yield

Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. Refer:

Simple code generate iterator

Generate iterator with python syntax

Python provides generator functions as a convenient shortcut to building iterators. Lets us rewrite the above iterator(simple example) as a generator function:

Python Extending and Embedding

Python's C API

Build Simple C API module:

Cython

refer: http://en.wikipedia.org/wiki/Cython
Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement. It works by producing a standard Python module. The difference from standard Python behavior however, is that the original code of the module is actually written in Python but is then translated into C

Cython has an unusually involved hello world program because it interfaces with the Python C API and the distutils extension building facility. At least three files are required for a basic project:

The following code listings demonstrate the build and launch process:

Python Unit Test

Below are steps to write unittest:

  1. You define your own class derived from unittest Testcase
  2. Write your test functions start with test_
  3. You run the tests by placing the code unittest.main() in your file, usually at the bottom of file

Below are example code: