Вы находитесь на странице: 1из 95

Course :- Learning to Program With Python

Instructor – Tim Warner

Python is based (in large parts) on the ABC Programming Language which is mostly a learner’s language
and not a programmers language, for more information visit “A Short introduction to the ABC Languge” :
Homepages.cwi.nl/~steven/abc. The language was developed in Netherland and was extensively used by the
founder and creator of Python Guido van Rossum. Python is a high-level language which has an interactive mode
where we can interact with the interpretor in a live session where we can issue statements and commands and the
interpretor will procees it immediately and give us feedback.

Things Needed to get Started

1. Python 3 (pthon.org/download) We will be working with Python 3.1.2 as it’s a stable release and has more
compatibility with regard to this lesson however we should note that version 3.x is not backward compatible
with version 2.x and there are some key changes in the 3.x version where “print “_”” statement has become a
function print(“_”) so it has to be enclosed in parenthesis. The good news is we can download and run 3.x and
2.x side by side with no problems.
2. Exercise Files from Nuggetlab.com search and download exercise files for python pl (files having extensions -
.py).

What is Python?
Python is generally defined as a general purpose high-level programming language that features a high degree of
readability and user friendliness,(we say general purpose because Python is not specifically targeted to any specific
application domain such as a desktop, web browsers and mobile devices.
A high level programming language such as Python abstracts or hides the details of the underlying hardware or
Operating System, C is considered a low level language because when we are programming in C we are actually
mapping out memory address registers and RAM and we are defining calls that go right down to the nooks and
cranny of our OS and in some cases interact directly with the hardware and the processors on the system. High
Level Programming Languages tend to have more user friendly syntax then the low levels pl’s. The C language has
some security issues such as if we are not properly allocating memory and not performing bounds checking on the
memory allocation we can a have a malicious user crash that app using buffer overflow.
In case of Python our source code is read line by line by a virtual machine whereas in C once we compile it we have
executable files such as in case of Windows we have external libraries that are stored as .dll, so a fully compiled
application written in a low level language like C is a standalone unit while in Python we have to have a runtime
environment on the host OS to read the code which can be a hindrance in Windows as other platforms like MacOS
or the *NIX have a Python interpreter or a VM by default, but when it comes to readability on different platform
the source code remains the same.
Python is also a multi paradigm language(paradigm in this context simply means programming style), such as
procedural or imperative programming style, Object Oriented Programming style(important ones) and functional
programming style.
Procedural Prog. Style involves procedures where we do not modulize our codes into objects but have a more firm
control over the direction of code execution or branching.

Python is brought to us by the Python Software Foundation whose president is Guido van Rossum the inventor of
Python language. Python is an Open Source programming language that can be used by anyone to develop, bundle
and sell their application to anyone without owing anything to the PSF, the only requirement being that you leave
a Python copyright notice in your source code.

Python is both a Scripting and a Programming Language, traditionally speaking scripting languages do not
generally compile to a source code, for example, the C language, when we run it through the compiler we get
machine language which is a very low level code that interacts directly to the OS on the computer on which it is
running. Python being an interpreted language ie to say we have a Python interpreter or VM running on that
platform and the code is first interpreted into bytecode which can interact with any Python VM irrespective of the
platform or the processor whether it’s a x64bit or x32bit it doesn’t matter. Then we have a module called
py2exe(visit www.py2exe.org) which takes us from scripting to fully fledged programming by providing us a
compiled end product or a standalone application that can be run on Windows Platform without having a
interpreter or a VM installed.

Another differentiation that we have in regard to Python being both a scripting and a programming language is
that a ScriptFile is generally speaking a Single file and that script file is created to accomplish a single call whereas
Multifile files have multiple .py file and several other code files and other .py files that are modules or function
libraries that are brought in which get us moving from scripting to more robust application environment.

Scripting Languages are instantiated as being embedded in the applications that they control (some good examples
are Sun Microsystems Virtual Box software which has built in support for Python scripting so we can automate a lot
of virtual machine tasks like batch creation or batch management of VM files by using a Python interface.

Reasons to use Python in the real world

1. Rapid Development: We can do the programming in python pl with a smaller amount of code as compared to
C++ or Java and we can run that code in testing environment faster than we can with compiled applicatons for
example the .NET framework is a compiled application so if we use for instance C# when it comes to test there
is a delay because the integrated development environment such as Visual Studio has to run compile and link
process before giving the app in the runtime environment whereas in Python we get to interact with the
interpretor directly and we can get feedbacks as we test things out.

2. Application Portability: Python has cross platform portability so we can run the same code without making
changes to the source on different platforms

3. Wide Variety of Libraries: Python has external modules like the .py files which can be used to do just about
anything in Python.

4. Integration with other platforms: The version of python available at python.org is technically called CPython
because the python interpreter is itself built in the C language. If we want to run our code in a Java
environment we have an interpretation of Python called Jython which allows us to code in Python and run it in
the Java Virtual Machine and we have access to all Java libraries in our Python code and there is IronPython
which can be used to code in Python and run it in the Microsoft environment or the .Net framework. WE can
access trypython.org to know how to embed the Python interpreter in a Microsoft Silverlight object. Microsoft
Silverlight is Microsoft answer to Adobe flash which is a web based interactive media display. Then there is 2to
3 which helps you to port your Python 2.x code to 3.x
5. We can do system administration and automation such as creating user accounts at once, mapping drives and
web service calls using the Python across different platforms then we have libraries available for just about
every major database like the Microsoft Sequel server or the Oracle Database or in the open source world like
MySQL, we can plug-in using the Application Programming Interface (API’s) to deal with Network and web
access. We will find that in the hacking world, Python is a very popular language to do ethical hacking or
penetration testing.

6. Python is not simply a console or command line interface by any means; we have lots of libraries like tkinter
that supports Graphical User Interface development.

7. Python is not only used in teaching but also in higher academic research in Science and Mathematics field in
general.

8. Python is extensively used by various organizations such as the Hewlett Packard to create scripts to automate
Hardware and Software Testing. Other examples of Python based applications include BitTorrent client written
in Python and Google’s Youtube.com involves a lot of Python behind it. Then there is Google’s App Engine
(where we can sign up for free and they will host our web applications, it’s a great platform for testing and
initially rolling out our web apps) which supports Python. Reddit.com involves Python framework in their site,
Pixar and Industrial Light and Magic use Python for their animation work, then in the science and math field
Python is used by NASA, Los Alamos and Fermilab. Spam Bayes an open source anti spam and malware plugin is
a Python application. Bug tracking application ‘Trac’ relies upon Python and then there is Anaconda the
software updater used by Fedora and Red Hat Linux is based on Python.

Downsides to Python

1. Execution Speed: As discussed earlier Code written in Python when compiled gets converted into byte code
which is then interpreted by the runtime environment or the VM’s which is going to be slower when compared
to the a fully compiled application that is already in machine language which interacts directly with the OS or
the hardware. To fix this we have a library called Psyco (download the Psyco extension module from
psyco.sourceforge.net) which has a primary purpose of making Python run faster and then inside the VM’s
itself we have have what is called caching that caches the code that is not changing during each run so that
those pieces of code remain in the bytecode format when we re-run the script.

2. Running in Browser: Python does not have the transparent browser friendliness that we find in other languages
like JavaScript. The way around is to use Python with Server-side Scripting using a protocol or an application
programming interface called the Common Gateway Interface. More commonly people use Python with web
application by using a framework that wraps Python and enables them to generate web content, commonly
used frameworks are Django, Google app Engine and PyJamas which enables us to do Asynchronous JavaScript
and XML (AJAX) by using Python.

What is an IDE

IDE stands for integrated development environment and it is going to principally be a source code editor which
keeps our multifile process organized. An IDE gives us features like code completion so as we are typing in
statements methods and functions it will give suggestions also, code folding which basically expands and collapses
code blocks and Syntax Highlighting which makes all the methods and instances to be colored. It also acts as a
compiler (in case of C) and interpreter (Python) for your codes, many ide’s come with builder which can compile
and build your runtime app, ide’s also act as debugger for your source code for setting break points and checking
the values of various variables in your code.
The IDE for Python is called IDLE which is a GUI application which relies upon the tkinter framework, idle has a few
compatibility issues which will be discussed later.
Some Programmers prefer using notepad textpads vim or emacs for writing their source code instead of idle, an
honorable mention here will be Sun Microsystem’s NetBeans and Eclipse both of which are open-source editors,
If we are willing to spend money for some premium features we can go for Wing IDE or Active State Komodo IDE.

Running Python Programs

Python Programs can be run using the following (Assuming the user has already installed Python on his machine)

1. Interactive Prompt: Opening a command prompt or Powershell and typing Python or you can directly open
IDLE and you can work in GUI environment. This method is mainly used for Development Testing and Learning.
2. To actually do some work we will go to a system command line and call Python executable and the Python
script file. For example type ”C:\projects\home.py” displays “Hello World!”.
3. Going directly to the .py files and launching them with double left click.

Data Types

Python Program Flow


1. Program containing module(s): A python program is one or more files that have .py extension that accomplish
certain goals set by the developer, a module is a .py file that calls upon other .py files to accomplish a certain
goal, standard library contains core module of the Python pl.
2. Modules consisting of Statements: a statement is a set of reserved words that call upon a specific action.
3. Statements consisting expressions: An expression is basically a math or computer science term which basically
performs specific operation on data values.
4. Expressions consist of Object which are fundamental building block of a Python Program.

Objects and Types in Python

Object is defined as an entity that may or may not have associated attributes, methods, functionality etc. An entity
can be class, function, a name or a variable etc anything we operate on or use, call or define in Python is an object.
Python is either Dynamic Typed or Static (strongly) Typed ie when we declare a variable in Python we don’t have
to specify the data type that it is let’s say we define a variable named t that was going to store date time
information, we don’t have to declare that as “t (some datatype)” such as in C# or Java which are strictly or static
typed languages. In Python when we assign a value to a variable or initialize it, the value becomes the data type
that gets associated with the variable such as we type n=1, t=2 then n and t gets associated with integer data type
similarly s=’Tim’ gets associated with String data type.

See image below


Variable is a representation of data that can have its value change overtime.
Data Type means classification of data with certain characteristics, in Python we can use the + sign for addition as
well as to concatenate non numeric data.
A function is a subset of a large program that defines some modular unit of code or performs some specific action
that returns or acts independently from the rest of our program.
As for being Strongly or Static Typed, Python will prevent mixing operations with mismatched types.
As shown in image below
As we can see above anything inside a quotation mark is considered string type so the interpreter is preventing
mixing operations. On the other hand we can input
‘Abhi’ + ‘Jaiswal’
And it will return as ‘Abhi Jaiswal’ which is an example of concatenation.

Core Data Types in Python are number, string and tuple which are the immutable data types or in other words
once you define a name or a number using that data type it is going to be that way forever. We can create a new
object for instance a=3.14 is a float data type, we can input int(a) and it will just cut out .14 and come up with a
value 3.
Mutable data types are list, dictionary and files of which list and dictionary are multi element data structures.
List can have multiple data types attached to single variable for example a=[1, ‘tim’, 2.03], dictionary have key and
value pairs attached to the same variable and files are used to create, delete, print, email files etc.

Numbers

Presence or absence of a decimal point is Python way of determining a data type, integers are 1, 2,-5,-100…..
whereas float are 3.14, 2.04, -3.14……

type () is a used to verify a data type for example if a = 1; type(a) will display class int
isinstance () tests for a particular data type for example division = 100 / 10 gives 10.0 so the data type for division is
float if we input isinstance (division, int) it will return false.

Expressions involves two operands (or object which is operated on) and one operator to perform actions upon
those operands for example in the expression “result = a+b” a and b are operands and + is the operator. Other
examples of operators are -, *, /(simple division), //(integer division where the digits after the decimal point are
removed), % (modulus gives back only the remainder), ** (exponent for example 10^2), <, >, = to associate a
variable to a data, == is the equality operator (a==b) is not true, != or bang equals is the inequality operator (a!=b)
is true, Boolean comparisons like AND, OR, NOT.
Order of operations PEMDAS(please excuse my dear aunt sally) or Parenthesis, Exponentiation, Multiplication,
Division, Addition, Subtraction is applied when doing math.

For complex Mathematics there are two modules provided to us by the standard library ie Fractions for fractions
and Math for doing trigonometry etc. We can download two important modules namely NumPy and SciPy
(numpy.scipy.org). The Python shell can be used as a calculator and to get online help for a function example
help (isinstance).

Strings

A string is an ordered sequence of single character data that relies upon an encoding mechanism. Python 2.x used
ASCII encoding mechanism whereas Python 3.x uses Unicode characters.

len(s) gives the character length


+ is for concatenation ( + is an example of overloaded operator because it has more than one function)
String * 10 is for repetition to print the given string given no. of times ie in our case 10.

See below image


String Slicing
Corp[:] returns the entire string

String Methods

A method is a function or an operation that acts on a given object.


“Stringname”.find () gives the offset id for the searched character
“st”.replace() replaces the substring
“st”.split() cuts string at the delimiter
“st”.upper changes the given string to uppercase
“st”.lower changes the string to lowercase
A Sample Program

Tuple in Python is an ordered set of data, while in a relational database theory it’s a single row in a database table.
In Python, tuple is an example of a sequence (like strings+lists). The main difference between a list and a tuple is
that a tuple is an immutable list in order to modify a tuple we are essentially going to need to create a new object.
Tuples are normally used for smaller datasets than the lists and due to their read only nature they are good for any
data that you want to protect against overwriting during program execution or static program data.

Syntax in tuple, when defining a tuple we always use parenthesis as a delimiter whereas in lists we use big brackets
and dictionary uses curved brackets or braces. The types of elements inside a tuple consist of strings float and
integer for this reasons Tuples can also be defined as ordered collections of arbitrary objects.

Slicing in a tuple each object that is defined is separately indexed for example

atuple = (‘cbt’, ‘nugget’, ‘abhi’, ‘jais’, ‘2.1’) is indexed as 0,1,2,3,4 forward and -1,-2-3,-4,-5 backwards

Tuples are read only sequences and therefore cannot be modified with methods such as insert(), remove(),
append() etc. We can use len() to determine the length of a tuple and we can perform type conversion.

Why use Tuples?

1. Tuples are considered to be faster than list because they are immutable, they are pre-calculated during the
program’s first run.
2. They are used for write protection purposes.
3. Tuples can be integrated with dictionaries.
Note to self: Because there is no clear screen function in a Python shell we can use command
def wiper():
print(‘\n’ *100)

and then wiper ()


to get a cleared workspace.

Lists
A list in Python is defined as a true workhorse datatype because they have mutable sequences.

Example of methods in list dt


So here we can see that sort() requires the values to be in the same data type and since we string, float and integer
it returned an error and even though we converted all data types into string the change wasn’t actually applied to
the original list idi1, that is the reason the compiler still returns an error.

Dictionaries
While strings, lists and tuples are all sequences because they consist of an ordered, indexed sequence of objects or
items dictionaries are mappings which are although mutable, they consist of unordered set of key:value pairs.
Technical synonym of dictionaries are associative arrays or hash tables. Python programmers use dictionaries
because they are fast as far as lookup is concerned chiefly because they have values associated with a key and each
key is run through a hash function. A hash is where we take source input and run it through some mathematical
algorithm to come up with a fixed length result and if we use hashes (or rather the interpreter cause we are
working with the key value in our code) that results in a much faster operation when compared to unhashed
operations.

Syntax in Dictionary

adict = {‘one’ : ’uno’, ‘two : ‘dos’, ‘three’ : ‘tres’}


Above is an example of a dictionary (notice the curly braces) which binds English to Spanish.
We can bring tuples in dictionary as keys as long as all the elements are either numbers or strings. Lists being
dynamic cannot be used as a key.
Methods in a Dictionary

Key(), item(), values() for retrieval of object from a dictionary, item is used for bringing back key:value pairs.
Pop() for removing key:value pair from a dictionary, Clear() will remove all items in dictionary.

Slicing is not allowed in a dictionary because they are unordered (explained earlier)
Adding a tuple to a dictionary

An example of embedding list on list

>>> l1= ["one", "two", "three", "four"]


>>> l2 = [1, 2, 3, 4]
>>> l1.append(l2)
>>> l1
['one', 'two', 'three', 'four', [1, 2, 3, 4]]

More Examples

adict = {'one':'uno', 'two':'dos', 'three':'tres'}


>>> len(adict)
3
>>> //number of key value pairs
l1
['one', 'two', 'three', 'four', [1, 2, 3, 4]]
>>> len(l1)
5
>>> string1 = "nuggets"
>>> len(string1)
7
>>> adict
{'three': 'tres', 'two': 'dos', 'one': 'uno'}
>>> \\here we can notice that when we call back a dictionary it never comes back in the same order

To remove “three” from the dictionary

>>> adict
{'three': 'tres', 'two': 'dos', 'one': 'uno'}
>>> adict.pop("three")
'tres'
>>> adict
{'two': 'dos', 'one': 'uno'}

A sample Program

val1 = input("Enter a string element 1/3: ")


val2 = int(input("Enter a integer element 2/3: "))
val3 = float(input("Enter a float element 3/3: "))

lst = [val1, val2, val3]


tpl = (val1, val2, val3)
dict = {"First Element ": val1, "Second Element ": val2, "Third Element ": val3}

print('\n') \\ to create a new line


print("Here is your list: ", lst)
print("Here is your tuple: ", tpl)
print("Here is your dictionary: ", dict)

print('\n')
val4 = input("Add a new string list element: ")
lst.append(val4)
print("Here is your new list: ", lst)
Output

>>>
Enter a string element 1/3: spam
Enter a integer element 2/3: 4000
Enter a float element 3/3: 3.232

Here is your list: ['spam', 4000, 3.232]


Here is your tuple: ('spam', 4000, 3.232)
Here is your dictionary: {'Second Element ': 4000, 'First Element ': 'spam', 'Third Element ': 3.232}

Add a new string list element: eggs


Here is your new list: ['spam', 4000, 3.232, 'eggs']
>>>

Statements, Documents and Online Help

A Python statement is simply a keyword identifier that is reserved by Python or in other words we cannot use them
as a variable. A few examples of keywords are if, else, and, break, def, del, assert etc.

A backslash (\) is an overloaded character which is used for escape sequences and line continuation ie if we have
multiple lines of code in a particular statement however we do not have to use \ for lists tuples and dictionary.

Simple Statements are simply termed as one line statements and Compound statements are multi line statements
or statement block. Python uses whitespace indentation to demarcate a statement block or compound statement
while languages like C use curly braces {} to limit the start and end of a code block.

An example of C program
# include <stdio.h>

int main()
{
printf(“Hello World:\n”);
return (0);
}

A compound statement in Python has two components called the header line and it is delimited by a colon (:) as
soon as you hit enter the IDE will understand that we are starting a code block and will automatically give you the
whitespace indentation which by default in Python is four spaces, we can further indent our code using tab key but
it is recommended that we do not do that as it affects readability when we port our code to another platform.
After the header line the indentation and the code block we have what is called a suite and its simply the second
part of our compound statement.
>>> l = ['apple', 'tomato', 'orange']
>>> 'grape' in l
False
>>> 'apple' in l // in is a test statement
True
>>> for i in l: // for is to iterate through the specified range and perform some task on each
print(i) item in the range here i is the storage name of each object in the range l

Output is

apple
tomato
orange

>>> for i in l:
print(i, len(i)) // displays the items in the range and their lengths

Output:

apple 5
tomato 6
orange 6
>>>

Some Examples of using \

l2 = ["This is a long string", \


"Another one"]
>>> l2
['This is a long string', 'Another one']
>>> al2 = "My name is "\
"Abhi"
>>> al2
'My name is Abhi'
>>> sls = "This is a super long string, \
the string contnues, etc."
>>> sls
'This is a super long string, the string contnues, etc.'
>>> sls1 = "abcdefghijklmnop\
qrstuvwxyz"
>>> sls1
'abcdefghijklmnopqrstuvwxyz'
>>>

Escape Charecters are formally defined as an alternate interpretation of string data an example of escape
character is \n to create a new line or \t for TAB.
Just a few example of using Escape Characters

txtfile = open(‘C:\new\textdoc.txt’ , ‘w’) here the interpreter will confuse the \n and \t as escape character, in
order to prevent that from happening we can do the following

txtfile = open(‘C:\\new\\textddoc.txt’ , ‘w’)


or
txtfile = open(r’C:\new\textdoc.txt’ ,’w’)

Sample showing usage of \, \n and \t

>>> ls = '''The quick brown fox jum over the lazy dog \
all day long.

More text. And more. And More. Are you enthralled ? I am now going to the next line.'''
>>> ls
'The quick brown fox jum over the lazy dog all day long.\n\nMore text. And more. And More. Are you enthralled ? I
am now going to the next line.'
>>> print(ls)
The quick brown fox jum over the lazy dog all day long.

More text. And more. And More. Are you enthralled ? I am now going to the next line.
>>> print('Cbt Nuggets')
Cbt Nuggets
>>> print("Cbt\nNuggets")
Cbt
Nuggets
>>> print('Cbt\tNugget')
Cbt Nugget
>>>

Variable Rules

The general form of variables in Python names is either a letter or an _ and some programmer defined length of
letters and digits for example we can have eggs or _eggs or Eggs here it is important to remember that eggs and
Eggs will be considered different variables as letters in different cases are considered different in Python. We
cannot use any special characters and no reserved words or Python statements.

Sample

>>> _spam = 1
>>> _spam \\ can use _
1
>>> Eggs = 'yummy' \\ uppercase and lowercase are considered different variables
>>> eggs = 'yukky'
>>> @name = 'Will this work?' \\ cannot use special characters in variables
SyntaxError: invalid syntax
>>>

Comments and Docstrings

We leave comments in our code for easier understanding and legibility. Any time Python comes across a # (hash
mark) it ignores whatever is written after it and considers it as comment for the rest of the line. For multiple line
comment we use triple quotation marks.

