Reed Cartwright
Learn
Python
Programming
Write code from the ground up in a clear
and succinct method, with a complete
standard training course. From beginners
to intermediate, an hands-on task with
examples, to comply with detailedCongratulation on downloading this ebook and thank You for doing
so.
Please enjoy !
© Copyright 2021 by All rights
__teserved __No part of this publication may be reproduced, distributed, or
transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, or by any
information storage and retrieval system without the prior written
permission of the publisher, except in the case of very brief
quotations embodied in critical reviews and certain other
noncommercial uses permitted by copyright law.TABLE OF CONTENTSay | ree
| mh "ldinddddldsatll Uses doldaaasCHAPTER 1
LEARN PYTHON PROGRAMMING
INTRODUCTION
Python isan example ofa high level language. Other high level languages you might have heard
of are C+#,PHP,Pascal,C#,and Java. Python is an easy to learn, powerful programming
language. Ithas efficient high-level data structures anda simple but effective approach to abjct-
oriened programming
Python was originally conceived by Van Rossum as a hobby language in December 1989, Also,
the major and backwatd-Incompatible version of the general-purpose programming language was
released on 3rd December 2008. But Python is recently rated by a numberof surveyors asthe
‘most poplar coding language of 2015, The massive popularity indicates Python's effectiveness
asa modem programming language. At the same time, Python 3 is currently used by developers
across the worlds for creating a variety of desktop GUI, web and mobile applications.
Python is a high-level, interpreted scripting language developed in the late 1980s by Guido van
‘Rossum atthe National Research Institute for Mathematics and Computer Selence inthe
Netherlands. The inital version was published at the atsources newsgroup in 1991, and version
1.0 was released in 1994.Python 2.0 was released in 2000, andthe 2.x versions were the prevalent releases until December
2008, At that time, the development team made the decision to release version 3.0, which
‘contained a few relatively small but significant changes that were not backward compatible with
‘the 2.x versions. Python 2 and 3 are very similar, and some features of Python 3 have been
bbackported to Python 2. But in general, they remain not quite compatible
Both Python 2 and 3 have continued to be maintained and developed, with periodic release
‘updates for both, As of this writing, the most recent versions available are 2.7.15 and 3.6.
However, an official End Of Life date of January 1, 2020 has been established for Python 2, after
‘which time it will no longer be maintained. If you area newcomer to Python, its recommended
‘that you focus on Python 3, as this tutorial will do,
Python is still maintained by a core development team atthe Institute, and Guido Is still in
‘charge, having been given the ttle of BDFL (Benevolent Dictator For Life) by the Python
‘community. The name Python, by the way, derives not from the snake, but from the British
‘comedy tcoupe Monty Python's Flying Cireu, of which Guido was, and presumably stil is, a
fan. Iis common to find references to Monty Python sketches and movies scatered throughout
the Python documentation,
“There are also a numberof reasons whi the huge popularity and marketshare of Python will
remain inact over a longer period of time.REASONS WHY THE MASSIVE POPULARITY OF PYTHON WILL REMAIN
INTACT IN THE FUTURE
‘Supports Multiple Programming Paradigms
Good developers often take advantage of different programming paradigms to reduce the amount
‘of time and efforts re quired for developing large and complex applications. Like other modem
programming languages, Python also supports a number of commonly used programming styles
Including object-oriented, functional, procedural and imperative. t further features automatic
‘memory management, along with a dynamic type system. So programmers can use the language
tw effectuate development of large and complex software applications
Doesn't Require Programmers to Write Lengthy Code
Python is designed with complete focus on code readability, So the programmers can create
readable code base that can be used by members of distributed teams. At the same time, the
‘simple syntax ofthe programming language enables them to express concepts without writing
longer lines of code. The feature makes it easier for developers to large and complex applications
‘within a stipulated amount of time, As they can easly skip certain tasks required by other
programming languages, i becomes easier for developers to maintain and updae thelr
applications.
Provides a Comprehensive Standard Library
Python further scores over ther programming languages due tits extensive standard li
‘The programmers can use these libraries to accomplish a variety of tasks without writing longer
lines of code, Also, the standard library of Python is designed with a large numberof high use
Programming tasks scripted into it. Thus, it helps programmers to accomplish tasks like string
‘operations, development and implementation of web services, working with internet protocols,
‘and handling operating system interface.Effectuates Web Application Development
Python is designed as a general-purpose programming language, and lacks built-in web
development features. But the web developers use a variety of add-on modules to write modern
‘web applications in Python, While writing web applications in Python, programmers have option
to use several high-level web frameworks including Django, web2py, TurboGears, CubieWeb,
and Reabl. These web frameworks help programmers to perform a numberof operations, without
‘writing additional code, like database manipulation, URL routing, session storage and retrieval,
and output template formatting. They can futher use the web frameworks to protect the web
application from cross-site scripting attacks, SQL injection, and crosssite request forgery
Facilitates Development of High Quality GUI, Scientific and Numeric Applications
Python is currently available on major operating systems like Windows, Mac OS X, Linux and
LUNIX, So the desktop GUI applications written in the programming language can be deployed
‘on multiple platforms. The programmers can further speedup cross-platform desktop GUL
application development using frameworks like Kivy, wxPython and PyGitk. A number of reports
have highlighted that Python is used widely for development of numeric and scientific
applications. While writing scientific and numeric applications in Python, the developers can
‘ake advantage of tools like Scipy, Pandas, IPython, along with the Python Imaging Library
Simplifies Prototyping of Applications
Nowadays, each organization wants to beat competition by developing software with distinet and
Innovative features, That is why; prototyping has become an integral part of modern software
development lifecycle. Before writing the code, developers have to create prototype of the
application to display its features an functionality to various stakeholders. As a simple and fast
programming language, Python enables programmers to develop the final system without puting
any extra time and effort. At the same time, the developers also have option to start developing
the system directly from the prototype simply by refactoring the codeCan also be used for Mobile App Development
Frameworks like Kivy also make Python usable for developing mobile apps. As a library, Kivy
can be used for creating both desktop applications and mobile apps. Bu it allows developers to
\wrte the code once, and deploy the same code on multiple platforms. Along with interfacing
wih the hardware ofthe mobile device, Kivy also comes with bul wlapters, modules
to render and play videos, and modules to accept user input through multitouch and gestures.
‘Thus, programmers can use Kivy to creat different versions of the same applications for 108,
Android and Windows Phone. Also, the framework does not require developers to write longer
lines of code while creating Kivy programs. Afler creating different versions of the mobile app,
1m makes it easier for
they can package the app separately for individual app store. The opt
developers to create different versions of the mobile app without deploying separate developers.
(Open Source
Despite being rated as the most popular coding language of 2015, Python is still available as
‘open source and fre software. Along with large IT companies, the startups and freelance
software developers can also use the programming language without paying any fees or royalty
“Thus, Python makes it easier for businesses to reduce development cost significantly. At the
same time, the programmers can also avail the asistance of large and active community to add
out-of-box features tothe software application.
‘The last major release of Python took place in December 2008, Python 3 was released as @
backward-incompatible version with most of the major features back ported to Python 2.6 and
2.7. However, the programming language is being updated by the community at regular
Intervals. The community released Python 3.4.3 on 23rd February with several features and
patches. So the developer can always use the most recent version of the Python programming
language to effectuate development of various software applications.CHAPTER 2
HOW TO ACCEPT USER INPUTS AND DISPLAY OUTPUTS,
‘The Input Function
The hello program of The Classic First Program always does the same thing This is not very
interesting, Programs are only going to be reused if they can act on a variety of data. One way to
jet data is directly from the user. Modify the hello py program as follows in the editor, and save
Itwith Fle» Save As... using the name hello_youpy.
person = input(Enter your name: ‘)
print Hello, person)
Run the program. Inthe Shell you should seeEnter your name:
Follow the instruction (and press Enter). Make sure the typing cursor is in the Shell window, at
the end ofthis line. After you type your response, you can see thatthe program has taken in the
line you typed. That i what the built-in function input does: First it prints the string you give asa
‘parameter (in this case “Enter your name: ‘), and then it waits fora line tobe typed in, and returns
the string of characters you typed. In the hello_you.py program this value is assigned tothe
variable person, for use later.
The parameter inside the parentheses after input is important. tis a prompt, prompting you that
keyboard input is expected at that point, and hopefully indicating what is being requested.
‘Without the promp, the user would not know what was happening, and the computer would just
sit there waiting!
‘Open the example program, interview py. Before running it (with any made-up data), se if you
can figure out what it will do:
‘lustrate input and print.
applicant = input("Enter the applicant's name: ”)
Imerviewer = input(Enter the interviewer's name:
time
input "Enter the appointment time: ")
prini{incerviewer, "will interview”, applicant,
" time)
‘The statements are executed inthe order they appear inthe text of the program: se q uentially
‘Thisis the simplest way forthe execution ofthe program to flow. You will se instructions later
‘that alter that natural flow.
If we want to reload and modify the hello_you.py program to put an exclamation point at theend, you could try:
person
punter your names’)
prini(Hello, person,
Run it and you see that i isnot spaced right. There should be no space after the person’s name,
butte default behavior of the prin function isto have each field printed separated by a space.
‘There are several ways fix this. You should know one. Think about it before going on to the
next section. Hint: 1]
[1] The + operation on strings ads no extra space
1.102, Print with Keyword Parameter sep
‘One way to put punctuation but no space after the person in hello_you.py is to use the plus
‘operator, +. Another approach i to change the default separator between fields inthe print
function. This will introduce a new syntax feature, Keyword parameters. The print function has &
keyword parameter named sep. Ifyou leave it out ofa call to print, as we have so fa, itis set
‘eq val toa space by default. Ifyou adda final fled, sep
‘you get the following example file, hello_you2.py:
in the print function in hello. youpy,
“Hello to you! Illustrates sep with empty string in print
person = input(Enter your name:
prini(Hello*, person," sep:
‘Try the program.
Keyword paramaters must be listed atthe end ofthe parameter list1.10.3, Numbers and Strings of Digits
Consider the following problem: Prompt the user fortwo numbers, and then print ou a sentence
stating the sum, For instance if the user entered 2 and 3, you would print “The sum of 2 and 3 is
a
‘You might imagine a solution like the example file addition py, shown below. There isa
problem. Can you figure it out before you try it? Hint [2]
Error in addition from input.
x= input("Enter a number:
{y= Input("Ener a second number:
priniThe sum of x, "andl
ysis ty, sep
End up running it in any case.
‘We do not want string concatenation, but integer addition, We need integer operands, Briefly
mentioned in Whirlwind Introduction To Types and Functions was te fact thet we can use type
‘names as functions to convert types. One approach would be todo that. Further variable names
are also introduced inthe example addltion2.py fle below to emphasize the distinctions in types.
Read and run
"Conversion of stings to int before addition’
String = input("Enter a number:
x= int(Sting)
String = inpu("Enter a second number: "y= in(yString)
printThe sum of "andy," x+y, e
[Needing 1o convert string input vo numbers @ common situation, both with Keyboard Input and
later in web pages. While the extra variables above emphasized the steps, itis more concise to
‘write a inthe variation in example fil, adition3.py, doing the conversions to type int
immediatly:
Two numeric inputs, with immediate conversion
x= In(input("Ener a number")
{y= in(input(“Enter a second number: )
print(The sum of , x," and y,"is', x+y,
‘The simple programs so far have followed a basic programming pattem: input-calculate-output.
Get all the data firs, calculate with it second, and output the results lst. The pattern se q wence
‘would be even clearer if we explicitly create a named result variable in the mid, as in
addition py
“Two numeric inputs, explicit sum”
X= int(input("Enter an integer: "))
{y= In(input("Enter another integer: "))
Hy
priniThe sum of x, "and
‘We will see more complicated patterns, which involve repetition, inthe future[2] The input function produces values of string type.
1,103.1. Exercise for Addition
‘Write aversion, add3.py, that asks for three numbers, and lists all tree, and their sum, in similar
format to addtion4 py displayed above.
1.10.3.2. Exercise for Quotients
‘Write a program, quotient. py, that prompts the use fortwo integers, and then prints them out in
4 sentence with an integer division problem like
‘The votient of 14 and 3 is 4 with a remainder of 2
Review Division and Remainders if you forget the integer division or remainder operator.
1.10.4, Suing Format Operation
In grade school q ulzzes a common convention is to use fill-n-the blanks. For instance,
Hello__
and you can fill n the name of the person greeted, and combine given text with a chosen
insertion. We use this as an analogy: Python has a similar construction, better called fillin-the-
‘braces. There is a particular operation on strings called format, that makes substitutions into
places enclosed in braces. For instance the example file, hello_you3,py, creates and prints the
‘same string as in hello_you2.py from the previous section:
“Hello co you! Illustrates format with (} In print.person = input(Enter your name:
‘greeting = Hello, )fformat(person)
prin(greeting)
‘There are several new ideas here!
First method calling syntax for objects is used. You will se this very important modern syntax
in more detail atthe beginning ofthe next chapter in Object Orientation. All data in Python are
‘objects, including strings. Objects have a special syntax for functions, called methods, associated
With the particular type of object. In particular str objects have a method called format. The
‘syntax for methods has the object followed by a period followed by the method name, and
futher parameters in parentheses,
‘object methodname(parameters)
Inthe example above, the object i the string Hello (}!. The method is named format. There is
‘one further parameter, person.
‘The string for the format method has a special form, with braces embedded. Places where braces
are embedded are replaced by the value ofan expression taken from the parameter list forthe
format method. There are many variations on the syntax between the braces. In this case we use
the syntax where the first (and only) location in the string with braces has a substitution made
from the first (and only) parameter.
Inthe code above, this new string is assigned tothe identifier greeting, and then the string is
printed
‘The identifier greeting was introduced to break the operations into a clearer se q uence of step.
However, since the value of greting is oly referenced once, it can be eliminated with the moreperson
sputter your name: ‘)
rin Hello (}formar(person))
‘Consider the interview program. Suppose we want to add a period atthe end ofthe sentence
(with no space before it). One approach would be to combine everything with plus signs.
Another way is printing with keyword sep. Another approach is with string formating, Using
‘our grade schoo! analogy, the idea i to fill in the blanks in
will incerview at
“There are multiple places to substitute, and the format approach can be extended to multiple
substitutions: Each place in the format string where there is’, the format operation will
substitute the value ofthe next parameter inthe format parameter lis.
Run the example file interview2.py, and check thatthe results from all three methods match,
"Compare print with concatenation and with format string”
put( "Enter the applicant's name:
ver the interviewer's name: ")
input "Enter the appointment time:
prin(interviewer +° will interview * + applicant +" at” + time +)
Drin(inerviewer, will interview ' applicant, ° a ime, sep=")
prin((() will interview {) at (}-\format(interviewer, applicant, time))
Sometimes you want a single string, but not just for printing. You can combine pieces with the +
‘operator, but then all pieces must be strings or explicitly converted to strings. An advantage ofthe format method is that i will convert types to string automaticaly, like the print function.
Here is another variant of our addition sentence example, addition4a.py, using the format
method
“Two numeric inputs, explicit sun
X= int(inpur("Enter an intege
»
{¥ © In(input("Enter another integer
oy
sentence = "The sum of () and () is (format, , sum)
prin(sentence)
Conversion to strings was not needed in interview2.py. (Everything started out as a string.) In
addlition4a.py, however, the automatic conversion ofthe integers to strings is useful.
So far there is no situation that requires a format string instead of using ther approaches.
Sometimes a format string provides a shorter and simpler expression. Except where specifically
instructed in an exercise for practice, use whatever approach to combining strings and data that
‘you like. There are many elaborations to the fields in braces to contro formatting, We will look
at one later, String Formats for Float Precision, where format strings ae particularly useful
AA technical point: Since braces have special meaning ina format string, there must be a sped
rule if you want braces to actually be included inthe final formated sting. The rule isto double
the braces: '{{’ and} The example code formatBraces.py, shown below, makes setStr reer to
the string The set is (5,9). The inital and final doubled braces in the format string generate
literal braces inthe formatted string:
illustrate braces in a formated sting.ass
b=9
setStr= The set is {({), (})-sformat(a,b)
prinisetStr)
‘This kind of format string depends directly onthe order ofthe parameters tothe format method,
‘There is another approach with a dictionary, that was used inthe fist sample program,
madlib,py, and willbe discussed more in Dictionaries and String Formatting. The dictionary
approach is probably the best in many cases, but the count-based approach isan easier stat,
particularly if the parameters ae just used once, in order.
Optional elaboration with explicitly numbered entries,
Imagine the format parameters numbered in order, starting from 0. In this case , 1, and 2. The
numberof the parameter position may be included inside the braces, so an alternative tothe last
line of interview2.py is (added in example file interview3.py)
print {0} will interview (1} at (2}-.format(inerviewer, applicant, time))
‘This is more verbose than the previous version, with no obvious advantage. However, if you
desire to use some ofthe parameters more than once, then the approach with the numerical
identification with the parameters is useful. Every place the string includes "{0), the format
‘operation will substitute the value of the inital parameter in thelist. Wherever "(L} appears, the
next format parameter will be substtued..
Predict the results of the example file arith.py shown below, if you enter 5 and 6, Then check
‘yourself by running it, In this case the numbers referring to the parameter positions are
necessary. They are both repeated and used out of order:
"Fancier format string example withparameter identification numbers
~ useful when some parameters are used several times."
x= in(input(Eoter an inceger:))
{y= In(input(Enter another integer: )
formatsur="0} + (1) = (2); (0}* (1) = (3)
equations = formatSt format, y, +9, .*9)
rine q uations)
“Try the program with other data
[Now that you have a few building blocks, you will see more exercises where you need to star to
do creative things, You are encouraged to go back and reread Learning to Problem-Solve
1.10.4.1, Addition Format Exercise
‘Write a version of Exercise for Addition, add3t py, that uses the string format method to
‘construct the same final string as befor.
1.10.4. Quotient Format Exercise
‘Write aversion ofthe quotient problem in Exercise for Quotients, q uosientformat.py, that uses
the string format method to construct the same final string as before, Again be sure to give a full
sentence stating both the integer quotient and the remainder.CHAPTER 3
HOW TO DEFINE YOUR OWN FUNCTIONS AND
MODULES
Modules refer toa file containing Python statements and definitions
A file containing Python code, for e.g: example py, is called a module and its module name
would be example
‘We use modules to break down large programs into small manageable and organized files.
Furthermore, modules provide reusability of code.
‘We can define our most used functions ina module and import it, instead of copying their
definitions into different programs.Let us create a module. Type the following and save it as example.py.
+ Python Module example
def ada, by:
‘This program adds two
‘numbers and return the result”
+b
return result
Here, we have defined a function add inside a module named example. The function takes in
‘two numbers and returns their sum,
How to import modules in Python?
‘We can impor the definitions inside a module to another madule or the interactive interpreter in
Python
‘We use the import keyword to do this. To import our previously defined module example we
type the following in the Python prompt.
>>> import example
“This does not enter the names of the functions defined in example directly in te current symbol
table. It only enters the module name example there
Using the module name we can access the function using the dot . operator. For example:
>>> example.add4.5.5)95
Python has aton of standard modules available.
‘You can check out the full lst of Python standard modules and what they ate for. These files are
in the Lib directory inside the location where you installed Python.
Standard modules can be imported the same way as we import our user-defined modules
‘There are various ways to import modules. They are listed as follows.
Python import statement
‘We can import a module using impor statement and access the definitions inside it using the dot
‘operator as described above, Here is an example.
4 Import statement example
4 co import standard module math
import math)
print("The value of pi is", math pi
‘When you run the program, the output will be:
“The value of pl is 3.141592653589793Import with renaming
‘We can importa module by renaming it s follows.
4# Import module by renaming it
Import math as m
print("The value of pi is", mpi)
‘We have renamed the math module asm, This can save us typing time in some cases,
[Note that the name math is not recognized in our scope, Hence, math.pl i invalid, m.p is the
correct implementation.
Python from. impor statement
‘We can impor specific names from a module without importing the module asa whole. ere is,
an example,
4# impor only pt from math module
from math import pi
print("The value of pl is pi)
‘We imported only he atvibute pi from the module.
In such case we don't use the dot operator. We could have imported multiple atributes as>>> from math impor pi, €
3,141592653589793
2.718281828459045,
Import all names
‘We can impor all names(definiions) from a module using the following construct.
‘import all names from the standard module math
from math import *
print("The value of pi is" pi)
‘We imported all the definitions from the math module. This makes all names except those
bbeginnig with an underscore, visible in our scope.
Importing everything withthe asterisk (*) symbol isnot a good programming practice. This can
lead to duplicate definitions for an identifier. It also hampers the readability of our code
Python Module Search Path
‘While importing @ module, Python looks at several places, Interpreter first looks for a built-in
‘module then (if not found) into a list of directories defined in sys.path. The search isin this
order.
“The current directory.PYTHONPATH (an environment variable witha listofetectory)
The insallation-dependent default directory.
>>> impor sys
>>> sys.path
7
“CA\Python33\LAbvialets,
“CAWindows\system32\python33 zip,
“CAPython33NDLLs,
“CA\PythonBib,
“cA\Pythona3,
“C2NPython2a\libsite-packages)
‘We can add modify this list t0 add our own path
Reloading a module
‘The Python interpreter imports a module only once during a session. This makes things more
efficent. Here isan example to show how this works.
‘Suppose we have the following code in a module named my_module,
+ This module shows the effect of
'® multiple imports and reload
print("This code got executed”)
Now we se the effect of multiple import.
>>> import my_module‘This code got executed
>>> import my_module
>>> import my_module
‘We can see that our code got executed only once. This goes to say that ur module was imported
only one.
[Now if our module changed during the course ofthe program, we would have to reload ine
‘way to do this is to restart the interpreter. But this does not help much.
Python provides @ neat way of doing this. We can use the reload) function inside the imp
module to reload a module, This is how its done,
>>> impor imp
>>> Import my_madule
This code go executed
>>> import my_module
>>> imp relay module)
“This code go executed
“The dif builtin function
‘We can use the dirt) function to find out names that are defined inside a module
For example, we have defined a function add) in the module example that we had in the
beginning,
>>> die(example)
t
*_uitins_“cached
‘oc
“file:
“tniaizing_s,
“loader
“package.
‘ad
Here, wecan see a sorted list of names (along with add). Al ther names that begin with an
‘underscore are default Python attributes assoratd with the module (we id not define them
ourself,
For example, the _name__atrbute contains the name of the module,
>>> impor example
>>> example._name_
“example
All the names defined in our current namespace can be found out using the de) function without
any arguments
pearl
>>> b= “hello”
>>> import math,
>>> dit)
builtins math; ‘pyscripter]
eck out these examples to learn more:Python Program to Shuffle Deck of Cards
Python Program to Display Calendar,CHAPTER 4
HOW TO WRITE YOUR OWN CLASS
ara)
FY Python Class
In object-oriented computer languages such as Python, classes are basically a template to create
‘your own objects. Objects are an encapsulation of variables and functions imo a single entity.
‘Objects get tele variables and functions from classes
Here are some examples that will belp you understand—read on. There Is also an interactive
‘code shel, simply press the “Run” button at the top of the specific window.
‘The simplest way to describe classes and how to use them is this:
Imagine you have great powers. You create a species (“class”)
‘Then you create attributes for that species (“properties")—height, weight, limbs, color, powers,and $0 on
‘Then you create an instance of that species—Fido the dog, Drogon from Game of Thrones, and
so on. Then you work with these instances:
Ina game, for instance, they would engage in action, interact, using their atributes
Ina banking app, they would be the diferent transactions.
Ina vehicle buy/sel/trade/lease app, the vehicle class could then spawn subclasses such as car,
Each would have atuibutes such as mileage, options, features, color, and tim.
‘You can already see why this is useful. You are creating, re-using, adapting, and enhancing items
ina very efficient, logical, and useful way.
By now, you have probably realized that this isa way to classify and group, one that that i
similar to how humans learn:
Animals ae living things that are not human or ees, ina basic sense
then you move on to different types of animals —dogs, cats are probably the frst animals most
‘of us Ieamt about
then you move to different aributes of animals—shapes, sizes, sounds, appendages and so on.
For instance, when you were a child, your first understanding of a dog was probably something
‘with four leg that barked. Then you learn to distinguish that some were real dogs, others were
toys. That this “dog” concept commained many types.
Creating and using classes is basically:
building a template to put “things” in—a classification
which can then be operated on. For example, pulling up all the people with dogs that you could
re q uest link toa blog on pes, oral bank clients who might be good prospects for a newcreditcard,
"The main point here is classes are objects that can produce instances of those templates, on
‘which operations and methods can be applied. Its an excellent way to conceptualize, organize,
nd build a hierarchy for any organization or process.
[As our world gets more complex, this is away to mimic that complexity from a hierarchical
Perspective. Italso builds a deeper understanding of the processes and interactions for business,
technical, and social setings from a viral information technology point
‘An example might be a video game you create. Each character could be a “cass”, with its own,
attributes, that interacts with instances of other classes. King George of the “King” class might
interact with Coutt Jester Funnyman of the “Clown” class, and so on. A King might have a royal
“servant” class, anda “servant” class would always have a “King” lass, for example.
‘Thisis what we will do:
create a class and use it
create a module and move the class creation and initiation to the module
call the module in a new program to use the class.
{#TSB - Create Class in Python - rocket positions (x,y) and graph
‘some items and comments bolded to call atention to process
import matpltib.pyplt as pt
class Rocket):
def init__(self, x=0, y=0}.
‘each rocket has (x,y) position; user or calling function has choice
Wo passing in x and y values, or by default they are set at 0selfx=x
selfy=y
def move_up(sell)
selfy 41
def move_down(selt):
self
def move_right(sell
selfx 41
def move_lefi(se):
self
‘Make a series of rockets - x,y positions 1am calling it rocket
rockets={}
rockets append(Rocket())
rockets append(Rocket(0,2))
rockets append(Rocket(4)
rockets.append(Rocket2,6))
rockets append(Rocket(3.7)
rockets append(Rocket(5,9))
rockets append(Rocket(, 15))
‘#Show on a graph where each rocket is
for index, rocket in enumerate(rockets}:original postion of rockets
print@"Rocket 6d i a (éd, é)." 96 (index, rocket.x,rockety))
pltplot(rocket.x, rockety, ro, linewidth=2,linestyle~dashed!, markersize=12)
‘move the rocket one up
rocket.move_up()
print("New Rocket position dis at (%, Yd)” % (index, rocket.x, rackety))
‘plot the new position
pliplox(rocket.x, rockety, ‘bo, linewidth=2,linestyle='dashed, markersize=12)
‘move the rocket lef, then plot the new position
rocket. move. lef
pliplot(rocket-x, rockety, yo, linewidth=2,linestyle~'dashed, markersize=12)
‘show graph legend to match colors with position
plt.gca( egend\(Corginal position, - Moved up’, ‘<- Moved left))
pltshow(
‘plulegenci
1pper left’)
So there you have it. You can creste many different classes, with parent classes, sub-classes andOBJECT ORIENTED
Python has been an object-oriented language since it existed. Because ofthis, creating and using
lasses and objects are downright easy. This chapter helps you become an expert in using
Python's object-oriented programming suppor.
If you do not have any previous experience with object-oriented (OO) programming, you may
‘want to consult an introductory course on itr atleast a tutorial of some sort so that you have a
‘rasp of the basic concepts
However, here is small introduction of Object-Oriented Programming (OOP) w bring you at
speed ~
Overview of OOP Terminology
Class ~ A user-defined prototype for an object that defines a set of attributes that characterize
any object ofthe class. The attributes are data members (class variables and instance variables)
and methods, accessed via dot notation,
(Class variable ~ A variable that is shared by al instances ofa class. Class variables are defined
‘within a class but outside any ofthe class's methods. Clas variables are not used as freq uently
as instance variables are,
Data member ~ A class variable or instance variable that holds data associated with a class and
its object.
Function overloading ~ The assignment of more than one behavior to 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 toa class Circle, for
‘example, isan instance ofthe clas Circle,
Instantiation ~ The creation of an instance of a clas.
Method ~ A special kind of function that is defined ina class definition,
‘Object ~ A unig ue instance ofa data structure that's defined by its clas. An object comprises
‘both data members (class variables and instance variables) and methods.
Operator overloading ~ The assignment of more than one function toa particular operator.
Creating Classes
“The class statement creates a new class definition. The name ofthe lass immediately follows the
keyword class followed by a colon as follows ~
lass 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 member, data attributesand functions
Example
Following isthe example ofa simple Python class ~
,‘empCount 2,
“dispayEmployee': ,
“doc "Common base clas fr all employees’
“int function _init_ at oxb7ea4sbe>)
Destroying Objects (Garbage Collection)
Python deletes unneeded objects (built-in types or lass instances) automaticaly to fre the
memory space. The process by which Python periodically reclaims blocks of memory that no
longer are in use is termed Garbage Collection.
Python's garbage collector runs during program execution and is triggered when an object's
reference count reaches zero. An objec’ reference count changes asthe numberof aliases that
point to it changes.
‘An objec’ reference count increases when it is assigned a new name or placed in a container
(st, tuple, or dictionary) The objects reference count decreases wien it's deleted with del, is
reference is reassigned, or is reference goes out of scope. When an object's reference countreaches zero, Python collects it automatically
8= 40 # Greate object <40>
=a # Increase ref. count of <40>
c= [b] | # Increase ref. count of <40>
dela # Decrease ref. count of <40>
b= 100 # Decrease ref. count of <40>
clO}=-1 # Decrease ref. count of <40>
‘You normally wll not notice when the garbage collector destroys an orphaned instance and
reclaims its space. But a class can implement the special method __del_0, called a destructor,
thats invoked when the instance is about tobe 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 ofan instance that is about tobe destroyed ~
‘#usebinvpython
lass Point:
def _init_{( self,
selfix
selty=y
def _del_ (sey
class_name = self,_class_.__name_
prin class_name, “destroyed”pul = Poin’)
p= ptt
p= pe
print tpt), (p23) # prints te ds ofthe obefers
del pat
del pra
aelpa
‘When the above cade is executed, it produces following result ~
‘302401324 3083401324 3082401224
Point destroyed
[Note ~ Ideally, you should define your classes in separate file, then you should import ther in
‘your main program file using import statement
Class Inheritance
Instead of starting from seratch, 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 ofits parent class, and you can use those atrbutes 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 lke their parent class; however, alist of base classes to inherit
from is given after the class name ~
‘lass SubClassName (ParentClass1[, ParentClass2, Dy"Optional class documentation string’
class_suite
‘itusebnvpython
class Parent: # define parent class
parentAatr = 100,
def _ init__(self):
print "Calling parent constructor”
def parent Method self)
print Calling parent method!
def setAtusel, at):
Parent parentActr
def getAtrself)
print "Parent attribute", Parent parent
‘lass Child(Parent) # define child class
def _init_{self):
print "Calling child constructor”
def childMethod{sel)
print Calling child methodc= Child) #instance of child
CcchildMethod() — # child calls its method
cparentMethod() _# calls parents method
{esetAtu(200)—_# again call parent's method
getAur) —— #again call paren’s method
‘When the above code is executed, it produces the following resalt~
Calling child constructor
Calling child method
Calling parent method
Parent aibute: 200
Similar way, you can drive a class from multiple parent classes as follows ~
class A: # define your class A
class B: define your class B
class C(A, BY: # subclass of A and B.
‘You can use issubclass() or isinstance( functions to check a relationships of two classes and
instances.
‘The issubclass(sub, sup) boolean function returns tre Ifthe given subclass sub is indeed a
subclass ofthe superclass sup.The isinsance(obj, Class) boolean function returns true if obj is an instance of class Class oF is
an instance ofa 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 subclas,
Example
‘#husePbinvpython
class Parent: # define paren class
def myMethodsel:
print Calling parent method!
lass Child(Parent) # define child class
def myMethodse)
print Calling child method"
= Child). #instance of child
‘cmyMethod) _ # child calls overridden method
‘When the above cade is executed, it produces the following result
Calling child method
Base Overloading MethodsFollowing table lists some generic functionality that you can override in your own classes ~
Sr.No, Method, Description & Sample Call
1
init__(self Largs.)
Constructor (with any optional arguments)
Sample Call: obj = className(args)
2
—del_(sel)
Destructor, deletes an object
Sample Call: del obj
a
—repr_(self)
Evaluable string representation
Sample Call: repr(obj)
—stt_(self)Printable string representation
Sample Call: su(ob))
5
—emp_( self, x)
‘Object comparison
Sample Call : emp(obj, x)
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? Mos 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
‘/usebinvpython
class Vector:
def _ init__(self a,b
sella=aself
def _str_ (elf
return "Vector (6d, 96d)’ % (self, selEb)
def _adld_(selfother):
return Vector(self.a + other.a, sel + other:b)
vL=Vector(2,10)
v2 = Vector(5-2)
print vl + v2
‘When the above code is executed, it produces the following resalt~
Vector(7.8)
Data Hiding
‘An abject’ atwibutes may or may not be visible outside the class definition. You need to name
attributes witha double underscore prefix, and those attributes then ate not be directly visible to
outsiders.
Example
‘/usebinvpython
lass JustCounter:
—secretCountdef coun(sef)
self_seeretCount
print slf,_secretCount
‘counter = JustCounter()
ccounter-count()
counter-count()
print counter, secretCount
‘When the above code is executed, it produces the following result ~
1
2
“Traceback (most recent call last)
File “test.py", line 12, in
print counter, seeretCount
AtaibuteError:JustCounter instance has no atrbute"_secretCount’
Python protects those members by internally changing the name to include the class name. You
‘cam access such attributes as objec_className_attrName. If you would replace your last line
as following, then it works for you ~
print counter. JustCounter__secretCount.CHAPTER 5
HOW TO WORK WITH
XTERNAL FILES
Why use Files? & python
_—
—
Extomal Fle
Read rom S22 | rescondary
‘ile (Load) afcrage)
@
Al programs must deal with extemal data, They will either accep data from sources outside the
text ofthe program, or they will produce some kind of output, oF they will do both. Think about
it if the program produces no output, how do you know it did anything?
By external data, we mean data ouside of volatile, high-speed, primary memory; we mean data
‘on peripheral devices. This may be persistent data on a disk, or transient data on a network
Imerface. For now, it may mean transient data displayed on our termina.“Most operating systems provide simple, uniform access to external data via the abstraction called
a file. We'll look a the operating system implementation, as well as the Python class that gives
us access tothe operating system file in our programs.
In File Objects - Our Connection To The File System, we provide definitions of how Python
orks with files. We cover the built-in functions for working with files in The File and Open
Functions. In Methods We Use on File Objects, we describe some method functions of file
objects. We'll look at fil-processing statements in File Statements: Reading and Writing (but no
Aithmetic),
File Objects ~ Our Connection To The File System
Abstractions Built on Top of Abstractions, Files do a huge numberof things for us. To support
this broad spectrum of capabilities, there are two layers of abstraction involved: the OS and,
Python. Unfortunately, both layers use the same words, so we have to be careful about casually
misusing the word “file.
‘The operating system has devices of various kinds. Al ofthe various devices are unified using a
n abstraction that we cll the file system. All of a computer's devices appear as OS files
of one kind oF another, Some things which aren’t physial devices also appear a files, Files are
the plumbing that move data around our information infrastructure.
Additionally, Python defines file objects, These file objects are the fixtures that give our Python
rogram access to OS fies.
Python File and OS File
How Files Work. When your program evaluates a method function of a Python file object,
Python transforms this into an operation on the underlying OS file. An OS file operation
becomes an operation on one of the various kinds of devices attached to our computer, Or, a OS
file operation can become a network operation that reaches through the Internet to access data
from remote computers. The two layers of abstraction mean that one Python program can do awide variety of things on a wide variety of devices.
Python File Objects
In Python, we create a file object to work with files inthe file system. In alton to files in the
(0's filesystem, Python recognizes a spectrum of file-like objects, including abstractions for
network interfaces called pipes and sockets and even some kind of in-memory buffers.
Unlike sequences, sets and mappings, there are no Python literals fr file objects. Lacking
literals, we create a file object using the file() or open) factory function. We provide two pieces
of information o this function. We can provide a third, optional, piece of information that may
Limprove the performance of our program.
‘The name of the file, The operating system will interpret this name using its “working directory"
rules. Ifthe name starts with / (or device) it’s an absolute name. Otherwise, i’ a relative name;
the current working directory plus this name Identifies the fle,
Python can translate standard paths (
having to really understand the differences. We can name all of our files using /, and avoid the
messy details
1) to Windows-specific paths. This saves us from
{we want, use raw strings to specify Windows path names using the \ character
The access mode fo the file. This is some combination of read, write and append. The mode can
also include instructions for interpreting the bytes as characters.
Optionally, we can include the buffering forthe file. Generally, we omit this. Ifthe buffering
argument is given, 0 means each byte is transferred as itis read or writen. A value of 1 means
the data is buffered a line at atime, suitable for reading from a console, or writing to an error log.
Larger numbers specify the buffer size: numbers over 4,096 may speed up your program.‘Once we create the file object, we can do operations to read characters from the fle or write
‘characters tothe file, We can read individual characters or whole lines. Similarly, we can write
Individual characters of whole lines.
‘When Python reads file a ase q wence of lines, each line wll become a separate string, The wn"
‘characteris preserved at the end ofthe string, This extra character can be removed from the
‘sting using the rstrip) method function.
A file object (like a sequence) can create an iterator which will yield the individual lines ofthe
file. You can, consequently, use the file object ina for statement. This makes reading text files
very simple
‘When the work is finished, we also need to use the file's close() method. This empties the in-
‘memory buffers and releases the connection with the operating system file. Inthe case of a
socket connection, this will release all ofthe resources used to assure that data travels through
the Internet sucessfully
‘The File and Open Functions
Here’s the formal definition of the fileQ and openg) factory functions. These functions create
Python file objects and connect them tothe appropriate operating system resources.
‘opencfilename, model, buffering) ~ file
‘The filename isthe name of the ile. This is simply given tothe operating system. The OS
‘expects either absolute or relative paths; the operating system folds inthe current working,
itectory to relative paths.
“The mode is covered in detail below. In can be 'w’ ors for reading (default), writing oF
appending, Ifthe file doesn’t exist when opened for writing or appending, it will be created. If 2
file existed when opened for writing, it will be truncated and overwriten. Add a tothe modeforbinary files. Add ato the mode to allow simultaneous reading and writing,
If the buffering argument is given, 0 means unbuffered, 1 means lin buffered, and larger
numbers specify the buffer size.
filefitename, model, buffering) ~ file
‘This is another name for the open() function. It parallels other factory functions like int() and
diag,
Python expects the POSIX standard punctuation of /to separate elements of the filename path for
all operating systems. f necessary, Python will translate these standard name stings to the
Windows punctuation of \. Using standardized punctuation makes your program portable ta all
‘operating systems, The os.path module has functions for creating valid names in a way that,
works on all operating systems.
‘Tip Constructing File Names
‘When using Windows-specifie punctuation for filenames, you'll have problems because Python
lnerprets the \as an escape character. To create a string with a Windows filename, you'll either
need to use \ in the string, or use an r"™ string literal. For example, you can use any of the
following: E:writing\echnical\pythonbook’ python. tml” oF
“EA\writing\technical\pythonbook’\python hum”.
[Note that you can often use "E:/wrting/technical/pythonbook/python.huml". This uses the
POSIX standard punctuation for files pats, J, and isthe most portable. Python generally
translates standard file names to Windows file names for you.
Generally, you should either use standard names (using ) or us the s.path module to construct
filenames. This module eliminates the need to use any specific punctuation, The os.pathjoin()
function makes properly punctuated filenames from se q uences of strings‘The Mode String. The mode string specifies how the OS file will be accessed by your program.
‘There are four separate issues addressed by the mode string: opening, bytes, newlines and
‘operations,
‘Opening. For the opening part of the mode string, there ae three alternatives:
+: Open for reading, Start at the beginning ofthe OS file. Ifthe OS file does not exist, raise an
OError exception. This isthe default.
‘ws Open for writing. Start at he beginning of the OS file, Ifthe OS file does not exist, create the
OS file
4: Open for appending, Star atthe end ofthe OS file. Ifthe OS file does not exist, create the OS
file
Bytes or Characters. For the byte handling part of the mode sting, there are two alternatives:
’: The OS file isa se q uence of bytes; do not interpret the fle as a sequence of characters. This
Is sultable for sv files as well as images, movies, sound samples, etc.
‘The default, if bis not included, is to interpret the file isa sequence of ordinary characters. The
Python file object will be an iterator that yields each individual lin from the OS file asa
‘separate string. Translations from various encoding schemes like UTF-8 and UTF-16 will be
handled automatically.
Universal Newlines, The newline part ofthe mode string has two alternatives:
U: Universal newline interpretation. The firs instance of, \n (or) will define the newline
character(s). Any ofthese three newline se q uences will be silently translated to the standard ‘wn’
‘character, The vin is a Windows Feature.
‘The default, if U isnot included, is o only handle this operating system's standard newline
character(s).Mixed Operations. For the additional operations part ofthe mode string, there are two
altematives:
+: Allow both read and write operations to the OS file
‘The default, if + is not include, i to allow only limited operations: only reads for files opened
‘with "7"; only writes for OS files pened with “w" or “a”
‘Typical combinations include the following
"to read text files
“0” to read binary files. A .csv file, for example, is often processed in binary mode.
‘w+ to create new text file for reading and writing.
‘The following examples create Python file objects for further processing
dataSource= open( "name_addr.csv", "tb" )
nnewPage= open("addressbook htm
theErrors= open(“iustlocaliog/eror log"
dataSource:
‘This example opens the existing file name_adde.csv in the current working directory for reading.
‘The variable dataSource identifies this fi
from this fi
‘object, and we can use this vaiable for reading stings
‘This file is opened in binary mode.
newPage:
‘This example creates a new file addressbook hun (ort will truncate this file if it exists), The fle
‘wil be inthe current working directory The variable newPage identifies te file object, We can
then use this variable to write strings to the filetheErrors
‘This example appends to the file ertorJog (or creates a new file, i he fle doesn’t exis), The fle
hhas the directory path /us/local/lo. Since this isan absolute name, it doesn’t depend on the
‘current working directory.
Buffering files is typically left as a default, specifying nothing, However, for some situations,
adjusting the buffering can improve performance. Error logs, for instance, are often unbuffered,
so the data is available immediately. Large input files may be opened with large buffer numbers
{ encourage the operating system to optimize input operations by reading a few large chunks of
data from the device instead of a large number of smaller chunks.
‘Tip Debugging Files
“There are a number of things that can go wrong in attempting to create a file object.
Ifthe file name is invalid, you will get operating system errors. Usually they will look like this:
“Traceback (most recent call last
File "", lie 1, in
TOEmor: [Ero 2] No such file or directory: 'wakawakat
tis very important to get the file's path completely correct. You'l notice that each time you
start IDLE, thinks the current working directory is something like C\Python26, You're
probably doing your work ina different default directory.
‘When you open a module file in IDLE, you'll notice that IDLE changes the current working,
lrectory isthe directory that contains your module. If you have your py files and your data files
all in one directory, you'l find that chings work out wel‘The next most common error is to have the wrong permissions. This usually means trying to
writing toa file you don't own, or attempting to create a file ina directory where you don’t have
\wrte permission. If you are using a server, oF a computer owned by a corporation, this may
require some work with your system administrators to sort out what you want to do and how
‘you can accomplish it without compromising security
‘The [Errno 2] note inthe error message isa reference to the intemal operating system error
numbers. There are over 100 ofthese error numbers, all collected ino the module named ero,
‘There area lot of different things that can go wrong, many of which are very, very obscure
situations.
Methods We Use on File Objects
‘The Python file object is our view of the underlying operating system file. The OS fie, in tum,
ives us access toa specific device.
‘The Python file object has a number of operations that transform the file object, read from or
write to the OS file, or access information about the file objec.
Reading. The following read methods get data from the OS file. These operations may also
‘change the Python ile object’s internal status and buffers. For example, at end-o-file, the
imernal status ofthe file object wll be changed. Most importantly, these methods have the very
visible effect of consuming data from the OS file,
fileread(size) string
Read as many as size characters from file {asa single large string. If size is negative or omitted,
‘the rest ofthe file is read into a single string
from _future__ import print function
dataSource= open “name_add.csv","t")form in theData splitines(
rina)
daaSource.closeQ,
fileeadline(size) ~ string
Read the next line or as many as size characters from fie f an incomplete line can be read. If
size is negative or omitted, the next complete line is read. IF a complete line is read, it includes
‘the railing newline character. If che file is atthe end, f. readline() returns a zero length sting. If
‘the file has a blank line this will be a string of length 1, just the newline character.
from _future_ import print function
dataSource= file “name_addr.cs
ey
n= dataSourcerreadline()
‘while len(ny > 0:
print(nrstip()
n= dataSource.realineQ)
dataSource.closeQ)
filexeadlines(hint)
Read the next lines or as many lines from the next hint characters from fief. The hint size may
’be rounded up to match an ieenal butfer size I hint is negative oF omitted, the rest ofthe file is
read. All lines will include the trailing newline character. Ifthe file fs at the end, freadlines()
returns a zero length list
When we simply reference a file object in a for statement, this s the function that's sed for
iteration over the file,
dataSouree= file “name_addt.es
for in dataSource:print(nsstip()
dataSource.closeQ,
‘Writing, The following methods send data tothe OS file, These operations may also change the
Python file object's internal status and buffers. Mast importantly, these methods have the very
visible effect of producing data tothe OS file
file flash)
Flush all accumulated data from the internal buffers of file tothe device or interface, Ifa file is
buffered, this can help to force writing ofa buffer that i less than completely full. This is
appropriate for log files, prompts written to sysstdout and error messages.
file.sruncate(size)
“Truncate file f If size isnot given the ile is truncated atthe current position. If size is given the
file willbe truncated ator before size, This function is not available on all platforms.
‘ie. writestring)
‘Write the given string o file. Buffering may mean that the string does not appear on a console
Lunt a closeQ or fush() operation is used.
newPage= file "adessbook hum, "w" )
newPage.write("\nchead>Hello World\ncbody>\n" )
newPageswrite"Hello World
in" )
newPage write "\nin")
newPage-close()
file swritlinestist)
‘Write the list of strings to file Buffering may mean tha the strings do not appear on any
console until a close() or flush() operation is usednewPage= file “adessbook hum", "w")
newPage.wrteine( ["cm>\n*, “chead>Hello Worl title>n", “” 1)
new Pageseritelines( ["Hello World
a’ })
newPageswrtelines, {*\n", "tum" J)
newPage close)
[Accessor The following file accessors provide information about the file object
filesell) + integer
Return the position from which file willbe processed. This is a parner tothe seek() method;
any position returned by the el’) method can be used as an argument to the sek() method to
restore the file to that position,
fileilenoQ) ~ integer
Return the interna ile descriptor (fd) number used by the OS library when working with fief
A number of modules provide access to these low-level libraries for advanced operations on
devices and files
filesattyQ) + boolean
Return True if file fis connected to an OS file that is a console or keyboard.
file closed) ~ boolean
“This attribute of fle fis True if the file is closed.
file.mode() ~ sting
This attribute isthe mode argument the file) function that was used to create the file objec
filename‘This attribute of fle fi the filename argument co the fle( function that was used to create the
file objec.
‘Transfomers. The following file transforms change the file object itself. This includes closing it
{and releasing all OS resources) or change the position at which reading or writing happens.
file.close()
Close file. The closed flag is set. Any further operations (except a redundant close) raise an
lOEmor exception,
file seek(offsett, whencel)
‘Change the position from which file f will be processed, There are three values for whence which
determine the direction of the move.
If whence is 0 (the default, move 1 the absolute position given by offset. f.seek(0) will rewind
fief
If whence is 1, move relative tothe current position by offset bytes. If offset is negative, move
backwards; otherwise move forward.
I whence is 2, move relative tothe end of file. £eek{0,2) will advance file fo the end.
File Statements: Reading and Writing (but no Arithmetic)
A file object like a se q uence) can create an iterator which will yield the individual lines of the
file, We looked at how se q uences work with the for statement in Looping Back: Iterator, the
forsatement and Generators. Here, we'll use the file object ina for statement to read al of the
lines.Additionally, the prin statement can make use ofa file other than standard output as
destination forthe printed characters. This will change with Python 3.0, so we won't emphasize
this,
‘Opening and Reading From a Fie. Let's say we have the following fle. f you use an email
service like HotMall, Yahoo! or Google, you can download an address book in Comma-
Separated Values (CSV ) format that will look similar to this file. Yahoo!'s format wll have
many more columns than this example.
name_adacsv
loe","[email protected]
Howard’,"Curly","[email protected]’, actor”
arry’,[email protected]’,"musician"
‘Jerome’, "Besser" Joe", stor”
joe@ 3stooges.com*,
"Joe", DeRita,"CurlyJoe,"[email protected]’,"actor*
"Shemp’,""/"Howard?,"Shemp”, "[email protected]", ‘actor
Here's a quick example that shows one way to read this file using the file's iterator. This isn't the
‘best way, that will have to wait for The csv Module,
1
2
3
4
dataSoutce = fle “name_addrcsv
foraddr in datSource:
rint(addr)daaSource.closeQ)
‘We create a Python file object forthe name_ad-esv inthe current working directory in read
mode. We call this object dataSource.
“The for statement creates an iterator for ths file; the iterator will yield each individual line from,
the file.
‘We cam print each line.
‘We close the file when we're done. This releases any operating system resources that our
rogram ted up while it was running.
[A More Complete Reader. Here's. a program that reads ths file and reformat the individual
records, I prints the results to standard output. This approach to reading CSV files isn’t very
‘800d. Inthe next chapter, we'll look atthe csv module that handles some of the addtional details,
re quired for a really reliable program.
nameaddrpy
10
‘#usr@biaveny python.
"Read the name_addr.csv file."
dataSource = file *name_add.csvfor addr in dataSource:
4 split the string onthe
from each field
stip the
fields=[f
ip") for Fn quotes}
in Fields), fet, ieldt2), elds)
‘dataSource.close()
We open the file name_addrcsv in our current working directory. The variable dataSource is our
Python file object
“The for statement gets an iterator from the ile t can then use the iterator, which yields the
Individual ines ofthe fle. Each line isa Tong sting. The fields are surounded by "sand are
separated by
We use the split) function to break the string up using the. This particular process won't work
if there ares inside te quoted fields, We'll look a the csv module to see how todo this beter.
We use the spt function o remove the "s fom each field. Notice that we used a ist,
‘comprehension to map from alist of field wrapped insta ist of fields that are not wrapped in
Seeing Ourpu with pint. The print) function does two things. When we introduced prin) back
in Seving Results: The print Statement, we hustled past both ofthese things because they were
realy «ite advanced concepts.
We covered string
chapter. Now we can open up the hood and look closely tthe print() function,
‘Seq wences of Characters: strand Unicode. We're covering files inthis
‘The print function evaluates all of its expressions and converts them to stings. In effect, it calls
the st) built-in function for each argument value
“The print) function writes these strings, separated by a separator character, sep. The default
separator isa space,"
‘The print function also writes an end character, end. The default end isthe newline character,ow
‘The print) function has one more feature which can be very helpful to us. We can provide a file
parameter to redirect the output to a particular file.
We can use this to write lines to sys.stders
1
2
3
4
5
from _future__import print
Import sys
priny(*normal ourpue
rin’Rea Aer”, ile=sysstder)
print normal ouput, le=sysstdout)
‘We enable the prin function
‘We impor the sys module.
‘We write a message to standard output using the undecorted print statement
‘We use the file parameter to write to syste.
‘We also use the:vomame:ile parameter to write sysstdout
‘When you run this in IDLE, you'll notice thatthe error messages display in red, while the
standard output displays in blue.
Print Command. Here isthe syntax for an extension tothe print statementprint >> file, expression ,..}
‘The >> isan essential part ofthis peculiar syntax. This is an odd special case punctuation that
doesn’t appear elsewhere in the Python language. It's called the “chevron print”.
Important Python 3
“This chevron print syntax will go away in Python 3, Instead of a print statement with a bunch of
special cases, we'll use the print() function
‘Opening A File and Printing. This example shows how we open a file inthe local directory and
‘write data to that file, In this example, we'll create an HTML file named addressbook him
‘We'll write some content to this file. We can then open this file with FireFox or Intemet
Explorer and se the resulting web page.
adapage py
u
‘#iuseibinveny python‘Write the addressbook hum! page.”
from _future__import print function
nnew_page = open( “addressbook.huml
prin, new_page)
prin’ ’
“meta hup-e ¢ viv="content-type” content="texUhiml charset=us-asci>"
“atitle>addressbook-title>, fil
new. page)
ring’ Hello worl, ile-new_ page )
rin
"w_page)
new page.close()
Basic File Exercises
Device Structures.
Som disk devices are organized into cylinders and tracks instead of blocks. A disk may have @
‘number of parallel platters; a cylinder isthe stack of tracks across the platters available without
moving the read-write head. A track isthe data on one circular section ofa single disk patter
‘What advantages does this have? What (if any) complexity could this lea 10? How does an
application program specify the tracks and sectors to be used?
Some disk devices are described asa simple se q uence of block, inno particular order. Each
block has a unique numeric identifier. What advantages could this have?
Some disk devices can be partitioned, What (if any) relevance does this have to file processing?
Skip The Header Record
‘Our name addres file has a header record, We can skip this record by geting the iterator and
advancing to the nex item.‘Write a variation on nameaddr.py which uses the iter to get the iterator forthe dataSource file,
Assign this iterator object to dataSrcter. If you replace the file, dataSource, with the iterator,
dataSrcter, how does the processing change? What i the value retuned by dataSrclter.next()
‘before the for statement? How does adding this change the processing of te for statement?
‘Combine The Two Examples.
(Our ewo examples, adrpage py and name_add.py are really two halves of a single program.
‘One program reads the names and address, the other program writes an HTML file. We can
‘combine these two programs to reformat a CSV source file into a resulting HTML page.
‘The name and adresses could be formatted in a web page that looks like the following:
Address Bookcitile>
lastnames/d>fist names/td> | emsil adessStr>
-lastname<>fist nameemal adress <>
last name | fist name | emall address |
<>
Each of our input fields becomes an output field sandwiched in between and | . In this
‘case, we uses phrases like last name, first name and email address to show where real data would
‘be inserted, The other HTML elements like have tobe printed as they're shown in this‘Your final program should open two files: name_add.csv and addressbook:html. Your program
should write the intial HTML material (up co the first ) tothe output file. It should then ead
the CSV records, writing a complete address line between to . After it finishes reading
nd writing names and addresses, it has to write the lst of the HTML file, from to
.CHAPTER 6
DISCOVER VARIABLES, STRINGS, INTEGERS, AND
MORE TO DESIGN CONVERSATIONAL PROGRAMS
Before we tat to write the program, we need to generate a token for our bot. The oken is
needed to access the Telegram API, and install the necessary dependencies.
1, Create a new bot in BotFather
If you want to make a bot in Telegram, you have to “register” your bot frst before using it.
‘When we “register” our bot, we will et the token to access the Telegram API.
Go tothe BotFather (if you open it in desktop, make sure you have the Telegram app) den
create new bot by sending the /newbot command. Follow the steps unl you get the username
ad token for your bot. You can go to your bot by accessing this URL:
Iatpsuitelegram.me/YOUR_BOT_USERNAME and your token should looks like thisTOAABO3I:AAEICZ**eereneees
2. Install the library
Since weare going to use a library for this tutorial, instal it using this command,
pip3 install python-telegram-bot
If the library is successfully installed then we are good to go.
‘Write the program
Let's make our firs bot. This bot should return a dog image when we send the bop command.
‘To be able to do this, we can use the public API from RandomDog to help us generate random
dog images.
“The workflow of our bot is as simple as this
access the API-> get the image URL -> send the image
1 Import the libraries
First, import all the Mbraries we need.
from telegram ext import Updater, CommandHandler
impor req uests
import re
2. Access the APL and ge the image URL.
Let's create a function to get the URL. Using there quests library, we can access the APL and
‘get the jon data
‘contents = req uestsget(htps:/irandom.dog/woot json’) json()
‘You can check the json data by accessing that URL: htps:/random.dog/woof,json in yourbrowser. You will see something ike this on your screen:
‘(ur”s"hups:trandom.dog/***** JPG")
Get the image URL since we need that parameter 1 be able to send the Image.
image_l = contents ur)
‘Wrap this code into a function called get_uel
def get_urld:
contents = req uests. gets: random. dog/woot json) json)
vl = contents[ut)
recurn url
3, Send the image
‘To send a messagefimage we need two parameters, the image URL and the recipient's ID—his
‘can be group ID oF user ID.
‘We can get the image URL. by calling our get_url0 function,
url= get_url
Got the recipient's ID using this code:
chat id = update,messagechat_id
[After we get the image URL. and the recipient's ID, it’s time to send the message, which is an
Image.
bot.send_photo(chat_id=chat_ photo=url)‘Wrap that code in a function called bop , and make sure your code looks lke this
def bop(bot, update):
vu = get_urlo
chat_id = update:messagechat_id
bot send_photo(chat_id=chat_d, photo=url)
4, Main program
Lastly, create another function called main to run our program. Don't forget to change
YOUR_TOKEN with the token that we generated earlier in this tutorial.
def main:
‘updater = Updater(' YOUR TOKEN)
dp = updater dispatcher
5:
se:
selX train, :]= np random.random(d) + meansty,
selfYtain{i] =2.0* y-1sell X_test = mp.zeros((N, ))
for in vange(N
if mp.random.randnQ) > 5:
else:
yoo
selfX_test(i,:] = np.andom.random(d) + meansfy, :]
self testi] = 2.0 *y-1
lass LogisticRegression():
© A simple logistic regression model with L2 regularization (zero-mean
Gaussian priors on parameters).
defi
t_ (elf, x tain=
lone, y_train-None, x_test=None, y_test=None,4 Set L2 regularization strength
selfalpha = alpha
1 Set the data
self.set_data(x train, y_train, x test, y_test)
4 Initialize parameters to zero, fr lack of a better choice
p.zeres(self.x_tainshapel 1)
def negative_lik(elf, betas):
return -1 *sel.uk(betas)
def like, betas)"™ Likelihood ofthe data under the current settings of parameters.
4 Data likelihood
for {in range(self.n):
1 log(sigmoie(selfy_train(i]*
padorbetas, slf.x_saini:)))
# Prior likelihood
fork in range(t, selfx train shape( 1
= (selfalpha /2.0)* self.betas{k]**2def train(self):
*" Define the gradient and hand it off to a scipy gradient-based
‘optimizer
4 Define the derivative ofthe likelihood with respect to beta_k.
+ Need to multiply by -I because we will be minimizing
{dB _ke= lambda B, ke: np.sum({-self.alpha * Btk] +
sel,y train) * sel£x tain, k] *
sigmoid(sel.y-train{i]*
pdot(, sex tai)
for ‘in range(self.n)) * -1
+# The fll gradient is just an array of componentwise derivatives«dB = lambda B : npartay((aB_K(B, k)
for cin range(selfx train shapel1)))
# Optimize
self.bet
imin_bfgs(self.negative_lik, self.betas,fprime=dB)
def set_data(self, x_tain, y_train, x test, y_tes)
‘Take data that’s already been generated."
self train
train
selfy trai
selfx_test = x test
selfy_test= y_test
self = y-train shape( 0)def training reconstruction(selt):
p_yl = np.zeros(self.n)
for {in range(selt.n:
p_ylll] = sigmotd(np dotself. beta, selfx_tranli))
return py
def test_predictionsself):
yl = np.zeros(self.n)
for iin range(self.n):
yl = sigmoid(ap douse betas sex testi)
return p_yldef plot_training_reconstruction(sel)
plotinp.arange(sel.n),.5 +5 * selty_teain, "bo.
plot(np.arange(selfn, self.raining_reconstuction(), x’)
ylim((-1, 1.1),
def plot_test_predictions(sl)
plog(np.arange(self.n), 5 +5 sey test, yo')
plow(np.arange(selfn), self.est_predictions), 1x)
slim((-1, 1.10,
if __ame__=="_main_’
{rom pylab import *
‘Create 20 dimensional data set with 25 points ~ this will be
4 susceptible to overfittngdata
yntheticClassifierData(25, 20)
1# Run fora variety of regularization strengths
alphas = [0,.001, 01, .1]
for j, ain enumerate(alphas)
‘# Create a new learner, but use the same data for each run
logisticRegression(x_trel
x test=data.X_tst, y_te
alpha=a)
print "Initial likelihood
print rstk(ibetas)Train the model
Iearain)
4 Display execution info
print "Final betas:
print Inbetas
print "Final lk
print rsik(icbetas)
4 Plot the results
subplot(len(alphas), 2, 2+] + 1)
Ir-plot_training_reconstruction()
‘ylabel("Alpha=%s" 96 a)sile("Training set reconstructions")
subplot(len(alphas), 2, 24) +2)
Irplot_test_predictions()
tile("Test set predictions")
show)CHAPTER 12
CAN PYTHON WEB APPLICATIONS BE TESTED USING
SELENIUM?
Python is
currently more popular than other modem programming languages. The interpreted and object-
oriented programming language is also hugely popular among developers across the world as a
strong server side scripting language. As Python enables developers to express concepts by
‘writing less and readable code, it becomes easier for programmers to reduce the development
time significantly. At the same time, the developers also have option to use popular web
frameworks like Django to create high-performing and complex Python web applications
rapidly. However, the developers stll need 10 assess the look, feel and performance of the
Python web application thoroughly to boost its popularity and profitability
‘While testing the intemet applications the developers have option to choose from a number of
browser automation tools like PAMIE, PyXPCOM, windmill, SST and Selenium. But most
‘developers prefer Selenium to other frameworks to tes thelr Python web applications efficiently
Unlike other web browser automation tools, Selenium allows testing professionals to write test
Scripts in a number of languages including Python, Ct, Java, PHP, Ruby and Python. So the
testers have option to test the Python web application by writing test scripts in Python. There are
also a number of reasons why developers across the world use Selenium for testing Python webapplications
‘Why QA Professionals Prefer Using Selenium for Testing Python Web Applications?
‘Supports Major Operating Systems and Web Browsers
At present, Selenium supports all major operating systems and web browsers. The framework
‘curently supports both Microsoft Windows and Linux. Likewise, itis compatible with most
popular web browsers like Firefox, Chrome, Internet Explorer, Safari and Opera. The
‘compatibility makes it easier for QA professionals to test the Python web application tools across
multiple platforms and web browsers without writing separate codes, or using addi
automation tools. Selenium further comes with features to generate and execute test scripts
nal test.
automatically across different web browsers and systems simultaneously
Allows Users to Create Complete Test Automation Suite
‘The Selenium testing professionals can create a complete test automation suite by combining
Selenium WebDriver and Selenium IDE. They can use Selenium WebDriver to quickly create
browser-based regression automation suites and tests Further, they can scale and distribute the
test crip across multiple environments. The Selenium IDE, on the other hand, makes it easier
for testers to create bug-reproducton scripts rapidly. Thus, the QA professionals can combine
listinct parts of Selenium to create a complete est automation too, without req ulring any
licensed or third-party APIs.EXECUTES TESTS FASTER.
‘To identify all bugs and performance issues in the web applications, QA professionals have to
perform tests repeatedly and freq wently. But the testers also have to complete all tests within a
limited amount of time, Selenium allows testing professionals to take advantage of cloud-based
testing grids to boost the performance of thelr test uns. In alton to optimizing the test
Infrastructure, these tools further enable testes to run parallel tess. Thus, it becomes easier for
the testers to execute tests quickly and repeatedly. The testers also have option to choose from
several open source cloud-based functional testing grids to avoid increased project overheads.
Requires Basic HTML Concepts
Selenium supports a number of modern programming languages. But while testing a Python web
application, i re q wires only basic HTML concepts. HTML is used for describing a web page,
whereas individual HTML tags represent document content. Thus, HTTML tags decide how the
‘content is appears on the web browsers. Selenium divides the HTML. elements or attributes into
three distinct categories, i, single, group and customized. I locates single elements by their id,
link or link text, whereas the group elements are identified based on combined values or index
property, So it becomes easier for testers to find out the location of the defector bug. The feature
makes it easier for them to identify the exact bugs and performance issues q uickly.
Helps Testers to Address Maintainability Issues
{Im addition to creating and executing test scripts quickly, QA professionals are also re q uired to
‘maintain the test cases effectively. Selenium helps testers co overcome maintainability issues by
structuring the automated test code using a pater called page objects. The page objects focuses
‘onthe structure of HTML code of a particular web page instead of checking how the services are
implemented. Thus, testers can take advantage of page objects to locate the code easily, navigate
between vatious web pages smoothly, and making changes only once. As most Selenium code
ill be located inside page objects, the testers can easily increase the code base without adding,
fresh Selenium code.
Provides Selenium Python API
‘As noted earlier, Python supports several programming languages including Python. So thetesters have option to write test scripts in Python. Also, they can use Selenium Python APL to
‘write acceptance and functional tests by accessing Selenium WebDrivers like common, suppor,
chrome, Firefox, ie, emote and phantomjs, The most recent version of the API further supports
‘multiple versions of Python including 3.2, 3.3, 3.4 and 2.7, I can further be accessed simply by
downloading and installing the Selenium Python bindings. Thus, an organization can leverage
the skills of existing Python programmers to perform acceptance and functional testing
efficiently
‘Works wih several esting frameworks
‘While testing the Python web application with Selenium, QA professionals have option to use a
numberof tes framework. At present, the portable web browser automation framework works
‘with Pytest,PyUnit, unittest, and robot framework. As a part of Python 2.1 standard library,
PyUnit enables testers to write tests easily, and execute multiple tests in text or GUL mode.
Likewise, pytest comes with a number of features that help testers to write beter programs. So
the QA professional can take advantage ofthese test frameworks to ensure thatthe Python web
application delivers flawless user experience across many web browsers.
Python isan open source programming language, whereas Selenium is an open source web
‘browser automation tool. Thus, organizations can use the programming language and web testing
tool together to bring down the project costs. However, the combination will further enable them
{est the application across major web browsers within a stipulated amount of time,CHAPTER 13
PERL AND PYTHON
Both Python and Perl are mature, open source, general purpose, high level, and interpreted
programming languages. Bur de usage stalstcs posted on various websites depict ”hat Python Is
ccarently more popu
bby switching form Perl co Python.
than Perl, Hence, a software developer can enhance his carver prospects
A beginner can further learn and use Python programming language without potting extra time
and effort. However, vou must not switch to a new programming language just because its
popalarty and usage. You must keepin mind the major differences between the reo
programming languages while deciding about migrating from Pet! to Python,
Points You Must Keep in Mind while Switching, from Perl ta Python
1) Design Goal
Perl was originally designed as a scripting language to simplify report processing capabilities.
Hence, it comes with built-in text processing capability. On the other hand, Python was designed
inigally asa bobby programming language. But it was designed with features to help‘programmers build applications with concise, readable and reusable code. The two programming
languages still diffe in the category of features and performance.
2) Symtax Rules
‘The syntax rules of both Python and Perl are influenced by several other programming,
languages. For instance, Perl borrows features from a number of programming languages
including C, shell script, sed, AWK and Lisp. Likewise, Python implements functional
‘rogramming features in a manner similar to Lisp. But Python is hugely popular among modem
programming languages due to its simple syntax rules. In addition to being easy to use, the
‘syntax rules of Python further enable programmers to except many concepts with less and
readable code.
3) Family of Languages
Perl belongs o a family of high-level programming languages that includes Perl and Perl 6
“The versions 5 and 6 of Perl are compatible with each other. A developer can easily migrate from
Perl Sto Perl 6 without putting extra time and effort. The programmers have option to choose
from two distinct versions of Python - Python 2 and Python 2. But the wo versions of Python are
not compatible with each other. Hence, a programmer has to choose from two distinct versions
‘of the programming language
4) Ways to Achieve Same Results
Python enables programmers to express concepts without writing longer lines of code, But it
req uites programmers to accomplish asks or achieve results in a specific and single way. ON
the other hand, Per! enable programmers to accomplish a single task or achieve the same results
ina number of ways. Hence, many programmers find Per! 10 be more flexible than Python. But
the multiple ways to achieve the same result often make the code written in Perl messy and
application difficult to maintain,
'5) Web Scripting Language
Perl was originally designed as a UNIX scripting language. Many developers use Perl asa
‘scripting language to avail ts builtin text processing capabilities. However, there are many web
{developers who complain that Perl is slower than other widely used scripting language. Python is
also used widely by programmers for web application development. But it lacks built-in webdevelopment capabilities. Hence, developers have to avail various frameworks and tools to write
Python efficiently and rapidly.
‘web application
{6) Web Application Frameworks
Most developers nowadays aval the tools and features provided by various
neworks to build
‘web applications efficiently and rapidly. Perl web programmers have option to choose from an
array of frameworks including Catalyst, Dancer, Mojoliciou, Poet, Imerchange,Jfty, and
Gantry, Likewise, the web developers also have option to use a numberof Python web
frameworks including Django, Flask, Pyramid, Botle and Cherrypy. However, the number of
Pytlion web framework is much higher than the number of Peel web frameworks,
7) Usage
‘As mentioned earlier, both Python and Perl are general-purpose programt
ing languages. Hence,
‘each programming language is used for developing a variety of software applications. Pet is,
used widely for graphic and network programming, system administration, and development of
finance and biometic applications. But Python comes with a robust standard library simplifies
artificial
g Python for development of advanced and
‘web application developmen, scientific computing, big data solution development, a
ielligence tasks. Hence, developers prefer
mission-critical software applications.
£8) Performance and Speed
‘A number of studies have shown than Python is slower than other programming languages like
Java and C++. Hence, developers frequently explore ways to enhance the execution speed of
Python code. Some developers even replace default Python runtime
their own custom
runtime to make the Python applications run faster. Many programmers even find Perl to be
faster than Python. Many web developers use Petl asa scripting language make the web
applications faster, and deliver enhanced user experience
9) Structured Data Analysis
At present, big data is one of the hottest trends in software development. Many enterprises
nowadays build custom applications for collecting, storing, and analyzing huge amount of
structured and unstructured data. The PDL provided by Pet! enables developers to analyze big,data. The built-in text processing capability of Perl further simplifies and speeds up analysis of,
huge amount of structured data, But Python is used widely by programmers for data analysis
The developers further take advantage of robust Python libraries like Numpy t process and,
analyze huge volumes of data in a faster and more efficient way.
10) JVM Interoperability
At present, Java is one of the programming languages that are used widely for development of
desktop, web, and mobile applications. In comparison to Per, Python Interoperates with Java
Virtual Machine (IVM) seamlessly and efficiently. Hence, the developers have option to write
Python code than runs smoothly on JVM, while taking advantage of robust Java APIs and
objects. The interoperability helps programmers to build application by targeting the popular
Java platform, while writing code in Python instead of Java.
11) Advanced Object Oriented Programming
Both Per! and Python are object-oriented programming languages. But Python implements
advanced object oriented programming languages in a better way than Perl. While writing code
ln Perl, programmers still need to use packages instead of classes. Python programmers can write
high quality and modular code by using classes and abjects. Many developers find i difficult to
keep the code simple and readable while writing object oriented code in Perl. But Pet! makes it
easier for programmers to accomplish a variety of tasks simply by using one lines on the
command line.
12) Text Processing Capability
Unlike Python, Perl was designed with built-in ext processing capabilites. Hence, many
programmers prefer using Perl for report generation, Perl Further makes it easier for
programmers to perform regex and string comparison operations like matching, replacement, and
substitution. Ic further does notre q wire developers to write additional code to perform exception
handling and 1/0 operations. Hence, many programmers prefer Per to Python while building
applications that need to process te
3 data or generate reports
(On the whole, a large number of modem software developers prefer Python to Perl. But there are
a number of programming languages - Java, C, C++ and CH which are currently more popular
| |