Python Programming
Python Programming
Python Programming
1.1 INTRODUCTION
1
• Mobile devices
• Embedded devices
1.2 NUMBERS
2
print(type(y))
print(type(z))
output:
<class 'float'>
<class 'float'>
<class 'float'>
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
Example:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
output:
<class 'complex'>
<class 'complex'>
<class 'complex'>
1.3 STRINGS
Strings in python are surrounded by either single quotation marks, or double quotation
marks. You can display a string literal with the print() function.'hello' is the same as "hello".
Example:
print("Hello")
print('Hello')
output:
Hello
Hello
3
Assign String to a Variable
Assigning a string to a variable is done with the variable name followed by an equal
assign and the string.
Example:
a = "Hello"
print(a)
output:
Hello
4
n
a
String Length
To get the length of a string, use the len() function.
Example:
The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))
output:
13
Check String
To check if a certain phrase or character is present in a string, we can use the keyword
in.
Example:
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
output:
Yes, 'free' is present.
Check if NOT
To check if a certain phrase or character is NOT present in a string, we can use the
keyword not in.
Example:
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
output:
5
No, 'expensive' is NOT present
Lower Case
Example:
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
output:
hello, world!
Replace String
Example:
The replace() method replaces a string with another string
a = "Hello, World!"
print(a.replace("H", "J"))
output:
Jello, World!
Split String
The split() method returns a list where the text between the specified separator
becomes the list items. The split() method splits the string into substrings if it finds instances
of the separator.
6
Example:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
output:
['Hello', ' World!']
String Concatenation
To concatenate, or combine, two strings you can use the + operator.
Example:
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c=a+b
print(c)
output:
HelloWorld
1.4 VARIABLES
7
Get the Type
You can get the data type of a variable with the type( ) function.
Example:
x=5
y = "John"
print(type(x))
print(type(y))
output:
<class 'int'>
<class 'str'>
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
1.5 LISTS
8
Example :
mylist = ["apple", "banana", "cherry"]
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value.
Example:
Lists allow duplicate values:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
output:
['apple', 'banana', 'cherry', 'apple', 'cherry']
List Length
To determine how many items a list has, use the len( ) function:
Example:
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
output:
3
9
The list( ) Constructor
It is also possible to use the list( ) constructor when creating a new list.
Example:
thislist = list(("apple", "banana", "cherry")) # note the
double round-brackets
print(thislist)
output:
['apple', 'banana', 'cherry']
Access Items
List items are indexed and you can access them by referring to the index number:
Example:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
output:
banana
Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
Example:
Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
output:
cherry
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range. When specifying a range, the return value will be a new list with the specified items.
10
Example:
Return the third, fourth, and fifth item
thislist = ["apple", "banana", "cherry", "orange", "kiwi",
"melon", "mango"]
print(thislist[2:5])
output:
['cherry', 'orange', 'kiwi']
11
Example:
thislist = ["apple", "banana", "cherry", "orange", "kiwi",
"mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
output:
['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi',
'mango']
Insert Items
• To insert a new list item, without replacing any of the existing values, we can use the
insert( ) method.
• The insert( ) method inserts an item at the specified index.
Example:
Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
output:
['apple', 'banana', 'watermelon', 'cherry']
Append Items
To add an item to the end of the list, use the append( ) method.
Example:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
output:
['apple', 'banana', 'cherry', 'orange']
12
Example:
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
output:
['apple', 'cherry']
List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list.
13
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter
"a" in the name.
Without list comprehension you will have to write a for statement with a conditional
test inside.
Example:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
output:
['apple', 'banana', 'mango']
1.6 TUPLES
14
• Tuples are written with round brackets.
Example:
mytuple = ("apple", "banana", "cherry")
Create a Tuple
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
output:
('apple', 'banana', 'cherry')
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and
that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after
the tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value.
Example:
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
output:
('apple', 'banana', 'cherry', 'apple', 'cherry')
Tuple Length
To determine how many items a tuple has, use the len() function:
Example:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
output:
3
15
Create Tuple With One Item
To create a tuple with only one item, you have to add a comma after the item,
otherwise Python will not recognize it as a tuple.
Example:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
output:
<class 'tuple'>
<class 'str'>
16
Access Tuple Items
You can access tuple items by referring to the index number, inside square brackets:
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
output:
banana
Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
output:
cherry
17
Tuples are unchangeable, so you cannot remove items from it, but you can use the
same workaround as we used for changing and adding tuple items.
Example:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
output
('banana', 'cherry')
Example:
thistuple = ("apple", "banana", "cherry")
i=0
while i < len(thistuple):
print(thistuple[i])
18
i=i+1
output:
apple
banana
cherry
1.7 DICTIONARIES
19
• Dictionary items are presented in key:value pairs, and can be referred to by using the
key name.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
output:
Ford
Dictionary Length
To determine how many items a dictionary has, use the len( ) function.
Example:
20
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
output:
3
21
output:
{'name': 'John', 'age': 36, 'country': 'Norway'}
Accessing Items
You can access the items of a dictionary by referring to its key name, inside square
brackets.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
output:
Mustang
Get Keys
The keys( ) method will return a list of all the keys in the dictionary.
Example:
car =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
output:
22
dict_keys(['brand', 'model', 'year'])
dict_keys(['brand', 'model', 'year', 'color'])
Get Values
The values( ) method will return a list of all the values in the dictionary.
Example:
car =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
output:
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 2020])
23
dictionary")
output:
Yes, 'model' is one of the keys in the this dict dictionary
Update Dictionary
• The update( ) method will update the dictionary with the items from the given
argument.
• The argument must be a dictionary, or an iterable object with key:value pairs.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a
value to it.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
24
thisdict["color"] = "red"
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
Removing Items
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
output:
{'brand': 'Ford', 'year': 1964}
The del keyword removes the item with the specified key name.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
output:
{'brand': 'Ford', 'year': 1964}
Copy a Dictionary
• You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only
be a reference to dict1, and changes made in dict1 will automatically also be made in
dict2.
• There are ways to make a copy, one way is to use the built-in Dictionary method
copy( ).
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
26
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
1.8 SETS
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
27
Example:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
output:
{'banana', 'cherry', 'apple'}
28
output:{'apple', 'banana', 'cherry'}
{'banana', 'apple', 'cherry'}
Access Items
• You cannot access items in a set by referring to an index or a key.
• But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword
Example:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
output:
True
Add Items
• Once a set is created, you cannot change its items, but you can add new items.
• To add one item to a set use the add( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
output:
{'cherry', 'orange', 'apple', 'banana'}
Add Sets
To add items from another set into the current set, use the update( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
output:
29
{'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'}
Remove Item
To remove an item in a set, use the remove( ), or the discard( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
output:
{'cherry', 'apple'}
Loop Items
You can loop through the set items by using a for loop.
Example:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
output:
cherry
banana
apple
30
{1, 3, 2, 'c', 'b', 'a'}
Example:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
output:
{'b', 3, 1, 'c', 'a', 2}
1.9 COMPARISION
31
>>> y = 20
>>> x <= y
True
>>> y <= x
False
32
2.1 CODE STRUCTURES
Example:
33
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
else:
print "1 - Got a false expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
else:
print "2 - Got a false expression value"
print var2
print "Good bye!"
output:
1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!
Syntax:
34
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example:
var = 100
if var == 200:
print "1 - Got a true expression value"
print var
elif var == 150:
print "2 - Got a true expression value"
print var
elif var == 100:
print "3 - Got a true expression value"
print var
else:
print "4 - Got a false expression value"
print var
print "Good bye!"
output:
3 - Got a true expression value
100
Good bye!
Else
35
The else keyword catches anything which isn't caught by the preceding conditions.
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
output:
a is greater than b
Python while loop is used to run a block code until a certain condition is met.
• A while loop evaluates the condition
• If the condition evaluates to True, the code inside the while loop is executed.
• condition is evaluated again.
• This process continues until the condition is False.
• When condition evaluates to False, the loop stops.
The syntax of while loop is:
while condition:
# body of while loop
Example:
# program to display numbers from 1 to 5
# initialize the variable
i=1
n=5
# while loop from i = 1 to 5
36
while i <= n:
print(i)
i=i+1
output
1
2
3
4
5
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
• This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object-orientated programming languages.
• With the for loop we can execute a set of statements, once for each item in a list,
tuple, set etc.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
output:
apple
banana
cherry
Example:
37
for x in "banana":
print(x)
output
b
a
n
a
n
a
break Statement
With the break statement we can stop the loop before it has looped through all the
items.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
output:
apple
banana
Continue Statement
With the continue statement we can stop the current iteration of the loop, and continue
with the next:
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
38
print(x)
output:
apple
cherry
Range( ) Function
• To loop through a set of code a specified number of times, we can use the range( )
function,
• The range( ) function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
Example:
for x in range(6):
print(x)
output:
0
1
2
3
4
5
39
1
2
3
4
5
Finally finished!
2.5 COMPREHENSIONS
40
print(newlist)
output:
['apple', 'banana', 'mango']
Dictionary comprehension
Dictionary comprehension is a method for transforming one dictionary into another
dictionary. During this transformation, items within the original dictionary can be
conditionally included in the new dictionary, and each item can be transformed as needed.
Example:
square_dict = dict()
for num in range(1, 11):
square_dict[num] = num*num
print(square_dict)
output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Let's compare this syntax with dictionary comprehension from the above example.
41
dollar_to_pound = 0.76
new_price = {item: value*dollar_to_pound for (item, value)
in old_price.items()}
print(new_price)
output:
{'milk': 0.7752, 'coffee': 1.9, 'bread': 1.9}
42
}
print(dictionary)
output:
{2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15}, 4:
{1: 4, 2: 8, 3: 12, 4: 16, 5: 20}}
set comprehension
A set comprehension is similar to a list comprehension but returns a set instead of a
list. The syntax is slightly different in the sense that we use curly brackets instead of square
brackets to create a set. The list includes a lot of duplicates, and there are names with only a
single letter.
The syntax for set comprehension :
newSet= { expression for element in iterable }
43
{3, 6, 9, 12, 15, 18, 21, 24, 27, 30}
Generator Comprehension
• Generator Comprehension is a way to initialize a generator to access elements from a
container object or a sequence of data.
• Generally, We create a generator object by implementing a generator function using
the yield statement. For example, suppose you want to create a generator that gives
squares of elements of a given list as output.
We can create such a generator using the yield statement as follows:
Example:
def num_generator():
for i in range(1, 11):
yield i
gen = num_generator()
print("Values obtained from generator function are:")
for element in gen:
print(element)
output:
Values obtained from generator function are:
1
2
3
4
5
6
7
8
9
10
44
The syntax for generator comprehension is almost identical to list comprehension.
The syntax for set comprehension is:
generator= (expression for element in iterable if condition)
• The iterable can be any iterable object such as list, set, tuple, or dictionary from which
we have to create a new generator to access its elements.
• The element is the element of the iterable object for which we are creating the
generator.
• The expression contains a value or any mathematical expression derived from the
element.
• The condition is the conditional expression required to exclude or include an element
in the generator. The conditional statement is optional and “if condition” can be
omitted if you have to access all the elements of the iterable.
• generator is the name of a newly created generator using generator comprehension in
Python.
Benefits of generator comprehension
• Generator comprehension makes us able to implement the same functionality using
fewer lines of code.
• Generator comprehension does not initialize any object unlike list comprehension or
set comprehension. Thus, you can use generator comprehension instead of list
comprehension or set comprehension to reduce the memory requirement of the
program.
• Generator comprehension also makes the code more readable that helps in debugging
and maintenance of the source code.
2.6 FUNCTIONS
45
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis.
Example:
def my_function():
print("Hello from a function")
my_function()
output:
Hello from a function
Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses. You can add
as many arguments as you want, just separate them with a comma.
Example:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
output:
Emil Refsnes
Tobias Refsnes
Linus Refsnes
Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning
that if your function expects 2 arguments, you have to call the function with 2 arguments, not
more, and not less.
Example:
46
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
output:
Emil Refsnes
Keyword Arguments
You can also send arguments with the key = value.
Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 =
"Linus")
output:
The youngest child is Linus
47
This way the function will receive a dictionary of arguments, and can access the items
accordingly.
Example:
If the number of keyword arguments is unknown, add a
double ** before the parameter name:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
output:
His last name is Refsnes
48
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
output:
apple
banana
cherry
Return Values
To let a function return a value, use the return statement.
Example:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
output:
15
25
45
2.7 GENERATORS
49
def generator_name(arg):
# statements
yield something
When the generator function is called, it does not execute the function body
immediately. Instead, it returns a generator object that can be iterated over to produce the
values.
Example:
def my_generator(n):
# initialize counter
value = 0
# loop until counter is less than n
while value < n:
# produce the current value of the counter
yield value
# increment the counter
value += 1
#iterate over the generator object produced by
my_generator
for value in my_generator(3):
# print each value produced by generator
print(value)
output:
0
1
2
50
A generator expression has the following syntax
(expression for item in iterable)
Here, expression is a value that will be returned for each item in the iterable.
The generator expression creates a generator object that produces the values of
expression for each item in the iterable, one at a time, when iterated over.
Example:
# create the generator object
squares_generator = (i * i for i in range(5))
# iterate over the generator and print the values
for i in squares_generator:
print(i)
output:
0
1
4
9
16
2.8 DECORATORS
• A decorator is a design pattern in Python that allows a user to add new functionality to
an existing object without modifying its structure. Decorators are usually called
before the definition of a function you want to decorate.
51
• In Python, a decorator is a design pattern that allows you to modify the functionality
of a function by wrapping it in another function.
• The outer function is called the decorator, which takes the original function as an
argument and returns a modified version of it.
• A Python decorator is a function that takes in a function and returns it by adding some
functionality.
• In fact, any object which implements the special _call( )_ method is termed callable.
So, in the most basic sense, a decorator is a callable that returns a callable.
• Basically, a decorator takes in a function, adds some functionality and returns it.
Example:
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
output:
I am ordinary
We are calling the ordinary() function normally, so we get the output "I am ordinary".
Now, let's call it using the decorator function. def make_pretty(func):
Example:
# define the inner function
def inner():
# add some additional behavior to decorated function
print("I got decorated")
# call original function
func()
# return the inner function
return inner
52
# define ordinary function
def ordinary():
print("I am ordinary")
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
# call the decorated function
decorated_func()
output:
I got decorated
I am ordinary
53
0.4
I am going to divide 2 and 0
Whoops! cannot divide
54
2.9 NAMESPACES AND SCOPE
A variable is only available from inside the region it is created. This is called scope.
Local Scope
A variable created inside a function belongs to the local scope of that function, and
can only be used inside that function.
Example:
def myfunc():
x = 300
print(x)
myfunc()
output:
300
55
myinnerfunc()
myfunc()
output:
300
• The try block lets you test a block of code for errors.
• The except block lets you handle the error.
• The else block lets you execute code when there is no error.
• The finally block lets you execute code, regardless of the result of the try- and except
blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and
generate an error message
These exceptions can be handled using the try statement
Example:
try:
print(x)
except:
print("An exception occurred")
output:
An exception occurred
Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to execute a
special block of code for a special kind of error:
Example:
try:
print(x)
except NameError:
56
print("Variable x is not defined")
except:
print("Something else went wrong")
output:
Variable x is not defined
Else
You can use the else keyword to define a block of code to be executed if no errors
were raised
Example:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
output:
Hello
Nothing went wrong
Finally
The finally block, if specified, will be executed regardless if the try block raises an
error or not.
Example:
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
output:
57
Something went wrong
The 'try except' is finished
Exceptions need to be derived from the Exception class, either directly or indirectly.
Although not mandatory, most of the exceptions are named as names that end in “Error”
similar to the naming of the standard exceptions in python.
Example:
# define Python user-defined exceptions
class InvalidAgeException(Exception):
"Raised when the input value is less than 18"
pass
# you need to guess this number
number = 18
try:
input_num = int(input("Enter a number: "))
58
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException:
print("Exception occurred: Invalid Age")
output:
Enter a number: 35
Eligible to Vote
59
UNIT- III
Python Command line arguments are input parameters passed to the script when
executing them. Almost all programming language provide support for command line
arguments. Then we also have command line options to set some specific options for the
program.
Here Python script name is script.py and rest of the three arguments - arg1 arg2
arg3 are command line arguments for the program.
Python Command Line Arguments provides a convenient way to accept some
information at the command line while running the program. The arguments that are given
after the name of the Python script are known as Command Line Arguments and they are
used to pass some information to the program.
Example:
$ python script.py arg1 arg2 arg3
.
There are following three Python modules which are helpful in parsing and managing the
command line arguments:
• sys module
• getopt module
60
• argparse module
sys module - System-specific parameters
The Python sys module provides access to any command-line arguments via the
sys.argv.
This serves two purposes −
• sys.argv is the list of command-line arguments.
• len(sys.argv) is the number of command-line arguments.
Example:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Now run above script as below. All the programs in this tutorial need to be run from
the command line, so we are unable to provide online compile & run option for these
programs.
$ python test.py arg1 arg2 arg3
output:
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Syntax:
getopt.getopt(args, options, [long_options])
61
3.1.3 MODULES AND THE IMPORT STATEMENT
Use a Module
Now we can use the module we just created, by using the import statement.
Example:
import mymodule
mymodule.greeting("Jonathan")
output:
Hello, Jonathan
Variables in Module
The module can contain functions, as already described, but also variables of all types
(arrays, dictionaries, objects etc):
Example:
person1 =
{
"name": "John",
"age": 36,
62
"country": "Norway"
}
Example:
import mymodule
a = mymodule.person1["age"]
print(a)
output:
36
Naming a Module
You can name the module file whatever you like, but it must have the file extension
.py
Re-naming a Module
You can create an alias when you import a module, by using the as keyword.
Example:
import mymodule as mx
a = mx.person1["age"]
print(a)
output:
36
63
"country": "Norway"
}
The Python Standard Library is a collection of script modules that may be used by a
Python program, making it unnecessary to rewrite frequently used commands and
streamlining the development process. By "calling/importing" them at the start of a script,
they can be used.
The Python Standard Library contains the exact syntax, semantics, and tokens of
Python. It contains built-in modules that provide access to basic system functionality like I/O
and some other core modules. Most of the Python Libraries are written in the C programming
language.
The Python standard library consists of more than 200 core modules. All these work
together to make Python a high-level programming language. Python Standard Library plays
a very important role. Without it, the programmers can’t have access to the functionalities of
Python. But other than this, there are several other libraries in Python that make a
programmer’s life easier.
Let’s have a look at some of the commonly used libraries:
TensorFlow: This library was developed by Google in collaboration with the Brain
Team. It is an open-source library used for high-level computations. It is also used in
machine learning and deep learning algorithms. It contains a large number of tensor
operations. Researchers also use this Python library to solve complex computations in
Mathematics and Physics.
64
Matplotlib: This library is responsible for plotting numerical data. And that’s why it
is used in data analysis. It is also an open-source library and plots high-defined figures like
pie charts, histograms, scatterplots, graphs, etc.
Pandas: Pandas are an important library for data scientists. It is an open-source
machine learning library that provides flexible high-level data structures and a variety of
analysis tools. It eases data analysis, data manipulation, and cleaning of data. Pandas support
operations like Sorting, Re-indexing, Iteration, Concatenation, Conversion of data,
Visualizations, Aggregations, etc.
Numpy: The name “Numpy” stands for “Numerical Python”. It is the commonly used
library. It is a popular machine learning library that supports large matrices and multi-
dimensional data. It consists of in-built mathematical functions for easy computations. Even
libraries like TensorFlow use Numpy internally to perform several operations on tensors.
Array Interface is one of the key features of this library.
SciPy: The name “SciPy” stands for “Scientific Python”. It is an open-source library
used for high-level scientific computations. This library is built over an extension of Numpy.
It works with Numpy to handle complex computations. While Numpy allows sorting and
indexing of array data, the numerical data code is stored in SciPy. It is also widely used by
application developers and engineers.
Scrapy: It is an open-source library that is used for extracting data from websites. It
provides very fast web crawling and high-level screen scraping. It can also be used for data
mining and automated testing of data.
Scikit-learn: It is a famous Python library to work with complex data. Scikit-learn is
an open-source library that supports machine learning. It supports variously supervised and
unsupervised algorithms like linear regression, classification, clustering, etc. This library
works in association with Numpy and SciPy.
PyGame: This library provides an easy interface to the Standard Directmedia Library
(SDL) platform-independent graphics, audio, and input libraries. It is used for developing
video games using computer graphics and audio libraries along with Python programming
language.
PyTorch: PyTorch is the largest machine learning library that optimizes tensor
computations. It has rich APIs to perform tensor computations with strong GPU acceleration.
It also helps to solve application issues related to neural networks.
PyBrain: The name “PyBrain” stands for Python Based Reinforcement Learning,
Artificial Intelligence, and Neural Networks library. It is an open-source library built for
65
beginners in the field of Machine Learning. It provides fast and easy-to-use algorithms for
machine learning tasks. It is so flexible and easily understandable and that’s why is really
helpful for developers that are new in research fields.
66
3.2.2 INHERITANCE
• Inheritance allows us to define a class that inherits all the methods and properties
from another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class.
Example:
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute
the printname method:
x = Person("John", "Doe")
x.printname()
output:
John Doe
Use the Student class to create an object, and then execute the printname method.
Example:
class Person:
67
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
x = Student("Mike", "Olsen")
x.printname()
output:
Mike Olsen
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
To keep the inheritance of the parent's __init__() function, add a call to the parent's
__init__() function:
Example:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
68
Person.__init__(self, fname, lname)
x = Student("Mike", "Olsen")
x.printname()
output:
Mike Olsen
Overriding a method in the same class is not allowed. So, you need to do that in the
child class by implementing the Inheritance concept. If you want to override the Parent Class
method, create a function in the Child with the same name and number of parameters. This is
called function overriding in Python.
69
The term "override" refers to a method in a subclass that replaces a method in a
superclass when both methods share the same name, parameters, signature, and return type
(or sub-type).
The object that calls a method will determine which version of the method is
executed. When a method is called from an object of a parent class, the method's parent class
version is executed; however, when a method is called from an object of a subclass, the child
class version is executed. In other words, the version of an overridden method depends on the
object being referenced, not the type of the reference variable.
Example:
# program to determine the method overriding
# Defining a parent class
class Parent():
# producer
def __init__(self):
self.value = "Inside Parent"
# showing a parents method
def show(self):
print(self.value)
# Defining a child class
class Child(Parent):
# Constructor
def __init__(self):
self.value = "Inside Child"
70
# showing the child's method
def show(self):
print(self.value)
# Chauffeur's code
obj1 = Parent()
obj2 = Child()
obj1.show()
obj2.show()
output:
Inside Parent
Inside Child
Example:
philo = Dog("Philo", 5)
philo.speak("Gruff gruff!")
71
3.2.5 GET HELP FROM YOUR PARENT WITH SUPER
• The super( ) function is used to give access to methods and properties of a parent or
sibling class.
• The super( ) function returns an object that represents the parent class. The super()
function is used to give access to methods and properties of a parent or sibling class.
• The super( ) function returns an object that represents the parent class.
Syntax:
super( )
Example:
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x = Child("Hello, and welcome!")
x.printmessage( )
output:
Hello, and welcome!
We'll use self in classes to represent the instance of an object. We can create multiple
of a class and each instance will have different values. And self helps us to get those property
values within the class instance.
Example:
class Laptop:
72
# init method
def __init__(self, company, model):
# self
self.company = company
self.model = model
# creating instances for the class Laptop
laptop_one = Laptop('Lenovo', 'ideapad320')
laptop_two = Laptop('Dell', 'inspiron 7000')
# printing the properties of the instances
print(f"Laptop One: {laptop_one.company}")
print(f"Laptop Two: {laptop_two.company}")
output:
Laptop One: Lenovo
Laptop Two: Dell
For the purpose of data encapsulation, most object oriented languages use getters and
setters method. This is because we want to hide the attributes of a object class from other
classes so that no accidental modification of the data happens by methods in other classes.
As the name suggests, getters are the methods which help access the private attributes
or get the value of the private attributes and setters are the methods which help change or set
the value of private attributes.
Using getters and setters
we will make a class, initialize is and then add a getter and setter method to each of
them. Then access the variables in these methods by instantiating the class and using these
getter and setter methods. So you can hide your logic inside the setter method.
Example:
class year_graduated:
def __init__(self, year=0):
self._year = year
73
# getter method
def get_year(self):
return self._year
# setter method
def set_year(self, a):
self._year = a
grad_obj = year_graduated()
# Before using setter
print(grad_obj.get_year())
# After using setter
grad_obj.set_year(2019)
print(grad_obj._year)
output:
0
2019
The name mangling process helps to access the class variables from outside the class.
The class variables can be accessed by adding _classname to it. The name mangling is closest
to private not exactly private. The above class variable is accessed by adding the _classname
to it.
A process in which any given identifier with one trailing underscore and two leading
underscores is textually replaced with the __ClassName__Identifier is known as Name
mangling. In __ClassName__Identifier name, ClassName is the name of current class where
identifier is present.
Example:
# A testing class for identifier
class Testing:
# Giving Name as an identifier
def __init__(self, name):
74
# Identifier initializing
self.__name = name
def PrintName(self):
print(self.__name)
t1 = Testing("JavaTpoint") # Calling variable name with the
class
t1.PrintName() # Printing name in the output
output:
JavaTpoint
75
output:
This is the instance method and it can access the variables a
= 2 and b = 4 with the help of self.
Class Methods
• The purpose of the class methods is to set or get the details (status) of the class.
• That is why they are known as class methods. They can’t access or modify specific
instance data. They are bound to the class instead of their objects.
Two important things about class methods:
• In order to define a class method, you have to specify that it is a class method with the
help of the @classmethod decorator
• Class methods also take one default parameter- cls, which points to the class. Again,
this not mandatory to name the default parameter “cls”.
Example:
class My_class:
@classmethod
def class_method(cls):
return "This is a class method."
Static Methods
Static methods cannot access the class data. In other words, they do not need to access
the class data. They are self-sufficient and can work on their own. Since they are not
attached to any class attribute, they cannot get or set the instance state or class state.
Example:
class My_class:
@staticmethod
def static_method():
return "This is a static method."
76
3.2.10 DUCK TYPING
Duck typing is a concept related to dynamic typing, where the type or the class of an
object is less important than the methods it defines. When you use duck typing, you do not
check types at all. Instead, you check for the presence of a given method or attribute.
Example:
Define the three classes
class Duck:
def quack(self):
print("I am a duck and I quack.")
class Goose:
def quack(self):
print("I am a goose and I quack.")
class Cat:
def meow(self):
print("I am a dog and I meow.")
# Define the method
def quack(animal):
animal.quack()
A variety of instance methods that are reserved by Python, which affect an object’s
high level behavior and its interactions with operators. These are known as special methods.
__init__ is an example of a special method; recall that it controls the process of creating
instances of a class. Similarly, we will see that __add__ controls the behavior of an object
when it is operated on by the + symbol, for example.
In general, the names of special methods take the form of __<name>__, where the
two underscores preceed and succeed the name. Accordingly, special methods can also be
referred to as “dunder” (double-underscore) methods. Learning to leverage special methods
will enable us to design elegant and powerful classes of objects.
77
Example:
# Demonstrating (mis)use of special methods
class SillyClass:
def __getitem__(self, key):
"" Determines behavior of `self[key]` """
return [True, False, True, False]
def __pow__(self, other):
""" Determines behavior of `self ** other` """
return "Python Like You Mean It"
output:
silly = SillyClass()
silly[None]
[True, False, True, False]
silly ** 2
'Python Like You Mean It'
3.2.12 COMPOSITION
78
UNIT- IV
79
4.1.2 BINARY DATA
In Python, the boolean data type is the binary variable and defined as True or False.
Additionally, the bool() function converts the value of an object to a boolean value. This
function returns True for all values except the following values. Empty objects (list, tuple,
string, dictionary)
The byte and bytearrays are used to manipulate binary data in python. These bytes
and bytearrys are supported by buffer protocol, named memoryview. The memoryview can
access the memory of other binary object without copying the actual data.
The byte literals can be formed by these options.
• b‘This is bytea with single quote’
• b“Another set of bytes with double quotes”
• b‘’’Bytes using three single quotes’’’ or b“””Bytes using three double quotes”””.
Example:
hexStr = bytes.fromhex('A2f7 4509')
print(hexStr)
byteString = b'\xa2\xf7E\t'
print(byteString.hex())
bArray1 = b"XYZ"
bArray2 = bArray1.replace(b"X", b"P")
print(bArray2)
byteArray1 = b'ABBACACBBACA'
print(byteArray1.count(b'AC'))
print(byteArray1.find(b'CA'))
bArr = b'Mumbai,Kolkata,Delhi,Hyderabad'
partList = bArr.partition(b',')
print(partList)
myByteArray = bytearray('String', 'UTF-8')
memView = memoryview(myByteArray)
print(memView[2]) #ASCII of 'r'
print(bytes(memView[1:5]))
output:
80
b'\xa2\xf7E\t'
a2f74509
b'PYZ'
3
81
Example:
#!/usr/bin/python
str = raw_input("Enter your input: ")
print "Received input is : ", str
output:
Enter your input: Hello Python
Received input is : Hello Python
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
82
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
output:
Name of the file: foo.txt
Closed or not : False
Opening mode : wb
Softspace flag : 0
83
Syntax:
fileObject.write(string)
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")
# Close opend file
fo.close( )
output:
Python is a great language.
Yeah its great!!
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close( )
output:
Read String is : Python is
84
4.2.2 STRUCTURED TEXT FILES
A text file is structured as a sequence of lines. And, each line of the text file consists
of a sequence of characters. Termination of each line in a text file is denoted with the end of
line (EOL). There are a few special characters that are used as EOL, but comma {,} and
newlines are the most common ones.
There are two types of files that can be handled in python, normal text files and binary
files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text
is terminated with a special character called EOL (End of Line), which is the new line
character ('\n') in python by default.
To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode
opens the file in binary format for reading, while the "wb" mode opens the file in binary
format for writing. Unlike text files, binary files are not human-readable. When opened using
any text editor, the data is unrecognizable.
A binary file is a file whose content is in a binary format consisting of a series of
sequential bytes, each of which is eight bits in length. The content must be interpreted by a
program or a hardware processor that understands in advance exactly how that content is
formatted and how to read the data.
To write a binary string to a binary file, you need to open the file in “binary write”
mode using 'wb' as the second positional argument of the open() function. For instance, you'd
write open('my_file. bin', 'wb') instead of open('my_file. bin', 'w') .
Example:
# Open binary file for reading
f = open('myfile1.bin', 'rb')
# Get a string from binary file
d = f.read()
# Display this string.
# The output will be as a string of characters
print("d=",d)
85
#d=
b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
# If print as a separate character,
# then the character code will be displayed - as an integer
print("d[5] = ", d[5]) # d[5] = 40
print("d[0] = ", d[0]) # d[0] = 128
# Use bin function for single character
print(bin(d[2])) # 0b1011101
f.close()
output:
d=
b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
d[5] = 40
d[0] = 128
0b1011101
A relational database organizes data into rows and columns, which collectively form a
table. Data is typically structured across multiple tables, which can be joined together via a
primary key or a foreign key.
We can connect to relational databases for analysing data using the pandas library as
well as another additional library for implementing database connectivity. This package is
named as sqlalchemy which provides full SQL language functionality to be used in python.
Installing SQLAlchemy
The installation is very straight forward using Anaconda which we have discussed in
the chapter Data Science Environment. Assuming you have installed Anaconda as described
in this chapter, run the following command in the Anaconda Prompt Window to install the
SQLAlchemy package.
conda install sqlalchemy
86
Reading Relational Tables
We will use Sqlite3 as our relational database as it is very light weight and easy to
use. Though the SQLAlchemy library can connect to a variety of relational sources including
MySql, Oracle and Postgresql and Mssql. We first create a database engine and then connect
to the database engine using the to_sql function of the SQLAlchemy library.
In the example we create the relational table by using the to_sql function from a
dataframe already created by reading a csv file.
Then we use the read_sql_query function from pandas to execute and capture the
results from various SQL queries.
Example:
from sqlalchemy import create_engine
import pandas as pd
data = pd.read_csv('/path/input.csv')
# Create the db engine
engine = create_engine('sqlite:///:memory:')
# Store the dataframe as a table
data.to_sql('data_table', engine)
# Query 1 on the relational table
res1 = pd.read_sql_query('SELECT * FROM data_table',
engine)
print('Result 1')
print(res1)
print('')
# Query 2 on the relational table
res2 = pd.read_sql_query('SELECT dept,sum(salary)
FROM data_table group by dept', engine)
print('Result 2')
print(res2)
output:
Result 1
index id name salary start_date dept
87
0 0 1 Rick 623.30 2012-01-01 IT
1 1 2 Dan 515.20 2013-09-23 Operations
2 2 3 Tusar 611.00 2014-11-15 IT
3 3 4 Ryan 729.00 2014-05-11 HR
4 4 5 Gary 843.25 2015-03-27 Finance
5 5 6 Rasmi 578.00 2013-05-21 IT
6 6 7 Pranab 632.80 2013-07-30 Operations
7 7 8 Guru 722.50 2014-06-17 Finance
Result 2
dept sum(salary)
0 Finance 1565.75
1 HR 729.00
2 IT 1812.30
3 Operations 1148.00
88
# Read from the relational table
res= pd.read_sql_query('SELECT ID,Dept,Name,Salary,start_date
FROM data_table', engine)
print(res)
output:
id dept name salary start_date
0 1 IT Rick 623.30 2012-01-01
1 2 Operations Dan 515.20 2013-09-23
2 3 IT Tusar 611.00 2014-11-15
3 4 HR Ryan 729.00 2014-05-11
4 5 Finance Gary 843.25 2015-03-27
5 6 IT Rasmi 578.00 2013-05-21
6 7 Operations Pranab 632.80 2013-07-30
7 8 Finance Guru 722.50 2014-06-17
8 9 IT Ruby 711.20 2015-03-27
89
print(res)
output:
id dept name salary start_date
0 1 IT Rick 623.3 2012-01-01
1 2 Operations Dan 515.2 2013-09-23
2 3 IT Tusar 611.0 2014-11-15
3 4 HR Ryan 729.0 2014-05-11
4 6 IT Rasmi 578.0 2013-05-21
5 7 Operations Pranab 632.8 2013-07-30
6 8 Finance Guru 722.5 2014-06-17
As more and more data become available as unstructured or semi-structured, the need
of managing them through NoSql database increases. Python can also interact with NoSQL
databases in a similar way as is interacts with Relational databases.we will use python to
interact with MongoDB as a NoSQL database.
In order to connect to MongoDB, python uses a library known as pymongo. You can
add this library to your python environment, using the below command from the Anaconda
environment.
conda install pymongo
This library enables python to connect to MOngoDB using a db client. Once
connected we select the db name to be used for various operations.
NoSQL databases are more flexible than relational databases. In these types of
databases, the data storage structure is designed and optimized for specific requirements.
90
There are four main types for NoSQL libraries
• Documents
• key-value,
• wide-column
• graphs.
Inserting Data
To insert data into MongoDB we use the insert() method which is available in the
database environment. First we connect to the db using python code and then we provide the
document details in form of a series of key-value pairs.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to the test db
db=client.test
# Use the employee collection
employee = db.employee
employee_details =
{
'Name': 'Raj Kumar',
'Address': 'Sears Streer, NZ',
'Age': '42'
91
}
# Use the insert method
result = employee.insert_one(employee_details)
# Query for the inserted document.
Queryresult = employee.find_one({'Age': '42'})
pprint(Queryresult)
output:
{u'Address': u'Sears Streer, NZ',
u'Age': u'42',
u'Name': u'Raj Kumar',
u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}
Updating Data
Updating an existing MongoDB data is similar to inserting. We use the update()
method which is native to mongoDB. In the this code we are replacing the existing record
with new key-value pairs.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to db
db=client.test
employee = db.employee
# Use the condition to choose the record
# and use the update method
db.employee.update_one(
{"Age":'42'},
{
"$set":
92
{
"Name":"Srinidhi",
"Age":'35',
"Address":"New Omsk, WC"
}
}
)
Queryresult = employee.find_one({'Age':'35'})
pprint(Queryresult)
output:
{u'Address': u'New Omsk, WC',
u'Age': u'35',
u'Name': u'Srinidhi',
u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}
Deleting Data
Deleting a record is also straight forward where we use the delete method.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to db
db=client.test
employee = db.employee
# Use the condition to choose the record
# and use the delete method
db.employee.delete_one({"Age":'35'})
Queryresult = employee.find_one({'Age':'35'})
pprint(Queryresult)
93
output:
None
Python can be used to support web programming that falls into one of two general
categories
Client programming – accesses web sites or web applications
Server programming – script executed by the server to provide a web site, perform server-
side applications, etc.
Web client
Any program that retrieves data from a web server using the HTTP protocol.
94
Examples:
• web browsers – contact web servers using HTTP protocol and display HTTP
responses
• web crawlers – traverse the web automatically to gather information
• web service clients (service requester) – request and process data from a web service
provider; the web service provider responds using some web service protocol such as
RSS (Rich Site Summary) or RPC (remote procedure call)
Python web client programming
modules that come standard with Python
• urllib – interface for fetching data across the web
• urllib2 – an interface for fetching data across the web that allows you to specify
HTTP request headers, handle authentication and cookies
• httplib – makes http requests; is used by urllib and urllib2
• HTMLParser – for parsing HTML and XHTML files
• xmlrpclib – allows clients to call methods on a remote server
• cookielib (used to be Clientcookie) – provides classes for handling HTTP cookies
• utidylib and mxTidy – for cleaning up html
• BeautifulSoup – permissive HTML parser
• html5lib – for parsing html into a tree
Python urllib
• This module provides a high-level interface for fetching data across the World Wide.
• urlopen() function is similar to the built-in function open(), but accepts Universal
Resource Locators (URLs) instead of filenames (also can only open for reading).
95
urllib methods
urlopen( url[, data[, proxies]]) - Open a network object denoted by a URL for reading. If
the URL does not represent a local file, it opens a socket (a connection between two
programs) to a server somewhere on the network; file-like object is returned,
• typically the request is a GET; if data is given, then the request is a POST
• proxies is a dictionary that can be used to specify the proxy for different types of
requests
prx = {'http': '
the file-like object returned supports the following methods
• read – read max number of bytes or entire file if size omitted
• readline – read a single line
• readlines – returns list of lines read
• fileno – returns integer file number descriptor
• close – close file
• info – returns header info
• geturl – returns actual URL used to access the resource
Example:
import urllib
import re
page = urllib.urlopen("
text = page.read()
atagstr = '<a href="([^"]+)">(.*)</a>'
matches = re.findall(atagstr, text)
for link, name in matches:
name = name.strip()
print "%s: %s" % (name, link)
output
Home: index.html
Handouts: handouts.html
Resources: resources.html
Homeworks: homeworks.html
Slides: slides.html
CS 5530, fall 2007
96
4.3.2 WEB SERVERS
Web servers respond to Hypertext Transfer Protocol (HTTP) requests from clients
and send back a response containing a status code and often content such as HTML, XML or
JSON as well. One important aspect of internet is the web servers that are at the root of the
client server model.
Leading web servers include Apache, Microsoft's Internet Information Services (IIS)
and Nginx -- pronounced engine X.
Gunicorn
Gunicorn is a stand-alone web server which has a central master process tasked with
managing the initiated worker processes of differing types.
Important Features
• It supports WSGI and can be used with any WSGI running Python application and
framework
• It can also be used as a drop-in replacement for Paster (ex: Pyramid), Django's
Development Server, web2py, etc
• Offers the choice of various worker types/configurations and automatic worker
process management
• HTTP/1.0 and HTTP/1.1 (Keep-Alive) support through synchronous and
asynchronous workers
• Comes with SSL support
• Extensible with hooks
CherryPy WSGI Server
CherryPy is a self contained web framework as it can run on its own without the need
of additional software. It has its own WSGI, HTTP/1.1-compliant web server.
Important Features
• It can run any Python web applications running on WSGI.
• It can handle static files and it can just be used to serve files and folders alone.
97
• It is thread-pooled.
• It comes with support for SSL.
• It is an easy to adapt, easy to use pure-Python alternative which is robust and reliable.
Twisted Web
It is a web server that comes with the Twisted networking library. Whereas Twisted
itself is "an event-driven networking engine", the Twisted Web server runs on WSGI and it is
capable of powering other Python web applications.
Important Features
• It runs WSGI Python applications
• It can act like a Python web server framework, allowing you to program it with the
language for custom HTTP serving purposes
• It offers simple and fast prototyping ability through Python Scrips (.rpy) which are
executed upon HTTP requests
• It comes with proxy and reverse-proxy capabilities
• It supports Virtual Hosts
• It can even serve Perl, PHP et cetera
Web services are consumed by data scientists, quality engineers, and application
developer. They can be consumed in Python, R, or via the API. Users can consume the
service directly using a single consumption call, which is referred to as a "Request Response"
approach.
Python is used to fill up online forms automatically. Any suitable Python script can
extract data from a file and fill an equivalent online form. You can automate filling up online
forms like Google forms or other sign-up / login forms.
98
Components of Web Services
The basic web services platform is XML + HTTP. All the standard web services
work using the following components :
• SOAP (Simple Object Access Protocol)
• UDDI (Universal Description, Discovery and Integration)
• WSDL (Web Services Description Language)
How Does a Web Service Work?
A web service enables communication among various applications by using open
standards such as HTML, XML, WSDL, and SOAP.
A web service takes the help of :
• XML to tag the data
• SOAP to transfer a message
• WSDL to describe the availability of service.
Example:
Consider a simple account-management and order processing system. The accounting
personnel use a client application built with Visual Basic or JSP to create new accounts and
enter new customer orders.
The processing logic for this system is written in Java and resides on a Solaris
machine, which also interacts with a database to store information.
The steps to perform this operation are as follows :
• The client program bundles the account registration information into a SOAP
message.
• This SOAP message is sent to the web service as the body of an HTTP POST request.
• The web service unpacks the SOAP request and converts it into a command that the
application can understand.
• The application processes the information as required and responds with a new unique
account number for that customer.
• Next, the web service packages the response into another SOAP message, which it
sends back to the client program in response to its HTTP request.
99
UNIT-V
5.1 SYSTEMS
Python is cross-platform and will work on Windows, macOS, and Linux. It is mostly
a matter of personal preferences when it comes to choosing an operating system.
Python System Command
• Python os. system( ) function. We can execute system command by using os.system( )
function.
• Python subprocess. call( ) Function. In the previous section, we saw that OS.
• Python subprocess. check_output( ) function. So far, we executed the system
commands with the help of python.
5.1.1 FILES
• In Python, files are treated in two modes as text or binary. The file may be in the text
or binary format, and each line of a file is ended with the special character.
• A module is a file containing Python definitions and statements. The file name is the
module name with the suffix .py appended.
• File handling is an important part of any web application.
• Python has several functions for creating, reading, updating, and deleting files.
File Handling
• The key function for working with files in Python is the open( ) function.
• The open ( ) function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
• "r" - Read - Default value. Opens a file for reading, error if the file does not exist
• "a" - Append - Opens a file for appending, creates the file if it does not exist
• "w" - Write - Opens a file for writing, creates the file if it does not exist
• "x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
“t” - Text - Default value. Text mode
“b” - Binary - Binary mode (e.g. images)
To open a file for reading it is enough to specify the name of the file.
100
Syntax:
f = open("demofile.txt")
Read Lines
You can return one line by using the readline( ) method.
Example:
f = open("demofile.txt", "r")
print(f.readline())
output:
Hello! Welcome to demofile.txt
Close Files
It is a good practice to always close the file when you are done with it.
Example:
f = open("demofile.txt", "r")
print(f.readline( ))
f.close( )
output:
Hello! Welcome to demofile.txt
Write to an Existing File
101
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
Example:
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read( ))
output:
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!Now the file has more content!
Example:
102
Example:
Remove the file "demofile.txt":
Import os
os.remove("demofile.txt")
Delete Folder
To delete an entire folder, use the os.rmdir() method.
Example:
Remove the folder "myfolder":
import os
os.rmdir("myfolder")
5.1.2 DIRECTORIES
Directories are a way of storing, organizing, and separating the files on a computer.
The directory that does not have a parent is called a root directory. The way to reach the file
is called the path.
The dir() function returns all properties and methods of the specified object, without
the values.
This function will return all the properties and methods, even built-in properties
which are default for all object.
Syntax:
103
dir(object)
Example:
class Person:
name = "John"
age = 36
country = "Norway"
print(dir(Person))
output:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'age', 'country',
'name']
104
Program contains a set of instructions designed to complete a specific task. Process is
an instance of an executing program. Program is a passive entity as it resides in the secondary
memory. Process is a active entity as it is created during execution and loaded into the main
memory.
A program is a set of instruction codes that has been designed to complete a certain
task. It is a passive entity stored in the secondary memory of the computer system. A program
is considered as a passive and static entity.
A program is like a file which contains a set of instruction codes stored on a disk in
the form of an executable file. A program contains instructions written in any programming
language. Programs have an unlimited span of time.
A process is an instance of a program that is being currently executed. It is a dynamic
and active entity of a program. Processes are created when the programs are executing and
they reside in the main memory.
A process exists only for a limited time, and hence gets terminated as soon as the task
completes. A process always consists of instructions written in machine language. A process
contains temporary data, data selection, etc.
105
Python defines an inbuilt module calendar that handles operations related to the
calendar.
The calendar module allows us to output calendars like the program and provides
additional useful functions related to the calendar. Functions and classes defined in the
calendar module use an idealized calendar, the current Gregorian calendar extended
indefinitely in both directions. By default, these calendars have Monday as the first day of the
week, and Sunday as the last (the European convention).
Example:
Display the Calendar of a given month.
# Python program to display calendar of given month of
the year
# import module
import calendar
yy = 2017
mm = 11
# display the calendar
print(calendar.month(yy, mm))
output:
Example:
Display the calendar of the given year.
# Python code to demonstrate the working of
# calendar() function to print calendar
# importing calendar module
# for calendar operations
import calendar
106
# using calendar to print calendar of year
# prints calendar of 2018
print ("The calendar of year 2018 is : ")
print (calendar.calendar(2018))
output:
107
import time; # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:",
ticks
output:
Number of ticks since 12:00am, January 1, 1970:
7186862.73399
108
Local current time : Tue Jan 13 10:17:09 2009
5.2 CONCURRENCY
5.2.1 QUEUES
Queue in Python is a linear data structure with a rear and a front end, similar to a
stack. It stores items sequentially in a FIFO (First In First Out) manner.
Queue is a linear data structure that stores items in First In First Out (FIFO) manner.
With a queue the least recently added item is removed first. A good example of queue is any
queue of consumers for a resource where the consumer that came first is served first.
Example:
# Queue implementation in Python
class Queue
def __init__(self):
self.queue = []
# Add an element
def enqueue(self, item):
self.queue.append(item)
# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)
# Display the queue
def display(self):
109
print(self.queue)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
print("After removing an element")
q.display()
5.2.2 PROCESSES
5.2.3 THREADS
Threading in python is used to run multiple threads (tasks, function calls) at the same
time. Note that this does not mean that they are executed on different CPUs. Python threads
will NOT make your program faster if it already uses 100 % CPU time. In that case, you
probably want to look into parallel programming.
110
Python threading allows you to have different parts of your program run concurrently
and can simplify your design. If you've got some experience in Python and want to speed up
your program using threads.
111
5.2.5 TWISTED
5.2.6 REDIS
112
• Machine Learning.
• Real-time analytics.
Example
Ensure that you are able to use the following Redis command to connect to the Redis
instance.
redis-cli 127.0.0.1:6379>
The following Python code allows you to connect to the default Redis server instance
pip3 install redis
import redis
pool = redis.ConnectionPool(host='localhost', port=6379,
db=0)
redis = redis.Redis(connection_pool=pool)
edis.set('mykey', 'Hello from Python!')
value = redis.get('mykey')
print(value)
redis.zadd('vehicles', {'car' : 0})
redis.zadd('vehicles', {'bike' : 0})
vehicles = redis.zrange('vehicles', 0, -1)
print(vehicles)
5.3 NETWORKS
113
5.3.1 PATTERNS
Python patterns encode programs in different shapes and formats to create recognized
patterns. These patterns are built using different combinations of codes to allow programmers
logical practice to implement the same strategy in real-life courses and improve programming
skills.
There are a total of 11 behavioral patterns in Python: Chain of responsibility,
Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy,
Template, Visitor.
114
5.3.3 TCP/IP
5.3.4 SOCKETS
5.3.5 ZEROMQ
• ZeroMQ is a library that allows you to perform low-level message passing, but unlike
message-oriented middleware, an ZEROMQ system can run without a dedicated
message broker. To understand ZEROMQ, you need to think in the sense of network
sockets that carry atomic messages across various transports.
115
• ZeroMQ is an asynchronous messaging library, aimed at use in distributed or
concurrent applications. It provides a message queue, but unlike message-oriented
middleware, a ZeroMQ system can run without a dedicated message broker; the zero
in the name is for zero broker.
To access/exchange a large amount of data such as software, audio clips, video clips,
text files, other documents, etc., we need internet services. You must use an Internet service
to connect to the Internet. Data can be sent from Internet servers to your machine via Internet
service.
116
5.3.7 WEB SERVICES AND APIS
Remote Procedure Call (RPC) system enables you to call a function available on a
remote server using the same syntax which is used when calling a function in a local library.
This is useful in two situations.You can utilize the processing power from multiple machines
using rpc without changing the code for making the call to the programs located in the remote
systems.The data needed for the processing is available only in the remote system.
So in python we can treat one machine as a server and another machine as a client
which will make a call to the server to run the remote procedure. In our example we will take
the localhost and use it as both a server and client.
117
JSON or JavaScript Object Notation is a lightweight data-interchange format. It is
easy for humans to read and write. It is easy for machines to parse and generate. The RPC
call made based on JSON is able to send data in a much compact and efficient manner than
the normal XML based RPC call. The python module jsonrpclib is able to create a simple
JSON based server and client.
Example:
# server program
from jsonrpclib.SimpleJSONRPCServer import
SimpleJSONRPCServer
def findlen(*args):
res = [ ]
for arg in args:
try:
lenval = len(arg)
except TypeError:
lenval = None
res.append((lenval, arg))
return res
def main():
server = SimpleJSONRPCServer(('localhost', 1006))
server.register_function(findlen)
print("Start server")
server.serve_forever()
if __name__ == '__main__':
main()
# Call by client
from jsonrpclib import Server
def main():
conn = Server('http://localhost:1006')
print(conn.findlen(('a','x','d','z'), 11, {'Mt. Abu': 1602, 'Mt.
Nanda': 3001,'Mt. Kirubu': 102, 'Mt.Nish': 5710}))
118
if __name__ == '__main__':
main( )
output:
[[4, [u'a', u'x', u'd', u'z']], [None, 11], [4, {u'Mt. Abu': 1602,
u'Mt. Kirubu': 102, u'Mt. Nanda': 3001, u'Mt.Nish': 5710}]]
Big Data Analysis with Python teaches you how to use tools that can control this data
avalanche for you.
119
5.3.10 WORKING IN THE CLOUDS
Cloud Code helps you write, run, and debug cloud-native apps quickly and easily.
Extensions to IDEs provide turnkey support for Python development including code
completion, linting, and snippets.
How To Create Word Cloud in Python
• Import Necessary Libraries
• Selecting the Dataset
• Selecting the Text and Amount of Text for Word Cloud.
• Check for NULL values
• Adding Text to a Variable
• Creating the Word Cloud
• Plotting the Word Cloud
• The Complete Code.
How to make a word cloud in Python and Jupyter Notebook, Step by Step:
• Install the wordcloud package.
• Import wordcloud and matplotlib into your notebook.
• Create a term-document matrix with TF-IDF values (Optional Step)
120
MODEL QUESTION PAPER
SALEM CHRISTIAN COLLEGE OF ARTS AND SCIENCE
CLASS I MSC(CS)
PYTHON PROGRAMMING
Time: 3 Hours Max. Marks: 75
PART-A (1X15=15)
121
(a) The file read automatically (b) The file write automatically
(c) The file closed automatically (d) The file delete automatically
13. A variable defined outside a function is referred to as __________.
(a) global variable (b) a function variable (c) a block variable(d) a local variable
14. In Python code, on encountering a syntax error,the _____ does not execute the program.
(a) Interpeter (b) Processor (c) Editor (d)Converter
15. Which of the follwing keyword is used to access the numpy module in Python?
(a) import (b) access (c) fetch (d) from
PART-B (2X5=10)
Answer Any TWO Questions out of FIVE
16.Explain briefly sets.
17.Write a short note on while loop.
18.What do you mean by Override a Method?
19.Explain the role of Web Servers.
20.Explain the Green Threads and gevent.
PART-C (5X10=10)
Answer ALL the Questions.
21.a) Explain about Dictionaries. (OR)
b) Explaina about the Numbers
22.a) Explain the various Comprehensions. (OR)
b) Explain the various handle errors with try and except.
23.a) Explain about Modules and the import Statement. (OR)
b) Explain about the NoSQL Data Stores.
24. a) List and explain the Calendars and Clocks. (OR)
b) Write about Programs and Processes.
25 a) Discuss the The Publish-Subscribe Model. (OR)
b) Explain the Big Fat Data and MapReduce.
122
CLASS I MSC CS
PYTHON PROGRAMMING
123