If we add a documentation string such as author’s information, copyright, comment etc on the top of a module,
class, function or a method we can query that module, class, function or method to retrieve that document string
by using an attribute __doc__(double underscore doc double underscore).

Sample to demonstrate use of attributes such as doc, dir and help.

In Pyhton shell open new window and save it as wiper.py at C:\python31\lib which is the directory for all the built
in python modules.
'''
Module Name : wiper.py
Author : Abhi Jais
Contact : abhinav.jais05@gmail.com
Desc : The w() function clears the interpreter screen
'''

def w():
print('\n' * 100) # to print 100 new lines when the module is called upon

Save the module and exit onto the Python Shell

>>> import wiper


>>> dir(wiper) # the dir() function lists all the names
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'w'] or contents of the given object
>>> help(wiper) # to get online help on the object
Help on module wiper:

NAME
wiper

FILE
c:\python31\lib\wiper.py

DESCRIPTION
Module Name : wiper.py
Author : Abhi Jais
Contact : abhinav.jais05@gmail.com
Desc : The w() function clears the interpreter screen

FUNCTIONS
w()
>>> print(wiper.__doc__) # prints the description of the module

Module Name : wiper.py


Author : Abhi Jais
Contact : abhinav.jais05@gmail.com
Desc : The w() function clears the interpreter screen

>>> wiper.w() # prints 100 new lines and gives us a clear whitespace

Online Help

