Python-Sem5 (Pranv Sir)
Python-Sem5 (Pranv Sir)
BCA Semester - 5
Programing in Python
Unit-1
Introduction to Python
Page 1 of 63
Overview of Python
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 Object-Oriented − Python supports Object-Oriented style or technique of programming
that encapsulates code within objects.
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 3.0 (Emphasis on removing duplicative constructs and module) December 3, 2008
Python 3.5 (Last updated version) September 13, 2015
Python Features
A simple language which is easier to learn
Python has a very simple and elegant syntax. It's much easier to read and write Python programs compared
to other languages like: C++, Java, C#. Python makes programming fun and allows you to focus on the
solution rather than syntax. If you are a newbie, it's a great choice to start your journey with Python.
Free and open-source: You can freely use and distribute Python, even for commercial use. Not only can you
use and distribute software written in it, you can even make changes to the Python's source code. Python
has a large community constantly improving it in each iteration.
Portability: You can move Python programs from one platform to another, and run it without any changes. It
runs seamlessly on almost all platforms including Windows, Mac OS X and Linux.
Extensible and Embeddable: Suppose an application requires high performance. You can easily combine
pieces of C/C++ or other languages with Python code. This will give your application high performance as
well as scripting capabilities which other languages may not provide out of the box.
Page 2 of 63
A high-level, interpreted language: Unlike C/C++, you don't have to worry about daunting tasks like memory
management, garbage collection and so on. Likewise, when you run Python code, it automatically converts your
code to the language your computer understands. You don't need to worry about any lower-level operations.
Large standard libraries to solve common tasks: Python has a number of standard libraries which makes life of a
programmer much easier since you don't have to write all the code yourself. For example: Need to connect MySQL
database on a Web server? You can use MySQLdb library using import MySQLdb . Standard libraries in Python are
well tested and used by hundreds of people. So you can be sure that it won't break your application.
Object-oriented: Everything in Python is an object. Object oriented programming (OOP) helps you solve a complex
problem intuitively. With OOP, you are able to divide these complex problems into smaller sets by creating objects.
Python Environment Setup: Python is available on a wide variety of platforms including Linux and Mac OS X. Let's
understand how to set up our Python environment.
Applications of Python
Web Applications: You can create scalable Web Apps using frameworks and CMS (Content Management System)
that are built on Python. Some of the popular platforms for creating Web Apps are: Django, Flask, Pyramid, Plone,
Django [Link] like Mozilla, Reddit, Instagram and PBS are written in Python.
Scientific and Numeric Computing: There are numerous libraries available in Python for scientific and numeric
computing. There are libraries like: SciPy and NumPy that are used in general purpose computing. And, there are
specific libraries like: EarthPy for earth science, AstroPy for Astronomy and so [Link], the language is heavily used in
machine learning, data mining and deep learning.
Creating software Prototypes: Python is slow compared to compiled languages like C++ and Java. It might not be a
good choice if resources are limited and efficiency is a [Link], Python is a great language for creating
prototypes. For example: You can use Pygame (library for creating games) to create your game's prototype first. If
you like the prototype, you can use language like C++ to create the actual game.
Good Language to Teach Programming: Python is used by many companies to teach programming to kids and
[Link] is a good language with a lot of features and capabilities. Yet, it's one of the easiest languages to learn
because of its simple easy-to-use syntax.
Getting Python
The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official
website of Python [Link] You can download Python documentation from
[Link] The documentation is available in HTML, PDF, and PostScript formats.
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 [Link] 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.
Page 3 of 63
[Link]. Variable & Description
1 PYTHONPATH
It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files 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.
2 PYTHONSTARTUP
It contains the path of an initialization file containing Python source code. It is executed every time you start the
interpreter. It is named as .[Link] in Unix and it contains commands that load utilities or modify
PYTHONPATH.
3 PYTHONCASEOK
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.
4 PYTHONHOME
It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH
directories to make switching module libraries easy.
1. Immediate mode: Typing python in the command line will invoke the interpreter in immediate mode.
We can directly type in Python expressions and press enter to get the output. >>>is the Python prompt.
It tells us that the interpreter is ready for our input. Try typing in 1 + 1 and press enter. We get 2 as the
output. This prompt can be used as a calculator. To exit this mode type exit() or quit() and press enter.
2. Script mode: This mode is used to execute Python program written in a file. Such a file is called a script.
Scripts can be saved to disk for future use. Python scripts have the extension .py, meaning that the
filename ends with .[Link] execute this file in script mode we simply write python [Link] at the
command [Link] can use any text editing software to write a Python script file.
Page 4 of 63
We just need to save it with the .py extension. But using an IDE can make our life a lot easier. IDE is a piece
of software that provides useful features like code hinting, syntax highlighting and checking, file explorers
etc. to the programmer for application development. Using an IDE can get rid of redundant tasks and
significantly decrease the time required for application development. IDLE is a graphical user interface (GUI)
that can be installed along with the Python programming language and is available from the official website.
Python Keywords
Keywords are the reserved words in Python. We cannot use a keyword as variable name, function name or
any other identifier. They are used to define the syntax and structure of the Python language. In Python,
keywords are case sensitive. There are 33 keywords in Python 3.3. This number can vary slightly in course of
time. All the keywords except True, False and None are in lowercase and they must be written as it is. The
list of all the keywords are given below.
Keywords in Python programming language
as elif if or yield
Looking at all the keywords at once and trying to figure out what they mean might be overwhelming. If you
want to have an overview, here is the complete list of all the keywords with examples.
Python Identifiers
Identifier is the name given to entities like class, functions, variables etc. in Python. It helps differentiating
one entity from another.
Python Indentation
Most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses
indentation. A code block (body of a function, loop etc.) starts with indentation and ends with the first
Page 5 of 63
unindebted line. The amount of indentation is up to you, but it must be consistent throughout that block.
Generally four whitespaces are used for indentation and is preferred over tabs. Here is an example.
for i in range(1,11):
print(i)
if i == 5:
break
The enforcement of indentation in Python makes the code look neat and clean. This results into Python
programs that look similar and consistent. Indentation can be ignored in line continuation. But it's a good
idea to always indent. It makes the code more readable.
Python Comments
Comments are very important while writing a program. It describes what's going on inside a program so that
a person looking at the source code does not have a hard time figuring it out. You might forget the key
details of the program you just wrote in a month's time. So taking time to explain these concepts in form of
comments is always fruitful. In Python, we use the hash (#) symbol to start writing a comment. It extends up
to the newline character. Comments are for programmers for better understanding of a program. Python
Interpreter ignores comment.
Multi-line comments
If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of
each line. Another way of doing this is to use triple quotes, either ''' or """. These triple quotes are generally
used for multi-line strings. But they can be used as multi-line comment as well. Unless they are not
docstrings, they do not generate any extra code.
Docstring in Python
Docstring is short for documentation string. It is a string that occurs as the first statement in a module,
function, class, or method definition. We must write what a function/class does in the docstring. Triple
quotes are used while writing docstrings. For example:
def double(num):
"""Function to double the value"""
return 2*num
Docstring is available to us as the attribute __doc__ of the function. Issue the following code in shell once
you run the above program.
print(double.__doc__)
Variable
In most of the programming languages a variable is a named location used to store data in the memory. Each
variable must have a unique name called identifier. It is helpful to think of variables as container that hold
data which can be changed later throughout programming. None technically, you can suppose variable as a
bag to store books in it and those books can be replaced at any [Link]: In Python we don't assign values
to the variables, whereas Python gives the reference of the object (value) to the variable.
Constants
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as
containers that hold information which cannot be changed [Link] technically, you can think of constant
as a bag to store some books and those books cannot be replaced once placed inside the bag.
Literals
Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as
follows:
Numeric Literals
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types
Integer, Float and Complex.
Page 6 of 63
String literals
A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple
quotes for a string. And, a character literal is a single character surrounded by single or double quotes.
Boolean literals
A Boolean literal can have any of the two values: True or False.
Special literals
Python contains one special literal i.e. None. We use it to specify to that field that is not created.
Python List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the
items in a list do not need to be of the same type. Declaring a list is pretty straight forward. Items separated
by commas are enclosed within brackets [ ].
a = [1, 2.2, 'python']
>>> a = [1,2,3]
>>> a[2]=4
>>> a
[1, 2, 4]
Python Tuple
Tuple is an ordered sequence of items same as list. The only difference is that tuples are immutable. Tuples
once created cannot be modified. Tuples are used to write-protect data and are usually faster than list as it
cannot change dynamically. It is defined within parentheses () where items are separated by commas.
t = (5,'program', 1+3j)
Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings.
Multi-line strings can be denoted using triple quotes, ''' or """. Like list and tuple, slicing operator [ ] can be
used with string. Strings are immutable.
Python Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }.
Items in a set are not ordered. We can perform set operations like union, intersection on two sets. Set have
unique values. They eliminate duplicates.
>>> a = {1,2,2,3,3,3}
>>> a
{1, 2, 3}
Python Dictionary
Dictionary is an unordered collection of key-value pairs. It is generally used when we have a huge amount of
data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. In Python,
dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can
be of any type.
>>> d = {1:'value','key':2}
>>> type(d)
<class 'dict'>
We use key to retrieve the respective value. But not the other way around.
Page 7 of 63
Conversion between data types
We can convert between different data types by using different type conversion functions like int(), float(),
str() etc.
Output formatting
Sometimes we would like to format our output to make it look attractive. This can be done by using the
[Link]() method. This method is visible to any string object.
Python Input
Up till now, our programs were static. The value of variables were defined or hard coded into the source
code. To allow flexibility we might want to take the input from the user. In Python, we have the input()
function to allow this. The syntax for input() is
input([prompt])
Python Import
When our program grows bigger, it is a good idea to break it into different modules.
A module is a file containing Python definitions and statements. Python modules have a filename and end
with the extension .py. Definitions inside a module can be imported to another module or the interactive
interpreter in Python. We use the import keyword to do this. For example, we can import the math module
by typing in import math.
import math
print([Link])
Now all the definitions inside math module are available in our scope. We can also import some specific
attributes and functions only, using the from keyword.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication
etc.
/ Divide left operand by the right one (always results into float) x/y
x % y (remainder of
% Modulus - remainder of the division of left operand by the right
x/y)
Floor division - division that results into whole number adjusted to the
// x // y
left in the number line
** Exponent - left operand raised to the power of right x**y (x to the power y)
Page 8 of 63
Comparison operators
Comparison operators are used to compare values. It either returns True or False according to the condition.
> Greater that - True if left operand is greater than the right x>y
< Less that - True if left operand is less than the right x<y
>= Greater than or equal to - True if left operand is greater than or equal to the right x >= y
<= Less than or equal to - True if left operand is less than or equal to the right x <= y
Logical operators
Logical operators are the and, or, not operators.
Bitwise operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the
name. For example, 2 is 10 in binary and 7 is 111. In the table below: Let x = 10 (0000 1010 in binary) and y =
4 (0000 0100 in binary)
Assignment operators
Assignment operators are used in Python to assign values to variables. a = 5 is a simple assignment operator
that assigns the value 5 on the right to the variable a on the left. There are various compound operators in
Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.
Page 9 of 63
Operator Example Equivatent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
Identity operators
is and is not are the identity operators in Python. They are used to check if two values (or variables) are
located on the same part of the memory. Two variables that are equal does not imply that they are identical.
is True if the operands are identical (refer to the same object) x is True
is not True if the operands are not identical (do not refer to the same object) x is not True
Membership operators
in and not in are the membership operators in Python. They are used to test whether a value or variable is
found in a sequence (string, list, tuple, set and dictionary). In a dictionary we can only test for presence of
key, not the value.
Page 10 of 63
What is a Namespace in Python?
So now that we understand what names are, we can move on to the concept of namespaces. To simply put
it, namespace is a collection of names. In Python, you can imagine a namespace as a mapping of every name,
you have defined, to corresponding objects. Different namespaces can co-exist at a given time but are
completely isolated. A namespace containing all the built-in names is created when we start the Python
interpreter and exists as long we don't exit. This is the reason that built-in functions like id(), print() etc. are
always available to us from any part of the program. Each module creates its own global namespace.
These different namespaces are isolated. Hence, the same name that may exist in different modules do not
collide. Modules can have various functions and classes. A local namespace is created when a function is
called, which has all the names defined in it. Similar, is the case with class. Following diagram may help to
clarify this concept.
Here, the variable a is in the global namespace. Variable b is in the local namespace of outer_function() and c
is in the nested local namespace of inner_function(). When we are in inner_function(), c is local to us, b is
nonlocal and a is global. We can read as well as assign new values to c but can only read b and a from
inner_function(). If we try to assign as a value to b, a new variable b is created in the local namespace which
is different than the nonlocal b. Same thing happens when we assign a value to a. However, if we declare a
as global, all the reference and assignment go to the global a. Similarly, if we want to rebind the variable b, it
must be declared as nonlocal.
Here, val is the variable that takes the value of the item inside the sequence on each iteration. Loop
continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the
code using indentation.
Page 12 of 63
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop
does not terminate but continues on with the next iteration. Syntax of Continue
continue
def function_name(parameters):
"""docstring"""
statement(s)
Above shown is a function definition which consists of following components.
For example:
def greet(name):
"""This function greets to
the person passed in as
parameter"""
print("Hello, " + name + ". Good morning!")
Page 13 of 63
The return statement
The return statement is used to exit a function and go back to the place from where it was called. Syntax of
return:
return [expression_list]
This statement can contain expression which gets evaluated and the value is returned. If there is no
expression in the statement or the return statement itself is not present inside a function, then the function
will return the None object.
x = 20
my_func()
print("Value outside function:",x)
Output:
Value inside function: 10
Value outside function: 20
Here, we can see that the value of x is 20 initially. Even though the function my_func() changed the value of
x to 10, it did not effect the value outside the function. This is because the variable x inside the function is
different (local to the function) from the one outside. Although they have same names, they are two
different variables with different scope. On the other hand, variables outside of the function are visible from
inside. They have a global scope. We can read these values from inside the function but cannot change
(write) them. In order to modify the value of variables outside the function, they must be declared as global
variables using the keyword global.
Types of Functions
Basically, we can divide functions into the following two types:
Built-in functions - Functions that are built into Python.
User-defined functions - Functions defined by the users themselves.
In the above example, calc_factorial() is a recursive functions as it calls itself. When we call this function with
a positive integer, it will recursively call itself by decreasing the number. Each function call multiples the
number with the factorial of number 1 until the number is equal to one. This recursive call can be explained
in the following steps.
Advantages of Recursion
1. Recursive functions make the code look clean and elegant.
2. A complex task can be broken down into simpler sub-problems using recursion.
3. Sequence generation is easier with recursion than using some nested iteration.
Disadvantages of Recursion
1. Sometimes the logic behind recursion is hard to follow through.
2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
3. Recursive functions are hard to debug.
What is a file?
File is a named location on disk to store related information. It is used to permanently store data in a non-
volatile memory (e.g. hard disk). Since, random access memory (RAM) is volatile which loses its data when
computer is turned off, we use files for future use of the data. When we want to read from or write to a file
we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the
file are freed. Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform operation)
3. Close the file
Mode Description
'w' Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
'x' Open a file for exclusive creation. If the file already exists, the operation fails.
Page 16 of 63
'a' Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
f = open("[Link]",encoding = 'utf-8')
# perform file operations
[Link]()
Method Description
close() Close an open file. It has no effect if the file is already closed.
Page 17 of 63
detach() Separate the underlying binary buffer from the TextIOBaseand return it.
Read atmost n characters form the file. Reads till end of file if it is
read(n)
negative or None.
Read and return one line from the file. Reads in at most nbytes if
readline(n=-1)
specified.
Resize the file stream to size bytes. If size is not specified, resize to
truncate(size=None)
current location.
write(s) Write string s to the file and return the number of characters written.
Page 18 of 63
implement two special methods, __iter__() and __next__(), collectively called the iterator protocol. An
object is called iterable if we can get an iterator from it. Most of built-in containers in Python like: list, tuple,
string etc. are iterables. The iter() function (which in turn calls the __iter__() method) returns an iterator
from them.
#prints 7
print(next(my_iter))
## next(obj) is same as obj.__next__()
#prints 0
print(my_iter.__next__())
#prints 3
print(my_iter.__next__())
## This will raise error, no items left
next(my_iter)
Page 19 of 63
yield n
n += 1
print('This is printed second')
yield n
n += 1
print('This is printed at last')
yield n
Page 20 of 63
CS- 33Programing in Python
Unit-2
OOP Using Python
CS-33: Programming in Python
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when
corresponding errors occur. This will return us a dictionary of built-in exceptions, functions and attributes.
Some of the common built-in exceptions in Python programming along with the error that cause then are
tabulated below.
KeyboardInterrupt Raised when the user hits interrupt key (Ctrl+c or delete).
RuntimeError Raised when an error does not fall under any other category.
ValueError Raised when a function gets argument of correct type but improper value.
We can also define our own exception in Python (if required). Visit this page to learn more about user-
defined exceptions. We can handle these built-in and user-defined exceptions in Python using try, except
and finally statements.
Page 2 of 63
CS-33: Programming in Python
# import module sys to get the type of exception
import sys
randomList = ['a', 0, 2]
In this program, we loop until the user enters an integer that has a valid reciprocal. The portion that can
cause exception is placed inside try [Link] no exception occurs, except block is skipped and normal flow
continues. But if any exception occurs, it is caught by the except [Link], we print the name of the
exception using ex_info() function inside sys module and ask the user to try again. We can see that the
values 'a' and '1.3' causes ValueError and '0' causes ZeroDivisionError.
try...finally
The try statement in Python can have an optional finally clause. This clause is executed no matter what, and
is generally used to release external resources. For example, we may be connected to a remote data center
through the network or working with a file or working with a Graphical User Interface (GUI). In all these
circumstances, we must clean up the resource once used, whether it was successful or not. These actions
(closing a file, GUI or disconnecting from network) are performed in the finally clause to guarantee
execution. Here is an example of file operations to illustrate this.
try:
f = open("[Link]",encoding = 'utf-8')
# perform file operations
finally:
Page 3 of 63
[Link]()
This type of construct makes sure the file is closed even if an exception occurs.
In Python, users can define such exceptions by creating a new class. This exception class has to be derived,
either directly or indirectly, from Exception class. Most of the built-in exceptions are also derived form this
class. When we are developing a large Python program, it is a good practice to place all the user-defined
exceptions that our program raises in a separate file. Many standard modules do this. They define their
exceptions separately as [Link] or [Link] (generally but not always).
User-defined exception class can implement everything a normal class can do, but we generally make them
simple and concise. Most implementations declare a custom base class and derive others exception classes
from this base class. This concept is made clearer in the following example.
Page 4 of 63
CS-33: Programming in Python
It is also a debugging tool as it brings the program on halt as soon as any error is occurred and shows on
which point of the program error has occurred. You can learn more about assertions in the article: The
benefits of programming with Assertions. We can be clear by looking at the flowchart below:
Polymorphism A concept of using common operation in different ways for different data input.
Class
A class is a blueprint for the object. We can think of class as an sketch of a parrot with labels. It contains all
the details about the name, colors, size etc. Based on these descriptions, we can study about the parrot.
Here, parrot is an object. The example for class of parrot can be :
class Parrot:
pass
Here, we use class keyword to define an empty class Parrot. From class, we construct instances. An instance
is a specific object created from a particular class.
Object
An object (instance) is an instantiation of a class. When class is defined, only the description for the object is
defined. Therefore, no memory or storage is allocated. The example for object of parrot class can be:
obj = Parrot()
Here, obj is object of class Parrot. Suppose we have details of parrot. Now, we are going to show how to
build the class and objects of parrot. Example to Creating Class and Object in Python:
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
[Link] = name
[Link] = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
Page 6 of 63
CS-33: Programming in Python
# access the instance attributes
print("{} is {} years old".format( [Link], [Link]))
print("{} is {} years old".format( [Link], [Link]))
When we run the program, the output will be:
Blu is a bird
Woo is also a bird
Blu is 10 years old
Woo is 15 years old
In the above program, we create a class with name Parrot. Then, we define attributes. The attributes are a
characteristic of an object. Then, we create instances of the Parrot class. Here, blu and woo are references
(value) to our new objects.
Then, we access the class attribute using __class __.species. Class attributes are same for all instances of a
class. Similarly, we access the instance attributes using [Link] and [Link]. However, instance attributes
are different for every instance of a class. To learn more about classes and objects, go to Python Classes and
Objects
Methods
Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.
Example to Creating Methods in Python:
class Parrot:
# instance attributes
def __init__(self, name, age):
[Link] = name
[Link] = age
# instance method
def sing(self, song):
return "{} sings {}".format([Link], song)
def dance(self):
return "{} is now dancing".format([Link])
# instantiate the object
blu = Parrot("Blu", 10)
# call our instance methods
print([Link]("'Happy'"))
print([Link]())
When we run program, the output will be:
Blu sings 'Happy'
Blu is now dancing
In the above program, we define two methods i.e sing() and dance(). These are called instance method
because they are called on an instance object i.e blu.
Inheritance
Inheritance is a way of creating new class for using details of existing class without modifying it. The newly
formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent
class).Example to Use of Inheritance in Python:
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
Page 7 of 63
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
[Link]()
[Link]()
[Link]()
When we run this program, the output will be:
Bird is ready
Penguin is ready
Penguin
Swim faster
Run faster
In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The child class
inherits the functions of parent class. We can see this from swim() method. Again, the child class modified
the behavior of parent class. We can see this from whoisThis() method. Furthermore, we extend the
functions of parent class, by creating a new run() [Link], we use super() function before
__init__() method. This is because we want to pull the content of __init__() method from the parent class
into the child class.
Encapsulation
Using OOP in Python, we can restrict access to methods and variables. This prevent data from direct
modification which is called encapsulation. In Python, we denote private attribute using underscore as prefix
i.e single “ _ “ or double “ __“.Example for Data Encapsulation in Python:
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Computer()
[Link]()
# change the price
c.__maxprice = 1000
[Link]()
# using setter function
[Link](1000)
[Link]()
When we run this program, the output will be:
Selling Price: 900
Selling Price: 900
Selling Price: 1000
In the above program, we defined a class Computer. We use __init__() method to store the maximum selling
price of computer. We tried to modify the price. However, we can’t change it because Python treats the
__maxprice as private attributes. To change the value, we used a setter function i.e setMaxPrice() which
takes price as parameter.
Polymorphism
Polymorphism is an ability (in OOP) to use common interface for multiple form (data types). Suppose, we
need to color a shape, there are multiple shape option (rectangle, square, circle). However we could use
same method to color any shape. This concept is called Polymorphism. Example for Using Polymorphism in
Python:
Page 8 of 63
CS-33: Programming in Python
class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
[Link]()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)
When we run above program, the output will be:
Parrot can fly
Penguin can't fly
In the above program, we defined two classes Parrot and Penguin. Each of them have common method fly()
method. However, their functions are different. To allow polymorphism, we created common interface i.e
flying_test() function that can take any object. Then, we passed the objects blu and peggy in the flying_test()
function, it ran effectively.
class MyClass:
"This is my second class"
Page 9 of 63
a = 10
def func(self):
print('Hello')
# Output: 10
print(MyClass.a)
# Output: <function [Link] at 0x0000000003079BF8>
print([Link])
# Output: 'This is my second class'
print(MyClass.__doc__)
When you run the program, the output will be:
10
<function 0x7feaa932eae8="" at="" [Link]="">
This is my second class
Creating an Object in Python
We saw that the class object could be used to access different attributes. It can also be used to create new
object instances (instantiation) of that class. The procedure to create an object is similar to a function call.
>>> ob = MyClass()
This will create a new instance object named ob. We can access attributes of objects using the object name
prefix. Attributes may be data or method. Method of an object are corresponding functions of that class. Any
function object that is a class attribute defines a method for objects of that class. This means to say, since
[Link] is a function object (attribute of class), [Link] will be a method object.
class MyClass:
"This is my second class"
a = 10
def func(self):
print('Hello')
# create a new MyClass
ob = MyClass()
# Output: <function [Link] at 0x000000000335B0D0>
print([Link])
# Output: <bound method [Link] of <__main__.MyClass object at
0x000000000332DEF0>>
print([Link])
# Calling function func()
# Output: Hello
[Link]()
You may have noticed the self parameter in function definition inside the class but, we called the method
simply as [Link]() without any arguments. It still worked. This is because, whenever an object calls its
method, the object itself is passed as the first argument. So, [Link]() translates into [Link](ob). In
general, calling a method with a list of n arguments is equivalent to calling the corresponding function with
an argument list that is created by inserting the method's object before the first argument.
For these reasons, the first argument of the function in class must be the object itself. This is conventionally
called self. It can be named otherwise but we highly recommend to follow the convention. Now you must be
familiar with class object, instance object, function object, method object and their differences.
Constructors in Python
Class functions that begins with double underscore (__) are called special functions as they have special
meaning. Of one particular interest is the __init__() function. This special function gets called whenever a
new object of that class is instantiated. This type of function is also called constructors in Object Oriented
Programming (OOP). We normally use it to initialize all the variables.
class ComplexNumber:
def __init__(self,r = 0,i = 0):
[Link] = r
[Link] = i
def getData(self):
print("{0}+{1}j".format([Link],[Link]))
Page 10 of 63
CS-33: Programming in Python
# Create a new ComplexNumber object
c1 = ComplexNumber(2,3)
# Call getData() function
# Output: 2+3j
[Link]()
# Create another ComplexNumber object
# and create a new attribute 'attr'
c2 = ComplexNumber(5)
[Link] = 10
# Output: (5, 0, 10)
print(([Link], [Link], [Link]))
# but c1 object doesn't have attribute 'attr'
# AttributeError: 'ComplexNumber' object has no attribute 'attr'
[Link]
In the above example, we define a new class to represent complex numbers. It has two functions, __init__()
to initialize the variables (defaults to zero) and getData() to display the number [Link] interesting thing
to note in the above step is that attributes of an object can be created on the fly. We created a new attribute
attr for object c2 and we read it as well. But this did not create that attribute for object c1.
What is Inheritance?
Inheritance is a powerful feature in object oriented programming. It refers to defining a new class with little
or no modification to an existing class. The new class is called derived (or child) class and the one from which
it inherits is called the base (or parent) class. Python Inheritance Syntax:
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Derived class inherits features from the base class, adding new features to it. This results into re-usability of
code.
Generally when overriding a base method, we tend to extend the definition rather than simply replace it.
The same is being done by calling the method in base class from the one in derived class (calling
Polygon.__init__() from __init__() in Triangle).A better option would be to use the built-in function super().
Page 11 of 63
So, super().__init__(3) is equivalent to Polygon.__init__(self,3) and is preferred. You can learn more about
the super() function in Python.
Two built-in functions isinstance() and issubclass() are used to check inheritances. Function isinstance()
returns True if the object is an instance of the class or other classes derived from it. Each and every class in
Python inherits from the base class object.
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
Page 12 of 63
CS-33: Programming in Python
% __mod__(self, other)
** __pow__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operators:
OperatorMagic Method
== __eq__(self, other)
!= __ne__(self, other)
Assignment Operators:
Operator Magic Method
-= __isub__(self, other)
+= __iadd__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
%= __imod__(self, other)
Page 13 of 63
>>= __irshift__(self, other)
|= __ior__(self, other)
^= __ixor__(self, other)
Unary Operators:
Operator Magic Method
– __neg__(self)
+ __pos__(self)
~ __invert__(self)
Search Algorithms
A search algorithm is a method for finding an item or group of items with specific properties within a
collection of items. We refer to the collection of items as a search space. The search space might be
something concrete, such as a set of electronic medical records, or something abstract, such as the set of all
integers. A large number of problems that occur in practice can be formulated as search problems.
Many of the algorithms presented earlier in this book can be viewed as search algorithms. We formulated
finding an approximation to the roots of a polynomial as a search problem, and looked at three algorithms—
exhaustive enumeration, bisection search, and Newton-Raphson—for searching the space of possible
answers. In this section, we will examine two algorithms for searching a list. Each meetsthe specification:
The astute reader might wonder if this is not semantically equivalent to the Python expression e in L. The
answer is yes, it is. And if one is unconcerned about the efficiency of discovering whether e is in L, one
should simply write that expression.
Sorting Algorithms
We have just seen that if we happen to know that a list is sorted, we can exploit that information to greatly
reduce the time needed to search a list. Does this mean that when asked to search a list one should first sort
it and then perform the search?
Let O(sortComplexity(L)) be the complexity of sorting a list. Since we know that we can always search a list in
O(len(L)) time, the question of whether we should first sort and then search boils down to the question, is
(sortComplexity(L) + log(len(L))) < len(L)? The answer, sadly, is no. One cannot sort a list without looking at
each element in the list at least once, so it is not possible to sort a list in sub-linear time.
Does this mean that binary search is an intellectual curiosity of no practical import? Happily, no. Suppose
that one expects to search the same list many times. It might well make sense to pay the overhead of sorting
the list once, and then amortize the cost of the sort over many searches. If we expect to search the list k
times, the relevant question becomes, is (sortComplexity(L) + k*log(len(L))) less than k*len(L)? As k becomes
large, the time required to sort the list becomes increasingly irrelevant.
How big k needs to be depends upon how long it takes to sort a list. If, for example, sorting were exponential
in the size of the list, k would have to be quite large.
Hash Tables
If we put merge sort together with binary search, we have a nice way to search lists. We use merge sort to
preprocess the list in O(n*log(n)) time, and then we use binary search to test whether elements are in the
list in O(log(n)) time. If we search the list k times, the overall time complexity is O(n*log(n) + k*log(n)). This is
good, but we can still ask, is logarithmic the best that we can do for search when we are willing to do some
preprocessing?
When we introduced the type dict we said that dictionaries use a technique called hashing to do the lookup
in time that is nearly independent of the size of the dictionary. The basic idea behind a hash table is simple.
We convert the key to an integer, and then use that integer to index into a list, which can be done in
Page 15 of 63
constant time. In principle, values of any immutable type can be easily converted to an integer. After all, we
know that the internal representation of each object is a sequence of bits, and any sequence of bits can be
viewed as representing an integer. For example, the internal representation of 'abc' is the string of bits
011000010110001001100011, which can be viewed as a representation of the decimal integer 6,382,179. Of
course, if we want to use the internal representation of strings as indices into a list, the list is going to have
to be pretty darn long.
Page 16 of 63
CS- 33Programing in Python
Unit-3
Plotting using PyLab
CS-33: Programming in Python
Often text is the best way to communicate information, but sometimes there is a lot of truth to the Chinese
proverb, “A picture's meaning can express ten thousand words”. Yet most programs rely on textual output
to communicate with their users. Why? Because in many programming languages presenting visual data is
too hard. Fortunately, it is simple to do in Python.
Pylab is a programming environment, built on a set of unofficial python tools and libraries that turns Python
into a high-performance scientific computing platform. The name pylab comes in part from the resemblance
of the resulting environment to MATLAB. The components of pylab have developed largely independently,
so there's no unique or "official" distribution. Nonetheless, there are at least four core components (in
addition the standard python distribution) required to have an environment that can reasonably be
considered a pylab environment. These are:
NumPy: this is a set of high-performance libraries (implemented in Fortran and C) that implement
contiguous-memory multidimensional arrays, BLAS and LAPACK linear algebra routines and many other
useful numerical tools. This is listed first because all other components depend on it.
Matplotlib: this is pylab's plotting library. It is set up to seem familiar to users accustomed to Matlab's
plotting utilities, but it is in many ways much more powerful and flexible (It lets you choose between many
different backend renderers, for example, and allows you to build plots in an object-oriented manner).
Matplotlib is an excellent 2D and 3D graphics library for generating scientific figures. Some of the many
advantages of this library include:
Easy to get started
Support for LATEX formatted labels and texts
Great control of every element in a figure, including figure size and DPI.
High-quality output in many formats, including PNG, PDF, SVG, EPS.
GUI for interactively exploring figures and support for headless generation of figure files (useful for
batch jobs).
One of the key features of matplotlib is that all aspects of the figure can be controlled programmatically (i.e.,
without needing to muck around with the GUI). This is important for reproducibility and convenient when
one needs to regenerate the figure with updated data or change its [Link] information at the
Matplotlib web page: [Link]
Matplotlib is automatically included as part of the interactive pylab namespace, but if you need to import it
in its own namespace (e.g., in a non-interactive script or module).
SciPy: this is a set of mostly distinct modules implementing a variety of really useful scientific computing
tasks, including signal processing, FFTs, optimization, statistics, interpolation, numerical integration, etc. It
contains a lot of stuff that you would expect to see in any good scientific programming environment. Though
it depends strongly on Numpy, it is not as completely integrated into the pylab environment as are the other
components. As a result, you'll need to explicitly import most modules from scipy even if you've already
imported the pylab namespace.
Page 1 of 63
Let’s start with a simple example that uses [Link] to produce two plots.
import pylab
[Link](1) #create figure 1
[Link]([1,2,3,4], [1,7,3,5]) #draw on figure 1
[Link]() #show figure on screen
will cause a window to appear on your computer monitor. Its exact appearance may depend on the
operating system on your machine, but it will look similar to the following:
Parts of a Chart:
Page 2 of 63
CS-33: Programming in Python
Functions used for plotting:
Plot():
The two parameters of [Link] must be sequences of the same length. The first specifies the x-
coordinates of the points to be plotted, and the second specifies the y-coordinates. Together, they provide a
sequence of four <x, y> coordinate pairs, [(1,1), (2,7), (3,3), (4,5)]. These are plotted in order. As each point is
plotted, a line is drawn connecting it to the previous point. plot() is a versatile command, and will take an
arbitrary number of arguments. For example, to plot x versus y. For every x, y pair of arguments, there is an
optional third argument which is the format string that indicates the color and line type of the plot. The
letters and symbols of the format string are from MATLAB, and you concatenate a color string with a line
style string. The default format string is ‘b-‘, which is a solid blue line.
Show():
[Link](), causes the window to appear on the computer screen. If that line were not present, the figure
would still have been produced, but it would not have been displayed. [Link]() causes the process
running Python to be suspended until the figure is closed. The usual workaround is to ensure that
[Link]() is the last line of code to be executed.
Xlabel():
[Link](), causes to set title of x-axis. One should pass the string value with the method that shows the
values on x-axis.
Ylabel():
[Link](), causes to set title of y-axis. One should pass the string value with the method that shows the
values on x-axis.
Title():
[Link](), causes to display title on the entire plot. One should pass the string value with the method, and
it should be relevant with the entire plot area.
Hist():
Plot a histogram. Compute and draw the histogram of x. The return value is a tuple (n, bins, patches) or ([n0,
n1, ...], bins, [patches0, patches1,...]) if the input contains multiple [Link] data can be provided via x
as a list of datasets of potentially different length ([x0, x1, ...]), or as a 2-D ndarray in which each column is a
dataset. Note that the ndarray form is transposed relative to the list form.
Legend():
Places a legend on the axes. To make a legend for lines which already exist on the axes (via plot for instance),
simply call this function with an iterable of strings, one for each legend item. For example:
[Link]([1, 2, 3])
[Link](['A simple line'])
Towards the end of the twentieth century, mortgages started getting a lot more complicated. People could
get lower interest rates by paying “points” at the time they took on the mortgage. A point is a cash payment
of 1% of the value of the loan. People could take mortgages that were “interest-only” for a period of time.
That is to say, for some number of months at the start of the loan the borrower paid only the accrued
interest and none of the principal. Other loans involved multiple rates. Typically the initial rate (called a
Page 3 of 63
“teaser rate”) was low, and then it went up over time. Many of these loans were variable-rate—the rate to
be paid after the initial period would vary depending upon some index intended to reflect the cost to the
lender of borrowing on the wholesale credit market. We worked our way through a hierarchy of mortgages
as way of illustrating the use of subclassing.
We concluded that chapter by observing that “our program should be producing plots designed to show how
the mortgage behaves over time.” enhances class Mortgage by adding methods that make it convenient to
produce such plots. (The function findPayment, which is used in Mortgage)The methods plotPayments and
plotBalance are simple one-liners, but they do usea form of [Link] that we have not yet seen.
def findPayment(loan, r, m):
"""Assumes: loan and r are floats, m an int
Returns the monthly payment for a mortgage of size
loan at a monthly rate of r for m months"""
return loan*((r*(1+r)**m)/((1+r)**m - 1))
class Mortgage(object):
"""Abstract class for building different kinds of mortgages"""
def __init__(self, loan, annRate, months):
"""Create a new mortgage"""
[Link] = loan
[Link] = annRate/12.0
[Link] = months
[Link] = [0.0]
[Link] = [loan]
[Link] = findPayment(loan, [Link], months)
[Link] = None #description of mortgage
def makePayment(self):
"""Make a payment"""
[Link]([Link])
reduction = [Link] - [Link][-1]*[Link]
[Link]([Link][-1] - reduction)
def getTotalPaid(self):
"""Return the total amount paid so far"""
return sum([Link])
def __str__(self):
return [Link]
Page 4 of 63
CS-33: Programming in Python
Notice that we are computing the same values over and over again. For example fib gets called with 3 three
times, and each of these calls provokes four additional calls of fib. It doesn’t require a genius to think that it
might be a good idea to record the value returned by the first call, and then look it up rather than compute it
each time it is needed. This is called memoization, and is the key idea behind dynamic programming.
def fastFib(n, memo = {}):
"""Assumes n is an int >= 0, memo used only by recursive calls
Returns Fibonacci of n"""
if n == 0 or n == 1:
return 1
try:
return memo[n]
except KeyError:
result = fastFib(n-1, memo) + fastFib(n-2, memo)
memo[n] = result
return result
If you try running fastFib, you will see that it is indeed quite fast: fib(120) returns almost instantly. What is
the complexity of fastFib? It calls fib exactly once for each value from 0 to n. Therefore, under the
assumption that dictionary lookup can be done in constant time, the time complexity of fastFib(n) is O(n).
Fortunately, the situation is not as bad as it seems. Dynamic programming provides a practical method for
solving most 0/1 knapsack problems in a reasonable amount of time. As a first step in deriving such a
solution, we begin with an exponential solution based on exhaustive enumeration. The key idea is to think
about exploring the space of possible solutions by constructing a rooted binary tree that enumerates all
states that satisfy the weight constraint. A rooted binary tree is an acyclic directed graph in which
• There is exactly one node with no parents. This is called the root.
• Each non-root node has exactly one parent.
• Each node has at most two children. A childless node is called a leaf.
Each node in the search tree for the 0/1 knapsack problem is labeled with a quadruple that denotes a partial
solution to the knapsack problem
For example, computing the 19th Fibonacci number is not a substantially smaller problem than computing
the 20th Fibonacci number. Another important distinction is that the efficiency of divide-and-conquer
algorithms does not depend upon structuring the algorithm so that the same problems are solved
repeatedly. In contrast, dynamic programming is efficient only when the number of distinct subproblems is
significantly smaller than the total number of subproblems.
Page 5 of 63
CS- 33Programing in Python
Unit-4
Regular Expressions
RegEx Introduction:
A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For example,
^a...s$
The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending
withs.A pattern defined using RegEx can be used to match against a string.
abs No match
alias Match
Alias No match
An abacus No match
Here, we used [Link]() function to search pattern within the test_string. The method returns a match
object if the search is successful. If not, it returns [Link] are other several functions defined in the re
module to work with RegEx. Before we explore that, let's learn about regular expressions [Link] you
already know the basics of RegEx, jump to Python RegEx.
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of
metacharacters:[] . ^ $ * + ? {} () \ |
a 1 match
[abc]
ac 2 matches
Page 1 of 63
Unit – 4Regular Expressions
abc de ca 5 matches
a No match
ac 1 match
..
acd 1 match
a 1 match
^a abc 1 match
bac No match
abc 1 match
^ab
acb No match (starts with a but not followed by b)
$ - Dollar: The dollar symbol $ is used to check if a string ends with a certain character.
a 1 match
a$ formula 1 match
cab No match
* - Star: The star symbol * matches zero or more occurrences of the pattern left to it.
mn 1 match
maaan 1 match
Page 2 of 63
Expression String Matched?
woman 1 match
+ - Plus: The plus symbol * matches one or more occurrences of the pattern left to it.
man 1 match
woman 1 match
? - Question Mark: The question mark symbol ? matches zero or one occurrence of the pattern left to it.
mn 1 match
man 1 match
woman 1 match
{} – Braces: Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to
it.
Page 3 of 63
Unit – 4Regular Expressions
1 and 2 No match
| - Alternation: Vertical bar | is used for alternation (or operator).
cde No match
ab xz No match
Special Sequences: Special sequences make commonly used patterns easier to write. Here's a list of special
sequences: \A - Matches if the specified characters are at the start of a string.
football Match
afootball No match
Page 4 of 63
Expression String Matched?
football No match
afootball Match
Page 5 of 63
Unit – 4Regular Expressions
PythonRegEx No match
a b 2 matches (at a b)
\S
No match
Page 6 of 63
Expression String Matched?
Tip: To build and test regular expressions, you can use RegEx tester tools such as regex101. This tool not only
helps you in creating regular expressions, but it also helps you learn it. Now you understand the basics of
RegEx, let's discuss how to use RegEx in your Python code.
Python RegEx
Python has a module named re to work with regular expressions. To use it, we need to import the module.
Import re. The module defines several functions and constants to work with RegEx.
[Link](): The [Link]() method returns a list of strings containing all matches.
[Link]():The [Link] method splits the string where there is a match and returns a list of strings where the
splits have occurred.
[Link](): The [Link]() is similar to [Link]() expect it returns a tuple of 2 items containing the new string
and the number of substitutions made.
[Link](): The [Link]() method takes two arguments: a pattern and a string. The method looks for the
first location where the RegEx pattern produces a match with the string. If the search is successful,
[Link]() returns a match object; if not, it returns None.
match = [Link](pattern, str)
Match object
You can get methods and attributes of a match object using dir() function. Some of the commonly used
methods and attributes of match objects are:
[Link](): The group() method returns the part of the string where there is a match.
[Link](), [Link]() and [Link](): The start() function returns the index of the start of the
matched substring. Similarly, end() returns the end index of the matched substring.
[Link] and [Link]: The re attribute of a matched object returns a regular expression object.
Similarly, string attribute returns the passed string. Using r prefix before RegEx. When r or R prefix is used
before a regular expression, it means raw string. For example, '\n' is a new line whereas r'\n' means two
Page 7 of 63
Unit – 4Regular Expressions
characters: a backslash \ followed by n. Backlash \ is used to escape various characters including all
metacharacters. However, using r prefix makes \ treat as a normal character.
Text Processing:
Comma Separated values
In python, we use [Link]() module to read the csv file. Here, we will show you how to read different
types of csv files with different delimiter like quotes(""), pipe(|) and comma(,). We have a csv file called
[Link] having default delimiter comma(,) with following data:
SN, Name, City
1, John, Washington
2, Eric, Los Angeles
3, Brad, Texas
Example: Read [Link] file, where delimiter is comma (,)
import csv
with open('[Link]', 'r') as csvFile:
reader = [Link](csvFile)
for row in reader:
print(row)
[Link]()
When we run the above program, the output will be
['SN', ' Name', ' City']
['1', ' John', ' Washington']
['2', ' Eric', ' Los Angeles']
['3', ' Brad', ' Texas']
In Python we use [Link]() module to write data into csv files. This module is similar to the [Link]()
module. Writing on Existing File, We have a [Link] file with following data.
SN, Name, City
1, John, Washington
2, Eric, Los Angeles
3, Brad, Texas
Now, we are going to modify [Link] file.
Example 1: Modifying existing rows of [Link]
import csv
row = ['2', ' Marie', ' California']
with open('[Link]', 'r') as readFile:
reader = [Link](readFile)
lines = list(reader)
lines[2] = row
with open('[Link]', 'w') as writeFile:
writer = [Link](writeFile)
[Link](lines)
[Link]()
[Link]()
When we open the [Link] file with text editor, then it will show:
SN, Name, City
1, John, Washington
2, Marie, California
3, Brad, Texas
Page 8 of 63
JavaScript Object Notation (JSON)
JSON (JavaScript Object Notation) is a popular data format used for representing structured data. It's
common to transmit and receive data between a server and web application in JSON format. In Python,
JSON exists as a string. For example:
p = '{"name": "Bob", "languages": ["Python", "Java"]}'
It's also common to store a JSON object in a file. Import json Module To work with JSON (string, or file
containing JSON object), you can use Python's json module. You need to import the module before you can
use it.
import json
Parse JSON in Python: The json module makes it easy to parse JSON strings and files containing JSON object.
Example 1: Python JSON to dict
You can parse a JSON string using [Link]() method. The method returns a dictionary.
import json
person = '{"name": "Bob", "languages": ["English", "Fench"]}'
person_dict = [Link](person)
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
print( person_dict)
# Output: ['English', 'French']
print(person_dict['languages'])
Page 9 of 63
Unit – 4Regular Expressions
Content of xml file([Link]) which we will be parsing using different parser in this tutorial.
<Emp>
<firstName>Rahul</firstName>
<lastName>Anand</lastName>
<dob>20/10/1990</dob>
<gender>Male</gender>
<department>Finance</department>
<department>Admin</department>
</Emp>
Parse XML using re Pattern Module. Python has inbuilt support of pattern matching which is available under
re Pattern Module. One can parse xml file using pattern matching as follow:
import re
content = open("[Link]").read();
#get all departments
departments = [Link]('<department>(.*)</department>', content)
for department in departments : print(department)
#get firstName
firstName = [Link]('<firstName>(.*)</firstName>', content)
print(firstName)
Mailmerge
When we want to send the same invitations to many people, the body of the mail does not change. Only the
name (and maybe address) needs to be changed. Mail merge is a process of doing this. Instead of writing
each mail separately, we have a template for body of the mail and a list of names that we merge together to
form all the mails. Source Code to Merge Mails:
# Python program to mail merger
# Names are in the file [Link]
# Body of the mail is in [Link]
# open [Link] for reading
with open("[Link]",'r',encoding = 'utf-8') as names_file:
# open [Link] for reading
with open("[Link]",'r',encoding = 'utf-8') as body_file:
# read entire content of the body
body = body_file.read()
# iterate over names
for name in names_file:
mail = "Hello "+name+body
# write the mails to individual files
with open([Link]()+".txt",'w',encoding = 'utf-8') as
mail_file:
mail_file.write(mail)
For this program, we have written all the names in separate lines in the file "[Link]". The body is in the
"[Link]" file. We open both the files in reading mode and iterate over each name using a for loop. A new
file with the name "[name].txt" is created, where name is the name of that person. We use strip() method to
clean up leading and trailing whitespaces (reading a line from the file also reads the newline '\n' character).
Finally, we write the content of the mail into this file using the write() method.
Case Study: Create Regular expressions (Custom)Process telephone numbers, Generate log data, HTML
Generators, Tweet Scrub, Amazone Screen Scrapper.
Page 10 of 63
CS- 33Programing in Python
Unit-5
Python and Data Analytics
Understand the problem By Understanding the Data
This chapter has two purposes. One is to familiarize you with data sets that will be used later as examples of
different types of problems to be solved using the algorithms. “Penalized Linear Regression,” and “Ensemble
Methods.” The other purpose is to demonstrate some of the tools available in Python for data exploration.
The data are arranged into rows and columns. Each row represents an individual case (also called an
instance, example, or observation). The columns in Table are given designations that indicate the roles they
will play in the machine learning problem. The columns designated as attributes will be used to make
predictions of the dollars spent on books. In the column designated as labels, you’ll see how much each
customer spent last year on books.
Page 1 of 63
Unit – 5Python and Data Analytics
Classification Problems: Detecting Unexploded Mines Using Sonar
This section steps through several checks that you might make on a classification problem as you begin
digging into it. It starts with simple measurements of size and shape, reporting data types, counting missing
values, and so forth. Then it moves on to statistical properties of the data and interrelationships between
attributes and between attributes and the labels. The data set comes from the UC Irvine Data Repository
[Ref 1.]. The data result from some experiments to determine if sonar can be used to detect unexploded
mines left in harbors subsequent to military actions. The sonar signal is what’s called a chirped signal. That
means that the signal rises (or falls) in frequency over the duration of the
How to Use Python Pandas to Summarize the Rocks versus Mines Data Set
The Python package Pandas can help automate the process of data inspection and handling. It proves
particularly useful for the early stages of data inspection and preprocessing. The Pandas package makes it
possible to read data into a specialized data structure called a data frame. The data frame is modeled after
the CRAN‐R data structure of the same [Link] can think of a data frame as a table or matrix‐like structure
as in Table The data frame is oriented with a row representing a single case (experiment, example,
measurement) and columns representing particular attributes. The structure is matrix‐like, but not a matrix
because the elements in various columns may be of different types. Formally, a matrix is defined over a field
(like the real numbers, binary numbers, complex numbers), and all the entries in a matrix are elements from
that field. For statistical problems, the matrix is too confining because statistical samples typically have a mix
of different types.
Predictive Model Building: Balancing Performance Complexity, and the Big Data
The goal of selecting and fitting a predictive algorithm is to achieve the best possible performance. Achieving
performance goals involves three factors: complexity of the problem, complexity of the algorithmic model
Page 2 of 63
employed, and the amount and richness of the data available. The chapter includes some visual examples
that demonstrate the relationship between problem and model complexity and then provides technical
guidelines for use in design and development.
Another important concept is that modern machine learning algorithmsgenerate families of models, not just
single models. The algorithms covered in this chapter each generate hundreds or even thousands of
different models.
Choosing a nonlinear model (say an ensemble method) entails training a numberof different models of
differing complexity. For example, the ensemble model thatgenerated the decision boundary in Figure was
one of roughly a thousand different models generated during the training process. These models had a
varietyof different complexities. Some of them would have given a much cruder approximation to the
boundaries that are visually apparent in Figure. The model thatgenerated the decision boundary in Figure 3-
6 was chosen because it performed thebest on out-of-sample data. This process holds for many modern
machine learning algorithms. Examples will be covered in covered in the section “Choosing a Modelto
Balance Problem Complexity, Model Complexity, and Data Set Size.”
Choosing a Model to Balance Problem Complexity,Model Complexity, and Data Set Size
A couple of examples will illustrate how modern machine learning techniquescan be tuned to best fit a given
problem and data set. The first example is amodification to ordinary least squares regression called forward
stepwiseregression. Here’s how it works. Recall Equations 3-1 and 3-2, which define theproblem being
solved (see Equations 3-10 and 3-11 here, which repeat thoseequations). The vector Y contains the labels.
And the matrix X contains theattributes available to predict the labels.
Page 5 of 63