Curso de Python 2
Curso de Python 2
Python Overview
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be
highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer
syntactical constructions than other languages.
Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to compile
your program before executing it. This is similar to PERL and PHP.
Python is Interactive − You can actually sit at a Python prompt and interact with the interpreter directly
to write your programs.
Python is a Beginner's Language − Python is a great language for the beginner-level programmers
and supports the development of a wide range of applications from simple text processing to WWW
browsers to games.
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research
Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix
shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License
(GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still holds a
vital role in directing its progress.
Python Features
Python's features include −
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows
the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
A broad standard library − Python's bulk of the library is very portable and cross-platform compatible
on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
Extendable − You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
GUI Programming − Python supports GUI applications that can be created and ported to many system
calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of
Unix.
Scalable − Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are listed below −
It can be used as a scripting language or can be compiled to byte-code for building large applications.
It provides very high-level dynamic data types and supports dynamic type checking.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Win 9x/NT/2000
OS/2
Windows CE
Acorn/RISC OS
BeOS
Amiga
VMS/OpenVMS
QNX
VxWorks
Psion
Python has also been ported to the Java and .NET virtual machines
Getting Python
The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official
website of Python https://www.python.org/
You can download Python documentation from https://www.python.org/doc/ . The documentation is available in
HTML, PDF, and PostScript formats.
Installing Python
Python distribution is available for a wide variety of platforms. You need to download only the binary code
applicable for your platform and install Python.
If the binary code for your platform is not available, you need a C compiler to compile the source code manually.
Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.
Follow the link to download zipped source code available for Unix/Linux.
make
make install
This installs Python at standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the
version of Python.
Windows Installation
Here are the steps to install Python on Windows machine.
Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to
install.
To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the
installer file to your local machine and then run it to find out if your machine supports MSI.
Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just
accept the default settings, wait until the install is finished, and you are done.
Macintosh Installation
Recent Macs come with Python installed, but it may be several years out of date. See
http://www.python.org/download/mac/ for instructions on getting the current version along with extra tools to
support development on the Mac. For older Mac OS's before Mac OS X 10.3 (released in 2003), MacPython is
available.
Jack Jansen maintains it and you can have full access to the entire documentation at his website −
http://www.cwi.nl/~jack/macpython.html . You can find complete installation details for Mac OS installation.
Setting up PATH
Programs and other executable files can be in many directories, so operating systems provide a search path that
lists the directories that the OS searches for executables.
The path is stored in an environment variable, which is a named string maintained by the operating system. This
variable contains information available to the command shell and other programs.
The path variable is named as PATH in Unix or Path in Windows (Unix is case sensitive; Windows is not).
In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory,
you must add the Python directory to your path.
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
In the bash shell (Linux) − type export ATH="$PATH:/usr/local/bin/python" and press Enter.
PYTHONPATH
It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files
1
imported into a program. It should include the Python source library directory and the directories
containing Python source code. PYTHONPATH is sometimes preset by the Python installer.
PYTHONSTARTUP
It contains the path of an initialization file containing Python source code. It is executed every time
2
you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load
utilities or modify PYTHONPATH.
PYTHONCASEOK
3 It is used in Windows to instruct Python to find the first case-insensitive match in an import statement.
Set this variable to any value to activate it.
PYTHONHOME
Running Python
There are three different ways to start Python −
Interactive Interpreter
You can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell
window.
$python # Unix/Linux
or
python% # Unix/Linux
or
C:> python # Windows/DOS
-d
1
It provides debug output.
-O
2
It generates optimized bytecode (resulting in .pyo files).
-S
3
Do not run import site to look for Python paths on startup.
-v
4
verbose output (detailed trace on import statements).
-X
5
disable class-based built-in exceptions (just use strings); obsolete starting with version 1.6.
-c cmd
6
run Python script sent in as cmd string
file
7
run Python script from given file
or
or
Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.
Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main
website, downloadable as either MacBinary or BinHex'd files.
If you are not able to set up the environment properly, then you can take help from your system admin. Make sure
the Python environment is properly set up and working perfectly fine.
Note − All the examples given in subsequent chapters are executed with Python 2.4.3 version available on
CentOS flavor of Linux.
We already have set up Python Programming environment online, so that you can execute all the available
examples online at the same time when you are learning theory. Feel free to modify any example and execute it
online.
$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Type the following text at the Python prompt and press the Enter −
If you are running new version of Python, then you would need to use print statement with parenthesis as in print
("Hello, Python!");. However in Python version 2.4.3, this produces the following result −
Hello, Python!
Let us write a simple Python program in a script. Python files have extension .py. Type the following source code
in a test.py file −
$ python test.py
Hello, Python!
Let us try another way to execute a Python script. Here is the modified test.py file −
#!/usr/bin/python
We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows
−
Hello, Python!
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier
starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0
to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive
programming language. Thus, Manpower and manpower are two different identifiers in Python.
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use them as constant
or variable or any other identifier names. All the Python keywords contain lowercase letters only.
assert finally or
def if return
elif in while
else is with
The number of spaces in the indentation is variable, but all statements within the block must be indented the
same amount. For example −
if True:
print "True"
else:
print "False"
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Thus, in Python all the continuous lines indented with same number of spaces would form a block. The following
example has various statement blocks −
Note − Do not try to understand the logic at this point of time. Just make sure you understood various blocks
even if they are without braces.
#!/usr/bin/python
import sys
try:
# open file stream
file = open(file_name, "w")
except IOError:
print "There was an error writing to", file_name
sys.exit()
print "Enter '", file_finish,
print "' When finished"
while file_text != file_finish:
file_text = raw_input("Enter text: ")
if file_text == file_finish:
# close the file
file.close
break
file.write(file_text)
file.write("\n")
file.close()
file_name = raw_input("Enter filename: ")
if len(file_name) == 0:
print "Next time please enter something"
sys.exit()
try:
file = open(file_name, "r")
except IOError:
print "There was an error reading file"
sys.exit()
file_text = file.read()
file.close()
print file_text
Multi-Line Statements
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation
character (\) to denote that the line should continue. For example −
total = item_one + \
item_two + \
item_three
Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For
example −
Quotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type
of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following are legal −
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
Comments in Python
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of
the physical line are part of the comment and the Python interpreter ignores them.
#!/usr/bin/python
# First comment
print "Hello, Python!" # second comment
You can type a comment on the same line after a statement or expression −
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.
In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.
#!/usr/bin/python
Here, "\n\n" is used to create two new lines before displaying the actual line. Once the user presses the key, the
program ends. This is a nice trick to keep a console window open until the user is done with an application.
Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or
more lines which make up the suite. For example −
if expression :
suite
elif expression :
suite
else :
suite
Command Line Arguments
Many programs can be run to provide you with some basic information about how they should be run. Python
enables you to do this with -h −
$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser (also PYTHONDEBUG=x)
-E : ignore environment variables (such as PYTHONPATH)
-h : print this help message and exit
[ etc. ]
You can also program your script in such a way that it should accept various options. Command Line
Arguments is an advanced topic and should be studied a bit later once you have gone through rest of the
Python concepts.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the
reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or
characters in these variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator
is the value stored in the variable. For example −
#!/usr/bin/python
print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and name variables, respectively. This
produces the following result −
100
1000.0
John
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously. For example −
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory
location. You can also assign multiple objects to multiple variables. For example −
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string
object with the value "john" is assigned to the variable c.
Numbers
String
List
Tuple
Dictionary
Python Numbers
Number data types store numeric values. Number objects are created when you assign a value to them. For
example −
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax of the del statement
is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example −
del var
del var_a, var_b
long (long integers, they can also be represented in octal and hexadecimal)
Python allows you to use a lowercase l with long, but it is recommended that you use only an uppercase
L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x
and y are the real numbers and j is the imaginary unit.
Python Strings
Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python
allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and
[:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −
#!/usr/bin/python
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python Lists
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them
is that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the
beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the
asterisk (*) is the repetition operator. For example −
#!/usr/bin/python
Python Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated
by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and
size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be
thought of as read-only lists. For example −
#!/usr/bin/python
The following code is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar
case is possible with lists −
#!/usr/bin/python
Python Dictionary
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and
consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings.
Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces
([]). For example −
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order";
they are simply unordered.
There are several built-in functions to perform conversion from one data type to another. These functions return a
new object representing the converted value.
int(x [,base])
1
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
2
Converts x to a long integer. base specifies the base if x is a string.
float(x)
3
Converts x to a floating-point number.
complex(real [,imag])
4
Creates a complex number.
str(x)
5
Converts object x to a string representation.
repr(x)
6
Converts object x to an expression string.
eval(str)
7
Evaluates a string and returns an object.
tuple(s)
8
Converts s to a tuple.
list(s)
9
Converts s to a list.
set(s)
10
Converts s to a set.
11 dict(d)
chr(x)
13
Converts an integer to a character.
unichr(x)
14
Converts an integer to a Unicode character.
ord(x)
15
Converts a single character to its integer value.
hex(x)
16
Converts an integer to a hexadecimal string.
oct(x)
17
Converts an integer to an octal string.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
Python language supports the following types of operators.
Arithmetic Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
[ Show Example ]
>= If the value of left operand is greater than or equal (a >= b) is not true.
to the value of right operand, then condition
becomes true.
[ Show Example ]
/= Divide It divides left operand with the right operand and c /= a is equivalent to c = c / ac /= a is equivalent to
AND assign the result to left operand c=c/a
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
~a = 1100 0011
[ Show Example ]
~ Binary
(~a ) = -61 (means 1100 0011 in 2's complement
Ones It is unary and has the effect of 'flipping' bits.
form due to a signed binary number.
Complement
<< Binary The left operands value is moved left by the number
a << 2 = 240 (means 1111 0000)
Left Shift of bits specified by the right operand.
[ Show Example ]
[ Show Example ]
Evaluates to true if it does not finds a variable in the x not in y, here not in results in a 1 if x is not a
not in
specified sequence and false otherwise. member of sequence y.
Python Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators explained
below −
[ Show Example ]
[ Show Example ]
**
1
Exponentiation (raise to the power)
~+-
2
Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % //
3
Multiply, divide, modulo and floor division
+-
4
Addition and subtraction
>> <<
5
Right and left bitwise shift
&
6
Bitwise 'AND'
7 ^|
Bitwise exclusive `OR' and regular `OR'
<> == !=
9
Equality operators
= %= /= //= -= += *= **=
10
Assignment operators
is is not
11
Identity operators
in not in
12
Membership operators
not or and
13
Logical operators
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to
determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the programming languages
−
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or
null, then it is assumed as FALSE value.
Python programming language provides following types of decision making statements. Click the following links
to check their detail.
if statements
1 An if statement consists of a boolean expression followed by one or more statements.
if...else statements
2 An if statement can be followed by an optional else statement, which executes when the boolean
expression is FALSE.
nested if statements
3 You can use one if or else if statement inside another if or else if statement(s).
#!/usr/bin/python
var = 100
Python - Loops
In general, statements are executed sequentially: The first statement in a function is executed first, followed by
the second, and so on. There may be a situation when you need to execute a block of code several number of
times.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. The following diagram
illustrates a loop statement −
Python programming language provides following types of loops to handle looping requirements.
while loop
1 Repeats a statement or group of statements while a given condition is TRUE. It tests the condition
before executing the loop body.
for loop
2 Executes a sequence of statements multiple times and abbreviates the code that manages the loop
variable.
nested loops
3 You can use one or more loop inside any another while, for or do..while loop.
Python supports the following control statements. Click the following links to check their detail.
break statement
1 Terminates the loop statement and transfers execution to the statement immediately following the
loop.
continue statement
2 Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.
pass statement
3 The pass statement in Python is used when a statement is required syntactically but you do not want
any command or code to execute.
Python - Numbers
Number data types store numeric values. They are immutable data types, means that changing the value of a
number data type results in a newly allocated object.
Number objects are created when you assign a value to them. For example −
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax of the del statement
is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example −
del var
del var_a, var_b
int (signed integers) − They are often called just integers or ints, are positive or negative whole
numbers with no decimal point.
long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and
followed by an uppercase or lowercase L.
float (floating point real values) − Also called floats, they represent real numbers and are written with
a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E
or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j) represents
the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary
part is b. Complex numbers are not used much in Python programming.
Examples
Here are some examples of numbers
Python allows you to use a lowercase L with long, but it is recommended that you use only an
uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a
is the real part and b is the imaginary part of the complex number.
Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x and
y are numeric expressions
Mathematical Functions
Python includes following functions that perform mathematical calculations.
Sr.No. Function & Returns ( description )
abs(x)
1 The absolute value of x: the (positive) distance between x and zero.
ceil(x)
2 The ceiling of x: the smallest integer not less than x
cmp(x, y)
3 -1 if x < y, 0 if x == y, or 1 if x > y
exp(x)
4
The exponential of x: ex
fabs(x)
5 The absolute value of x.
floor(x)
6 The floor of x: the largest integer not greater than x
log(x)
7 The natural logarithm of x, for x> 0
log10(x)
8 The base-10 logarithm of x for x> 0.
max(x1, x2,...)
9 The largest of its arguments: the value closest to positive infinity
min(x1, x2,...)
10 The smallest of its arguments: the value closest to negative infinity
modf(x)
11 The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The
integer part is returned as a float.
pow(x, y)
12 The value of x**y.
round(x [,n])
13 x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker:
round(0.5) is 1.0 and round(-0.5) is -1.0.
14 sqrt(x)
The square root of x for x > 0
Random Number Functions
Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes
following functions that are commonly used.
choice(seq)
1 A random item from a list, tuple, or string.
random()
3 A random float r, such that 0 is less than or equal to r and r is less than 1
seed([x])
4 Sets the integer starting value used in generating random numbers. Call this function before calling
any other random module function. Returns None.
shuffle(lst)
5 Randomizes the items of a list in place. Returns None.
uniform(x, y)
6 A random float r, such that x is less than or equal to r and r is less than y
Trigonometric Functions
Python includes following functions that perform trigonometric calculations.
acos(x)
1 Return the arc cosine of x, in radians.
asin(x)
2 Return the arc sine of x, in radians.
atan(x)
3 Return the arc tangent of x, in radians.
atan2(y, x)
4 Return atan(y / x), in radians.
5 cos(x)
Return the cosine of x radians.
hypot(x, y)
6 Return the Euclidean norm, sqrt(x*x + y*y).
sin(x)
7 Return the sine of x radians.
tan(x)
8 Return the tangent of x radians.
degrees(x)
9 Converts angle x from radians to degrees.
radians(x)
10 Converts angle x from degrees to radians.
Mathematical Constants
The module also defines two mathematical constants −
pi
1
The mathematical constant pi.
e
2
The mathematical constant e.
Python - Strings
Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in
quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a
value to a variable. For example −
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.
For example −
#!/usr/bin/python
var1[0]: H
var2[1:5]: ytho
Updating Strings
You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to
its previous value or to a completely different string altogether. For example −
#!/usr/bin/python
Escape Characters
Following table is a list of escape or non-printable characters that can be represented with backslash notation.
An escape character gets interpreted; in a single quoted as well as double quoted strings.
\b 0x08 Backspace
\cx Control-x
\C-x Control-x
\e 0x1b Escape
\f 0x0c Formfeed
\M-\C-x Meta-Control-x
\n 0x0a Newline
\t 0x09 Tab
\x Character x
[] Slice - Gives the character from the given index a[1] will give e
#!/usr/bin/python
Here is the list of complete set of symbols which can be used along with % −
%c character
%o octal integer
Other supported symbols and functionality are listed in the following table −
Symbol Functionality
- left justification
add the octal leading zero ( '0' ) or hexadecimal leading '0x' or '0X', depending on
#
whether 'x' or 'X' were used.
m is the minimum total width and n is the number of digits to display after the
m.n.
decimal point (if appl.)
Triple Quotes
Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim
NEWLINEs, TABs, and any other special characters.
The syntax for triple quotes consists of three consecutive single or double quotes.
#!/usr/bin/python
When the above code is executed, it produces the following result. Note how every single special character has
been converted to its printed form, right down to the last NEWLINE at the end of the string between the "up." and
closing triple quotes. Also note that NEWLINEs occur either with an explicit carriage return at the end of a line or
its escape code (\n) −
Raw strings do not treat the backslash as a special character at all. Every character you put into a raw string
stays the way you wrote it −
#!/usr/bin/python
print 'C:\\nowhere'
C:\nowhere
Now let's make use of raw string. We would put expression in r'expression' as follows −
#!/usr/bin/python
print r'C:\\nowhere'
C:\\nowhere
Unicode String
Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode.
This allows for a more varied set of characters, including special characters from most languages in the world. I'll
restrict my treatment of Unicode strings to the following −
#!/usr/bin/python
Hello, world!
As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.
capitalize()
1 Capitalizes first letter of string
center(width, fillchar)
2 Returns a space-padded string with the original string centered to a total of width columns.
3 Counts how many times str occurs in string or in a substring of string if starting index beg and
ending index end are given.
decode(encoding='UTF-8',errors='strict')
4 Decodes the string using the codec registered for encoding. encoding defaults to the default string
encoding.
encode(encoding='UTF-8',errors='strict')
5 Returns encoded string version of string; on error, default is to raise a ValueError unless errors is
given with 'ignore' or 'replace'.
6 Determines if string or a substring of string (if starting index beg and ending index end are given)
ends with suffix; returns true if so and false otherwise.
expandtabs(tabsize=8)
7 Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided.
isalnum()
10 Returns true if string has at least 1 character and all characters are alphanumeric and false
otherwise.
isalpha()
11 Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.
isdigit()
12 Returns true if string contains only digits and false otherwise.
islower()
13 Returns true if string has at least 1 cased character and all cased characters are in lowercase and
false otherwise.
isnumeric()
14 Returns true if a unicode string contains only numeric characters and false otherwise.
isspace()
15 Returns true if string contains only whitespace characters and false otherwise.
istitle()
16 Returns true if string is properly "titlecased" and false otherwise.
isupper()
17 Returns true if string has at least one cased character and all cased characters are in uppercase
and false otherwise.
join(seq)
18 Merges (concatenates) the string representations of elements in sequence seq into a string, with
separator string.
len(string)
19 Returns the length of the string
ljust(width[, fillchar])
20 Returns a space-padded string with the original string left-justified to a total of width columns.
lower()
21 Converts all uppercase letters in string to lowercase.
22 lstrip()
Removes all leading whitespace in string.
maketrans()
23 Returns a translation table to be used in translate function.
max(str)
24 Returns the max alphabetical character from the string str.
min(str)
25 Returns the min alphabetical character from the string str.
rfind(str, beg=0,end=len(string))
27 Same as find(), but search backwards in string.
rjust(width,[, fillchar])
29 Returns a space-padded string with the original string right-justified to a total of width columns.
rstrip()
30 Removes all trailing whitespace of string.
split(str="", num=string.count(str))
31 Splits string according to delimiter str (space if not provided) and returns list of substrings; split into
at most num substrings if given.
splitlines( num=string.count('\n'))
32 Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
startswith(str, beg=0,end=len(string))
33 Determines if string or a substring of string (if starting index beg and ending index end are given)
starts with substring str; returns true if so and false otherwise.
strip([chars])
34 Performs both lstrip() and rstrip() on string.
swapcase()
35 Inverts case for all letters in string.
36 title()
Returns "titlecased" version of string, that is, all words begin with uppercase and the rest are
lowercase.
translate(table, deletechars="")
37 Translates string according to translation table str(256 chars), removing those in the del string.
upper()
38 Converts lowercase letters in string to uppercase.
zfill (width)
39 Returns original string leftpadded with zeros to a total of width characters; intended for numbers,
zfill() retains any sign given (less one zero).
isdecimal()
40 Returns true if a unicode string contains only decimal characters and false otherwise.
Python - Lists
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its
position or index. The first index is zero, the second index is one, and so forth.
Python has six built-in types of sequences, but the most common ones are lists and tuples, which we would see
in this tutorial.
There are certain things you can do with all sequence types. These operations include indexing, slicing, adding,
multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a
sequence and for finding its largest and smallest elements.
Python Lists
The list is a most versatile datatype available in Python which can be written as a list of comma-separated values
(items) between square brackets. Important thing about a list is that items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For example −
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
#!/usr/bin/python
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Updating Lists
You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment
operator, and you can add to elements in a list with the append() method. For example −
#!/usr/bin/python
#!/usr/bin/python
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1
cmp(list1, list2)
1 Compares elements of both lists.
len(list)
2 Gives the total length of the list.
max(list)
3 Returns item from the list with max value.
min(list)
4 Returns item from the list with min value.
5 list(seq)
Converts a tuple into list.
Python includes following list methods
list.append(obj)
1 Appends object obj to list
list.count(obj)
2 Returns count of how many times obj occurs in list
list.extend(seq)
3 Appends the contents of seq to list
list.index(obj)
4 Returns the lowest index in list that obj appears
list.insert(index, obj)
5 Inserts object obj into list at offset index
list.pop(obj=list[-1])
6 Removes and returns last object or obj from list
list.remove(obj)
7 Removes object obj from list
list.reverse()
8 Reverses objects of list in place
list.sort([func])
9 Sorts objects of list, use compare func if given
Python - Tuples
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between
tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use
square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-
separated values between parentheses also. For example −
To write a tuple containing a single value you have to include a comma, even though there is only one value −
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
#!/usr/bin/python
tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)
Updating Tuples
Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to
take portions of existing tuples to create new tuples as the following example demonstrates −
#!/usr/bin/python
To explicitly remove an entire tuple, just use the del statement. For example −
#!/usr/bin/python
This produces the following result. Note an exception raised, this is because after del tup tuple does not exist
any more −
In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter −
#!/usr/bin/python
cmp(tuple1, tuple2)
1 Compares elements of both tuples.
len(tuple)
2 Gives the total length of the tuple.
max(tuple)
3 Returns item from the tuple with max value.
min(tuple)
4 Returns item from the tuple with min value.
tuple(seq)
5 Converts a list into tuple.
Python - Dictionary
Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is
enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but
the keys must be of an immutable data type such as strings, numbers, or tuples.
dict['Name']: Zara
dict['Age']: 7
If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows −
#!/usr/bin/python
dict['Alice']:
Traceback (most recent call last):
File "test.py", line 4, in <module>
print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
Updating Dictionary
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an
existing entry as shown below in the simple example −
#!/usr/bin/python
dict['Age']: 8
dict['School']: DPS School
To explicitly remove an entire dictionary, just use the del statement. Following is a simple example −
#!/usr/bin/python
This produces the following result. Note that an exception is raised because after del dict dictionary does not
exist any more −
dict['Age']:
Traceback (most recent call last):
File "test.py", line 8, in <module>
print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable
(a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys
encountered during assignment, the last assignment wins. For example −
#!/usr/bin/python
dict['Name']: Manni
(b) Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but
something like ['key'] is not allowed. Following is a simple example −
#!/usr/bin/python
cmp(dict1, dict2)
1 Compares elements of both dict.
len(dict)
2 Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.
str(dict)
3 Produces a printable string representation of a dictionary
type(variable)
4 Returns the type of the passed variable. If passed variable is dictionary, then it would return a
dictionary type.
dict.clear()
1 Removes all elements of dictionary dict
dict.copy()
2 Returns a shallow copy of dictionary dict
dict.fromkeys()
3 Create a new dictionary with keys from seq and values set to value.
dict.get(key, default=None)
4 For key key, returns value or default if key not in dictionary
dict.has_key(key)
5 Returns true if key in dictionary dict, false otherwise
dict.items()
6 Returns a list of dict's (key, value) tuple pairs
dict.keys()
7 Returns list of dictionary dict's keys
8 dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict
dict.update(dict2)
9 Adds dictionary dict2's key-values pairs to dict
dict.values()
10 Returns list of dictionary dict's values
What is Tick?
Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds
since 12:00am, January 1, 1970(epoch).
There is a popular time module available in Python which provides functions for working with times, and for
converting between representations. The function time.time() returns the current system time in ticks since
12:00am, January 1, 1970(epoch).
Example
#!/usr/bin/python
import time; # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks
Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form.
Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038 for UNIX and
Windows.
What is TimeTuple?
Many of Python's time functions handle time as a tuple of 9 numbers, as shown below −
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
The above tuple is equivalent to struct_time structure. This structure has following attributes −
0 tm_year 2008
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
6 tm_wday 0 to 6 (0 is Monday)
#!/usr/bin/python
import time;
localtime = time.localtime(time.time())
print "Local current time :", localtime
This would produce the following result, which could be formatted in any other presentable form −
#!/usr/bin/python
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime
#!/usr/bin/python
import calendar
cal = calendar.month(2008, 1)
print "Here is the calendar:"
print cal
time.altzone
The offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the
1 local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if daylight
is nonzero.
time.asctime([tupletime])
2 Accepts a time-tuple and returns a readable 24-character string such as 'Tue Dec 11 18:07:14 2008'.
time.clock( )
3 Returns the current CPU time as a floating-point number of seconds. To measure computational costs
of different approaches, the value of time.clock is more useful than that of time.time().
4 time.ctime([secs])
Like asctime(localtime(secs)) and without arguments is like asctime( )
time.gmtime([secs])
5 Accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the UTC
time. Note : t.tm_isdst is always 0
time.localtime([secs])
6 Accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the local time
(t.tm_isdst is 0 or 1, depending on whether DST applies to instant secs by local rules).
time.mktime(tupletime)
7 Accepts an instant expressed as a time-tuple in local time and returns a floating-point value with the
instant expressed in seconds since the epoch.
time.sleep(secs)
8 Suspends the calling thread for secs seconds.
time.strftime(fmt[,tupletime])
9 Accepts an instant expressed as a time-tuple in local time and returns a string representing the instant
as specified by string fmt.
time.time( )
11 Returns the current time instant, a floating-point number of seconds since the epoch.
time.tzset()
12 Resets the time conversion rules used by the library routines. The environment variable TZ specifies
how this is done.
There are following two important attributes available with time module −
time.timezone
1 Attribute time.timezone is the offset in seconds of the local time zone (without DST) from UTC (>0 in
the Americas; <=0 in most of Europe, Asia, Africa).
2 time.tzname
Attribute time.tzname is a pair of locale-dependent strings, which are the names of the local time zone
without and with DST, respectively.
The calendar Module
The calendar module supplies calendar-related functions, including functions to print a text calendar for a given
month or year.
By default, calendar takes Monday as the first day of the week and Sunday as the last one. To change this, call
calendar.setfirstweekday() function.
calendar.calendar(year,w=2,l=1,c=6)
Returns a multiline string with a calendar for year year formatted into three columns separated by c
1
spaces. w is the width in characters of each date; each line has length 21*w+18+2*c. l is the number
of lines for each week.
calendar.firstweekday( )
2 Returns the current setting for the weekday that starts each week. By default, when calendar is first
imported, this is 0, meaning Monday.
calendar.isleap(year)
3
Returns True if year is a leap year; otherwise, False.
calendar.leapdays(y1,y2)
4
Returns the total number of leap days in the years within range(y1,y2).
calendar.month(year,month,w=2,l=1)
Returns a multiline string with a calendar for month month of year year, one line per week plus two
5
header lines. w is the width in characters of each date; each line has length 7*w+6. l is the number of
lines for each week.
calendar.monthcalendar(year,month)
6 Returns a list of lists of ints. Each sublist denotes a week. Days outside month month of year year are
set to 0; days within the month are set to their day-of-month, 1 and up.
calendar.monthrange(year,month)
Returns two integers. The first one is the code of the weekday for the first day of the month month in
7
year year; the second one is the number of days in the month. Weekday codes are 0 (Monday) to 6
(Sunday); month numbers are 1 to 12.
8 calendar.prcal(year,w=2,l=1,c=6)
Like print calendar.calendar(year,w,l,c).
calendar.prmonth(year,month,w=2,l=1)
9
Like print calendar.month(year,month,w,l).
calendar.setfirstweekday(weekday)
10 Sets the first day of each week to weekday code weekday. Weekday codes are 0 (Monday) to 6
(Sunday).
calendar.timegm(tupletime)
11 The inverse of time.gmtime: accepts a time instant in time-tuple form and returns the same instant as
a floating-point number of seconds since the epoch.
calendar.weekday(year,month,day)
12 Returns the weekday code for the given date. Weekday codes are 0 (Monday) to 6 (Sunday); month
numbers are 1 (January) to 12 (December).
Python - Functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions
provide better modularity for your application and a high degree of code reusing.
As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own
functions. These functions are called user-defined functions.
Defining a Function
You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the function
or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to the caller. A
return statement with no arguments is the same as return None.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior and you need to inform them in the same order that they were
defined.
Example
The following function takes a string as input parameter and prints it on standard screen.
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be included in the function and
structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly
from the Python prompt. Following is the example to call printme() function −
#!/usr/bin/python
#!/usr/bin/python
Here, we are maintaining reference of the passed object and appending values in the same object. So, this would
produce the following result −
There is one more example where argument is being passed by reference and the reference is being overwritten
inside the called function.
#!/usr/bin/python
The parameter mylist is local to the function changeme. Changing mylist within the function does not affect mylist.
The function accomplishes nothing and finally this would produce the following result −
Function Arguments
You can call a function by using the following types of formal arguments −
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the number of
arguments in the function call should match exactly with the function definition.
To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error as
follows −
#!/usr/bin/python
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the
caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able to use the
keywords provided to match the values with parameters. You can also make keyword calls to the printme()
function in the following ways −
#!/usr/bin/python
My string
The following example gives more clear picture. Note that the order of parameters does not matter.
#!/usr/bin/python
Name: miki
Age 50
Default arguments
A default argument is an argument that assumes a default value if a value is not provided in the function call for
that argument. The following example gives an idea on default arguments, it prints default age if it is not passed −
#!/usr/bin/python
Name: miki
Age 50
Name: miki
Age 35
Variable-length arguments
You may need to process a function for more arguments than you specified while defining the function. These
arguments are called variable-length arguments and are not named in the function definition, unlike required and
default arguments.
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments.
This tuple remains empty if no additional arguments are specified during the function call. Following is a simple
example −
#!/usr/bin/python
# Function definition is here
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print "Output is: "
print arg1
for var in vartuple:
print var
return;
Output is:
10
Output is:
70
60
50
Lambda forms can take any number of arguments but return just one value in the form of an expression.
They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression
Lambda functions have their own local namespace and cannot access variables other than those in their
parameter list and those in the global namespace.
Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline
statements in C or C++, whose purpose is by passing function stack allocation during invocation for
performance reasons.
Syntax
The syntax of lambda functions contains only a single statement, which is as follows −
#!/usr/bin/python
All the above examples are not returning any value. You can return a value from a function as follows −
#!/usr/bin/python
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This depends on where you
have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular identifier. There
are two basic scopes of variables in Python −
Global variables
Local variables
This means that local variables can be accessed only inside the function in which they are declared, whereas
global variables can be accessed throughout the program body by all functions. When you call a function, the
variables declared inside it are brought into scope. Following is a simple example −
#!/usr/bin/python
Python - Modules
A module allows you to logically organize your Python code. Grouping related code into a module makes the
code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can
bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A
module can also include runnable code.
Example
The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a
simple module, support.py
When the interpreter encounters an import statement, it imports the module if the module is present in the search
path. A search path is a list of directories that the interpreter searches before importing a module. For example, to
import the module support.py, you need to put the following command at the top of the script −
#!/usr/bin/python
Hello : Zara
A module is loaded only once, regardless of the number of times it is imported. This prevents the module
execution from happening over and over again if multiple imports occur.
The from...import Statement
Python's from statement lets you import specific attributes from a module into the current namespace. The
from...import has the following syntax −
For example, to import the function fibonacci from the module fib, use the following statement −
This statement does not import the entire module fib into the current namespace; it just introduces the item
fibonacci from the module fib into the global symbol table of the importing module.
This provides an easy way to import all the items from a module into the current namespace; however, this
statement should be used sparingly.
Locating Modules
When you import a module, the Python interpreter searches for the module in the following sequences −
If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
If all else fails, Python checks the default path. On UNIX, this default path is normally
/usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable
contains the current directory, PYTHONPATH, and the installation-dependent default.
A Python statement can access variables in a local namespace and in the global namespace. If a local and a
global variable have the same name, the local variable shadows the global variable.
Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions.
Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned
a value in a function is local.
Therefore, in order to assign a value to a global variable within a function, you must first use the global statement.
The statement global VarName tells Python that VarName is a global variable. Python stops searching the local
namespace for the variable.
For example, we define a variable Money in the global namespace. Within the function Money, we assign Money
a value, therefore Python assumes Money as a local variable. However, we accessed the value of the local
variable Money before setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes
the problem.
#!/usr/bin/python
Money = 2000
def AddMoney():
# Uncomment the following line to fix the code:
# global Money
Money = Money + 1
print Money
AddMoney()
print Money
The list contains the names of all the modules, variables and functions that are defined in a module. Following is
a simple example −
#!/usr/bin/python
content = dir(math)
print content
If locals() is called from within a function, it will return all the names that can be accessed locally from that
function.
If globals() is called from within a function, it will return all the names that can be accessed globally from that
function.
The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function. The reload()
function imports a previously imported module again. The syntax of the reload() function is this −
reload(module_name)
Here, module_name is the name of the module you want to reload and not the string containing the module
name. For example, to reload hello module, do the following −
reload(hello)
Packages in Python
A package is a hierarchical file directory structure that defines a single Python application environment that
consists of modules and subpackages and sub-subpackages, and so on.
Consider a file Pots.py available in Phone directory. This file has following line of source code −
#!/usr/bin/python
def Pots():
print "I'm Pots Phone"
Similar way, we have another two files having different functions with the same name as above −
Phone/__init__.py
To make all of your functions available when you've imported Phone, you need to put explicit import statements in
__init__.py as follows −
from Pots import Pots
from Isdn import Isdn
from G3 import G3
After you add these lines to __init__.py, you have all of these classes available when you import the Phone
package.
#!/usr/bin/python
Phone.Pots()
Phone.Isdn()
Phone.G3()
In the above example, we have taken example of a single functions in each file, but you can keep multiple
functions in your files. You can also define different Python classes in those files and then you can create your
packages out of those classes.
#!/usr/bin/python
raw_input
input
The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the
trailing newline).
#!/usr/bin/python
This prompts you to enter any string and it would display same string on the screen. When I typed "Hello
Python!", its output is like this −
#!/usr/bin/python
This would produce the following result against the entered input −
Python provides basic functions and methods necessary to manipulate files by default. You can do most of the
file manipulation using a file object.
Syntax
file object = open(file_name [, access_mode][, buffering])
file_name − The file_name argument is a string value that contains the name of the file that you want to
access.
access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read,
write, append, etc. A complete list of possible values is given below in the table. This is optional
parameter and the default file access mode is read (r).
buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line
buffering is performed while accessing a file. If you specify the buffering value as an integer greater than
1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the
system default(default behavior).
1 Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default
mode.
rb
2 Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
This is the default mode.
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+
4 Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of
the file.
5 Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new
file for writing.
wb
6 Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.
w+
7 Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does
not exist, creates a new file for reading and writing.
8 wb+
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If
the file does not exist, creates a new file for reading and writing.
9 Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in
the append mode. If the file does not exist, it creates a new file for writing.
ab
10 Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists.
11
The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if
12
the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
file.closed
1
Returns true if file is closed, false otherwise.
file.mode
2
Returns access mode with which file was opened.
file.name
3
Returns name of the file.
file.softspace
4
Returns false if space explicitly required with print, true otherwise.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file.
Syntax
fileObject.close();
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
Syntax
fileObject.write(string);
Here, passed parameter is the content to be written into the opened file.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n");
The above method would create foo.txt file and would write given content in that file and finally it would close that
file. If you would open this file, it would have following content.
Syntax
fileObject.read([count]);
Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading from
the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of
file.
Example
Let's take a file foo.txt, which we created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
File Positions
The tell() method tells you the current position within the file; in other words, the next read or write will occur at
that many bytes from the beginning of the file.
The seek(offset[, from]) method changes the current file position. The offset argument indicates the number of
bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved.
If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current
position as the reference position and if it is set to 2 then the end of the file would be taken as the reference
position.
Example
Let us take a file foo.txt, which we created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
To use this module you need to import it first and then you can call any related functions.
Syntax
os.rename(current_file_name, new_file_name)
Example
Following is the example to rename an existing file test1.txt −
#!/usr/bin/python
import os
Syntax
os.remove(file_name)
Example
Following is the example to delete an existing file test2.txt −
#!/usr/bin/python
import os
Directories in Python
All files are contained within various directories, and Python has no problem handling these too. The os module
has several methods that help you create, remove, and change directories.
Syntax
os.mkdir("newdir")
Example
Following is the example to create a directory test in the current directory −
#!/usr/bin/python
import os
Syntax
os.chdir("newdir")
Example
Following is the example to go into "/home/newdir" directory −
#!/usr/bin/python
import os
Syntax
os.getcwd()
Example
Following is the example to give current directory −
#!/usr/bin/python
import os
Syntax
os.rmdir('dirname')
Example
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified name of the directory,
otherwise it would search for that directory in the current directory.
#!/usr/bin/python
import os
File Object Methods : The file object provides functions to manipulate files.
OS Object Methods : This provides methods to process files as well as directories.
Exception Handling − This would be covered in this tutorial. Here is a list standard Exceptions
available in Python: Standard Exceptions .
Exception
1
Base class for all exceptions
StopIteration
2
Raised when the next() method of an iterator does not point to any object.
SystemExit
3
Raised by the sys.exit() function.
StandardError
4
Base class for all built-in exceptions except StopIteration and SystemExit.
ArithmeticError
5
Base class for all errors that occur for numeric calculation.
OverflowError
6
Raised when a calculation exceeds maximum limit for a numeric type.
FloatingPointError
7
Raised when a floating point calculation fails.
ZeroDivisionError
8
Raised when division or modulo by zero takes place for all numeric types.
AssertionError
9
Raised in case of failure of the Assert statement.
10 AttributeError
EOFError
11 Raised when there is no input from either the raw_input() or input() function and the end of file is
reached.
ImportError
12
Raised when an import statement fails.
KeyboardInterrupt
13
Raised when the user interrupts program execution, usually by pressing Ctrl+c.
LookupError
14
Base class for all lookup errors.
IndexError
15
Raised when an index is not found in a sequence.
KeyError
16
Raised when the specified key is not found in the dictionary.
NameError
17
Raised when an identifier is not found in the local or global namespace.
UnboundLocalError
18 Raised when trying to access a local variable in a function or method but no value has been assigned
to it.
EnvironmentError
19
Base class for all exceptions that occur outside the Python environment.
IOError
20 Raised when an input/ output operation fails, such as the print statement or the open() function when
trying to open a file that does not exist.
21 IOError
IndentationError
23
Raised when indentation is not specified properly.
SystemError
24 Raised when the interpreter finds an internal problem, but when this error is encountered the Python
interpreter does not exit.
SystemExit
25 Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code,
causes the interpreter to exit.
TypeError
26
Raised when an operation or function is attempted that is invalid for the specified data type.
ValueError
27 Raised when the built-in function for a data type has the valid type of arguments, but the arguments
have invalid values specified.
RuntimeError
28
Raised when a generated error does not fall into any category.
NotImplementedError
29 Raised when an abstract method that needs to be implemented in an inherited class is not actually
implemented.
Assertions in Python
An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the
program.
The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more accurate, a raise-if-not
statement). An expression is tested, and if the result comes up false, an exception is raised.
Assertions are carried out by the assert statement, the newest keyword to Python, introduced in version 1.5.
Programmers often place assertions at the start of a function to check for valid input, and after a function call to
check for valid output.
If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError
exceptions can be caught and handled like any other exception using the try-except statement, but if not handled,
they will terminate the program and produce a traceback.
Example
Here is a function that converts a temperature from degrees Kelvin to degrees Fahrenheit. Since zero degrees
Kelvin is as cold as it gets, the function bails out if it sees a negative temperature −
#!/usr/bin/python
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)
32.0
451
Traceback (most recent call last):
File "test.py", line 9, in <module>
print KelvinToFahrenheit(-5)
File "test.py", line 4, in KelvinToFahrenheit
assert (Temperature >= 0),"Colder than absolute zero!"
AssertionError: Colder than absolute zero!
What is Exception?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the
program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises
an exception. An exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates
and quits.
Handling an exception
If you have some suspicious code that may raise an exception, you can defend your program by placing the
suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code
which handles the problem as elegantly as possible.
Syntax
Here is simple syntax of try....except...else blocks −
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
A single try statement can have multiple except statements. This is useful when the try block contains
statements that may throw different types of exceptions.
You can also provide a generic except clause, which handles any exception.
After the except clause(s), you can include an else-clause. The code in the else-block executes if the
code in the try: block does not raise an exception.
The else-block is a good place for code that does not need the try: block's protection.
Example
This example opens a file, writes content in the, file and comes out gracefully because there is no problem at all −
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
fh.close()
Example
This example tries to open a file where you do not have write permission, so it raises an exception −
#!/usr/bin/python
try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.
This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-except statement
is not considered a good programming practice though, because it catches all exceptions but does not make the
programmer identify the root cause of the problem that may occur.
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
You cannot use else clause as well along with a finally clause.
Example
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can\'t find file or read data"
If you do not have permission to open the file in writing mode, then this will produce the following result −
#!/usr/bin/python
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print "Going to close the file"
fh.close()
except IOError:
print "Error: can\'t find file or read data"
When an exception is thrown in the try block, the execution immediately passes to the finally block. After all the
statements in the finally block are executed, the exception is raised again and is handled in the except
statements if present in the next higher layer of the try-except statement.
Argument of an Exception
An exception can have an argument, which is a value that gives additional information about the problem. The
contents of the argument vary by exception. You capture an exception's argument by supplying a variable in the
except clause as follows −
try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...
If you write the code to handle a single exception, you can have a variable follow the name of the exception in the
except statement. If you are trapping multiple exceptions, you can have a variable follow the tuple of the
exception.
This variable receives the value of the exception mostly containing the cause of the exception. The variable can
receive a single value or multiple values in the form of a tuple. This tuple usually contains the error string, the
error number, and an error location.
Example
Following is an example for a single exception −
#!/usr/bin/python
Raising an Exceptions
You can raise exceptions in several ways by using the raise statement. The general syntax for the raise
statement is as follows.
Syntax
raise [Exception [, args [, traceback]]]
Here, Exception is the type of exception (for example, NameError) and argument is a value for the exception
argument. The argument is optional; if not supplied, the exception argument is None.
The final argument, traceback, is also optional (and rarely used in practice), and if present, is the traceback object
used for the exception.
Example
An exception can be a string, a class or an object. Most of the exceptions that the Python core raises are classes,
with an argument that is an instance of the class. Defining new exceptions is quite easy and can be done as
follows −
Note: In order to catch an exception, an "except" clause must refer to the same exception thrown either class
object or simple string. For example, to capture above exception, we must write the except clause as follows −
try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...
User-Defined Exceptions
Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions.
Here is an example related to RuntimeError. Here, a class is created that is subclassed from RuntimeError. This
is useful when you need to display more specific information when an exception is caught.
In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to
create an instance of the class Networkerror.
class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg
So once you defined above class, you can raise the exception as follows −
try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args
If you do not have any previous experience with object-oriented (OO) programming, you may want to consult an
introductory course on it or at least a tutorial of some sort so that you have a grasp of the basic concepts.
However, here is small introduction of Object-Oriented Programming (OOP) to bring you at speed −
Class variable − A variable that is shared by all instances of a class. Class variables are defined within
a class but outside any of the class's methods. Class variables are not used as frequently as instance
variables are.
Data member − A class variable or instance variable that holds data associated with a class and its
objects.
Function overloading − The assignment of more than one behavior to a particular function. The
operation performed varies by the types of objects or arguments involved.
Instance variable − A variable that is defined inside a method and belongs only to the current instance
of a class.
Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
Instance − An individual object of a certain class. An object obj that belongs to a class Circle, for
example, is an instance of the class Circle.
Object − A unique instance of a data structure that's defined by its class. An object comprises both data
members (class variables and instance variables) and methods.
Operator overloading − The assignment of more than one function to a particular operator.
Creating Classes
The class statement creates a new class definition. The name of the class immediately follows the keyword class
followed by a colon as follows −
class ClassName:
'Optional class documentation string'
class_suite
The class has a documentation string, which can be accessed via ClassName.__doc__.
The class_suite consists of all the component statements defining class members, data attributes and
functions.
Example
Following is the example of a simple Python class −
class Employee:
'Common base class for all employees'
empCount = 0
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
The variable empCount is a class variable whose value is shared among all instances of a this class.
This can be accessed as Employee.empCount from inside the class or outside the class.
The first method __init__() is a special method, which is called class constructor or initialization method
that Python calls when you create a new instance of this class.
You declare other class methods like normal functions with the exception that the first argument to each
method is self. Python adds the self argument to the list for you; you do not need to include it when you
call the methods.
Accessing Attributes
You access the object's attributes using the dot operator with object. Class variable would be accessed using
class name as follows −
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
#!/usr/bin/python
class Employee:
'Common base class for all employees'
empCount = 0
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
You can add, remove, or modify attributes of classes and objects at any time −
Instead of using the normal statements to access attributes, you can use the following functions −
The setattr(obj,name,value) − to set an attribute. If attribute does not exist, then it would be created.
__module__ − Module name in which the class is defined. This attribute is "__main__" in interactive
mode.
__bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the
base class list.
For the above class let us try to access all these attributes −
#!/usr/bin/python
class Employee:
'Common base class for all employees'
empCount = 0
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
Python's garbage collector runs during program execution and is triggered when an object's reference count
reaches zero. An object's reference count changes as the number of aliases that point to it changes.
An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or
dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or its
reference goes out of scope. When an object's reference count reaches zero, Python collects it automatically.
You normally will not notice when the garbage collector destroys an orphaned instance and reclaims its space.
But a class can implement the special method __del__(), called a destructor, that is invoked when the instance is
about to be destroyed. This method might be used to clean up any non memory resources used by an instance.
Example
This __del__() destructor prints the class name of an instance that is about to be destroyed −
#!/usr/bin/python
class Point:
def __init__( self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "destroyed"
pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3
Note − Ideally, you should define your classes in separate file, then you should import them in your main program
file using import statement.
Class Inheritance
Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent
class in parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined
in the child class. A child class can also override data members and methods from the parent.
Syntax
Derived classes are declared much like their parent class; however, a list of base classes to inherit from is given
after the class name −
Example
#!/usr/bin/python
def parentMethod(self):
print 'Calling parent method'
def getAttr(self):
print "Parent attribute :", Parent.parentAttr
def childMethod(self):
print 'Calling child method'
Similar way, you can drive a class from multiple parent classes as follows −
You can use issubclass() or isinstance() functions to check a relationships of two classes and instances.
The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass
of the superclass sup.
The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an
instance of a subclass of Class
Overriding Methods
You can always override your parent class methods. One reason for overriding parent's methods is because you
may want special or different functionality in your subclass.
Example
#!/usr/bin/python
__del__( self )
__str__( self )
__cmp__ ( self, x )
5 Object comparison
Overloading Operators
Suppose you have created a Vector class to represent two-dimensional vectors, what happens when you use the
plus operator to add them? Most likely Python will yell at you.
You could, however, define the __add__ method in your class to perform vector addition and then the plus
operator would behave as per expectation −
Example
#!/usr/bin/python
class Vector:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
def __add__(self,other):
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2
Vector(7,8)
Data Hiding
An object's attributes may or may not be visible outside the class definition. You need to name attributes with a
double underscore prefix, and those attributes then are not be directly visible to outsiders.
Example
#!/usr/bin/python
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount
1
2
Traceback (most recent call last):
File "test.py", line 12, in <module>
print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'
Python protects those members by internally changing the name to include the class name. You can access such
attributes as object._className__attrName. If you would replace your last line as following, then it works for you
−
.........................
print counter._JustCounter__secretCount
1
2
2
The module re provides full support for Perl-like regular expressions in Python. The re module raises the
exception re.error if an error occurs while compiling or using a regular expression.
We would cover two important functions, which would be used to handle regular expressions. But a small thing
first: There are various characters, which would have special meaning when they are used in regular expression.
To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
pattern
1
This is the regular expression to be matched.
string
2
This is the string, which would be searched to match the pattern at the beginning of string.
flags
3 You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the
table below.
The re.match function returns a match object on success, None on failure. We usegroup(num) or groups()
function of match object to get matched expression.
group(num=0)
1
This method returns entire match (or specific subgroup num)
groups()
2
This method returns all matching subgroups in a tuple (empty if there weren't any)
Example
#!/usr/bin/python
import re
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"
pattern
1
This is the regular expression to be matched.
string
2
This is the string, which would be searched to match the pattern anywhere in the string.
flags
3 You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the
table below.
The re.search function returns a match object on success, none on failure. We use group(num) or groups()
function of match object to get matched expression.
group(num=0)
1
This method returns entire match (or specific subgroup num)
groups()
2
This method returns all matching subgroups in a tuple (empty if there weren't any)
Example
#!/usr/bin/python
import re
if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"
When the above code is executed, it produces following result −
Example
#!/usr/bin/python
import re
No match!!
search --> matchObj.group() : dogs
Syntax
re.sub(pattern, repl, string, max=0)
This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max
provided. This method returns modified string.
Example
#!/usr/bin/python
import re
re.I
1
Performs case-insensitive matching.
re.L
2 Interprets words according to the current locale. This interpretation affects the alphabetic group (\w
and \W), as well as word boundary behavior(\b and \B).
re.M
3 Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any
line (not just the start of the string).
re.S
4
Makes a period (dot) match any character, including a newline.
re.U
5 Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b,
\B.
re.X
6 Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when
escaped by a backslash) and treats unescaped # as a comment marker.
^
1
Matches beginning of line.
$
2
Matches end of line.
.
3
Matches any single character except newline. Using m option allows it to match newline as well.
[...]
4
Matches any single character in brackets.
[^...]
5
Matches any single character not in brackets
re*
6
Matches 0 or more occurrences of preceding expression.
re+
7
Matches 1 or more occurrence of preceding expression.
re?
8
Matches 0 or 1 occurrence of preceding expression.
re{ n}
9
Matches exactly n number of occurrences of preceding expression.
re{ n,}
10
Matches n or more occurrences of preceding expression.
re{ n, m}
11
Matches at least n and at most m occurrences of preceding expression.
12 a| b
Matches either a or b.
(re)
13
Groups regular expressions and remembers matched text.
(?imx)
(?-imx)
15 Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that
area is affected.
(?: re)
16
Groups regular expressions without remembering matched text.
(?imx: re)
17
Temporarily toggles on i, m, or x options within parentheses.
(?-imx: re)
18
Temporarily toggles off i, m, or x options within parentheses.
(?#...)
19
Comment.
(?= re)
20
Specifies position using a pattern. Doesn't have a range.
(?! re)
21
Specifies position using pattern negation. Doesn't have a range.
(?> re)
22
Matches independent pattern without backtracking.
\w
23
Matches word characters.
\W
24
Matches nonword characters.
25 \s
\S
26
Matches nonwhitespace.
\d
27
Matches digits. Equivalent to [0-9].
\D
28
Matches nondigits.
\A
29
Matches beginning of string.
\Z
30
Matches end of string. If a newline exists, it matches just before newline.
\z
31
Matches end of string.
\G
32
Matches point where last match finished.
\b
33 Matches word boundaries when outside brackets. Matches backspace (0x08) when inside
brackets.
\B
34
Matches nonword boundaries.
\1...\9
36
Matches nth grouped subexpression.
37 \10
Matches nth grouped subexpression if it matched already. Otherwise refers to the octal
representation of a character code.
python
1
Match "python".
Character classes
Sr.No. Example & Description
[Pp]ython
1
Match "Python" or "python"
rub[ye]
2
Match "ruby" or "rube"
[aeiou]
3
Match any one lowercase vowel
[0-9]
4
Match any digit; same as [0123456789]
[a-z]
5
Match any lowercase ASCII letter
[A-Z]
6
Match any uppercase ASCII letter
[a-zA-Z0-9]
7
Match any of the above
[^aeiou]
8
Match anything other than a lowercase vowel
9 [^0-9]
.
1
Match any character except newline
\d
2
Match a digit: [0-9]
\D
3
Match a nondigit: [^0-9]
\s
4
Match a whitespace character: [ \t\r\n\f]
\S
5
Match nonwhitespace: [^ \t\r\n\f]
\w
6
Match a single word character: [A-Za-z0-9_]
\W
7
Match a nonword character: [^A-Za-z0-9_]
Repetition Cases
Sr.No. Example & Description
ruby?
1
Match "rub" or "ruby": the y is optional
ruby*
2
Match "rub" plus 0 or more ys
3 ruby+
\d{3}
4
Match exactly 3 digits
\d{3,}
5
Match 3 or more digits
\d{3,5}
6
Match 3, 4, or 5 digits
Nongreedy repetition
This matches the smallest number of repetitions −
<.*>
1
Greedy repetition: matches "<python>perl>"
<.*?>
2
Nongreedy: matches "<python>" in "<python>perl>"
\D\d+
1
No group: + repeats \d
(\D\d)+
2
Grouped: + repeats \D\d pair
([Pp]ython(, )?)+
3
Match "Python", "Python, python, python", etc.
Backreferences
This matches a previously matched group again −
([Pp])ython&\1ails
1
Match python&pails or Python&Pails
(['"])[^\1]*\1
2 Single or double-quoted string. \1 matches whatever the 1st group matched. \2 matches whatever the
2nd group matched, etc.
Alternatives
Sr.No. Example & Description
python|perl
1
Match "python" or "perl"
rub(y|le))
2
Match "ruby" or "ruble"
Python(!+|\?)
3
"Python" followed by one or more ! or one ?
Anchors
This needs to specify match position.
^Python
1
Match "Python" at the start of a string or internal line
Python$
2
Match "Python" at the end of a string or line
\APython
3
Match "Python" at the start of a string
4 Python\Z
Match "Python" at the end of a string
\bPython\b
5
Match "Python" at a word boundary
\brub\B
6
\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone
Python(?=!)
7
Match "Python", if followed by an exclamation point.
Python(?!!)
8
Match "Python", if not followed by an exclamation point.
R(?#comment)
1
Matches "R". All the rest is a comment
R(?i)uby
2
Case-insensitive while matching "uby"
R(?i:uby)
3
Same as above
rub(?:y|le))
4
Group only without creating \1 backreference
What is CGI?
The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with
information servers such as HTTP servers.
The current version is CGI/1.1 and CGI/1.2 is under progress.
Web Browsing
To understand the concept of CGI, let us see what happens when we click a hyper link to browse a particular web
page or URL.
Your browser contacts the HTTP web server and demands for the URL, i.e., filename.
Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the
browser, otherwise sends an error message indicating that you requested a wrong file.
Web browser takes response from web server and displays either the received file or error message.
However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that
file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your
browser to display. This function is called the Common Gateway Interface or CGI and the programs are called
CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.
<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
Here, we assume that you have Web Server up and running successfully and you are able to run any other CGI
program like Perl or Shell, etc.
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'
This hello.py script is a simple Python script, which writes its output on STDOUT file, i.e., screen. There is one
important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\n. This line is
sent back to the browser and it specifies the content type to be displayed on the browser screen.
By now you must have understood basic concept of CGI and you can write many complicated CGI programs
using Python. This script can interact with any other external system also to exchange information such as
RDBMS.
HTTP Header
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to understand the
content. All the HTTP header will be in the following form −
HTTP Field Name: Field Content
For Example
Content-type: text/html\r\n\r\n
There are few other important HTTP headers, which you will use frequently in your CGI Programming.
Content-type:
1
A MIME string defining the format of the file being returned. Example is Content-type:text/html
Expires: Date
2 The date the information becomes invalid. It is used by the browser to decide when a page needs to
be refreshed. A valid date string is in the format 01 Jan 1998 12:00:00 GMT.
Location: URL
3 The URL that is returned instead of the URL requested. You can use this field to redirect a request to
any file.
Last-modified: Date
4
The date of last modification of the resource.
Content-length: N
5 The length, in bytes, of the data being returned. The browser uses this value to report the estimated
download time for a file.
Set-Cookie: String
6
Set the cookie passed through the string
CONTENT_TYPE
1 The data type of the content. Used when the client is sending attached content to the server. For
example, file upload.
2 CONTENT_LENGTH
The length of the query information. It is available only for POST requests.
HTTP_COOKIE
3
Returns the set cookies in the form of key & value pair.
HTTP_USER_AGENT
4 The User-Agent request-header field contains information about the user agent originating the
request. It is name of the web browser.
PATH_INFO
5
The path for the CGI script.
QUERY_STRING
6
The URL-encoded information that is sent with GET method request.
REMOTE_ADDR
7
The IP address of the remote host making the request. This is useful logging or for authentication.
REMOTE_HOST
8 The fully qualified name of the host making the request. If this information is not available, then
REMOTE_ADDR can be used to get IR address.
REQUEST_METHOD
9
The method used to make the request. The most common methods are GET and POST.
SCRIPT_FILENAME
10
The full path to the CGI script.
SCRIPT_NAME
11
The name of the CGI script.
SERVER_NAME
12
The server's hostname or IP Address
SERVER_SOFTWARE
13
The name and version of the software the server is running.
Here is small CGI program to list out all the CGI variables. Click this link to see the result Get Environment
#!/usr/bin/python
import os
http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2
The GET method is the default method to pass information from browser to web server and it produces a long
string that appears in your browser's Location:box. Never use GET method if you have password or other
sensitive information to pass to the server. The GET method has size limitation: only 1024 characters can be sent
in a request string. The GET method sends information using QUERY_STRING header and will be accessible in
your CGI Program through QUERY_STRING environment variable.
You can pass information by simply concatenating key and value pairs along with any URL or you can use HTML
<FORM> tags to pass information using GET method.
/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
Below is hello_get.py script to handle input given by web browser. We are going to use cgi module, which
makes it very easy to access passed information −
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
Here is the actual output of the above form, you enter First and Last Name and then click submit button to see
the result.
First Name:
Below is same hello_get.py script which handles GET as well as POST method.
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
Let us take again same example as above which passes two values using HTML FORM and submit button. We
use same CGI script hello_get.py to handle this input.
Here is the actual output of the above form. You enter First and Last Name and then click submit button to see
the result.
First Name:
Below is checkbox.cgi script to handle input given by web browser for checkbox button.
#!/usr/bin/python
if form.getvalue('physics'):
physics_flag = "ON"
else:
physics_flag = "OFF"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"
Here is example HTML code for a form with two radio buttons −
Below is radiobutton.py script to handle input given by web browser for radio button −
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Submit
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"
Here is example HTML code for a form with one drop down box −
Maths Submit
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
In many situations, using cookies is the most efficient method of remembering and tracking preferences,
purchases, commissions, and other information required for better visitor experience or site statistics.
How It Works?
Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie.
If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another
page on your site, the cookie is available for retrieval. Once retrieved, your server knows/remembers what was
stored.
Expires − The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the
browser.
Path − The path to the directory or web page that sets the cookie. This may be blank if you want to
retrieve the cookie from any directory or page.
Secure − If this field contains the word "secure", then the cookie may only be retrieved with a secure
server. If this field is blank, no such restriction exists.
Name=Value − Cookies are set and retrieved in the form of key and value pairs.
Setting up Cookies
It is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to Content-
type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done as follows −
#!/usr/bin/python
From this example, you must have understood how to set cookies. We use Set-Cookie HTTP header to set
cookies.
It is optional to set cookies attributes like Expires, Domain, and Path. It is notable that cookies are set before
sending magic line "Content-type:text/html\r\n\r\n.
Retrieving Cookies
It is very easy to retrieve all the set cookies. Cookies are stored in CGI environment variable HTTP_COOKIE and
they will have following form −
#!/usr/bin/python
if environ.has_key('HTTP_COOKIE'):
for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
(key, value ) = split(cookie, '=');
if key == "UserID":
user_id = value
if key == "Password":
password = value
This produces the following result for the cookies set by above script −
User ID = XYZ
Password = XYZ123
<html>
<body>
<form enctype = "multipart/form-data"
action = "save_file.py" method = "post">
<p>File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>
</body>
</html>
The result of this code is the following form −
Upload
Above example has been disabled intentionally to save people uploading file on our server, but you can try above
code with your server.
#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
If you run the above script on Unix/Linux, then you need to take care of replacing file separator as follows,
otherwise on your windows machine above open() statement should work fine.
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
For example, if you want make a FileName file downloadable from a given link, then its syntax is as follows −
#!/usr/bin/python
# HTTP Header
print "Content-Type:application/octet-stream; name = \"FileName\"\r\n";
print "Content-Disposition: attachment; filename = \"FileName\"\r\n\n";
# Actual File Content will go here.
fo = open("foo.txt", "rb")
str = fo.read();
print str
Hope you enjoyed this tutorial. If yes, please send me your feedback at: Contact Us
You can choose the right database for your application. Python Database API supports a wide range of database
servers such as −
GadFly
mSQL
MySQL
PostgreSQL
Informix
Interbase
Oracle
Sybase
Here is the list of available Python database interfaces: Python Database Interfaces and APIs . You must
download a separate DB API module for each database you need to access. For example, if you need to access
an Oracle database as well as a MySQL database, you must download both the Oracle and the MySQL database
modules.
The DB API provides a minimal standard for working with databases using Python structures and syntax
wherever possible. This API includes the following −
We would learn all the concepts using MySQL, so let us talk about MySQLdb module.
What is MySQLdb?
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python
Database API v2.0 and is built on top of the MySQL C API.
How do I Install MySQLdb?
Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your
Python script and execute it −
#!/usr/bin/python
import MySQLdb
If it produces the following result, then it means MySQLdb module is not installed −
Note − Make sure you have root privilege to install above module.
Database Connection
Before connecting to a MySQL database, make sure of the followings −
This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
Example
Following is the example of connecting with MySQL database "TESTDB"
#!/usr/bin/python
import MySQLdb
While running this script, it is producing the following result in my Linux machine.
If a connection is established with the datasource, then a Connection Object is returned and saved into db for
further use, otherwise db is set to None. Next, db object is used to create a cursor object, which in turn is used
to execute SQL queries. Finally, before coming out, it ensures that database connection is closed and resources
are released.
Example
Let us create Database table EMPLOYEE −
#!/usr/bin/python
import MySQLdb
cursor.execute(sql)
INSERT Operation
It is required when you want to create your records into a database table.
Example
The following example, executes SQL INSERT statement to create a record into EMPLOYEE table −
#!/usr/bin/python
import MySQLdb
#!/usr/bin/python
import MySQLdb
Example
Following code segment is another form of execution where you can pass parameters directly −
..................................
user_id = "test123"
password = "password"
READ Operation
READ Operation on any database means to fetch some useful information from the database.
Once our database connection is established, you are ready to make a query into this database. You can use
either fetchone() method to fetch single record or fetchall() method to fetech multiple values from a database
table.
fetchone() − It fetches the next row of a query result set. A result set is an object that is returned when a
cursor object is used to query a table.
fetchall() − It fetches all the rows in a result set. If some rows have already been extracted from the
result set, then it retrieves the remaining rows from the result set.
rowcount − This is a read-only attribute and returns the number of rows that were affected by an
execute() method.
Example
The following procedure queries all the records from EMPLOYEE table having salary more than 1000 −
#!/usr/bin/python
import MySQLdb
Update Operation
UPDATE Operation on any database means to update one or more records, which are already available in the
database.
The following procedure updates all the records having SEX as 'M'. Here, we increase AGE of all the males by
one year.
Example
#!/usr/bin/python
import MySQLdb
DELETE Operation
DELETE operation is required when you want to delete some records from your database. Following is the
procedure to delete all the records from EMPLOYEE where AGE is more than 20 −
Example
#!/usr/bin/python
import MySQLdb
Performing Transactions
Transactions are a mechanism that ensures data consistency. Transactions have the following four properties −
Consistency − A transaction must start in a consistent state and leave the system in a consistent state.
Isolation − Intermediate results of a transaction are not visible outside the current transaction.
Durability − Once a transaction was committed, the effects are persistent, even after a system failure.
The Python DB API 2.0 provides two methods to either commit or rollback a transaction.
Example
You already know how to implement transactions. Here is again similar example −
COMMIT Operation
Commit is the operation, which gives a green signal to database to finalize the changes, and after this operation,
no change can be reverted back.
db.commit()
ROLLBACK Operation
If you are not satisfied with one or more of the changes and you want to revert back those changes completely,
then use rollback() method.
db.rollback()
Disconnecting Database
To disconnect Database connection, use close() method.
db.close()
If the connection to a database is closed by the user with the close() method, any outstanding transactions are
rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your
application would be better off calling commit or rollback explicitly.
Handling Errors
There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a
connection failure, or calling the fetch method for an already canceled or finished statement handle.
The DB API defines a number of errors that must exist in each database module. The following table lists these
exceptions.
Warning
1
Used for non-fatal issues. Must subclass StandardError.
Error
2
Base class for errors. Must subclass StandardError.
InterfaceError
3
Used for errors in the database module, not the database itself. Must subclass Error.
DatabaseError
4
Used for errors in the database. Must subclass Error.
DataError
5
Subclass of DatabaseError that refers to errors in the data.
OperationalError
6 Subclass of DatabaseError that refers to errors such as the loss of a connection to the database.
These errors are generally outside of the control of the Python scripter.
IntegrityError
7 Subclass of DatabaseError for situations that would damage the relational integrity, such as
uniqueness constraints or foreign keys.
8
InternalError
Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor
no longer being active.
ProgrammingError
9 Subclass of DatabaseError that refers to errors such as a bad table name and other things that can
safely be blamed on you.
NotSupportedError
10
Subclass of DatabaseError that refers to trying to call unsupported functionality.
Your Python scripts should handle these errors, but before using any of the above exceptions, make sure your
MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0
specification.
Python also has libraries that provide higher-level access to specific application-level network protocols, such as
FTP, HTTP, and so on.
This chapter gives you understanding on most famous concept in Networking - Socket Programming.
What is Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a
process, between processes on the same machine, or between processes on different continents.
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so
on. The socket library provides specific classes for handling the common transports as well as a generic interface
for handling the rest.
Domain
1 The family of protocols that is used as the transport mechanism. These values are constants such
as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
2 type
The type of communications between the two endpoints, typically SOCK_STREAM for connection-
oriented protocols and SOCK_DGRAM for connectionless protocols.
protocol
3
Typically zero, this may be used to identify a variant of a protocol within a domain and type.
hostname
A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon
(and possibly dot) notation
4
A string "<broadcast>", which specifies an INADDR_BROADCAST address.
port
5 Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a
string containing a port number, or the name of a service.
Once you have socket object, then you can use required functions to create your client or server program.
Following is the list of functions required −
s.bind()
1
This method binds address (hostname, port number pair) to socket.
2 s.listen()
s.connect()
1
This method actively initiates TCP server connection.
s.recv()
1
This method receives TCP message
s.send()
2
This method transmits TCP message
s.recvfrom()
3
This method receives UDP message
s.sendto()
4
This method transmits UDP message
s.close()
5
This method closes socket
socket.gethostname()
6
Returns the hostname.
A Simple Server
To write Internet servers, we use the socket function available in socket module to create a socket object. A
socket object is then used to call other functions to setup a socket server.
Now call bind(hostname, port) function to specify a port for your service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the port you
specified, and then returns a connection object that represents the connection to that client.
A Simple Client
Let us write a very simple client program which opens a connection to a given port 12345 and given host. This is
very simple to create a socket client using Python's socket module function.
The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have a
socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file.
The following code is a very simple client that connects to a given host and port, reads any available data from
the socket, and then exits −
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
Now run this server.py in background and then run above client.py to see the result.
Please check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.
Further Readings
This was a quick start with Socket Programming. It is a vast subject. It is recommended to go through the
following link to find more detail −
Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to
any Internet machine with an SMTP or ESMTP listener daemon.
Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail −
import smtplib
host − This is the host running your SMTP server. You can specify IP address of the host or a domain
name like tutorialspoint.com. This is optional argument.
port − If you are providing host argument, then you need to specify a port, where SMTP server is
listening. Usually this port would be 25.
local_hostname − If your SMTP server is running on your local machine, then you can specify just
localhost as of this option.
An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a
message. It takes three parameters −
Example
Here is a simple way to send one e-mail using Python script. Try it once −
#!/usr/bin/python
import smtplib
sender = '[email protected]'
receivers = ['[email protected]']
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers
correctly. An e-mail requires a From, To, and Subject header, separated from the body of the e-mail with a blank
line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine and then use the
sendmail method along with the message, the from address, and the destination address as parameters (even
though the from and to addresses are within the e-mail itself, these aren't always used to route mail).
If you are not running an SMTP server on your local machine, you can use smtplib client to communicate with a
remote SMTP server. Unless you are using a webmail service (such as Hotmail or Yahoo! Mail), your e-mail
provider must have provided you with outgoing mail server details that you can supply them, as follows −
smtplib.SMTP('mail.your-domain.com', 25)
While sending an e-mail message, you can specify a Mime version, content type and character set to send an
HTML e-mail.
Example
Following is the example to send HTML content as an e-mail. Try it once −
#!/usr/bin/python
import smtplib
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
A boundary is started with two hyphens followed by a unique number, which cannot appear in the message part
of the e-mail. A final boundary denoting the e-mail's final section must also end with two hyphens.
Attached files should be encoded with the pack("m") function to have base64 encoding before transmission.
Example
Following is the example, which sends a file /tmp/test.txt as an attachment. Try it once −
#!/usr/bin/python
import smtplib
import base64
filename = "/tmp/test.txt"
sender = '[email protected]'
reciever = '[email protected]'
marker = "AUNIQUEMARKER"
body ="""
This is a test email to send an attachement.
"""
# Define the main headers.
part1 = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
%s
--%s
""" % (body,marker)
%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, reciever, message)
print "Successfully sent email"
except Exception:
print "Error: unable to send email"
Multiple threads within a process share the same data space with the main thread and can therefore
share information or communicate with each other more easily than if they were separate processes.
Threads sometimes called light-weight processes and they do not require much memory overhead; they
are cheaper than processes.
A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track
of where within its context it is currently running.
It can temporarily be put on hold (also known as sleeping) while other threads are running - this is called
yielding.
This method call enables a fast and efficient way to create new threads in both Linux and Windows.
The method call returns immediately and the child thread starts and calls function with the passed list of args.
When function returns, the thread terminates.
Here, args is a tuple of arguments; use an empty tuple to call function without passing any arguments. kwargs is
an optional dictionary of keyword arguments.
Example
#!/usr/bin/python
import thread
import time
while 1:
pass
Although it is very effective for low-level threading, but the thread module is very limited compared to the newer
threading module.
The threading module exposes all the methods of the thread module and provides some additional methods −
threading.currentThread() − Returns the number of thread objects in the caller's thread control.
threading.enumerate() − Returns a list of all thread objects that are currently active.
In addition to the methods, the threading module has the Thread class that implements threading. The methods
provided by the Thread class are as follows −
start() − The start() method starts a thread by calling the run method.
Then, override the run(self [,args]) method to implement what the thread should do when started.
Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by
invoking the start(), which in turn calls run() method.
Example
#!/usr/bin/python
import threading
import time
exitFlag = 0
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2
Synchronizing Threads
The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to
synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock.
The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional
blocking parameter enables you to control whether the thread waits to acquire the lock.
If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if
the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.
The release() method of the new lock object is used to release the lock when it is no longer required.
Example
#!/usr/bin/python
import threading
import time
threadLock = threading.Lock()
threads = []
Starting Thread-1
Starting Thread-2
Thread-1: Thu Mar 21 09:11:28 2013
Thread-1: Thu Mar 21 09:11:29 2013
Thread-1: Thu Mar 21 09:11:30 2013
Thread-2: Thu Mar 21 09:11:32 2013
Thread-2: Thu Mar 21 09:11:34 2013
Thread-2: Thu Mar 21 09:11:36 2013
Exiting Main Thread
get() − The get() removes and returns an item from the queue.
qsize() − The qsize() returns the number of items that are currently in the queue.
Example
#!/usr/bin/python
import Queue
import threading
import time
exitFlag = 0
What is XML?
The Extensible Markup Language (XML) is a markup language much like HTML or SGML. This is recommended
by the World Wide Web Consortium and available as an open standard.
XML is extremely useful for keeping track of small to medium amounts of data without requiring a SQL-based
backbone.
The two most basic and broadly used APIs to XML data are the SAX and DOM interfaces.
Simple API for XML (SAX) − Here, you register callbacks for events of interest and then let the parser
proceed through the document. This is useful when your documents are large or you have memory
limitations, it parses the file as it reads it from disk and the entire file is never stored in memory.
Document Object Model (DOM) API − This is a World Wide Web Consortium recommendation wherein
the entire file is read into memory and stored in a hierarchical (tree-based) form to represent all the
features of an XML document.
SAX obviously cannot process information as fast as DOM can when working with large files. On the other hand,
using DOM exclusively can really kill your resources, especially if used on a lot of small files.
SAX is read-only, while DOM allows changes to the XML file. Since these two different APIs literally complement
each other, there is no reason why you cannot use them both for large projects.
For all our XML code examples, let's use a simple XML file movies.xml as an input −
Your ContentHandler handles the particular tags and attributes of your flavor(s) of XML. A ContentHandler object
provides methods to handle various parsing events. Its owning parser calls ContentHandler methods as it parses
the XML file.
The methods startDocument and endDocument are called at the start and the end of the XML file. The method
characters(text) is passed character data of the XML file via the parameter text.
The ContentHandler is called at the start and end of each element. If the parser is not in namespace mode, the
methods startElement(tag, attributes) and endElement(tag) are called; otherwise, the corresponding methods
startElementNS and endElementNS are called. Here, tag is the element tag, and attributes is an Attributes object.
xml.sax.make_parser( [parser_list] )
Example
#!/usr/bin/python
import xml.sax
if ( __name__ == "__main__"):
# create an XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
parser.parse("movies.xml")
*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Year: 2003
Rating: PG
Stars: 10
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Year: 1989
Rating: R
Stars: 8
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Stars: 10
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Stars: 2
Description: Viewable boredom
For a complete detail on SAX API documentation, please refer to standard Python SAX APIs .
The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the
document at a time. If you are looking at one SAX element, you have no access to another.
Here is the easiest way to quickly load an XML document and to create a minidom object using the xml.dom
module. The minidom object provides a simple parser method that quickly creates a DOM tree from the XML file.
The sample phrase calls the parse( file [,parser] ) function of the minidom object to parse the XML file designated
by file into a DOM tree object.
#!/usr/bin/python
type = movie.getElementsByTagName('type')[0]
print "Type: %s" % type.childNodes[0].data
format = movie.getElementsByTagName('format')[0]
print "Format: %s" % format.childNodes[0].data
rating = movie.getElementsByTagName('rating')[0]
print "Rating: %s" % rating.childNodes[0].data
description = movie.getElementsByTagName('description')[0]
print "Description: %s" % description.childNodes[0].data
This would produce the following result −
For a complete detail on DOM API documentation, please refer to standard Python DOM APIs .
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look this
option in this chapter.
JPython − JPython is a Python port for Java which gives Python scripts seamless access to Java class
libraries on the local machine http://www.jython.org .
There are many other interfaces available, which you can find them on the net.
Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way
to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps −
Import the Tkinter module.
Enter the main event loop to take action against each event triggered by the user.
Example
#!/usr/bin/python
import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
Tkinter Widgets
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These
controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. We present these widgets as well as a brief description in the
following table −
Button
1 The Button widget is used to display buttons in your application.
Canvas
2 The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your
application.
3 Checkbutton
The Checkbutton widget is used to display a number of options as checkboxes. The user can select
multiple options at a time.
Entry
4 The Entry widget is used to display a single-line text field for accepting values from a user.
Frame
5 The Frame widget is used as a container widget to organize other widgets.
Label
6 The Label widget is used to provide a single-line caption for other widgets. It can also contain images.
Listbox
7 The Listbox widget is used to provide a list of options to a user.
Menubutton
8 The Menubutton widget is used to display menus in your application.
Menu
9 The Menu widget is used to provide various commands to a user. These commands are contained
inside Menubutton.
Message
10 The Message widget is used to display multiline text fields for accepting values from a user.
Radiobutton
11 The Radiobutton widget is used to display a number of options as radio buttons. The user can select
only one option at a time.
Scale
12 The Scale widget is used to provide a slider widget.
Scrollbar
13 The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.
Text
14 The Text widget is used to display text in multiple lines.
Toplevel
15 The Toplevel widget is used to provide a separate window container.
Spinbox
16 The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from
a fixed number of values.
17 PanedWindow
A PanedWindow is a container widget that may contain any number of panes, arranged horizontally
or vertically.
LabelFrame
18 A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for
complex window layouts.
tkMessageBox
19 This module is used to display message boxes in your applications.
Standard attributes
Let us take a look at how some of their common attributes.such as sizes, colors and fonts are specified.
Dimensions
Colors
Fonts
Anchors
Relief styles
Bitmaps
Cursors
Geometry Management
All Tkinter widgets have access to specific geometry management methods, which have the purpose of
organizing widgets throughout the parent widget area. Tkinter exposes the following geometry manager classes:
pack, grid, and place.
The pack() Method − This geometry manager organizes widgets in blocks before placing them in the
parent widget.
The grid() Method − This geometry manager organizes widgets in a table-like structure in the parent
widget.
The place() Method − This geometry manager organizes widgets by placing them in a specific
position in the parent widget.
A Python extension module is nothing more than a normal C library. On Unix machines, these libraries usually
end in .so (for shared object). On Windows machines, you typically see .dll (for dynamically linked library).
On Unix machines, this usually requires installing a developer-specific package such as python2.5-
dev .
Windows users get these headers as part of the package when they use the binary Python installer.
Additionally, it is assumed that you have good knowledge of C or C++ to write any Python Extension using C
programming.
The C functions you want to expose as the interface from your module.
A table mapping the names of your functions as Python developers see them to C functions inside the
extension module.
An initialization function.
Make sure to include Python.h before any other headers you might need. You need to follow the includes with the
functions you want to call from Python.
The C Functions
The signatures of the C implementation of your functions always takes one of the following three forms −
Each one of the preceding declarations returns a Python object. There is no such thing as a void function in
Python as there is in C. If you do not want your functions to return a value, return the C equivalent of Python's
None value. The Python headers define a macro, Py_RETURN_NONE, that does this for us.
The names of your C functions can be whatever you like as they are never seen outside of the extension module.
They are defined as static function.
Your C functions usually are named by combining the Python module and function names together, as shown
here −
This is a Python function called func inside of the module module. You will be putting pointers to your C functions
into the method table for the module that usually comes next in your source code.
struct PyMethodDef {
char *ml_name;
PyCFunction ml_meth;
int ml_flags;
char *ml_doc;
};
ml_name − This is the name of the function as the Python interpreter presents when it is used in Python
programs.
ml_meth − This must be the address to a function that has any one of the signatures described in
previous seection.
ml_flags − This tells the interpreter which of the three signatures ml_meth is using.
This flag can be bitwise OR'ed with METH_KEYWORDS if you want to allow keyword
arguments into your function.
This can also have a value of METH_NOARGS that indicates you do not want to accept any
arguments.
ml_doc − This is the docstring for the function, which could be NULL if you do not feel like writing one.
This table needs to be terminated with a sentinel that consists of NULL and 0 values for the appropriate
members.
Example
For the above-defined function, we have following method mapping table −
The initialization function needs to be exported from the library you will be building. The Python headers define
PyMODINIT_FUNC to include the appropriate incantations for that to happen for the particular environment in
which we're compiling. All you have to do is use it when defining the function.
PyMODINIT_FUNC initModule() {
Py_InitModule3(func, module_methods, "docstring...");
}
#include <Python.h>
PyMODINIT_FUNC initModule() {
Py_InitModule3(func, module_methods, "docstring...");
}
Example
A simple example that makes use of all the above concepts −
#include <Python.h>
void inithelloworld(void) {
Py_InitModule3("helloworld", helloworld_funcs,
"Extension module example!");
}
Here the Py_BuildValue function is used to build a Python value. Save above code in hello.c file. We would see
how to compile and install this module to be called from Python script.
For the above module, you need to prepare following setup.py script −
Now, use the following command, which would perform all needed compilation and linking steps, with the right
compiler and linker commands and flags, and copies the resulting dynamic library into an appropriate directory −
On Unix-based systems, you'll most likely need to run this command as root in order to have permissions to write
to the site-packages directory. This usually is not a problem on Windows.
Importing Extensions
Once you installed your extension, you would be able to import and call that extension in your Python script as
follows −
#!/usr/bin/python
import helloworld
print helloworld.helloworld()
You can use API PyArg_ParseTuple function to extract the arguments from the one PyObject pointer passed into
your C function.
The first argument to PyArg_ParseTuple is the args argument. This is the object you will be parsing. The second
argument is a format string describing the arguments as you expect them to appear. Each argument is
represented by one or more characters in the format string as follows.
Compiling the new version of your module and importing it enables you to invoke the new function with any
number of arguments of any type −
This function returns 0 for errors, and a value not equal to 0 for success. tuple is the PyObject* that was the C
function's second argument. Here format is a C string that describes mandatory and optional arguments.
(...) as per ... A Python sequence is treated as one argument per item.
Returning Values
Py_BuildValue takes in a format string much like PyArg_ParseTuple does. Instead of passing in the addresses of
the values you are building, you pass in the actual values. Here's an example showing how to implement an add
function −
You can return two values from your function as follows, this would be cauptured using a list in Python.
Here format is a C string that describes the Python object to build. The following arguments of Py_BuildValue are
C values from which the result is built. The PyObject* result is a new reference.
Following table lists the commonly used code strings, of which zero or more are joined into string format.
{...} as per ... Builds Python dictionary from C values, alternating keys and values.
Code {...} builds dictionaries from an even number of C values, alternately keys and values. For example,
Py_BuildValue("{issi}",23,"zig","zag",42) returns a dictionary like Python's {23:'zig','zag':42}.
G Suite Referral Python Tutorial in PDF Create Your Free Python Tkinter
Program Webpage PanedWindow
Advertisements
Tutorials Point (India) Pvt. Ltd.
YouTube 127K