The online help in Python 3 is bought to us courtesy of a module called PyDoc (learn more about Pydoc at
docs.python.org/py3k/library/pydoc.html)
The online help module or PyDoc has several useful functions one of which is help() function (Note: We can either
use the help function to get help with an object eg. help(wiper) or simply run help() function to get into the
interactive online help utility (type q to exit). We can also use the PyDoc HTML viewer in Start -> Programs -
>Python3.1->Module Docs.

Advanced String Methods

Visit : http://docs.python.org/py3k/library/stdtypes.html

A sample string

>>>a = ‘abc’ # abc is the string datatype


>>>a = a + ‘abc’
>>>a
abcabc

As we discussed earlier that strings are immutable but in the above example we were able to change the value of
the variable a which contains string datatypes, what happens here is that although a = abc gets converted into a =
abcabc the value abc still exists in memory because there may still be references that exist in our code for a = abc,
which has been demonstrated in the above example itself a = a + ‘abc’, the a to the right of the operator = is a
reference to the old string value assigned to ‘a’.

Example of string methods

>>> str1 = 'this is a string'


>>> str1.capitalize () # the first letter becomes uppercase
'This is a string'
>>> str1.count ('is') # count the number of occurrences of the particular string literal
2
>>> str1.count ('s')
3
>>> str1.count (' is ')
1
>>> str1.find ('ing') # gives us the position of the particular substring
13
>>> str1.isupper() # determines whether the string is in uppercase
False
>>> str1.upper()
'THIS IS A STRING'
>>> str1 # here we can see an example of string immutability
'this is a string'
>>> str1.lower()
'this is a string'
>>> strcap = str1.upper() # to change the string to uppercase permanently we assign it a new name
>>> strcap
'THIS IS A STRING'
>>>
>>> str2 = ' spacebar used here '
>>> str2.lstrip() # removes space to the left of the string
'spacebar used here '
>>> str2.rstrip() #removes space to the right of the string
' spacebar used here'
>>> str2.strip() # removes space from either side of the string
'spacebar used here'
>>> str3 = '1,2, ,4,5'
>>> str3.split(',') # splits the string at every comma
['1', '2', ' ', '4', '5']
>>> days = ['Monday', 'Tuesday', 'Wednesday']
>>> week = ' '.join(days) # since lists do not have a join() method the quoted string is necessary
>>> week
'Monday Tuesday Wednesday'

String Formatting

String formatting in Python is a way to take our string data and personalize it based on static or dynamic
information that already exists in names.

Old type of String formatting (used in C and Python 2 and 3)


>>> s = '%s has %d vacation days left' %('Abhi', 2) # here ‘s’ and ‘d’ are string formatting type code\
>>> s ‘s’ is for string , ‘d’ is for signed integer decimal, ‘f’ is \
'Abhi has 2 vacation days left' for floating point decimal and ‘c’ is for single characters

New type of String formatting (recommended as the old formatting may be removed from future Python release)
>>> s = '{0} has {1} vacation days left'.format ('Abhi', 2) # 0 and 1 being the index number of the strings
>>> s
'Abhi has 2 vacation days left'

Few more examples of old and new formatting

>>> csc = 'No %s for %s!'% ('chocolate', 'you')


>>> csc
'No chocolate for you!'
>>> r = 'rest'
>>> w = 'the weary'
>>> csc = 'No %s for %s!'% (r,w) # declaring the string values beforehand
>>> csc
'No rest for the weary!'

>>> arb = 'To kill two {0}s with one {1}'.format('bird', 'stone')
>>> arb
'To kill two birds with one stone'
>>> arb = 'To kill two {1}s with one {0}'.format('bird', 'stone')
>>> arb
'To kill two stones with one bird'
Obtaining Keyboard Input

In order to make our Python programs as flexible as possible we need to have a method to bring in keyboard based
string data from the user. To do this we have the input() method (raw_input() in Python 2). The keyboard input in
Python is bought to us by the standard library module called sys, stdin(standard input) refers to keyboard based
input and stdout(standard output) to the output we get on screen. However it is possible to redirect the output of
a script file using the ‘>’ operator (Another example of operator overloading as ‘>’ is used in integer and string
comparison and also for redirection). Therefore at a shell prompt we can call our script file using the Python
executable and the ‘>’ and then supply a path and a destination filename (eg shell>python prog.py>file.txt) and all
the output of the program will be written to that text file instead of being displayed at the traditional stdout which
is the screen.

A sample program

lb = int(input("Enter lower bound: "))


ub = int(input("Enter upper bound: "))

print("\nYou chose {0} as your upper bound, \


and {1} as your lower bound.".format(ub,lb))
verif = input("\nProceed? (y/n): ")
response = verif.lower()
if response == 'y':
for multiplier in range (lb, (ub + 1)):
for i in range (1,11):
print(i, "X", multiplier, "=", i * multiplier)
else:
print("We're done!")

Output

Enter lower bound: 2


Enter upper bound: 3

You chose 3 as your upper bound, and 2 as your lower bound.

Proceed? (y/n): y
1X2=2
2X2=4
3X2=6
4X2=8
5 X 2 = 10
6 X 2 = 12
7 X 2 = 14
8 X 2 = 16
9 X 2 = 18
10 X 2 = 20
1X3=3
2X3=6
3X3=9
4 X 3 = 12
5 X 3 = 15
6 X 3 = 18
7 X 3 = 21
8 X 3 = 24
9 X 3 = 27
10 X 3 = 30
>>>

Enter lower bound: 2


Enter upper bound: 3

You chose 3 as your upper bound, and 2 as your lower bound.

Proceed? (y/n): n
We're done!

Control Flow

The IF statement

The if statement is used for decision making, branching and conditional logic in a program. For example

If <test 1>:
<statement block>
else: #optional
<statement block>

If there are more than two branches in a program we can use elif (else if)

If <test 1>:
<statement block>
Elif<test 2>:
<statement block>
else: #optional
<statement block>

The if statement is very similar to the switch statement used in C language.

Operators used in if statements are ‘==’, ‘!=’, ‘<’, ‘>’, ‘<=’ (less than or equal to), ‘=>’ (greater than or equal to),
‘and’ ‘or’ and ‘not’.
A few sample programs
1.
#!/usr/bin/python

'''
The first unassigned string in a module is the docstring!
What we see above is the "exec/hack", that enables if1.py
instead of python if1.py in *NIX
'''

num = input("Enter an integer: ")


num = int(num)

if num < 0:
print("The absolute value of", num, "is", -num)
else:
print("The absolute value of", num, "is", num)

Output

Enter an integer: 5
The absolute value of 5 is 5

Enter an integer: -6
The absolute value of -6 is 6

2.
#!/usr/bin/python

forms = ["animal", "mineral", "vegetable"]


answer = input("Are you an animal, mineral or a vegetable? ")

if answer == forms[0]:
print("You are an animal. GRR!")
elif answer == forms[1]:
print("You are a mineral. You must be healthy.")
elif answer == forms[2]:
print("You are a vegetable. Do you wish for 'peas' in the world.")
else:
print("Your response is invalid.")

Output

Are you an animal, mineral or a vegetable? vegetable


You are a vegetable. Do you wish for 'peas' in the world.
Are you an animal, mineral or a vegetable? minerals
Your response is invalid.

Here we see that if the compiler detects any change in response from the list given to it, in this case minerals
instead of mineral it goes to the else statement.

3.
#!/usr/bin/python

yn = input("Continue? Yes or No: ")


yn = yn.lower()

if yn[0] == 'y':
print("You typed 'Yes'.")
elif yn[0] == 'n':
print("You typed 'No'.")
elif yn == 'spam':
print("What are you doing?")
else:
print("You have entered an invalid response.")

Output

Continue? Yes or No: YES


You typed 'Yes'.

Continue? Yes or No: yup


You typed 'Yes'. # since we are evaluating only the first character and forcing it
to lower case it takes ‘yup’ as a valid response.
Continue? Yes or No: maybe
You have entered an invalid response.

Boolean Logic

Also called truth-testing is a primitive data type having one of two values ‘True’ or ‘False’. Normally used in algebra
was named after its inventor George Boole an English mathematician.

x or y, if x is false return y else x.


x and y, if x is false return x, otherwise return y that means both conditions will have to be true inorder to return
both x and y.
not x, if x is false, return 1 otherwise return 0.

To test membership we use is

x is (in) y
x is not y
A sample program

fmenu = {"spam":1.50, "ham":1.99, "eggs":0.99}

corder = input("What will you have today spam, ham or eggs? ")

if corder == "spam":
print("For the spam, that will be", "$", "%.2f" % fmenu.get("spam"),", please.") # by giving “%.2f” we are
elif corder == "ham": assigning two decimal
print("For the ham, that will be", "$", "%.2f" % fmenu.get("ham"),", please.") places to the get(name)
else: function for the dictionary
print("For the eggs, that will be", "$", "%.2f" % fmenu.get("eggs")) if we don’t do that we will
get the value of 1.5, 1.9 and
Output 0.9.

What will you have today spam, ham or eggs? ham


For the ham, that will be $ 1.99 , please.

What will you have today spam, ham or eggs? spam


For the spam, that will be $ 1.50 , please.

What will you have today spam, ham or eggs? nothing


For the eggs, that will be $ 0.99

Examples of Boolean logic

>>> a = 50
>>> b = 25
>>> c = "spam"
>>> a == b
False
>>> a > b
True
>>> b <= a
True
>>> b < = a # never leave a gap between the two operator
SyntaxError: invalid syntax
>>> a != b
True
>>> d = 50
>>> a is d
True
>>> e = "eggs"
>>> c is not e
True
Break and Continue statement

Break and continue statement are useful when we have to direct the flow of our program more strictly. Break and
continue are used often with the If statement but not within an if block, they are used within a looping construct ie
the while loop and for loop. Break statement jumps out of the closest or enclosing loop and continues on with the
program while continue jumps out of the statement block and goes to the header of the closest or enclosing loop.

For example
while<test 1>:
<statement>
If <test 2>:
break #exit loop and skip else
if <test 3>:
continue # go to test 1
else:
<statement 2> # processed if there is no ‘break’

A sample program to demonstrate break and continue

# run this program from an OS shell prompt; it works better

while True: # here by entering true we run the risk of


s = input("Enter a string of atleast four characters, or q to quit: ") getting into an infinite loop we can avoid
if s == 'q': that only if our statement block returns a
break boolean value False
if len(s) < 4:
print("Value is too small.")
continue
print("The string was of sufficient length.")
raise SystemExit #try this from the OS command-line

Output in Python Shell


Enter a string of atleast four characters, or q to quit: onetwothreefour
The string was of sufficient length.
Traceback (most recent call last):
File "C:/Python31/if5.py", line 11, in <module>
raise SystemExit #try this from the OS command-line
SystemExit
In Command Prompt

C:\Users\Sysadmin>cd C:\Python31\
c:\Python31>dir *.py
Volume in drive C is OSDisk
Volume Serial Number is 5C01-0733

Directory of c:\Python31

02/13/2016 12:11 PM 289 if1.py


02/13/2016 08:54 PM 363 if2.py
02/13/2016 11:48 AM 424 if3.py
02/13/2016 01:01 PM 418 if4.py
02/13/2016 02:47 PM 533 if5.py
5 File(s) 2,027 bytes
0 Dir(s) 314,408,017,920 bytes free

c:\Python31>if5.py
Enter a string of atleast four characters, or q to quit: ace
Value is too small.
Enter a string of atleast four characters, or q to quit: aced
The string was of sufficient length.

C:\Python31>if5.py # notice here that our SystemExit command worked here


Enter a string of atleast four characters, or q to quit: q and so we had to go into if5.py to run the module

C:\Python31> # entering q closed the module if5.py as expected.

The For Loop

The for loop is our counting loop, which cycles or loops through an iterable. Iteration is the act of repeating the
process usually with the aim of reaching a desired goal. An iterable is any sequence in Python such as a string, list,
tuple, dictionary (which is not a sequence). A for statements goes through character by character in a string, by
index number in a list and tuple and ‘key:value’ pairs in dictionaries.

for var in iterable: # we declare a variable dynamically (i, j, k, a,b, c etc.) and some predefined
# loop body using var iterable or sequence.
else:
# invoked if no ‘break’

We can perform nesting in for statements i.e. have for statement inside of another for statement or typically if, elif
and else statement inside a for statement. For loops are also used for iterating or scanning files for eg we might
want to create an administrative tool to help with our system administration and management, we can use for
with open() function to open a log file on our server and read each line one after the other and then we can do
whatever we want with that data. In addition to an iterable we can also use the for loop in a range with the range()
function. Range is typically an integer sequence eg (0,5) to loop through 0,1,2,3,4, the range function does not
include the terminating integer.
A sample

>>> for letter in 'Python':


print('The current letter is: ', letter)

The current letter is: P


The current letter is: y
The current letter is: t
The current letter is: h
The current letter is: o
The current letter is: n
>>>type(letter)
<class ‘str’>
>>> for veg in ['leek', 'mushroom', 'okra']:
print('Current vegetable is: ', veg)

Current vegetable is: leek


Current vegetable is: mushroom
Current vegetable is: okra

>>> for i in range(1,7): # if we do not define the lower value of the range range(5) the loop will start from 0.
print(i)

1
2
3
4
5
6

tuplist = [(1,2), (2,3), (3,4), (4,5)]


>>> for (a, b) in tuplist:
print(a, b)

12
23
34
45

>>> import os
>>> for k, v in os.environ.items():
print('%s = %s' % (k, v))
The above program gives us the dump of all the environment variables that are relevant under the currently logged
on user which in this case is a Windows system.

Note : Functions that are built into Python such as open(), dir() etc are called builtins.

Open() function opens a file from the filesystem and returns a stream of characters.

>>> f = open('C:/Python31/spam.txt')
>>> for line in f:
print(line)

This is the first line.

This is the second line.

This is the third line.

Using the for loop in a dictionary

>>> nm = {'Key 1':'Value 1', 'Key 2':'Value 2'}


>>> for k,v in nm.items():
print(k, v)

Key 1 Value 1
Key 2 Value 2

>>> i = ['boy', 312, (2,4), 2.03]


>>> query = [(2,4), 2.0]
>>> for key in query:
if key in i:
print(key, "was found")
else:
print(key, "was not found")

(2, 4) was found


2.0 was not found

A sample program

for multi in range(2,6): #runs 4 iterations 2, 3, 4 and 5


for i in range(1, 11):
print(i, "X", multi, " =", i * multi)
print() # to print a blank line in b/w every multiplication table
Output

>>>
1X2 =2
2X2 =4
3X2 =6
4X2 =8
5 X 2 = 10
6 X 2 = 12
7 X 2 = 14
8 X 2 = 16
9 X 2 = 18
10 X 2 = 20

1X3 =3
2X3 =6
3X3 =9
4 X 3 = 12
5 X 3 = 15
6 X 3 = 18
7 X 3 = 21
8 X 3 = 24
9 X 3 = 27
10 X 3 = 30

1X4 =4
2X4 =8
3 X 4 = 12
4 X 4 = 16
5 X 4 = 20
6 X 4 = 24
7 X 4 = 28
8 X 4 = 32
9 X 4 = 36
10 X 4 = 40

1X5 =5
2 X 5 = 10
3 X 5 = 15
4 X 5 = 20
5 X 5 = 25
6 X 5 = 30
7 X 5 = 35
8 X 5 = 40
9 X 5 = 45
10 X 5 = 50
>>>

The While Loop

A while loop is more of a continual evaluation


while<test>: #the test must evaluate to Boolean True in order to continue for eg while (g<6): the loop will run
<statement block> #when the boolean is true only until the g is less than 6

Depending upon how we handle our true case code we can use ‘break’ and ‘continue’ to give our program more
flexibility.
else: # optional
<statement> # runs in absence of ‘break’.

In a while loop we often run the risk of an infinite loop, so we should always have either a boolean False or a
‘break’ to terminate or break out of the loop.

An infinite loop

>>> while True:


print(‘20 GOTO 10’)

The above statement will print 20 GOTO 10 infinite times. (To abort that press ctrl + c).

A finite loop

>>> x = 0
>>> y = 13
>>> while x < y:
print(x)
x += 1 # here we are adding 1 to x after every print function ie after printing 0 it adds 1 to it and
checks if 1 is less than 13 and prints 1.

0
1
2
3
4
5
6
7
8
9
10
11
12
>>>
a=0
while a < 13: # max value of a will be 12
a += 1 # increment a by 1
if a == 5: # when value of a reaches 5 skip everything below and go back to the start of the loop.
continue
if a == 10: # when value of a reaches 10 break out of the loop
break
print(a) # print the value of a (only works when a is not equal to 5 or 10)

Output

>>>
1
2
3
4
6
7
8
9
>>>

A sample program

'''
Title: Guess the number game.
Adapted from: inventwithpython.com
Purpose: Demo a live Python Program
'''

import random

guessesTaken = 0

print("Hello! What is your name?")


myName = input()

number = random.randint(1, 20)


print('Well, ' + myName + ', I am thinking of a number between 1 and 20')

while guessesTaken < 6:


print('Take a guess: ')
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')

if guess > number:


print('Your guess is too high.')

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Good Job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
number = str(number)
print('Nope. The number i was thinking of was ' + number)

Output

>>>
Hello! What is your name?
Abhi
Well, Abhi, I am thinking of a number between 1 and 20
Take a guess:
16
Your guess is too high.
Take a guess:
10
Your guess is too high.
Take a guess:
7
Your guess is too high.
Take a guess:
3
Your guess is too low.
Take a guess:
5
Good Job, Abhi! You guessed my number in 5 guesses!
>>>

Sets

Sets are in general collection of distinct objects that are themself considered discreetly an object. In Python, the
sets are unordered collection of unique and immutable data types. They are not a sequence or a key mappings like
a dictionary, although they resemble the properties of a dictionary in a way, that instead of having index they have
hash bindings.
myset = set(‘Python’)
myset = set([0, 1, 2, 3, 4, 5])

Examples

>>> s1 = set([0, 2, 4, 5, 6])


>>> type(s1)
<class 'set'>
>>> s2 = set('cbtnuggets')
>>> s1
{0, 2, 4, 5, 6}
>>> s2
{'c', 'b', 'e', 'g', 'n', 's', 'u', 't'}
>>> s3 = set([10, 12, 13, 15])
>>> s1.update(s3) # although set members are immutable we are simply taking s1
>>> s1 and redirecting it to a new object in memory
{0, 2, 4, 5, 6, 10, 12, 13, 15} # works like A’Union’B in mathematics
>>> newset = set(s1)
{0, 2, 4, 5, 6, 10, 12, 13, 15}

The id(‘object’) function gives us the memory address of that object.

>>> id(s1)
38275336
>>> id(newset)
38654696

>>> s1
{0, 2, 4, 5, 6, 10, 12, 13, 15}
>>> sx = s1
>>> id(s1)
38275336
>>> id(sx)
38275336 # because they share the same data object reference, a modification to set s1 will affect sx and vice
versa.
>>> sx = sx.pop() # the pop() function arbitrarily removes a content from the set
>>> sx
0
>>> id(sx)
505968704
>>> s1.pop()
2
>>> s1
{4, 5, 6, 10, 12, 13, 15} # although in experience the content removed is generally the first element of the set
but according to Python documentation its actually arbitrary or random.
>>> s1.remove(10)
>>> s1
{4, 5, 6, 12, 13, 15} # to remove a specific element we use the remove() function.
>>> 8 in s1
False
>>> 6 in s1
True
>>> 12 not in s1
False
>>> 14 not in s1
True
>>> s1.clear()
>>> s1
set() # gives us an empty set

Sets are often referred to in python as valueless dictionaries because they have keys assigned to hash mark but no
values bound to the keys. We can isolate uniques with sets as sets require that each member of the set to be
unique because if we change the memory id of an item in the set then that element would compute to a new hash
value and Python will not be able to reliably track the location of each element in that object.

Set operations
We have union of sets where we where we merge two sets into one, intersection is retrieving common
items of two sets and difference is the exact opposite of intersection ie to retrieve all the item which are
not common in the given sets.

Membership Testing
We have already seen in and not in in the example above, we now look at ‘is subset’ and ‘is superset’.

>>> s1 = set()
>>> s1.add(0)
>>> s1.add(1)
>>> s1
{0, 1}
>>> s2 = set([2, 3, 4, 5])
>>> s1.update(s2)
>>> s1
{0, 1, 2, 3, 4, 5}
>>> lang = ['lisp', 'python', 'abc', 'c', 'python', 'ruby', 'perl', 'ruby']
>>> l1 = set(lang)
>>> lang
['lisp', 'python', 'abc', 'c', 'python', 'ruby', 'perl', 'ruby']
>>> l1
{'c', 'abc', 'lisp', 'perl', 'python', 'ruby'}
>>> morelang = ['sql', 'erlang', 'perl', 'haskell']
>>> l2 = set(morelang)
>>> l2
{'haskell', 'perl', 'erlang', 'sql'}
>>> l1 - l2 # difference of l1 from l2
{'lisp', 'python', 'c', 'abc', 'ruby'}
>>> l1.intersection(l2)
{'perl'}
>>> u = l1.union(l2)
>>> u
{'python', 'c', 'abc', 'sql', 'lisp', 'erlang', 'ruby', 'haskell', 'perl'} # notice that there is no repeat of elements

Using for loop in a set :

>>> l1
{'c', 'abc', 'lisp', 'perl', 'python', 'ruby'}
>>> for i in l1:
print(i)

c
abc
lisp
perl
python
ruby

>>> s1 = set([0, 2, 4, 6])


>>> s2 = set([10, 12, 14, 16])
>>> s1.update(s2)
>>> s1
{0, 2, 4, 6, 10, 12, 14, 16}
>>> s1.issubset(s2)
False
>>> s2.issubset(s1)
True
>>> s1.issuperset(s2)
True

Set Comprehension:

>>> s2
{16, 10, 12, 14}
>>> type(s2)
<class 'set'>
>>> setcomp = {i * 2 for i in s2}
>>> setcomp
{32, 24, 20, 28}
>>> type(setcomp)
<class 'set'>
List Comprehension (Listcomps)
Are Nifty shorthand method for creating new list. Since lists are indexed and mutable its easier for us to use
listcomps than set comprehension. If we want to perform some function on one list and bring that over as a new
list. There are two ways

>>> myList = ['MIXEd', 'cAsE', 'ELemENtS']


>>> t = []
>>> for x in myList:
t.append(x.lower()) # to convert the elements of list ‘t’ to lowercase
myList = t

>>> t
['mixed', 'case', 'elements']
>>> type(t)
<class 'list'>

OR

>>> myList = ['MIXEd', 'cAsE', 'ELemENtS']


>>> t = [x.lower() for x in myList]
>>> t
['mixed', 'case', 'elements']

List Comprehensions are related to another function called map(). What ‘map()’ function does is that instead of
iterating a method over a list (or in set comprehension over a set) it allows us to iterate a function over a list.

>>> alist = [1, 2, 4, 6, 8, 10]


>>> alist = [i * 4 for i in alist]
>>> alist
[4, 8, 16, 24, 32, 40]
>>> type(alist)
<class 'list'>
>>> phrase = "Lorem ipsum dolor sit amet consectetur adipiscing elit".split() # Free boiler plate text
>>> type(phrase) generator: lipsum.com
<class 'list'>
>>> phrase
['Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit']
>>> stuff = [[p.upper(), p.lower(), len(p)] for p in phrase] # this will give us a list inside a list
>>> stuff
[['LOREM', 'lorem', 5], ['IPSUM', 'ipsum', 5], ['DOLOR', 'dolor', 5], ['SIT', 'sit', 3], ['AMET', 'amet', 4], ['CONSECTETUR',
'consectetur', 11], ['ADIPISCING', 'adipiscing', 10], ['ELIT', 'elit', 4]]
Comprehension inside a dictionary

>>> dict1 = {'ay': 1, 'bee': 2, 'cee': 3}


>>> dict1.items()
dict_items([('ay', 1), ('cee', 3), ('bee', 2)])
>>> type(dict1)
<class 'dict'>
>>> dict2 = {value:key for key, value in dict1.items()} # we used items() function here as it works on the pair
>>> dict2 instead of just the keys
{1: 'ay', 2: 'bee', 3: 'cee'}

Creating a list out of lines in a text file:

>>> lang = open("C:/lang.txt")


>>> lang
<_io.TextIOWrapper name='C:/lang.txt' encoding='cp1252'> # notice here that we are not able to retrieve
>>> lang = list(open("C:/lang.txt")) the contents of the text file if we do a
>>> type(lang) straight open
<class 'list'>
>>> lang
['python\n', 'c\n', 'haskell\n', 'lisp\n', 'java\n', 'alice\n', 'abc\n', 'perl\n', 'ruby\n', 'basic\n', 'erlang'] # because all of
the content of the txt file were written in a different line we are getting ‘\n’ after each element, to get rid of that
we will do a list comprehension.

>>> lang = [n.rstrip() for n in lang] # the method rstrip() strips the character ‘\n’ from the list
>>> lang
['python', 'c', 'haskell', 'lisp', 'java', 'alice', 'abc', 'perl', 'ruby', 'basic', 'erlang']

A sample program

#!/usr/bin/python (#! is called SHEBANG!!! in Unix based systems)

S = [x**2 for x in range(10)] #global name (within a function a variable's scope is limited only to that function but
when we declare a variable outside of any function its a global variable).
def choice_a(x):
print(S)
return

def choice_b(x):
V = [2**i for i in range(x)]
print(V)
return

def choice_c(y):
M = [x for x in range(y) if x % 2 == 0]
print(M)
def choice_d():
raise SystemExit

print('Which operation do you want to perform?\n') # prints a new line after the string
print('''
a: x to the second power
b: to the xth power
c: mod 2
d: quit program
''') # displays all the choices we have
resp = input('Choice: ') # asks the user to make a choice a, b, c or d
resp2 = int(input('Upper Limit? ')) # asks for the upper limit or range of the choice

if resp == 'a':
choice_a(resp2)
elif resp == 'b':
choice_b(resp2)
elif resp == 'c':
choice_c(resp2)
else:
choice_d() # any other choice except a, b, c quits the program

Output

>>>
Which operation do you want to perform?

a: x to the second power


b: to the xth power
c: mod 2
d: quit program

Choice: a
Upper Limit? 90
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> ================================ RESTART ================================
>>>
Which operation do you want to perform?

a: x to the second power


b: to the xth power
c: mod 2
d: quit program
Choice: b
Upper Limit? 10
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
>>> ================================ RESTART ================================
>>>
Which operation do you want to perform?

a: x to the second power


b: to the xth power
c: mod 2
d: quit program

Choice: c
Upper Limit? 10
[0, 2, 4, 6, 8]
>>>

Demonstration of map():

def mult(val):
return val * 2

my_List = [0, 1, 2, 10, 100, 1000] # a simple list

iter = map(mult,my_List) # map function works such as we first put the name of the (function, the iterable)
collection on which it will work.
print(list(iter)) # in Python 3 the map function return the iterator, so we convert the iterator to a list here, in
Python 2 we just had to do print(iter) to get the list.

Output

>>>
[0, 2, 4, 20, 200, 2000]

Function

A function is a block of code that performs a specific action and returns a specific result.

Advantages :

1. In procedural programming functions enable to reuse code, instead of having just a single large script file
containing line after line of commands and we have to copy paste commands to reuse them, we can declare a
function and then, when needed again, can just call the function back to get the desired result.
2. Easier to maintain, as in the above example working with a single large script file and just copy pasting
commands can make it difficult to modify or correct a command while using function we just have to modify
the source function for the changes to apply in the whole code as long as we are not changing the functions
name.
3. The above example will also increase or enhance the readability of a code as we are modularizing our code
with functions.
4. When we are using functions we are encapsulating our code we are putting our logic into a single module and
calling the module wherever it is needed thus hiding the complexity from the user.

Functions Syntax

In other languages we may have to issue a function call command but in python we can a function straightaway
from our scripting environment

name = my_func(argument1, argument2, argument = ‘Default’)

ie we assign a name (which is optional but suggested) and then call the function(my_func) and then may or may
not give arguments.
Difference between a parameter and argument – Parameters are design time placeholders that we add into our
function’s definition while arguments are the runtime data objects that we bind to the parameters at the time of
running our code. In the above example note that we have three arguments argument 1, argument 2 and
argument = ‘Default’ that is we are supplying a value to the argument on the fly and if we don’t the give a value to
argument 3 at the runtime it will take the default value for that argument. Now although it is optional for us to give
our function call a name it is suggested that we do that because otherwise the function will lose its data
immediately, so by giving it a name we are binding the value of that function for later use.

Builtin Functions

Functions like str, float, dir etc are built into Python ie they have module called __bultin__ for Pyhton 2 and builtins
for Python 3 which when we launch a Python interpreter are automatically imported for use. A traceback or an
exception raised in our code is also stored in the builtin function. The builtin ‘__name__’ objects are typically
called special methods or magic methods and although while calling the function we simply write for example,

mylist.len() the interpereter reads it as mylist.__len__() which can also be termed as “syntactic sugar” which means
a syntax designed to make things easier to read or express while alternative ways of expressing them exists.

>>> import builtins


>>> dir(builtins) # gives us a list of all the names that are defined in that file or module in our case

>>> a = ‘Python’
Here we have assigned ‘a’ to string ‘Python’ now when we invoke a method associated with that name
Such as a.some method, we notice that even though we did not declare the type of data, Python automatically
recognizes it as a string and when we type a. the list that comes up here contains all the method that go with a
string data type, this feature that we see in Python and other OOP languages is called Duck typing.
Functions vs Methods

Functions and methods are both code blocks that are used and created for a specific purpose and return a specific
result. A function is a standalone code block that’s called independently by name whereas a method is a function
that is bound to or associated with a particular object.

mylist = mylist.append(‘New element’)

here we are adding a new element to a list called ‘mylist’ by using a method append(). We always want to make
sure that our functions and classes don’t collide with other for example we might have a program that imports two
modules called m1 and m2 which have a common function called ‘T’, so if we call the T function how will the
interpreter know which function we are calling, to that end we have the namespace which allows us to call one
function as m1.T() and the other as m2.T(). We can’t call a function directly without first importing the associated
module.

>>> randint (1, 10) will give us a traceback error saying randint is not defined although we know that it’s a function
of the random module. So we type

>>> import random # call the random module


>>> random.randint(1, 10) # tells the interpreter to randomly choose an integer in the range 1 to 10
4

Since we did not assign a name to the random.randint() function call we cannot use the integer generated because
it is not assigned to a name.

>>> result = random.randint(1, 10) # binds the generated integer to the name result
>>> result
7

Creating Functions

In Python we use def statement to define a function while in traditional programming languages such as C when
we define a function our source code the function is then permanently compiled into machine code by the
compiler in Python being a interpreted programming language we don’t have that permanent compilation, so def
is more or less executable code and when we perform an import Python compiles all the functions and creates
function objects.

def power(p): # define a function with a parameter p


'''
Calculates 2 to the power p # we can learn more about docstrings by looking up pep 257 on python.org and
''' ‘sphinx’, a tool to create intelligent and beautiful docstrings
r = 2 ** p
return r # optional but useful if we have to use the value elsewhere in our program
print(‘2 to the power of 2 is’, power(2))

Python functions always have a return value, although the names defined in a function without a return value go
away as the function exits, the function still returns a value ‘None’ back to our code.
We have Single Value return like in the above example the return r returns a value 4 or multiple values such as
‘return x, y’ where the interpreter returns the value of x and y as a tuple.

>>> def uc(a):


a = a.upper()
print(a)

>>> uc('python')
PYTHON
>>> result = uc('echo')
ECHO
>>> result # here we can see that although we assigned a name to the function call it still doesn’t returns
>>> the assigned value that happens because we don’t have a return value in our function

>>> def uc(a):


a = a.upper()
print(a)
return a

>>> res2 = uc('shell')


SHELL
>>> res2
'SHELL'

A sample program

# no return value
def typer(x):
'''
Defines datatype for x (Limited use)
'''
if type(x) == int:
print('This is an integer')
elif type(x) == str:
print('This is a string')
else:
print('This is neither an int nor a string')

print(typer(200))

Output

>>>
This is an integer
None
>>>
After restarting the shell we import the m8 module as we saved the module in a directory that the python
interpreter looks into when we do a module import(To check what directory the Python interpreter covers (in the
Python Shell or IDLE) go to File Path Browser).
>>> import m8
This is an integer
None
>>> dir(m8)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'typer']
>>> help(m8.typer)
Help on function typer in module m8:

typer(x)
Defines datatype for x (Limited use)

>>>

If we go ahead and delete the last print statement from the above program and save it we will be able to supply
any value to the program and it will display its datatype.

>>> typer(100) # will return a traceback error


>>> m8.typer(100)
This is an integer

Another Sample Program

def namer(fn, ln = 'Jais'): # In this example the value of one parameter is fixed
# return value, default value
r = print('Your name is', fn, ln)
return r

Save the program and inside Python shell we import the module
Output

>>> import m8
>>> dir(m8)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'namer']
>>> fullname = m8.namer('Abhi')
Your name is Abhi Jais
>>> fullname # because we have not specified a return value in the function definition
>>> m8.namer('Tim', 'Warner') the fullname returns nothing
Your name is Tim Warner
>>>
More Sample

def fun(name, location, year = 2010):


'''
This source code is from www.java2s.com, the site contains lots of sample code for Python as well as other
languages
'''
nly = "%s/%s/%d" % (name, location, year)
print(nly)
return(nly)

In the shell

>>> import m8
>>> rz = m8.fun(location = 'Noida', year = 2016, name = 'Abhi')
Abhi/Noida/2016
>>> rz
'Abhi/Noida/2016'

To write a function which has variable number of arguments, such as, code for a search engine or even a free text
application we have ‘*args’ and ‘**kwargs’ (only the asterisk is important we can have arguments like *foo, **bar),
generally we use *args for single value arbitrary assignments and **kwargs for keyword assignments

def foo(a, b, *args, **kwargs):


print("Here is 'a'", a)
print("Here is 'b'", b)
print("Here is '*args'", *args)

print(foo('spam', 'eggs', 'arb1', 'arb2', 'arb3'))

Output

>>>
Here is 'a' spam
Here is 'b' eggs
Here is '*args' arb1 arb2 arb3
None
Variable Scope - Local and Global

def demo(f_in):
global somevar # forcing a variable to be global even though declared inside a function body
demo.tom = 16 # an attribute accessible from main code
somevar += 1
another = 12 # a local variable independent of main code
res = f_in+14 # a value passed in (f_in)
return res

somevar = 27 #accessed in function via global


another = 17 # not accessed in function
pval = 16 #accessed in function via parameter

print demo(pval)

print demo.tom #function attribute


print somevar
print another

#!usr/bin/python
# adapted from tutorialspoint.com

# function definition is here


def changeme(mylist):
"This changes a passed list into this function"
mylist.append([1, 2, 3, 4]);
print('Values inside the function: ', mylist)
return

# Now you can call changeme function


mylist = [10,20,30];
changeme(mylist);
print('Values outside the function: ', mylist)

Output

>>>
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
>>>

Therefore we can say that Python uses call by sharing or call by object sharing where inside the function the object
(or the reference to the underlying object) is shared with the object as it exists outside the function.
SEE Evaluation Strategy in Wikipedia for more details.
File Handling

Python has three built-in file system handling modules namely

1. OS Module: We have several function in OS module i.e. to get the current working directory we have the
getcwd() which tells us where a currently logged on user is in their shell environment, chdir() to change from
one directory to another directory, listdir() to list the contents of a directory, we can also build directory path
using the path.join() this is particularly helpful if our Python program will be ported on more than one OS.
The OS module has a name function that can echo back OS specific identifier this is useful for porting code to
other OS, we can create conditional branching paths based on the value of OS.name on the system that the
program is running on.

2. The sys module: the most important function in the sys module is sys.platform function which gives us a much
more detailed id of the host OS.
3. Shutil module or the batch module to perform batch operations such as batch or bulk copy, batch or bulk
renames, batch or bulk deletions etc.

>>> import os
>>> dir(os) # lists all functions in the os module
>>> p = os.path.join('C:/', 'files')
>>> type (p)
<class 'str'>
>>> p
'C:/files'
>>> os.listdir(p)
['IMG-20130916-00155.jpg', 'itit.pdf', 'moretxt.txt', 'Oracle List O Holidays.docx', 'somtxt.txt', 'sub', 'Thumbs.db']
>>> os.name
'nt'
>>> # posix for linux and mac os
>>> import sys
>>> sys.platform
'win32'
>>> # darwin for mac os and linux 2 for ubuntu and other flavours of linux
>>> print(list(os.environ.items())) # prints all the environment variables in our OS as key:item pair (dictionary)
Inside a list
>>> for param in os.environ.keys():
print(param, os.environ[param]) # to list all parameter and their values in a much more readable way
>>> os.getcwd()
'C:\\Python31'
>>> os.listdir(os.curdir)
['DLLs', 'Doc', 'if1.py', 'if2.py', 'if3.py', 'if4.py', 'if5.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'm8.py', 'm8.pyc',
'Microsoft.VC90.CRT.manifest', 'msvcr90.dll', 'NEWS.txt', 'python.exe', 'python31.dll', 'pythonw.exe', 'README.txt',
'spam.txt', 'tcl', 'Tools', 'wellho.py']
>>> os.chdir('C:/files')
>>> os.getcwd()
'C:\\files'
>>> os.listdir(os.curdir)
['IMG-20130916-00155.jpg', 'itit.pdf', 'moretxt.txt', 'Oracle List O Holidays.docx', 'somtxt.txt', 'sub', 'Thumbs.db']
>>> os.rename('somtxt.txt', 'sptxt.txt')
>>> os.listdir(os.curdir)
['IMG-20130916-00155.jpg', 'itit.pdf', 'moretxt.txt', 'Oracle List O Holidays.docx', 'sptxt.txt', 'sub', 'Thumbs.db']
>>> os.mkdir('moredir')
>>> os.mkdir('deleteme')
>>> os.listdir(os.curdir)
['deleteme', 'IMG-20130916-00155.jpg', 'itit.pdf', 'moredir', 'moretxt.txt', 'Oracle List O Holidays.docx', 'sptxt.txt',
'sub', 'Thumbs.db']
>>> os.rmdir('deleteme')
>>> os.listdir(os.curdir)
['IMG-20130916-00155.jpg', 'itit.pdf', 'moredir', 'moretxt.txt', 'Oracle List O Holidays.docx', 'sptxt.txt', 'sub',
'Thumbs.db']
>>> os.remove('sptxtcop.txt') # since the current directory is C:\files we do not have to supply the full path,
>>> os.listdir(os.curdir) to operate on a file in any other directory we will have to supply the full path
['IMG-20130916-00155.jpg', 'itit.pdf', 'moredir', 'moretxt.txt', 'Oracle List O Holidays.docx', 'sptxt.txt', 'sub',
'Thumbs.db']
>>>

Opening Files

mfile = open(‘C:/docs/myfile.txt’, ‘r’)

‘r’ is for read only, we can use ‘w’ to create and make changes to a file (it is recommended that we use ‘w’ for
creating a new file only cause it will delete all preexisting contents of the file it works on), to add contents to afile
we use ‘a’, and we use ‘b’ to view binary data and ‘U’ for a new line.
Once we have created a file using open function we can make changes to it using write() and writelines() functions
with write() we can do triple quoted strings and add new lines etc. to the file with writelines() we can actually
insert lists into a file. To read from a file we use read(), readline() and readlines() functions, readline() is used when
we have to read a small portion of a file or particularly one line of the text file. To read entire file at once we can
use the read function with n arguments to read n number of byte of file or without arguments to get the file
packed into a single string and readlines() reads the file into a list of strings.
To release the file handle to the operating system or have other processes except Python access the file we have
close() function.

>>> os.getcwd()
'C:\\files'
>>> os.listdir(os.curdir)
['IMG-20130916-00155.jpg', 'itit.pdf', 'moredir', 'moretxt.txt', 'Oracle List O Holidays.docx', 'sptxt.txt', 'sub',
'Thumbs.db']
>>> f = open('testing.txt', 'w')
>>> f.write('The first line of text.\n')
24
>>> f.write('The second line of text.\n')
25
>>> f.close()
>>> f = open('testing.txt')
>>> f.readline()
'The first line of text.\n'
>>> f.readline()
'The second line of text.\n'
>>> f.readline()
''
>>> f.seek(0) # To place the cursor at the start of the file
0
>>> f.read()
'The first line of text.\nThe second line of text.\n'
>>> print(f.read()) # because the cursor is currently at the end of the file the print function prints
nothing.
>>> f.seek(0)
0
>>> print(f.read())
The first line of text.
The second line of text.

>>> f.close()
>>> f = open('testing.txt', 'a')
>>> f.write('''Now is the time for all good men...

... to come to the aid of their country.''')


77
>>> f.close()
>>> f = open('testing.txt')
>>> print(f.read())
The first line of text.
The second line of text.
Now is the time for all good men...

... to come to the aid of their country.


>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x0000000002DE1818>
>>> f.close()
>>> namelist = ['Abhi', 'Bob', 'Jais']
>>> f = open('testing.txt', 'w')
>>> f.write(namelist)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
f.write(namelist)
TypeError: must be str, not list
>>> f.writelines(namelist)
>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x0000000002DE1750>
>>> f.close()
>>> f = open('testing.txt')
>>> print(f.read())
AbhiBobJais
>>> type(f.read())
<class 'str'>
>>> for line in open('lines2.txt').readlines():
print(line, end = '')

perl
python
ruby
erlang
Alice
ABC
C
BASIC
JAVA
>>>

The function os.walk() is the most efficient way t traverse directory structures, which allows us to walk through the
contents of one or more directories recursively to get back whatever content we want such as pattern matching
searches, using the glob module alongwith ‘os’ gives us flexibility when traversing around in a directory also the
glob() function in the glob module allows us to do wildcard processing for example we can use *.txt to look for all
text files. Another module called the pickle module allows portable storage of structural data ie it makes complex
data structures portable.

a = [‘globby’, 2323, ‘text’, 23.43233]


import pickle
f = open(‘C:/files/pick.txt’, ‘wb’) # creating the file and editing it in binary mode
pickle.dump(a, f) # dump is used to taking input ‘a’ and pack it into output ‘f’
f.close()
f2 = open(‘C:/files/pick.txt’, ‘r’)
a = pickle.load(f2)
f2.close()
a # echo back the contents of the list ‘a’.

Thus we can take a data and pack it down into what is called a persistent state or ‘serialize’ a data. So it can be
ported from one place to another in form of binary digits. The downside to pickle is there is no security check so
we should be careful to use pickle objects from trusted sources as it can catastrophic changes to our filesystem.
>>> os.getcwd()
'C:\\Python31'
>>> os.chdir('C:/files')
>>> os.getcwd()
'C:\\files'
>>> for root, dirs, files in os.walk(os.curdir):
print('{0} has {1} files'.format(root, len(files)))

. has 8 files # . means the root directory which is C:\files


.\moredir has 0 files
.\sub has 3 files
>>> import glob
>>> glob.glob('C:/files/sub')
['C:/files/sub']
>>> glob.glob('C:/files/sub/*.txt')
['C:/files/sub\\in text.txt', 'C:/files/sub\\junk.txt', 'C:/files/sub\\random.txt']
>>> for name in glob.glob('C:/files/sub/*'):
print(name)

C:/files/sub\in text.txt
C:/files/sub\junk.txt
C:/files/sub\random.txt
>>>

Opening other File Types

Open function works well with files containing ascii and Unicode characters but it cant open image, word docs and
pdf’s. For images we have Python Imaging Library(PIL) which contain image modules (as of the time of this tutorial
PIL was only available for Python 2). For MS-Office we have python extension for widows or pywin32 and Python
office to write in Excel worksheets, for Adobe PDF we have ReportLab and SWFTOOLS.

IDLE 2.6.4 # here we are working with python 2.6 because pil for python 3.1 is not available
>>> import os
>>> os.getcwd()
'C:\\Python26'
>>> os.chdir('C:/files')
>>> import Image
>>> dir(Image)
>>> im = Image.open('C:/files/hou.jpg')
>>> type(im)
<type 'instance'>
>>> im.show()
>>> import gfx # will only work if we have swftools installed on our system
>>> doc = gfx.open('pdf', 'itit.pdf')
>>> type(doc)
<type 'gfx.Doc'>
>>> doc. # here if we press tab we can see there are not options available to us the just some basic
attributes such as getinfo, getpage and setparameter.

Accessing the Network and Internet

Accessing LAN Resources

OS module not only gives us access to the local filesystem but also it helps us to manipulate remote file systems as
well. Socket in unix, linux and mac os X environments the TCP/IP socket is an extremely low level abstraction and
gives us deep control over TCP/IP protocol stack in a programming environment. email module lets us manipulate
email messages using those associated protocols like simple mail transfer protocol(SMTP), postoffice
protocol()POP and even the Internet Message Access Protocol(IMAP) using separate libraries such as smtplib, POP
has its own separate module, ftplib is a FTP client library, urllib has two versions urllib and urllib2 (better at error
handling), httplib is similar to urllib (higher level of abstraction compared to httplib and ftplib).

The Python extension for Windows the pywin32 has a lot of usefulness in networking but it relies a lot on the
Component Object Model(COM) interface which is considered to be old as compared to the .NET framework, so if
we plan to do programming with Microsoft’s .NET Framework using Python we should build our applications using
the Iron Python environment, same applies for JAVA if we want our code to interact with the JAVA runtime
environment we should use Jython.

Sites to visit:

Pypi.python.org
Stackoverflow.com
Code.activestate.com

import os
d = os.listdir('\\\\192.168.1.106\\share') # connected to the given remote server, ‘d’ captures the list of file on
type(d) the server inside ‘share’ folder
<type 'list'>
d
['serverfile.txt']
f = open('\\\\192.168.1.106\\share\\testdoc.txt', 'w') #creating a file on the server, if it exists it will be
f.write('Hello there from accross the network.') overwritten
f.close()

A Sample Program

import os
import shutil
path = os.path.join('C:/', 'files')
destination = os.path.join('\\\\192.168.1.106', 'share')
for r, d, f in os.walk(path): # traverses the given path
for files in f:
if files.endwith('.txt'): # looks for files with .txt extension
try:
shutil.copy(os.path.join(r, files), destination)
except IOError, e:
print e

The above program copies all the files ending with .txt from the files folder on our local disk to the share folder of
the remote server.

Accessing Web Pages in Python

A webpage is formally defined as a file that is arbitrarily given html or htm extension and is parsed by a web
browser that displays hypertext.

URI vs URL

Uniform Resource Identifier(URI) is a general identifier while Uniform Resource Locator(URL) specifically locates a
network resource. A Uniform Resource Name(URN) is the item’s identity while the URL provides a method for
finding it. In a URL we not only get the specific location of a resource but also get how to access the resource, in
the URL we have what is called a scheme like http, https, ftp and file for local resources.

Urllib.urlopen() function can be used to handle any of these above mentioned schemes, the resource doesn’t have
to be specifically on a public web server or an intranet web server it can be a local resource. The main difference
between the urlopen() and open() function is that the urlopen() has a read only access to the data.

httplib is a very low level client interface to http it enable us to do things the urllib either cannot do or it is easier to
do with httplib, we can use the post method in httplib to post things on a website apart from accessing it. To
download things we have the GET method.

Twisted is an event driven networking framework (visit twistedmatrix.com) that is basically used if we intend to do
some heavy web work in Python.

>>> import urllib


>>> import sys
>>> wp = urllib.urlopen('http://cbtnuggets.com')
>>> for line in wp:
sys.stdout.write(line)
Gives us the actual content (scripts and code) of the cbtnuggets home page.
>>> print wp.info()
Cache-Control: private
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=edge,chrome=1
Content-Length: 80308
Date: Sat, 27 Feb 2016 11:10:52 GMT
Connection: close
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Server: NuggetServe/8.88
Vary: User-Agent

get(‘url’) can be useful to ascertain when a webpage or a website is redirected


>>> redirect = urllib.urlopen('http://microsoft.org')
>>> redirect.geturl()
'http://www.microsoft.com' # gives us the url target.
>>> urllib.urlretrieve('http://python.org', 'C:/files/py.html') # Captures the content of a website as it
('C:/files/py.html', <httplib.HTTPMessage instance at 0x02C9D3F0>) appears on the web browser without the
images and other interactive elements.

A Sample Program

import httplib
files = ['/', '/doc.html'] # list of pages and sub-directory we want to capture
h = httplib.HTTPConnection('www.python.org', 80) # connect to the (url, port number)
h.connect() # opening a connection

for f in files:
h.request('GET', f)
r = h.getresponse()
if r.status == httplib.OK:
data = r.read() # grab the contents of the files and put them on the screen.
print ':::: %s ::::' % f
print data

h.close()

On running the program we get the raw html text from both of those pages.

import win32wnet # download and install pywin32


from win32netcon import RESOURCETYPE_DISK as DISK

drive letter = "R:"


path = "\\\\192.168.1.106\\share"
win32wnet.WNetAddConnection2(DISK, drive_letter, path)

The above program maps the drive R to the supplied network path.

File Transfer Protocol (FTP) Accessing

FTP is used for transmission (or uploading and downloading) of files as an alternative of email.
Trivia File Transfer Protocol(TFTP) is optimized for the transmission of smaller files like router firmware files.
We can get to FTP client connectivity using ftplib we can also use the external chilkat module
(Check out: chilkatsoft.com/Python.asp) for Python 2 which supports FTPS (secure FTP or SSH(Secure Shell)FTP) or
urllib which provides a higher level interface to FTP client functionality.

Importing a specific name from a module

from ‘module name’ import function by doing this we are cutting down our memory footprint(the amount of main
memory that a program uses or references during runtime) and we can call the function by its name instead of
modulename.function() for future use in the code block.

>>> from ftplib import FTP


>>> ftp = FTP('ftp.redhat.com') # the tooltip asks us to supply a host argument and then optinally a user,
>>> password, account and timeout argument.
>>> type(ftp)
<type 'instance'>
>>> # the FTP is essentially a class and what we did is that instantiate an instance of that class FTP
>>> ftp.login() # here we supply our credentials ('user', 'password') or do an anonymous login in our case
'230 Login successful.'
>>> d = 'redhat/linux' # we are assigning 'd' to the redhat/linux sub directory
>>> ftp.cwd(d) #to set our current directory either to that path or just invoke the name to modularize it
'250 Directory successfully changed.'
>>> print ftp.dir() # directory listing
-rw-r--r-- 1 ftp ftp 131 Dec 20 2005 README
drwxr-xr-x 8 ftp ftp 4096 Dec 06 2012 beta
drwxr-xr-x 6 ftp ftp 4096 Jun 14 2007 eal
drwxrwsr-x 18 ftp ftp 4096 Jun 04 2014 enterprise
drwxr-xr-x 2 ftp ftp 4096 Feb 12 2006 preview
drwxr-xr-x 2 ftp ftp 4096 Dec 03 2008 rawhide
drwxrwsr-x 4 ftp ftp 4096 Sep 26 2008 updates
None
A sample program

import ftplib
import sys
import os

def ftext(ftp, filename, outfile = None):


# fetch a text file
if outfile is None:
#outfile = std.output
outfile = open('C:/Python26/outfile.txt', 'w')
ftp.retrlines("RETR " + filename, lambda s, w = outfile.write: w(s+"\n"))
'''
retrlines is a method to grab lines from a text file,
lambda expression is (used to create an embedded anonymous
function which does'nt needs a declaration) used here
to add new lines to the lines read from the file
visit: secnetix.de/olli/Python/lambda_functions.hawk,
RETR is an FTP command to retrieve a file.
'''

def fbin(ftp, filename, outfile=None):


# fetch a binary file
if outfile is none:
outfile=sys.stdout
ftp.retrbinary("RETR " + filename, outfile.write)

ftp = ftplib.FTP("ftp.redhat.com")
ftp.login()
d = 'redhat/dst2007/README'
ftp.cwd(d)
ftext(ftp, "README_FIRST.txt")
ftp.close()

print 'Currrent working directory is: ', os.getcwd(), '\n'

Output

>>>
Currrent working directory is: C:\Python26

>>>

The above program copies the content of the text file on the redhat ftp server and pastes it into the txt file in the
given directory on the local machine.
Exception Handling

There exceptions or exceptional events occur in all programming languages not just Python and are mainly caused
due to an Input/Output error, where for example a file that the interpreter was supposed to fetch from a directory
does’nt exist, or a syntax error in the program itself or a type error where the user was supposed to provide the
input value in a particular data type for example the user was supposed to give a integer and he gave a string. Then
there are cases of an edge error where a rogue user may cause an error in our program by going, what we can
term as, left instead of right in a program.

We often get a traceback object in Python while compiling a code such as in case of dividing a number by 0 will
always give a traceback, whats raised by a traceback is called a call stack. A traceback tells us which line of our code
has caused a traceback and the specific exception object that python has raised. When python has an exceptional
event it is called throwing an exception or raising an exception, we as a programmer can raise an exception so that
the exceptions occurs on our terms while the Python interpreter throws an exception which the programmer was
not expecting.

When an exception occurs it is up to us as programmers to handle, catch or trap those exceptions to that end
python standard library gives us a lot of built in exceptions, Python 2 has all those built in exceptions in a module
called exceptions while Python 3 has those exceptions in the builtins module.

In Python 3.1.2
>>> import exceptions
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import exceptions
ImportError: No module named exceptions
>>> import builtins
>>> dir(builtins)

The list of names in the builtins module have a lot of exceptions classes such as ArithmeticError, AttributerError
etc. While we are focusing on exceptions as errors there is a subset of those classes that deals with warning
messages called RuntimeWarning, difference between a warning and an error in Python the program execution
continues even after a warning but an error will stop our application.

One of many design consideration for us as a Python Software Developer is that where we are going to place our
exception handling code, there is term related to this context known as ‘bubbling up’ what it means is that we may
have inside our larger application a function that handles exceptions and if there is an exception, that is not
handled by modular code unit, the unhandled exception may lie dormant and eventually bubble up to a larger
even the global scope of that application, which will eventually fail our application.
Call stack is a data structure that stores information about the active subroutines of a computer program.

As our Python program runs and we are instantiating functions, classes and running methods we are adding one by
one what is called stack frames to that execution stack or call stack that is useful because if there is a handled or an
unhandled exception in our code that points us to the call stack that raised the exception. We can view stack
contents in Python in several ways, we can invoke the print_stack() function from the standard libary module
traceback, there are third party modules, to debug traces etc, on our code, built-in module inspect.stack() gives us
the current contents of the call stack.

Raising and Trapping Exceptions

Raise statement : We can use the raise statement interactively or in our code to manually generate an exception.
In our program code we are running tests etc wecan throw in strategically placed ‘try:’ and ‘except:’ blocks to
manually raise a custom exception class and then run the code that we want to run to recover or log etc from that
exception.

Try-Except Blocks : The way try-except works is that we wrap the code that contains volatile procedure inside the
block and if the try: block code executes without generating an exception, the interpreter ignores the try-except
block and continues with the program, if there is an exception it goes to except: block( we can generate an
exception for some builtin exception, we can handle multiple built in exception, we can trap our custom exception
as well or we can just give except_ with no parameter and any exception generated by the try block would be
handled by the except block) where we put our handler code which could be providing feedback to your user or
providing log or diagnostic information etc. Optionally, we have a ‘finally:’ clause which does a cleanup when we
run a try-except block for example we may have in our try block a call to the open() function where we are opening
a file and appending to it and we have except block so that if something goes wrong say an I/O error it gives us a
diagnostic message to let us know and then in the finally block we issue a close function so that whatever happens
either the try-except ends with or without an exception the file is always closed.

>>> raise exception # all our class names have to be in upper case so this will always raise an exception
>>> raise Exception # raising an instance of the exception object
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
raise Exception
Exception
>>> raise Exception('The flux capacitor is broken.') # adding your own text to the exception handle
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
raise Exception('The flux capacitor is broken.')
Exception: The flux capacitor is broken.
>>> import builtins
>>> dir(builtins) # we can raise any of the standard error in the builtins module with their class name
>>> raise IOError
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
raise IOError
IOError

a = int(input('First number please: '))


b = int(input('Second number: '))
print('Your result is : ', a/b)

>>>
First number please: 10
Second number: 2
Your result is : 5.0
>>>
First number please: 10.1
ValueError: invalid literal for int() with base 10: '10.1' T
>>> |
First number please: 10 |----# here we are seeing instances of different classes
Second number: 0 |
ZeroDivisionError: int division or modulo by zero _|

y:
a = int(input('First number please: '))
b = int(input('Second number: '))
print('Your result is : ', a/b)
except ZeroDivisionError:
print('Error: Division by zero will not work.')

>>>
First number please: 10
Second number: 0
Error: Division by zero will not work.
>>>
Here we can see that even though there is an error the program still terminates
# multiple exception handler
try:
a = int(input('First number please: '))
b = int(input('Second number: '))
print('Your result is : ', a/b)
except ZeroDivisionError:
print('Error: Division by zero will not work.')
except ValueError or IOError:
print('Error: You used an incorrect data type.')

Output
================================
>>>

First number please: 10


Second number: 0
Error: Division by zero will not work.
>>> ================================ RESTART ================================
>>>
First number please: 10
Second number: spam
Error: You used an incorrect data type.
>>>

# catching multiple exceptions at once


try:
a = float(input('First number please: '))
b = float(input('Second number: '))
print('Your result is : ', a/b)
except (ZeroDivisionError,ValueError, IOError): # python treats this as a tuple
print('Error: Invalid Input provided.')

# print more dynamic exception message text


try:
a = float(input('First number please: '))
b = float(input('Second number: '))
print('Your result is : ', a/b)
except (ZeroDivisionError,ValueError, IOError) as e:
print('Error: ', e)

>>>

First number please: 10


Second number: 0
Error: float division
>>> ================================ RESTART ================================
>>>
First number please: 10
Second number: spam
Error: could not convert string to float: spam
>>>
The above program displays the specific error that Python returns.

# Keep the program running after an exception


while True: # open the loop and keep it open until the condition becomes false
try:
a = float(input('First number please: '))
b = float(input('Second number: '))
print('Your result is : ', a/b)
except:
print('Invalid Input. Please retry.')
else:
break # to make the while condition false

>>>
First number please: 10
Second number: 0
Invalid Input. Please retry.
First number please: 10
Second number: spam
Invalid Input. Please retry.
First number please: 10
Second number: 5
Your result is : 2.0
>>>

# Using finally to clean up after an exception

def divlog(x, y):


try:
f = open('C:/files/divlog.txt', 'a')
f.write('{0:g} / {1:g} = {2:g}\n'.format(x, y, (x/y))) # 0, 1, 2 are positonal number for x ,y and x/y, g represents
except ZeroDivisionError: the floating point presentation type, which prints the
f.write('Error: Zero division not allowed\n') number at the fixed point.
raise
finally:
f.close()

divlog(100, 20)
divlog(20, 3)
divlog(35, 0)
If we save the file and go to the python shell we can access the devlog module
>>> import exception2 # name we gave to our module
>>> dir(exception2)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'divlog']

Output

>>>
Traceback (most recent call last):
File "C:/Python31/exception2.py", line 15, in <module>
divlog(35, 0)
File "C:/Python31/exception2.py", line 6, in divlog
f.write('{0:g} / {1:g} = {2:g}\n'.format(x, y, (x/y))) # O, 1, 2 are positonal number for x ,y and x/y, g represents the
floating point presentation type, which prints the number at the fixed point.
ZeroDivisionError: int division or modulo by zero
>>>
Although we get a traceback, if we navigate to the directory ‘C:/files’ we will find the divlog.txt file whose contents
are

100 / 20 = 5
20 / 3 = 6.66667
Error: Zero division not allowed

Thus we know that our program terminated successfully and it successfully closed the file divlog.txt, as we are able
to access it (if we don’t close a file or the if the file is being accessed by python than we will not be able to access it
from outside the Python Interpreter).

Creating Custom Exceptions

Apart from the exceptions that are supplied by the standard library’s exception classes we can also create our own
custom exceptions. The way to create an exception is by sub-classing any of the standard library exception classes.

A class is like a blueprint for example we can look at the built in templates in Microsoft word as classes. The
templates are pre-formatted they have their own custom page margins, their own fonts, heading styles etc
similarly classes in Python have their own properties which define the characteristics of that class as well as their
methods which are generally functions attached to a particular object. When we are creating a new document in
MS Word based on those templates we are not opening the template or class itself, we are instantiating or creating
an instance of that template the original template remains the same. Similarly, in OOP in Python we can create a
custom template based on Python’s internal libraries of templates, the benefit here is that by sub-classing from say
an Exception class we are taking advantage of all the builtin properties, variables and methods attached to that
exception class.

Incapsulation and Inheritance

By creating a custom class that inherits from an existing class we get access all its built-in properties, that also
means we can make changes to the base class and if we want to we can have our custom subclass inherit those
changes. Encapsulation means our class has properties and methods that kind of supervise that class and are
always available using the dot (.) notation. We can subclass any of the Python built-in classes, If we want to create
a custom IOError handler we might want to create a custom exception that inherits from IOError.

class CustomError(Exception): # ‘CustomError’ signifies the name of our custom class and then in parenthesis
pass thename of the class from which we are inheriting.

The ‘pass’ statement is often included in sample code or as we are testing or developing our application we might
know we need a class but are not ready at that time to develop specific code for the class we simply include the
pass statement and it keeps that class function method alive it will run just fine but it wont do anything, pass is
kind of a null operator class function or method. Once we have built our custom class we have to call it in our code.
Import the library that contains our custom exception classes but in our code we can manually raise the class by
using the raise keyword for example

raise CustomError(‘Something went wrong.’)

we can invoke an exception with raise, in order to check for a custom exception we would use try-except, finally
code block as usual.

>>> class CustomError(Exception):


pass

>>> raise CustomError('Something went kerflunky.')


Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
raise CustomError('Something went kerflunky.')
CustomError: Something went kerflunky.
>>>

A sample Program:

class MyError(Exception):
def __init__(self, value): # here self is internal to this class definition and we supply the argument(value).

'''
when we are doing OOP and creating a class we will want to include
as our very first element in that class declaration a function
called __init__, the above line works as a 'constructor' which is a
code that is an engine for creating an instance of that given class.
'''
self.value = value
def __str__(self):
'''
almost always when we build a class(exception or otherwise) we also
invoke the __str__ function which provides us the string representation
of the given object.
'''
return repr(self.value)

try:
raise MyError(2*2)
except MyError as e:
print('My Error occured, value:', e.value)

Output

>>>
My Error occured, value: 4

Modular Programming

A Python module is simply a plain text file that simply has that has a .py extension and contains one or more lines
of Python source-code. Python modules are also defied as self-contained namespace of names like classes,
functions and variables (generally global), variables also show up in modules as constants (names whose value are
immutable ie they cannot be changed) like the value of pi included in the standard library.

Import math
Math.pi
3.141592653589793

Benefits of Modular Programming:

1. Code Reuse- We don’t have to worry about opening a file or opening a url by writing a code form scratch we
can simply import the appropriate module or modules and take advantage of its classes, functions and varibles.
2. Shared Components: We can share modules put them in a central location for example on a network
sharepoint and the entire development team can access it components. ‘sys.path’ variable acts as a Python
search path for module files, includes a sub directory called site-packages, which provides by default a shared
location for installed third party modules.
3. To avoid name collision While we import two or more modules in our program it may happen that those
modules contain methods by the same name the namespace comes to rescue for example for module name
and b, one method will be called ‘a.fx()’ and the other as ‘b.fx()’.

Note: Since module code is run on 1st import, sometime what happens is that we have our top level script file we
want to bring in some, lets say, nifty math functionality, so we run an import on this math library we created and
we begin to see unexpected results like output of functions etc (We can work around here with the __main__
trick).

4. The modules when compiled get converted to bytecode as .pyc files so when we import a module its bytecode
version is also copied to the current working directory so all the functions, classes and modules get picked up
by our processor a lot faster (as bytecode) than in case of a imported being reprocessed from scratch every
time we run our top level file.
Ways to import module:

1. import os (no need to provide extension .py), this kind of import makes the whole namespace or functions
inside the modules available to us. We have to append the module name to work with the names inside the
module (os.chdir()). This method is considered safe as the chances of name collision are none.
2. ‘from os import chdir, getcwd(one or more names separated by a comma)’ this method is considered memory
efficient. We don’t need to append the module name to work with the functions or classes ‘chdir(‘C:/files’)’.
When we use the import from syntax the namespace is stripped away from those names as they come into our
top level script file, we wind up potentially overwriting objects in our current namespace, so the first method is
preffered over this.

The below schematic gives us how modules function in Python:

T.py (our script file)


/ | \
mod1.py <-> mod2.py (modules which have access to each other)
|
Standard library

>>> import sys


>>> sys.platform
'win32'
>>> from os import chdir, getcwd
>>> chdir('C:/files')
>>> getcwd()
'C:\\files'

Reloading a module (on which we have been working on) in Python:


>>> import imp
>>> help(imp)

DESCRIPTION
This module provides the components needed to build your own __import__ function.

>>> imp.reload(sys)
<module 'sys' (built-in)>
>>>

We can do the above with any script file we made ourself.


The __name__ trick()

Save the below script file as modtest2.py in a Python Path:

#!/usr/bin/python

if __name__ == "__main__":
print('This program is being run by itself')
else:
print('I am being imported from another module')

From IDLE:
>>> import modtest2
I am being imported from another module
>>>
From Command Prompt

In command prompt
C:\Users\Sysadmin>cd C:\Python31\
C:\Python31>python modtest2.py
This program is being run by itself

C:\Python31>

This is a useful hack in terms of making our code more portable, in other words being able to use it for instance
primarily as a library file that will be imported into a top level program but also we can have diagnostic code such
that when we run the file it can tell us ho the module works or just do unit testing.

Example of Reloading modules:

>>> import modtest2


I am being imported from another module
>>> import imp
>>> imp.reload(modtest2)
I am being imported from another module
<module 'modtest2' from 'C:\Python31\modtest2.py'>
>>>
#!/usr/bin/python

'''
Name : modtest.py
Author: A Jais (*****Tim Warner*****)ignore this
Purpose : Test out module imports(test out the new word i invented i.e. 'Plagiarism')
'''

def square(x):
# returns square of given integer
return x * x

if __name__ == "__main__":
print('Testing: Square of 2 == ', square(2))

Output on cmd:

C:\Users\Sysadmin>cd c:\python31

c:\Python31>python modtest.py
Testing: Square of 2 == 4

c:\Python31>

Output on IDLE:

>>> import modtest


>>> modtest.square(10)
100

Module Search Path

The module search path in Python makes imports transparent. To make a module search path:

In OS environment:

Export command in linux/unix or path statement from a command line, set statement with path keyword in
Windows environment, in GUI go to: My Computer – Properties – Advanced System Settings – Environment
Variables(Advanced Tab) – Create a new System variable named PYTHONPATH and we can pass in as many
directory location as we need to access our standard library as well as third party modules. When we start up
Python the interpreter populates an internal system variable named sys.path from the sys module which contains a
list of Python path and as it is a list we can run list methods on it like append, remove etc to modify the search
path.
Sys.path.append() enables us to import modules from directories that are not listed in Python Path, but this
method works only for that particular session, to make the change permanent we will have to add the directory to
the PYTHONPATH environment variable.

We can also make list of directory paths in a text file and change its extension to .pth and the directories will be
loaded to sys.path when we start Python.

To view Python Path:


>>> import sys
>>> sys.path
['C:\\Python31\\Lib\\idlelib', 'C:\\Python31\\python31.zip', 'C:\\Python31\\DLLs', 'C:\\Python31\\lib',
'C:\\Python31\\lib\\plat-win', 'C:\\Python31', 'C:\\Python31\\lib\\site-packages']

OR

File – Path Browser to view the read only list of Python Paths to see all the individual py files an the packages and
double those files to view them in IDLE.

Under C:\Python31\lib we have a bunch of packages that come to us from the standard library we can also expand
individual modules to see what function declaration etc are inside them.

>>> import sys


>>> sys.path.append('C:\projects') # to add a path temporarily to the path browser
>>> from leap import leapyr
>>> leapyr(2004)
The year, 2004 is a leap year.

Module Package

A module package is a collection of folders (one or more) and modules (one or more) which helps us to organize
and distribute our Python application.

To know more about third party modules and packages visit : pypi.python.org/pypi

Rules to create our own package:

We have the top level directory with the name of our package, we have a file __init__.py this is our what we can
‘bootstrapper’ file from which all of the other related modules in our project ultimately resolve, we can have
parameter for the running of our applications we can link sub-directory (which have their own initialization on
__init__.py file).
Distributing a Python Package:

The simplest way to install a package on a system is to use the builtin distutils functions and what it does is give us
a setup.py file that a user can run, it can be seen as the python version of install shield.

The other way to go is to use py2exe to create windows binary installer or py2app which creates a binary
installation package on Mac OS X systems.

Object Oriented Programming (OOP)

In Procedural programming we are structuring the flow of our application from statrt to finish and then trying to
capture any edge cases or sub procedures that the user might decide to take in our user interface. In order to
modularize or separate out components of a program in procedural paradigm is by using functions, but what if we
created a function that does some work for us but somewhere else in the program we want to use the same basic
function but add some twist in its functionality, we have copies of same functions again and again with minor
tweaks but there is no connection among them in a way that if we make changes to the first function there will be
no effect on the other copies of the same functions in the program (inheritance of properties by other instances of
that code(function)).

In OOP we can create objects that are free standing and/or relate to each other and each object has, encapsulated
within it, its own functions (methods in class) and attributes, what’s nice about these class objects is that we can
spawn as many instances of these objects we may want to in our code, which is essentially a ‘Design Pattern’
(different schemes of implementing OOP in different environment to solve our problems) of OOP.
Check out: O’Reilly’s book Head First Design Patterns by Eric and Elizabeth Freeman.
The syntax in OOP can be boiled down to ‘object.attribute’, there are many aspect of Python which are Object
Oriented such as in Python 3 we run the type() function for example
a = ‘foo’
type(a) # gives us:
<class ‘str’>

This happens because under the hood the design pattern of Python is OOP. We use the dot (.) notation to access
the attributes of an object. We have class as an object and access its attributes ie methods and function we can
also have dot notation just to define charecterstics or properties of that object.

In object we have encapsulated within it its core functionalities, so we start with a class which in itsel is an
‘instance factory’ (superclass) or template for creating other object and in that ‘superclass’ we have methods and
attributes that are encapsulated within this object and are usable immediately but by instantiating or creating an
instance of that class. In OOP we can do what is called a sub-class which is equivalent of the ‘save as’ operation in
an office productivity application where we have all the methods and attributes of a superclass copied down
through ‘inheritance’ in our subclass.

Note : A class is not usable directly, its used as an instance factory, when we instantiate an instance of a class that
instance becomes an object in its own. We have a ‘superclass A’, we have sub-classed ‘A’ as B and then we have an
instance of this ‘subclass B’ as C and law of inheritance says that ‘C’ can acces all the methods and properties of
both A and B. Similar to Name Resolution in TCP/IP networking where a computer looks in its own DNA resolver
cache and memory to look for a name to IP address mapping then it will go to the filesystem and look up a hosts
file to see if a mapping is in there, and then ultimately if that client computer is configured to talk to a DNS Server a
DNS lookup is performed similarly when we have instantiated an instance of a class and use that instance in our
code, when we call a method lets say .y the interpreter figures out where that method lives and we wind up with
what is called a ‘tree search’ as Python starts with the instance and what it has in its local collection of methods
and properties for that matching object if the method is not found it goes to the parent class which in our case is
‘subclass B’ and looks though its list of methods and properties if its not there it goes to the ‘superclass A’ from
which the subclass was defined and looks through its methods and properties as well.

We may create lets say a method called ‘x’ in a subclass even though there is another method called ‘x’ in the
superclass because we as a designer can choose to override any of the inherited methods or attributes that come
from a parent superclass the same applies to instances ie we may have a method ‘x’ in the instance C of the
subclass B which overrides the inherited method ‘x’ from B and A.
The above diagram shows a class that has some methods (mProcedure1, mProcedure2) and attributes (pBouyancy,
pColor etc.) that pertain to that class. In this example we are not sub-classing but directly creating instances of the
class and those instances by inheritance have all the methods and properties of the Class such that if we make a
change to our class code, those changes will be updated by inheritance to each of the instances.
Here we have superclass Circle that has radius and color as variables and some methods(getRadius, getColor,
toString(used in many programming languages such as .NET etc to do a string conversion similar to the str function
in Python)). Cylinder is a form of a circle but has different properties and methods so instead of creting a class from
scratch we can subclass cylinder which inherits the methods and attributes of the parent class, but we can override
certain methods of the class according to our need.

OOP Basics

Class statement creates a named class object, and we call the class object to create an instance of the class (like
making a new document in MS Word using a template, the template itself remains untouched but we have access
to a read copy of that template or an instance of that template). Within a class we can have one or more
assignments that are called attributes which can be variable data, constant data, methods (functions attached to
an object) and when we call a class object to make an instance we inherit all the attributes of the parent class. The
instance gets its own namespace for example

Class Circle
C = circle() # when we create a class in Python we are creating a new data type from scratch

As we have used the dot notation for our method call we call an attribute for example ‘area’ as ‘c.area()’
Self refers to the instance that’s being created:

Oc = OurClass(arg1, arg2) # we only give two parameters because self refers to the instance oc.

A sample program

#This is our superclass


class Car(object):
'''This is the class docstring.'''
def __init__(self, year, make, model):
self.year = str(year) # string representation of year
self.make = make
self.model = model
self.static = 'constant car data' # static will always return 'constant car data'
def __str__(self): # magic methods in OOP can be used to format classes in a way we want to be formatted
'''Formats a nice string.'''
return 'String representation: %s %s %s' % (self.year, self.make, self.model)
def printcar(self):
'''Echoes back the full name of the vehicle.'''
print('{0} {1} {2}'.format(self.year, self.make, self.model))
print(self.static) #just to show off an attribute fetch

ns = Car('1990', 'Nissan', 'Sentra') T


str(ns) |

print(ns) | These lines will be removed to import classes.py


so that we can use it as a module.
print(ns.static) |

ns.printcar() _|

Output
>>>
String representation: 1990 Nissan Sentra
constant car data
1990 Nissan Sentra
constant car data

>>> ================================ RESTART ================================


>>> import classes
>>> dir(classes)
['Car', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> classes.Car.__doc__
'This is the class docstring.'
>>> ns2 = classes.Car('1990', 'Honda', 'Element')
>>> type
<class 'type'>
>>> type(ns2)
<class 'classes.Car'>
>>> str(ns2)
'String representation: 1990 Honda Element'
>>> ns2.make
'Honda'
>>> ns2.year
'1990'
>>> ns2.year = 2010
>>> ns2.year
2010
>>> str(ns2)
'String representation: 2010 Honda Element'
>>> ns2.static
'constant car data'
>>> ns2.static = 'now it is variable data'
>>> ns2.static
'now it is variable data'
>>> ns2.printcar()
2010 Honda Element
now it is variable data
>>>

Special Methods

Magic methods that we can include in our class to customize the behavior of instances:

__str__: to manage string conversion


__repr__: also for string conversion
__add__: for addition(similarly __sub__ for subtraction etc.)

>>> class bar:


def __init__(self, a):
self.a = a
def __add__(self, b):
s = self.a + b.a # b.a = b
return s

>>> x = bar(10)
>>> y = bar(20)
>>> z = x + y
>>> print(z)
30

A sample program

class Car(object):
'''This is the class docstring.'''
def __init__(self, year, make, model):
self.year = str(year) # string representation of year
self.make = make
self.model = model
self.static = 'constant car data' # static will always return 'constant car data'
def __str__(self): # magic methods in OOP can be used to format classes in a way we want to be formatted
'''Formats a nice string.'''
return 'String representation: %s %s %s' % (self.year, self.make, self.model)
def printcar(self):
'''Echoes back the full name of the vehicle.'''
print('{0} {1} {2}'.format(self.year, self.make, self.model))
print(self.static) #just to show off an attribute fetch

#This is our subclass; inherits from Car


class Motorcycle(Car): # name of the subclass(class from which we are inheriting)
pass

Save and go to IDLE

>>> import classes


>>> mc = classes.Motorcycle('2008', 'Honda', 'Shadow')
>>> mc.printcar()
2008 Honda Shadow
constant car data
>>> mc.static
'constant car data'

Advanced Topics

1. Generators: A generator function is a kind of resumable function that instead of running through an iterable
just all at once and returning a single object we can actually stop and resume or in other words save state in a
functions execution.
2. Decorators: Decorators are functions that we can use to wrap other functions in.

A generator function being a resumable function returns results one at a time instead of giving you the functions
result as whole. Generators are used exclusively in looping constructs (for loop) for the purpose of 1. Saving
memory, instead of running straight ahead iteration which brings back all objects in a particular range or list we
can conserve memory by using the yield statement and suspend or resume the state of that function. We can go
through an iterable sequence at certain point yield that result come back later and pick up where we left off.
2. Save Computing Cycles, Generators work with functions as well as expressions, generator expressions are much
like list comps but instead of giving back an entire list which will increase memory footprint as well as consume
unnecessary computing cycles. With generator expression also called genexp we can return results on demand
instead of a full list. Optimizing Performance of program and minimizing memory footprints.

def gensq(n):
for i in range (n):
yield i ** 2

a = gensq(6)

next(a)
>>> def gensq(n):
for i in range(n):
yield i ** 2

>>>
>>> a = gensq(4)
>>> next(a)
0
>>> next(a)
1
>>> next(a)
4
>>> next(a)
9
>>> next(a)
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
next(a)
StopIteration
>>>

>>> def itsq(n):


result = []
for i in range(n):
result.append(i ** 2)
return result

>>> b = itsq(4)
>>> b
[0, 1, 4, 9]
>>> type(b)
<class 'list'>
>>>

Generator Expression

List Comprehension:
>>> lc = [i ** 2 for i in range(10)]
>>> lc
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Generator Expression:

>>> gx = (i ** 2 for i in range(1, 11)) # parenthesis instead of []


>>> gx
<generator object <genexpr> at 0x0000000002F4C798>
>>> next(gx)
1
>>>

>>> m1 = [1, 2, 4, 8, 16, 31, 64]


>>> genexp = (i for i in m1 if i > 10)
>>> genexp
<generator object <genexpr> at 0x0000000002F4C7E0>
>>> next(genexp)
16
>>> next(genexp)
31
>>> next(genexp)
64
>>> for j in genexp:
print(j)

>>> # Generators cannot be rewound


>>> genexp = (i for i in m1 if i > 10)
>>> for j in genexp:
print(j)

16
31
64
>>>

We have to redefine genexp and run our for loop again to print the numbers in j. If we now run the for loop again:

>>> for j in genexp:


print(j)

>>>

Again no result will be displayed because the genexp has been expended.
Decorators

Not to be confused with Decorator Design Pattern in OOP discussed earlier.

Decorator is a function that takes another function as its argument, decorators are an example of
metaprogramming(as writing of computer programs that write or manipulate other programs or themselves as
their data or the due part of the work at compile time that would otherwise be done at runtime).

>>> def et(myfunc):


def innerf(*args): #*args menas any no. of arguments
for i in range(8):
myfunc(*args)
return innerf

>>>
This function is going to be our decorator and its going to take another function as an argument and simply echo
the contents of the function 8 times.

>>> def nondec1():


print('eggs')

>>> nondec1()
eggs
>>> @et
def dec1():
print('spam')

>>> dec1()
spam
spam
spam
spam
spam
spam
spam
spam
>>>
Graphical User Interface (GUI) Development

We can build and deploy cross platform desktop applications in Python using the GUI frameworks or toolkits,
frameworks and toolkits at base are a collection of classes that we can subclass from or instantiate objects from
that will save us a lot of work in comparison to starting from scratch.
A widget is a combination of two words window and gadget, essentially any GU interface we have used is going to
be composed of one or more window objects or frames, tkinter in particular has a model with layered windows or
framed windows where we can lay other elements (buttons, textboxes, labesl, icons etc.) within that GUI window
and arrange them on our grid or geometry to the best way it suits us or the users.

The above image is an example tkinterv(Python’s native GUI toolkit) based python GUI.

Popular GUI toolkits:

1. WxPython is a port of wxWidgets framework some popular applications that rely on wxWidgets are BitTorrent,
Dropbox client.
2. PyQt port of QT toolkit popular applications are KDE desktop environment for Linux, Google Earth, Opera Web
browser and VLC media player.
3. PyGtk port of GTK+ C-library popular application are GNOME desktop environment and GIMP the open source
competitor to Photoshop.

All three of these are ports or bindings to Python of a cross platform GUI toolkit, traditionally the cross platform
toolkits are written in C.

About TKinter

TKinter means TK interface and TK is another GUI library that’s part of the TCL (Tool Command Language) or tickle
scripting language, IDLE ide is a TK application, Tkinter or GUI apps in general are event-driven which when started
run a mainloop() or main event loop.

Apps and Books for reference :

Visual Tkinter GUI for Python 2.6


Balsamiq Mockups for desktop built on Adobe air framework
Python and Tkinter Programming by John E. Grayson is a good (and perhaps the only) reference to know more
about of TKinter, New Mexico Tech (nmt.edu) has a good document called Tkinter 8.4 reference : a GUI for Python
and pythonware.com/library has ‘An Introduction to Tkinter’,

A sample Program:

from tkinter import *

root = Tk() # we create a root or a parent window and instantiating an instance of the Tk class
root.title('Button App')
Label(text ='I am a button').pack(pady = 15) # attaching the button to the parent form using the .pack
# pad y means we pad from the y axis 15 charecters
def quitapp():
root.destroy() # destroy method is one way to quit a GUI app

Button(text = 'Quit', command = quitapp).pack(side=BOTTOM)


root.mainloop() # this kicks off the main Tkinter routine for our Tkinter application.

Output
Another Exmaple :

# adapted from TkDocs.com

from tkinter import *


from tkinter import ttk # ttk is a themed widget set which gives us stylish widget controls and also intends to
seperate the apperance of the widget from its behaviour

def calculate(*args):
try:
value = float(feet.get()) # tkinter.get() returns the value of a variable as a string.
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass

root = Tk()
root.title("Feet to Meters")

mainframe = ttk.Frame(root, padding="3 3 12 12") # in Python the term frame and windows are synonymous
mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) # The Tkinter grid() geometry manager is a more robust
alternative to pack.
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

feet = StringVar() # feet and meters are separate instances of the StringVar() class.
meters = StringVar()

feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)


feet_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))


ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)


ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

feet_entry.focus()
root.bind('<Return>', calculate)

root.mainloop()
Output:

Python and Database Access

As our Python program goes, we may have need for persistent data storage, the way that most application
framework work in this regard is to make use of an application programming interface (API) to connect to one or
more database systems.
When we talk about connecting Python scripts to Relational database management systems (RDBMS) this involves
some kind of connector or translator or interface between those two systems an API addresses this need. For
instance if we had to connect Python to Oracle our job is to see if Oracle itself or some third party has published an
API specification that enables us to pipe our Python into Oracle database. Some standard database API’s are :

1. Open-Database Connectivity(ODBC)(most popular)


2. Jave Database Connectivity (JDBC)(in Java)
3. Active X Data Object (ADO) (Microsoft .Net Framework)

The API published by Python to support connectivity with Relational database systems is called DB-API 2.0. There
are lots of API providers so we can easily hook in Python to any contemporary RDBMS, we have MySQL for Python
library, cx_Oracle for Oracle, adodbapi for MS SQL Server, PyGre SQL for PostgreSQL etc. The thing to remember is
that as long as we are using the DB-API it will not only be easy for Python interacting to a particular RDMS but also
easy from going from one RDMS to another, we might have a python project that is currently interacting with
MySQL and then due to an architectural change we now need to get that code interacting with Oracle the amount
of code redo using DB-API will be very small.
(Read article about database programming on wiki.python.org)

SQLite Database interaction with Python

SQLite is a tremendously powerful database library which is nearly 300 KB in size written in C. SQLite is not a client
server system ie if we wanted to get a Python application writing to a database we created in MySQL it requires
that we have the MySQL Daemon or server process running on our own machine or a remote box, that can get
really complicated if we are porting our application from one environment to another, SQLite on the other hand is
a linked ACID-compliant (stands for Atomic, Consistent, Isolation, Durability, an acid test for whether a relational
database is truly standard compliant and in particular transaction safe) relational database management system.
Linked means its actually embedded as library in our application which is beneficial in portability, speed as we
don’t have any latency introduced as Inter-Process Communication or IPC traffic for instance we have our Python
as one process and mysqld running as another process there will be some latency in inter process communication
Whereas here we have python application and sql running in the same process which tends to be very fast. We can
create sqlite database in memory so we are not saving any data permanently or we can manifest our database as a
file in the filesystem. We use SQLite database day to day as in Mozilla Firefox, Thunderbird, Adobe AIR, Palm’s
WebOS and the Iphone OS etc.

Note: We do not need to download SQLite as current versions of Python come with SQLite included.

Pysqlite : is the project that is developed external to CPython, the module for sqlite that comes with Python is
sqlite3 (standard library module).

Using SQLite in Python is easy, the first object we create is a connection object which is a handle to a particular
database which functions a little bit like the read() function, we use the connection object to use open a handle to
an existing database or we can create a new one on the fly, the database are binary files that exist in our file
system we can give them any extension we want (no extension, .sdb, .db etc), we can also create a connection
object using the memory flag (:memory:), this generates a non-persistent in-memory database that is useful for
testing but its volatile (goes away when we close the session).

After the connection object we create the cursor object in Python SQLite a cursor object acts as a handle for a
particular database object, so once we are in a database we can create and edit tables we can do inserts, updates,
deletes we can run select statements (SQL statements), where the execute() method is used in conjunction with
the cursor object to execute SQL.

SQLite is very faithfull to ANSI ISO SQL-92 standard (used for defining standard compliant SQL), standard SQL Data
Definition Language (DDL) statements like create alter and drop and Data Manipulation Language (DML)
statements like select, insert, update and delete will work fine with SQLite.

>>> import os
>>> import sqlite3
>>> os.getcwd()
'C:\\Python31'
>>> con = sqlite3.connect('hrdb')
>>> type(con)
<class 'sqlite3.Connection'>
>>> cur = con.cursor()
>>> cur.execute('''CREATE TABLE emp (id integer primary key, fn varchar(20), ln varchar(20))''')
<sqlite3.Cursor object at 0x0000000002EFDD40>
>>> cur.execute('''INSERT INTO emp VALUES (1, 'Abhi', 'Jais')''')
<sqlite3.Cursor object at 0x0000000002EFDD40>
>>> cur.execute('''INSERT INTO emp VALUES (2, 'Tim', 'Warner')''')
<sqlite3.Cursor object at 0x0000000002EFDD40>
>>> con.commit() # makes changes made to the database persitent
>>> cur.execute('SELECT * FROM emp')
<sqlite3.Cursor object at 0x0000000002EFDD40>
>>> print(cur.fetchall())
[(1, 'Abhi', 'Jais'), (2, 'Tim', 'Warner')]
>>> cur.execute('SELECT * FROM emp WHERE id = 1')
<sqlite3.Cursor object at 0x0000000002EFDD40>
>>> print(cur.fetchall())
[(1, 'Abhi', 'Jais')]
>>> con.close()
>>>

Create a table called emp (to store employee information) and then in parenthesis we have column list and data
types (id column that uses integer data type and has a primary key constraint attached to it), varchar is a variable
length character string that saves disk space compared to char type (SQL lite supports binary large data object
(blob) like audio, video, image files etc.)

Porting Python Code

Python 3 being the first backwards incompatible version of Python and there being frameworks which are exclusive
to Python 2 (GUI framework) which makes it important for us to understand how to port python code from version
2 to version 3 (by using standard library module 2to3.py, remember there are no conversion process that are
totally automatic we do have to put in some effort of our own to get results).

A Comparison (search Dive Into Python 3 and read porting python 2 to 3, docs.python.org/whatsnew page):

Python 2.6 Python 3.1


Print statement Print function
Xrange() Range()
Long Int
½ = 0 (integer division) ½ = 0.5(float division) for integer division we use 1//2
ASCII Strings Unicode Strings
Supports C-style string formatting (%) Supports both c-style as well as the (.format) method
Using {} string format (preferred)
Raw input() Input()
Except Ex, e Except Ex as e (alias the exception as e)

Pyhton Code Porting Overview

Excellent test coverage means that the code should not be messy and unstable in the first place, secondly if the
source code is written on versions before Python 2.6 it should be first ported to Python 2.6 and then only we will
use the 2to3.py module.

There are also projects to develop a back porting utility (3to2 project) for Python.
The 2to3 Script

In Python 2 installation folder we can run Python with a -3 parameter to warn us about ‘blocker’ issues and then
switch to Python 3 inside \tools\scripts subdirectory we find 2to3.py module.

C:\Users\Sysadmin>cd C:\Python31\Tools\Scripts\

Syntax help on command prompt for the 2to3 module:

C:\Python31\Tools\Scripts>python 2to3.py --help


Usage: 2to3 [options] file|dir ...

Options:
-h, --help show this help message and exit
-d, --doctests_only Fix up doctests only
-f FIX, --fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a fixer from being run.
-l, --list-fixes List available transformations (fixes/fix_*.py)
-p, --print-function Modify the grammar so that print() is a function
-v, --verbose More verbose logging
--no-diffs Don't show diffs of the refactoring
-w, --write Write back modified files
-n, --nobackups Don't write backups for modified files.

A samlple program to demonstrate code porting using 2to3 module:

# adapted from docs.python.org

def greet(name):
print "Hello, number {0}!".format(name)
print "Enter a number: "
name = raw_input()
greet(name)

if int(name) <> 1:
print 'Sorry-the number should be 1'
else:
print 'You guessed correctly.'
We save the above code as convert.py in our Python folder then open command prompt and :

C:\Users\Sysadmin>cd C:\Python26\

C:\Python26>dir c*.py # to verify that file is inside that folder


Volume in drive C is OSDisk
Volume Serial Number is 5C01-0733

Directory of C:\Python26

05/03/2016 12:21 PM 313 convert.py


1 File(s) 313 bytes
0 Dir(s) 318,570,659,840 bytes free
C:\Python26>python -3 convert.py
convert.py:9: DeprecationWarning: <> not supported in 3.x; use !=
if int(name) <> 1: # in Python 2.x not equals is denoted by <>
Enter a number:

Now open another command prompt:

C:\Users\Sysadmin>cd C:\Python31\Tools\Scripts\

C:\Python31\Tools\Scripts>dir 2*.py
Volume in drive C is OSDisk
Volume Serial Number is 5C01-0733

Directory of C:\Python31\Tools\Scripts

09/08/2008 11:28 AM 112 2to3.py


1 File(s) 112 bytes
0 Dir(s) 318,571,065,344 bytes free

C:\Python31\Tools\Scripts>python 2to3.py -l C:\Python26\convert.py > refactor1.t # to get the standard output in


xt # a text file and see what part of code is getting converted
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored C:\Python26\convert.py
RefactoringTool: Files that need to be modified:
RefactoringTool: C:\Python26\convert.py

C:\Python31\Tools\Scripts>python 2to3.py -w C:\Python26\convert.py # -w is used to write the changes to the


RefactoringTool: Skipping implicit fixer: buffer # file
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored C:\Python26\convert.py
--- C:\Python26\convert.py (original)
+++ C:\Python26\convert.py (refactored)
@@ -1,12 +1,12 @@
# adapted from docs.python.org

def greet(name):
- print "Hello, number {0}!".format(name)
-print "Enter a number: "
-name = raw_input()
+ print("Hello, number {0}!".format(name))
+print("Enter a number: ")
+name = input()
greet(name)

-if int(name) <> 1: # in Python 2.x not equals is denoted by <>


- print "Sorry the number should be 1."
+if int(name) != 1: # in Python 2.x not equals is denoted by <>
+ print("Sorry the number should be 1.")
else:
- print "You guessed correctly."
+ print("You guessed correctly.")
RefactoringTool: Files that were modified:
RefactoringTool: C:\Python26\convert.py

The above operation also creates a backup of the file and stores it in the same folder as convert.py.bak, now we
edit the file convert.py with idle

# adapted from docs.python.org

def greet(name):
print("Hello, number {0}!".format(name))
print("Enter a number: ") # this line needs manual editing so we delete the line and make
name = input() # name = input("Enter a number: ")
greet(name)

if int(name) != 1: # in Python 2.x not equals is denoted by <>


print("Sorry the number should be 1.")
else:
print("You guessed correctly.")

Output on Python Shell 3.1.2:


>>>
Enter a number: 3
Hello, number 3!
Sorry the number should be 1.
>>>
Python Frameworks and IDEs

Python Web Application Frameworks

Web applications : Back in the old days a commercial website used to contain one or more static html pages
nowadays the front end or the user interface is rendering xhtml with css etc, simply put, those pages are being
generated dynamically using one or more scripting or web application languages like PHP etc. A tyical web app
involves a back end that involves one or ore relational database management systems for data storage. In the
middle between the user interface or the presentation layer and the database or the data model we have our
actual application code. This three tier architecture is formally referred to as the MVC (Model View Controller)
software architectural pattern, it allows developers to work on one component without interrupting the other
components. ‘V’ is the view or the user interface, ‘M’ is the model or the data model that represents the back end
relational database schema (designing of tables, columns, constraints etc), ‘C’ or the controller is the middle tier
application logic that interfaces between the data model and the front end interface presented to the user.

1. Django: MVC based web app framework, its architectural principle is ‘DRY’ (Don’t Repeat Yourselves).
2. Pylons: Pyhton Web app framework influenced by ROR (Ruby on rails), there is the Ruby programming
language and ‘on rails’ is the web app framework that surrounds the language, Pylons is based on the same
idea.
3. TurboGears: MVC based, features rapid development
4. Web2py: Originally developed as a teaching tool, supports Agile development (very useful in software
development).
5. Zope: because of its maturity some consider it be Python’s ‘killer app’.

Django (djangoproject.com) The washingtonpost.com website is built using Django framework.


Pylons (pylonshq.com can read Definative Guide to Pylons by James Gardner) The original bitorrent.com website is
built using Pylons framework.
TurboGears (turbogears.org) newmetalarmy.com is built using the TurboGears.
Web2py sitescrapper.com is built using Web2py
Zope (zope.org) AARP.com is built using Zope.
For more info on MVC visit Wikipedia.

Python IDE’s

Features to look for in Python IDE’s:

1: Cross Platform : Does the ide works natively on windows and *NIX machines.
2: Commercial or Free
3: Automatic Code-Completion
4: Multi-Language Support
5: Integrated Python Debugging: We have a debugging environment integrated in to the IDE.
6: Error Markup
7: Source Control Integration: Version Control Software (popular one is Sub-Version (SVN)) are code repositiories
that are a safe place for the code to be such that developers can check out and check in modules and keeps track
of changes so there is revision tracking and prevents code overwriting.
8: Smart Indentation
9: Bracket Matching: Not so useful in Python but in other pl’s like C and C++ etc.
10: Line Numbering: Not present in IDLE.
11: UML Editing/Viewing: UML is a markup language that we can use to design interfaces, relational databases and
software for example Microsoft VIZIO has built in UML templates.
12: Code Folding: To expand and collapse certain sections/blocks of our code.
13: Code Templates
14: Unit testing
15: UID: Integrated GUI Designer example QT, Eric etc.
16: Database – Integrated database support in Python we have mainly have MySQL, PostgreSQL or we can plug
into Oracle, IBM DB2 or MS-SQL as well.
17: RAD: Supports Rapid Application Development.

Highly-Rated IDE’s:

1. Active State (publishes its own distribution of Python) has an IDE called Komodo Edit (Free Version/Smaller)
and Komodo IDE (Paid Version/Enterprise).
2. Wingware’s Wing IDE Pro (Paid) is recommended for new programmers.
3. Eclipse (uses PyDev Plugin) is a language neutral framework i.e. it is a host or shell into which we can plugin our
development environment of choice.
4. NetBeans is another platform neutral ide from JAVA that supports Python and many other languages.
5. Boa Constructor: Very Popular GUI builder uses wxPython Framework.
6. Visual TKinter is very buggy and is WIP.
7. SPE: Free Open-Source IDE.

Links:
boaconstructor.sourceforge.net
pythonides.blogspot.com (stani’s python editor (SPE))

Mail questions @: timothy@nuggetlab.com

Вам также может понравиться