0% found this document useful (0 votes)
92 views

Python Maps

Python maps, also called ChainMaps, allow combining multiple dictionaries into a single unit while eliminating duplicate keys. The key-value pairs are maintained in the order of the dictionaries. ChainMaps behave like a stack data structure, with the order of dictionaries impacting the key-value pair order. New values added or existing values updated in the underlying dictionaries are instantly reflected in the ChainMap without recreating it. Linked lists in Python are implemented using nodes, with each node containing a data element and a pointer to the next node. Singly linked lists contain nodes connected in one direction using next pointers, and allow traversing, inserting, and removing elements from the list.

Uploaded by

AZM academy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Python Maps

Python maps, also called ChainMaps, allow combining multiple dictionaries into a single unit while eliminating duplicate keys. The key-value pairs are maintained in the order of the dictionaries. ChainMaps behave like a stack data structure, with the order of dictionaries impacting the key-value pair order. New values added or existing values updated in the underlying dictionaries are instantly reflected in the ChainMap without recreating it. Linked lists in Python are implemented using nodes, with each node containing a data element and a pointer to the next node. Singly linked lists contain nodes connected in one direction using next pointers, and allow traversing, inserting, and removing elements from the list.

Uploaded by

AZM academy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Python - Maps

Python Maps also called ChainMap is a type of data structure to manage multiple
dictionaries together as one unit. The combined dictionary contains the key and
value pairs in a specific sequence eliminating any duplicate keys. The best use of
ChainMap is to search through multiple dictionaries at a time and get the proper
key-value pair mapping. We also see that these ChainMaps behave as stack data
structure.

Creating a ChainMap
We create two dictionaries and club them using the ChainMap method from the
collections library. Then we print the keys and values of the result of the
combination of the dictionaries. If there are duplicate keys, then only the value from
the first key is preserved.
import collections

dict1 = {'day1': 'Mon', 'day2': 'Tue'}


dict2 = {'day3': 'Wed', 'day1': 'Thu'}

res = collections.ChainMap(dict1, dict2)

# Creating a single dictionary


print(res.maps,'\n')

print('Keys = {}'.format(list(res.keys())))
print('Values = {}'.format(list(res.values())))
print()

# Print all the elements from the result


print('elements:')
for key, val in res.items():
print('{} = {}'.format(key, val))
print()

# Find a specific value in the result


print('day3 in res: {}'.format(('day1' in res)))
print('day4 in res: {}'.format(('day4' in res)))

When the above code is executed, it produces the following result.


[{'day1': 'Mon', 'day2': 'Tue'}, {'day1': 'Thu', 'day3': 'Wed'}]

Keys = ['day1', 'day3', 'day2']


Values = ['Mon', 'Wed', 'Tue']

elements:
day1 = Mon
day3 = Wed
day2 = Tue
day3 in res: True
day4 in res: False

Map Reordering
If we change the order the dictionaries while clubbing them in the above example
we see that the position of the elements get interchanged as if they are in a
continuous chain. This again shows the behaviour of Maps as stacks.
import collections

dict1 = {'day1': 'Mon', 'day2': 'Tue'}


dict2 = {'day3': 'Wed', 'day4': 'Thu'}

res1 = collections.ChainMap(dict1, dict2)

print(res1.maps,'\n')

res2 = collections.ChainMap(dict2, dict1)

print(res2.maps,'\n')

When the above code is executed, it produces the following result.

[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day4': 'Thu'}]

[{'day3': 'Wed', 'day4': 'Thu'}, {'day1': 'Mon', 'day2': 'Tue'}]

Updating Map
When the element of the dictionary is updated, the result is instantly updated in the
result of the ChainMap. In the below example we see that the new updated value
reflects in the result without explicitly applying the ChainMap method again.
import collections

dict1 = {'day1': 'Mon', 'day2': 'Tue'}


dict2 = {'day3': 'Wed', 'day4': 'Thu'}

res = collections.ChainMap(dict1, dict2)

print(res.maps,'\n')

dict2['day4'] = 'Fri'

print(res.maps,'\n')

When the above code is executed, it produces the following result.


[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day4': 'Thu'}]
[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day4': 'Fri'}]

Python - Linked Lists


Advertisements

Previous Page
Next Page

A linked list is a sequence of data elements, which are connected together via links.
Each data element contains a connection to another data element in form of a
pointer. Python does not have linked lists in its standard library. We implement the
concept of linked lists using the concept of nodes as discussed in the previous
chapter. We have already seen how we create a node class and how to traverse the
elements of a node. In this chapter we are going to study the types of linked lists
known as singly linked lists. In this type of data structure there is only one link
between any two data elements. We create such a list and create additional
methods to insert, update and remove elements from the list.

Creation of Linked list


A linked list is created by using the node class we studied in the last chapter. We
create a Node object and create another class to use this ode object. We pass the
appropriate values thorugh the node object to point the to the next data elements.
The below program creates the linked list with three data elements. In the next
section we will see how to traverse the linked list.

class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None

list1 = SLinkedList()
list1.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
# Link first Node to second node
list1.headval.nextval = e2

# Link second Node to third node


e2.nextval = e3
Traversing a Linked List
Singly linked lists can be traversed in only forwrad direction starting form the first
data element. We simply print the value of the next data element by assgining the
pointer of the next node to the current data element.
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None

def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

# Link first Node to second node


list.headval.nextval = e2

# Link second Node to third node


e2.nextval = e3

list.listprint()

When the above code is executed, it produces the following result:


Mon
Tue
Wed

Insertion in a Linked List


Inserting element in the linked list involves reassigning the pointers from the existing
nodes to the newly inserted node. Depending on whether the new data element is
getting inserted at the beginning or at the middle or at the end of the linked list, we
have the below scenarios.
Inserting at the Beginning of the Linked List
This involves pointing the next pointer of the new data node to the current head of
the linked list. So the current head of the linked list becomes the second data
element and the new node becomes the head of the linked list.
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None

# Print the linked list


def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval
def AtBegining(self,newdata):
NewNode = Node(newdata)

# Update the new nodes next val to existing node


NewNode.nextval = self.headval
self.headval = NewNode

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtBegining("Sun")

list.listprint()

When the above code is executed, it produces the following result:


Sun
Mon
Tue
Wed
Inserting at the End of the Linked List
This involves pointing the next pointer of the the current last node of the linked list to
the new data node. So the current last node of the linked list becomes the second
last data node and the new node becomes the last node of the linked list.
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None

# Function to add newnode


def AtEnd(self, newdata):
NewNode = Node(newdata)
if self.headval is None:
self.headval = NewNode
return
laste = self.headval
while(laste.nextval):
laste = laste.nextval
laste.nextval=NewNode

# Print the linked list


def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtEnd("Thu")

list.listprint()

When the above code is executed, it produces the following result:


Mon
Tue
Wed
Thu
Inserting in between two Data Nodes
This involves chaging the pointer of a specific node to point to the new node. That is
possible by passing in both the new node and the existing node after which the new
node will be inserted. So we define an additional class which will change the next
pointer of the new node to the next pointer of middle node. Then assign the new
node to next pointer of the middle node.
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None

# Function to add node


def Inbetween(self,middle_node,newdata):
if middle_node is None:
print("The mentioned node is absent")
return

NewNode = Node(newdata)
NewNode.nextval = middle_node.nextval
middle_node.nextval = NewNode

# Print the linked list


def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Thu")

list.headval.nextval = e2
e2.nextval = e3

list.Inbetween(list.headval.nextval,"Fri")

list.listprint()

When the above code is executed, it produces the following result:


Mon
Tue
Fri
Thu

Removing an Item form a Liked List


We can remove an existing node using the key for that node. In the below program
we locate the previous node of the node which is to be deleted. Then point the next
pointer of this node to the next node of the node to be deleted.

class Node:
def __init__(self, data=None):
self.data = data
self.next = None

class SLinkedList:
def __init__(self):
self.head = None

def Atbegining(self, data_in):


NewNode = Node(data_in)
NewNode.next = self.head
self.head = NewNode

# Function to remove node


def RemoveNode(self, Removekey):

HeadVal = self.head

if (HeadVal is not None):


if (HeadVal.data == Removekey):
self.head = HeadVal.next
HeadVal = None
return

while (HeadVal is not None):


if HeadVal.data == Removekey:
break
prev = HeadVal
HeadVal = HeadVal.next

if (HeadVal == None):
return

prev.next = HeadVal.next

HeadVal = None

def LListprint(self):
printval = self.head
while (printval):
print(printval.data),
printval = printval.next

llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()

When the above code is executed, it produces the following result:


Thu
Wed
Mon
Python - Stack
In the english dictionary the word stack means arranging objects on over another. It
is the same way memory is allocated in this data structure. It stores the data
elements in a similar fashion as a bunch of plates are stored one above another in
the kitchen. So stack data strcuture allows operations at one end wich can be called
top of the stack. We can add elements or remove elements only form this en dof the
stack.
In a stack the element insreted last in sequence will come out first as we can
remove only from the top of the stack. Such feature is known as Last in First
Out(LIFO) feature. The operations of adding and removing the elements is known
as PUSH and POP. In the following program we implement it as add and and
remove functions. We dclare an empty list and use the append() and pop() methods
to add and remove the data elements.

PUSH into a Stack


class Stack:

def __init__(self):
self.stack = []

def add(self, dataval):


# Use list append method to add element
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False
# Use peek to look at the top of the stack

def peek(self):
return self.stack[-1]

AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.peek()
print(AStack.peek())
AStack.add("Wed")
AStack.add("Thu")
print(AStack.peek())

When the above code is executed, it produces the following result:


Tue
Thu
POP from a Stack
As we know we can remove only the top most data element from the stack, we
implement a python program which does that. The remove function in the following
program returns the top most element. we check the top element by calculating the
size of the stack first and then use the in-built pop() method to find out the top most
element.

class Stack:

def __init__(self):
self.stack = []

def add(self, dataval):


# Use list append method to add element
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False

# Use list pop method to remove element


def remove(self):
if len(self.stack) <= 0:
return ("No element in the Stack")
else:
return self.stack.pop()

AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.add("Wed")
AStack.add("Thu")
print(AStack.remove())
print(AStack.remove())

When the above code is executed, it produces the following result:


Thu
Wed

Python - Queue
We are familiar with queue in our day to day life as we wait for a service. The queue
data structure aslo means the same where the data elements are arranged in a
queue. The uniqueness of queue lies in the way items are added and removed. The
items are allowed at on end but removed form the other end. So it is a First-in-First
out method. An queue can be implemented using python list where we can use the
insert() and pop() methods to add and remove elements. Their is no insertion as
data elements are always added at the end of the queue.
Adding Elements to a Queue
In the below example we create a queue class where we implement the First-in-
First-Out method. We use the in-built insert method for adding data elements.
class Queue:

def __init__(self):
self.queue = list()

def addtoq(self,dataval):
# Insert method to add element
if dataval not in self.queue:
self.queue.insert(0,dataval)
return True
return False

def size(self):
return len(self.queue)

TheQueue = Queue()
TheQueue.addtoq("Mon")
TheQueue.addtoq("Tue")
TheQueue.addtoq("Wed")
print(TheQueue.size())

When the above code is executed, it produces the following result −


3

Removing Element from a Queue


In the below example we create a queue class where we insert the data and then
remove the data using the in-built pop method.
.

class Queue:

def __init__(self):
self.queue = list()

def addtoq(self,dataval):
# Insert method to add element
if dataval not in self.queue:
self.queue.insert(0,dataval)
return True
return False
# Pop method to remove element
def removefromq(self):
if len(self.queue)>0:
return self.queue.pop()
return ("No elements in Queue!")
TheQueue = Queue()
TheQueue.addtoq("Mon")
TheQueue.addtoq("Tue")
TheQueue.addtoq("Wed")
print(TheQueue.removefromq())
print(TheQueue.removefromq())

When the above code is executed, it produces the following result −


Mon
Tue

Python Stack and Queue


Data structure organizes the storage in computers so that we can easily access and
change data. Stacks and Queues are the earliest data structure defined in computer
science. A simple Python list can act as a queue and stack as well. A queue follows
FIFO rule (First In First Out) and used in programming for sorting. It is common for
stacks and queues to be implemented with an array or linked list.

Stack
A Stack is a data structure that follows the LIFO(Last In First Out) principle. To
implement a stack, we need two simple operations:

o push - It adds an element to the top of the stack.

o pop - It removes an element from the top of the stack.


Operations:

o Adding - It adds the items in the stack and increases the stack size. The
addition takes place at the top of the stack.
o Deletion - It consists of two conditions, first, if no element is present in the
stack, then underflow occurs in the stack, and second, if a stack contains
some elements, then the topmost element gets removed. It reduces the stack
size.
o Traversing - It involves visiting each element of the stack.

Characteristics:

o Insertion order of the stack is preserved.


o Useful for parsing the operations.

o Duplicacy is allowed.

Code

1. # Code to demonstrate Implementation of


2. # stack using list
3. x = ["Python", "C", "Android"]
4. x.push("Java")
5. x.push("C++")
6. print(x)
7. print(x.pop())
8. print(x)
9. print(x.pop())
10.print(x)

Output:

['Python', 'C', 'Android', 'Java', 'C++']


C++
['Python', 'C', 'Android', 'Java']
Java
['Python', 'C', 'Android']

Queue
A Queue follows the First-in-First-Out (FIFO) principle. It is opened from both the
ends hence we can easily add elements to the back and can remove elements from
the front.

To implement a queue, we need two simple operations:

o enqueue - It adds an element to the end of the queue.

o dequeue - It removes the element from the beginning of the queue.


Operations on Queue

o Addition - It adds the element in a queue and takes place at the rear end,
i.e., at the back of the queue.
o Deletion - It consists of two conditions - If no element is present in the
queue, Underflow occurs in the queue, or if a stack contains some elements
then element present at the front gets deleted.
o Traversing - It involves to visit each element of the queue.

Characteristics

o Insertion order of the queue is preserved.

o Duplicacy is allowed.
o Useful for parsing CPU task operations.

Note: The implementation of a queue is a little bit different. A queue follows the
"First-In-First-Out". Time plays an important factor here. The Stack is fast because
we insert and pop the elements from the end of the list, whereas in the queue, the
insertion and pops are made from the beginning of the list, so it becomes slow.
The cause of this time difference is due to the properties of the list, which is fast
in the end operation but slow at the beginning operations because all other
elements have to be shifted one by one.
Code
1. import queue
2. # Queue is created as an object 'L'
3. L = queue.Queue(maxsize=10)
4.
5. # Data is inserted in 'L' at the end using put()
6. L.put(9)
7. L.put(6)
8. L.put(7)
9. L.put(4)
10.# get() takes data from
11.# from the head
12.# of the Queue
13.print(L.get())
14.print(L.get())
15.print(L.get())
16.print(L.get())

Output:

9
6
7
4

Python filter()
The filter() method constructs an iterator from elements of an iterable for
which a function returns true.
In simple words, filter() method filters the given iterable with the help of a
function that tests each element in the iterable to be true or not.
The syntax of filter() method is:

filter(function, iterable)

filter() Parameters

filter() method takes two parameters:


 function - function that tests if elements of an iterable return true or
false
If None, the function defaults to Identity function - which returns false
if any elements are false
 iterable - iterable which is to be filtered, could be sets, lists, tuples, or
containers of any iterators

Return value from filter()

filter() method returns an iterator that passed the function check for each
element in the iterable.
filter() method is equivalent to:

# when function is defined

(element for element in iterable if function(element))


# when function is None

(element for element in iterable if element)

Example 1: How filter() works for iterable list?

# list of letters
letters = ['a', 'b', 'd', 'e', 'i', 'j', 'o']

# function that filters vowels


def filter_vowels(letter):
vowels = ['a', 'e', 'i', 'o', 'u']

if(letter in vowels):
return True
else:
return False

filtered_vowels = filter(filter_vowels, letters)

print('The filtered vowels are:')


for vowel in filtered_vowels:
print(vowel)

Output

The filtered vowels are:


a
e
i
o

Here, we have a list of letters and need to filter out only the vowels in it.
We could use a for loop to loop through each element in letters list and
store it in another list, but in Python, this process is easier and faster
using filter() method.
We have a function filter_vowels that checks if a letter is a vowel or not.
This function is passed onto filter() method with the list of letters.
filter() method then passes each letter to the filter_vowels() function to
check if it returns true or not. In the end, it creates an iterator of the ones
that return true (vowels).
Since the iterator doesn't store the values itself, we loop through it and print
out vowels one by one.

Example 2: How filter() method works without the filter function?

# random list
random_list = [1, 'a', 0, False, True, '0']

filtered_list = filter(None, random_list)

print('The filtered elements are:')


for element in filtered_list:
print(element)

Output

The filtered elements are:


1
a
True
0

Here, we have a random list of numbers, string, and boolean in random_list .

We pass random_list to the filter() method with first parameter (filter


function) as None .
With filter function as None, the function defaults to Identity function, and
each element in random_list is checked if it's true or not.
When we loop through the final filtered_list , we get the elements which
are true: 1, a, True and '0' ('0' as a string is also true).

Python filter() Function


❮ Built-in Functions

Example
Filter the array, and return a new array with only the values equal to or
above 18:

ages = [5, 12, 17, 18, 24, 32]

def myFunc(x):
if x < 18:
return False
else:
return True

adults = filter(myFunc, ages)

for x in adults:
print(x)

Try it Yourself »

Definition and Usage


The filter() function returns an iterator were the items are filtered through a
function to test if the item is accepted or not.

Syntax
filter(function, iterable)
Parameter Values

Parameter Description

function A Function to be run for each item in the iterable

iterable The iterable to be filtered

Python Advance Module

Python Collection Module


The Python collection module is defined as a container that is used to store
collections of data, for example - list, dict, set, and tuple, etc. It was introduced to
improve the functionalities of the built-in collection containers.

Python collection module was first introduced in its 2.4 release.

There are different types of collection modules which are as follows:

namedtuple()
The Python namedtuple() function returns a tuple-like object with names for each
position in the tuple. It was used to eliminate the problem of remembering the index
of each field of a tuple object in ordinary tuples.

Examples

1. pranshu = ('James', 24, 'M')


2. print(pranshu)

Output:

('James', 24, 'M')


OrderedDict()
The Python OrderedDict() is similar to a dictionary object where keys maintain the
order of insertion. If we try to insert key again, the previous value will be
overwritten for that key.

Example

1. import collections
2. d1=collections.OrderedDict()
3. d1['A']=10
4. d1['C']=12
5. d1['B']=11
6. d1['D']=13
7.
8. for k,v in d1.items():
9. print (k,v)

Output:

A 10
C 12
B 11
D 13

defaultdict()
The Python defaultdict() is defined as a dictionary-like object. It is a subclass of the
built-in dict class. It provides all methods provided by dictionary but takes the first
argument as a default data type.

Example

1. from collections import defaultdict


2. number = defaultdict(int)
3. number['one'] = 1
4. number['two'] = 2
5. print(number['three'])

Output:

0
Counter()
The Python Counter is a subclass of dictionary object which helps to count hashable
objects.

Example

1. from collections import Counter


2. c = Counter()
3. list = [1,2,3,4,5,7,8,5,9,6,10]
4. Counter(list)
5. Counter({1:5,2:4})
6. list = [1,2,4,7,5,1,6,7,6,9,1]
7. c = Counter(list)
8. print(c[1])

Output:

deque()
The Python deque() is a double-ended queue which allows us to add and remove
elements from both the ends.

Example

1. from collections import deque


2. list = ["x","y","z"]
3. deq = deque(list)
4. print(deq)

Output:

deque(['x', 'y', 'z'])

Chainmap Objects
A chainmap class is used to groups multiple dictionary together to create a single
list. The linked dictionary stores in the list and it is public and can be accessed by
the map attribute. Consider the following example.

Example

1. from collections import ChainMap


2. baseline = {'Name': 'Peter', 'Age': '14'}
3. adjustments = {'Age': '14', 'Roll_no': '0012'}
4. print(list(ChainMap(adjustments, baseline)))

Output:

['Name', 'Age', 'Roll_no' ]

UserDict Objects
The UserDict behaves as a wrapper around the dictionary objects. The dictionary can
be accessed as an attribute by using the UserDict object. It provides the easiness
to work with the dictionary.

It provides the following attribute.

data - A real dictionary used to store the contents of the UserDict class.

UserList Objects
The UserList behaves as a wrapper class around the list-objects. It is useful when we
want to add new functionality to the lists. It provides the easiness to work with the
dictionary.

It provides the following attribute.

data - A real list is used to store the contents of the User class.

UserString Objects
The UserList behaves as a wrapper class around the list objects. The dictionary can
be accessed as an attribute by using the UserString object. It provides the easiness
to work with the dictionary.

It provides the following attribute.

data - A real str object is used to store the contents of the UserString class.

Python Math Module


Python math module is defined as the most famous mathematical functions, which
includes trigonometric functions, representation functions, logarithmic functions, etc.
Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number,
etc.

Pie (n): It is a well-known mathematical constant and defined as the ratio of


circumstance to the diameter of a circle. Its value is 3.141592653589793.
Euler's number(e): It is defined as the base of the natural logarithmic, and its
value is 2.718281828459045.

There are different math modules which are given below:

math.log()
This method returns the natural logarithm of a given number. It is calculated to the
base e.

Example

1. import math
2. number = 2e-7 # small value of of x
3. print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))

Output:

log(fabs(x), base) is : -6.698970004336019


<

math.log10()
This method returns base 10 logarithm of the given number and called the standard
logarithm.

Example

1. import math
2. x=13 # small value of of x
3. print('log10(x) is :', math.log10(x))

Output:

log10(x) is : 1.1139433523068367

math.exp()
This method returns a floating-point number after raising e to the given number.

Example

1. import math
2. number = 5e-2 # small value of of x
3. print('The given number (x) is :', number)
4. print('e^x (using exp() function) is :', math.exp(number)-1)
Output:

The given number (x) is : 0.05


e^x (using exp() function) is : 0.05127109637602412

math.pow(x,y)
This method returns the power of the x corresponding to the value of y. If value of x
is negative or y is not integer value than it raises a ValueError.

Example

1. import math
2. number = math.pow(10,2)
3. print("The power of number:",number)

Output:

The power of number: 100.0

math.floor(x)
This method returns the floor value of the x. It returns the less than or equal value
to x.

Example:

1. import math
2. number = math.floor(10.25201)
3. print("The floor value is:",number)

Output:

The floor value is: 10

math.ceil(x)
This method returns the ceil value of the x. It returns the greater than or equal
value to x.

1. import math
2. number = math.ceil(10.25201)
3. print("The floor value is:",number)

Output:

The floor value is: 11


math.fabs(x)
This method returns the absolute value of x.

1. import math
2. number = math.fabs(10.001)
3. print("The floor absolute is:",number)

Output:

The absolute value is: 10.001

math.factorial()
This method returns the factorial of the given number x. If x is not integral, it raises
a ValueError.

Example

1. import math
2. number = math.factorial(7)
3. print("The factorial of number:",number)

Output:

The factorial of number: 5040

math.modf(x)
This method returns the fractional and integer parts of x. It carries the sign of x is
float.

Example

1. import math
2. number = math.modf(44.5)
3. print("The modf of number:",number)

Output:

The modf of number: (0.5, 44.0)

Python provides the several math modules which can perform the complex task in
single-line of code. In this tutorial, we have discussed a few important math
modules.

Python Random module


The Python random module functions depend on a pseudo-random number
generator function random(), which generates the float number between 0.0 and
1.0.

There are different types of functions used in a random module which is given
below:

random.random()
This function generates a random float number between 0.0 and 1.0.

random.randint()
This function returns a random integer between the specified integers.

random.choice()
This function returns a randomly selected element from a non-empty sequence.

Example

1. # importing "random" module.


2. import random
3. # We are using the choice() function to generate a random number from
4. # the given list of numbers.
5. print ("The random number from list is : ",end="")
6. print (random.choice([50, 41, 84, 40, 31]))

Output:

The random number from list is : 84

random.shuffle()
This function randomly reorders the elements in the list.

random.randrange(beg,end,step)
This function is used to generate a number within the range specified in its
argument. It accepts three arguments, beginning number, last number, and step,
which is used to skip a number in the range. Consider the following example.

1. # We are using randrange() function to generate in range from 100


2. # to 500. The last parameter 10 is step size to skip
3. # ten numbers when selecting.
4. import random
5. print ("A random number from range is : ",end="")
6. print (random.randrange(100, 500, 10))

Output:

A random number from range is : 290

random.seed()
This function is used to apply on the particular random number with the seed
argument. It returns the mapper value. Consider the following example.

1. # importing "random" module.


2. import random
3. # using random() to generate a random number
4. # between 0 and 1
5. print("The random number between 0 and 1 is : ", end="")
6. print(random.random())
7.
8. # using seed() to seed a random number
9. random.seed(4)

Output:

The random number between 0 and 1 is : 0.4405576668981033

Python statistics module


Python statistics module provides the functions to mathematical statistics of numeric
data. There are some popular statistical functions defined in this module.

mean() function
The mean() function is used to calculate the arithmetic mean of the numbers in the
list.

Example

1. import statistics
2. # list of positive integer numbers
3. datasets = [5, 2, 7, 4, 2, 6, 8]
4. x = statistics.mean(datasets)
5. # Printing the mean
6. print("Mean is :", x)
Output:

How to Install Unity on Ubuntu 18.04 [Complete Procedure]


Mean is : 4.857142857142857

median() function
The median() function is used to return the middle value of the numeric data in the
list.

Example

1. import statistics
2. datasets = [4, -5, 6, 6, 9, 4, 5, -2]
3. # Printing median of the
4. # random data-set
5. print("Median of data-set is : % s "
6. % (statistics.median(datasets)))

Output:

Median of data-set is : 4.5

mode() function
The mode() function returns the most common data that occurs in the list.

Example

1. import statistics
2. # declaring a simple data-set consisting of real valued positive integers.
3. dataset =[2, 4, 7, 7, 2, 2, 3, 6, 6, 8]
4. # Printing out the mode of given data-set
5. print("Calculated Mode % s" % (statistics.mode(dataset)))

Output:

Calculated Mode 2

stdev() function
The stdev() function is used to calculate the standard deviation on a given sample
which is available in the form of the list.

Example
1. import statistics
2. # creating a simple data - set
3. sample = [7, 8, 9, 10, 11]
4. # Prints standard deviation
5. print("Standard Deviation of sample is % s "
6. % (statistics.stdev(sample)))

Output:

Standard Deviation of sample is 1.5811388300841898

median_low()
The median_low function is used to return the low median of numeric data in the
list.

Example

1. import statistics
2. # simple list of a set of integers
3. set1 = [4, 6, 2, 5, 7, 7]
4. # Note: low median will always be a member of the data-set.
5. # Print low median of the data-set
6. print("Low median of data-set is % s "
7. % (statistics.median_low(set1)))

Output:

Low median of the data-set is 5

median_high()
The median_high function is used to return the high median of numeric data in the
list.

Example

1. import statistics
2. # list of set of the integers
3. dataset = [2, 1, 7, 6, 1, 9]
4. print("High median of data-set is %s "
5. % (statistics.median_high(dataset)))

Output:
High median of the data-set is 6

Python - Multithreaded Programming


Running several threads is similar to running several different programs
concurrently, but with the following benefits −
 Multiple threads within a process share the same data space with the main
thread and can therefore share information or communicate with each other
more easily than if they were separate processes.
 Threads sometimes called light-weight processes and they do not require
much memory overhead; they are cheaper than processes.
A thread has a beginning, an execution sequence, and a conclusion. It has an
instruction pointer that keeps track of where within its context it is currently running.
 It can be pre-empted (interrupted)
 It can temporarily be put on hold (also known as sleeping) while other threads
are running - this is called yielding.

Starting a New Thread


To spawn another thread, you need to call following method available
in thread module −
thread.start_new_thread ( function, args[, kwargs] )
This method call enables a fast and efficient way to create new threads in both
Linux and Windows.
The method call returns immediately and the child thread starts and calls function
with the passed list of args. When function returns, the thread terminates.
Here, args is a tuple of arguments; use an empty tuple to call function without
passing any arguments. kwargs is an optional dictionary of keyword arguments.
Example
#!/usr/bin/python

import thread
import time

# Define a function for the thread


def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows


try:
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print "Error: unable to start thread"

while 1:
pass

When the above code is executed, it produces the following result −


Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009
Although it is very effective for low-level threading, but the thread module is very
limited compared to the newer threading module.

The Threading Module


The newer threading module included with Python 2.4 provides much more
powerful, high-level support for threads than the thread module discussed in the
previous section.
The threading module exposes all the methods of the thread module and provides
some additional methods −
 threading.activeCount() − Returns the number of thread objects that are
active.
 threading.currentThread() − Returns the number of thread objects in the
caller's thread control.
 threading.enumerate() − Returns a list of all thread objects that are currently
active.
In addition to the methods, the threading module has the Thread class that
implements threading. The methods provided by the Thread class are as follows −
 run() − The run() method is the entry point for a thread.
 start() − The start() method starts a thread by calling the run method.
 join([time]) − The join() waits for threads to terminate.
 isAlive() − The isAlive() method checks whether a thread is still executing.
 getName() − The getName() method returns the name of a thread.
 setName() − The setName() method sets the name of a thread.

Creating Thread Using Threading Module


To implement a new thread using the threading module, you have to do the
following −
 Define a new subclass of the Thread class.
 Override the __init__(self [,args]) method to add additional arguments.
 Then, override the run(self [,args]) method to implement what the thread
should do when started.
Once you have created the new Thread subclass, you can create an instance of it
and then start a new thread by invoking the start(), which in turn calls run() method.
Example
#!/usr/bin/python

import threading
import time

exitFlag = 0

class myThread (threading.Thread):


def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Starting " + self.name
print_time(self.name, 5, self.counter)
print "Exiting " + self.name

def print_time(threadName, counter, delay):


while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1

# Create new threads


thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads


thread1.start()
thread2.start()

print "Exiting Main Thread"

When the above code is executed, it produces the following result −


Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2
Synchronizing Threads
The threading module provided with Python includes a simple-to-implement locking
mechanism that allows you to synchronize threads. A new lock is created by calling
the Lock() method, which returns the new lock.
The acquire(blocking) method of the new lock object is used to force threads to run
synchronously. The optional blocking parameter enables you to control whether the
thread waits to acquire the lock.
If blocking is set to 0, the thread returns immediately with a 0 value if the lock
cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the
thread blocks and wait for the lock to be released.
The release() method of the new lock object is used to release the lock when it is no
longer required.
Example
#!/usr/bin/python

import threading
import time

class myThread (threading.Thread):


def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Starting " + self.name
# Get lock to synchronize threads
threadLock.acquire()
print_time(self.name, self.counter, 3)
# Free lock to release next thread
threadLock.release()

def print_time(threadName, delay, counter):


while counter:
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1

threadLock = threading.Lock()
threads = []
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads


thread1.start()
thread2.start()

# Add threads to thread list


threads.append(thread1)
threads.append(thread2)

# Wait for all threads to complete


for t in threads:
t.join()
print "Exiting Main Thread"

When the above code is executed, it produces the following result −


Starting Thread-1
Starting Thread-2
Thread-1: Thu Mar 21 09:11:28 2013
Thread-1: Thu Mar 21 09:11:29 2013
Thread-1: Thu Mar 21 09:11:30 2013
Thread-2: Thu Mar 21 09:11:32 2013
Thread-2: Thu Mar 21 09:11:34 2013
Thread-2: Thu Mar 21 09:11:36 2013
Exiting Main Thread
Multithreaded Priority Queue
The Queue module allows you to create a new queue object that can hold a specific
number of items. There are following methods to control the Queue −
 get() − The get() removes and returns an item from the queue.
 put() − The put adds item to a queue.
 qsize() − The qsize() returns the number of items that are currently in the
queue.
 empty() − The empty( ) returns True if queue is empty; otherwise, False.
 full() − the full() returns True if queue is full; otherwise, False.

Example
#!/usr/bin/python

import Queue
import threading
import time

exitFlag = 0

class myThread (threading.Thread):


def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print "Starting " + self.name
process_data(self.name, self.q)
print "Exiting " + self.name

def process_data(threadName, q):


while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print "%s processing %s" % (threadName, data)
else:
queueLock.release()
time.sleep(1)

threadList = ["Thread-1", "Thread-2", "Thread-3"]


nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1

# Create new threads


for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1

# Fill the queue


queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()

# Wait for queue to empty


while not workQueue.empty():
pass

# Notify threads it's time to exit


exitFlag = 1

# Wait for all threads to complete


for t in threads:
t.join()
print "Exiting Main Thread"

When the above code is executed, it produces the following result −


Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
Exiting Thread-3
Exiting Thread-1
Exiting Thread-2
Exiting Main Thread

Python Multiprocessing
In this article, we will learn how we can achieve multiprocessing using Python. We
also discuss its advanced concepts.

What is Multiprocessing?
Multiprocessing is the ability of the system to run one or more processes in parallel.
In simple words, multiprocessing uses the two or more CPU within the single
computer system. This method is also capable to allocate the tasks between more
than one process.

Processing units share the main memory and peripherals to process programs
simultaneously. Multiprocessing Application breaks into smaller parts and runs
independently. Each process is allocated to the processor by the operating system.

Python provides the built-in package called multiprocessing which supports swapping
processes. Before working with the multiprocessing, we must aware with the process
object.

Why Multiprocessing?
Multiprocessing is essential to perform the multiple tasks within the Computer
system. Suppose a computer without multiprocessing or single processor. We assign
various processes to that system at the same time.

It will then have to interrupt the previous task and move to another to keep all
processes going. It is as simple as a chef is working alone in the kitchen. He has to
do several tasks to cook food such as cutting, cleaning, cooking, kneading dough,
baking, etc.

Therefore, multiprocessing is essential to perform several task at the same time


without interruption. It also makes easy to track all the tasks. That is why the
concept of multiprocessing is to arise.

o Multiprocessing can be represented as a computer with more than one central


processor.
o A Multi-core processor refers to single computing component with two or
more independent units.

In the multiprocessing, the CPU can assign multiple tasks at one each task has its
own processor.

Multiprocessing In Python
Python provides the multiprocessing module to perform multiple tasks within the
single system. It offers a user-friendly and intuitive API to work with the
multiprocessing.

Let's understand the simple example of multiple processing.

Example -

1. from multiprocessing import Process


2. def disp():
3. print ('Hello !! Welcome to Python Tutorial')
4. if __name__ == '__main__':
5. p = Process(target=disp)
6. p.start()
7. p.join()

Output:

'Hello !! Welcome to Python Tutorial'

Explanation:

In the above code, we have imported the Process class then create the Process
object within the disp() function. Then we started the process using
the start() method and completed the process with the join() method. We can also
pass the arguments in the declared function using the args keywords.

Let's understand the following example of the multiprocessing with arguments.

Example - 2

1. # Python multiprocessing example


2. # importing the multiprocessing module
3.
4. import multiprocessing
5. def cube(n):
6. # This function will print the cube of the given number
7. print("The Cube is: {}".format(n * n * n))
8.
9. def square(n):
10. # This function will print the square of the given number
11. print("The Square is: {}".format(n * n))
12.
13.if __name__ == "__main__":
14. # creating two processes
15. process1 = multiprocessing.Process(target= square, args=(5, ))
16. process2 = multiprocessing.Process(target= cube, args=(5, ))
17.
18. # Here we start the process 1
19. process1.start()
20. # Here we start process 2
21. process2.start()
22.
23. # The join() method is used to wait for process 1 to complete
24. process1.join()
25. # It is used to wait for process 1 to complete
26. process2.join()
27.
28. # Print if both processes are completed
29. print("Both processes are finished")

Output:

The Cube is: 125


The Square is: 25
Both processes are finished

Explanation -

In the above example, We created the two functions - the cube() function
calculates the given number's cube, and the square() function calculates the square
of the given number.

Next, we defined the process object of the Process class that has two arguments.
The first argument is a target that represents the function to be executed, and the
second argument is args that represents the argument to be passed within the
function.

1. process1 = multiprocessing.Process(target= square, args=(5, ))


2. process2 = multiprocessing.Process(target= cube, args=(5, ))

We have used the start() method to start the process.

1. process1.start()
2. process2.start()

As we can see in the output, it waits to completion of process one and


then process 2. The last statement is executed after both processes are finished.

Python Multiprocessing Classes


Python multiprocessing module provides many classes which are commonly used for
building parallel program. We will discuss its main classes - Process, Queue and
Lock. We have already discussed the Process class in the previous example. Now we
will discuss the Queue and Lock classes.

Let's see the simple example of a get number of CPUs currently in the system.

Example -

1. import multiprocessing
2. print("The number of CPU currently working in system : ", multiprocessing.cp
u_count())

Output:

('The number of CPU currently woking in system : ', 32)

The above number of CPUs can vary for your pc. For us, the number of cores is 32.

Python Multiprocessing Using Queue Class


We know that Queue is important part of the data structure. Python multiprocessing
is precisely the same as the data structure queue, which based on the "First-In-
First-Out" concept. Queue generally stores the Python object and plays an essential
role in sharing data between processes.

Queues are passed as a parameter in the Process' target function to allow the
process to consume data. The Queue provides the put() function to insert the data
and get() function to get data from the queues. Let's understand the following
example.

Example -

1. # Importing Queue Class


2.
3. from multiprocessing import Queue
4.
5. fruits = ['Apple', 'Orange', 'Guava', 'Papaya', 'Banana']
6. count = 1
7. # creating a queue object
8. queue = Queue()
9. print('pushing items to the queue:')
10.for fr in fruits:
11. print('item no: ', count, ' ', fr)
12. queue.put(fr)
13. count += 1
14.
15.print('\npopping items from the queue:')
16.count = 0
17.while not queue.empty():
18. print('item no: ', count, ' ', queue.get())
19. count += 1

Output:

pushing items to the queue:


('item no: ', 1, ' ', 'Apple')
('item no: ', 2, ' ', 'Orange')
('item no: ', 3, ' ', 'Guava')
('item no: ', 4, ' ', 'Papaya')
('item no: ', 5, ' ', 'Banana')

popping items from the queue:


('item no: ', 0, ' ', 'Apple')
('item no: ', 1, ' ', 'Orange')
('item no: ', 2, ' ', 'Guava')
('item no: ', 3, ' ', 'Papaya')
('item no: ', 4, ' ', 'Banana')

Explanation -

In the above code, we have imported the Queue class and initialized the list named
fruits. Next, we assigned a count to 1. The count variable will count the total
number of elements. Then, we created the queue object by calling
the Queue() method. This object will used to perform operations in the Queue. In
for loop, we inserted the elements one by one in the queue using the put() function
and increased the count by 1 with each iteration of loop.
Python Multiprocessing Lock Class
The multiprocessing Lock class is used to acquire a lock on the process so that we
can hold the other process to execute a similar code until the lock has been
released. The Lock class performs mainly two tasks. The first is to acquire a lock
using the acquire() function and the second is to release the lock using
the release() function.

Python Multiprocessing Example


Suppose we have multiple tasks. So, we create two queues: the first queue will
maintain the tasks, and the other will store the complete task log. The next step is
to instantiate the processes to complete the task. As discussed previously, the
Queue class is already synchronized, so we don't need to acquire a lock using the
Lock class.

In the following example, we will merge all the multiprocessing classes together.
Let's see the below example.

Example -

1. from multiprocessing import Lock, Process, Queue, current_process


2. import time
3. import queue
4.
5.
6. def jobTodo(tasks_to_perform, complete_tasks):
7. while True:
8. try:
9.
10. # The try block to catch task from the queue.
11. # The get_nowait() function is used to
12. # raise queue.Empty exception if the queue is empty.
13.
14. task = tasks_to_perform.get_nowait()
15.
16. except queue.Empty:
17.
18. break
19. else:
20.
21. # if no exception has been raised, the else block will execute
22. # add the task completion
23.
24.
25. print(task)
26. complete_tasks.put(task + ' is done by ' + current_process().name)
27. time.sleep(.5)
28. return True
29.
30.
31.def main():
32. total_task = 8
33. total_number_of_processes = 3
34. tasks_to_perform = Queue()
35. complete_tasks = Queue()
36. number_of_processes = []
37.
38. for i in range(total_task):
39. tasks_to_perform.put("Task no " + str(i))
40.
41. # defining number of processes
42. for w in range(total_number_of_processes):
43. p = Process(target=jobTodo, args=(tasks_to_perform, complete_tasks)
)
44. number_of_processes.append(p)
45. p.start()
46.
47. # completing process
48. for p in number_of_processes:
49. p.join()
50.
51. # print the output
52. while not complete_tasks.empty():
53. print(complete_tasks.get())
54.
55. return True
56.
57.
58.if __name__ == '__main__':
59. main()

Output:

Task no 2
Task no 5
Task no 0
Task no 3
Task no 6
Task no 1
Task no 4
Task no 7
Task no 0 is done by Process-1
Task no 1 is done by Process-3
Task no 2 is done by Process-2
Task no 3 is done by Process-1
Task no 4 is done by Process-3
Task no 5 is done by Process-2
Task no 6 is done by Process-1
Task no 7 is done by Process-3

Python Multiprocessing Pool


Python multiprocessing pool is essential for parallel execution of a function across
multiple input values. It is also used to distribute the input data across
processes (data parallelism). Consider the following example of a multiprocessing
Pool.

Example -

1. from multiprocessing import Pool


2. import time
3.
4. w = (["V", 5], ["X", 2], ["Y", 1], ["Z", 3])
5.
6.
7. def work_log(data_for_work):
8. print(" Process name is %s waiting time is %s seconds" % (data_for_work
[0], data_for_work[1]))
9. time.sleep(int(data_for_work[1]))
10. print(" Process %s Executed." % data_for_work[0])
11.
12.
13.def handler():
14. p = Pool(2)
15. p.map(work_log, w)
16.
17.if __name__ == '__main__':
18. handler()

Output:

Process name is V waiting time is 5 seconds


Process V Executed.
Process name is X waiting time is 2 seconds
Process X Executed.
Process name is Y waiting time is 1 seconds
Process Y Executed.
Process name is Z waiting time is 3 seconds
Process Z Executed.

Let's understand another example of the multiprocessing Pool.

Example - 2

1. from multiprocessing import Pool


2. def fun(x):
3. return x*x
4.
5. if __name__ == '__main__':
6. with Pool(5) as p:
7. print(p.map(fun, [1, 2, 3]))

Output:

[1, 8, 27]

Proxy Objects
The proxy objects are referred to as shared objects which reside in a different
process. This object is also called as a proxy. Multiple proxy objects might have a
similar referent. A proxy object consists of various methods which are used to
invoked corresponding methods of its referent. Below is the example of proxy
objects.

Example -

1. from multiprocessing import Manager


2. manager = Manager()
3. l = manager.list([i*i for i in range(10)])
4. print(l)
5. print(repr(l))
6. print(l[4])
7. print(l[2:5])

Output:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


<ListProxy object, typeid 'list' at 0x7f063621ea10>
16
[4, 9, 16]

The proxy objects are picklable so we can pass them between processes. These
objects are also used for level of control over the synchronization.

Commonly Used Functions of


Multiprocessing
So far, we have discussed the basic concepts of multiprocessing using Python.
Multiprocessing is a broad topic itself and essential for performing various tasks
within a single system. We are defining a few essential functions that are commonly
used to achieve multiprocessing.

Method Description

pipe() The pipe() function returns a pair of connection objects.

run() The run() method is used to represent the process


activities.

start() The start()method is used to start the process.

join([timeout]) The join() method is used to block the process until the
process whose join() method is called terminates. The
timeout is optional argument.

is_alive() It returns if process is alive.

terminate() As the name suggests, it is used to terminate the


process. Always remember - the terminate() method is
used in Linux, for Windows, we
use TerminateProcess() method.
kill() This method is similar to the terminate() but using the
SIGKILL signal on Unix.

close() This method is used to close the Process object and


releases all resources associated with it.

qsize() It returns the approximate size of the queue.

empty() If queue is empty, it returns True.

full() It returns True, if queue is full.

get_await() This method is equivalent get(False).

get() This method is used to get elements from the queue. It


removes and returns an element from queue.

put() This method is used to insert an element into the queue.

cpu_count() It returns the number of working CPU within the system.

current_process() It returns the Process object corresponding to the


current process.

parent_process() It returns the parent Process object corresponding to the


current process.

task_done() This function is used indicate that an enqueued task is


completed.

join_thread() This method is used to join the background thread


Python Sending Email using SMTP
Simple Mail Transfer Protocol (SMTP) is used as a protocol to handle the email
transfer using Python. It is used to route emails between email servers. It is an
application layer protocol which allows to users to send mail to another. The receiver
retrieves email using the protocols POP(Post Office
Protocol) and IMAP(Internet Message Access Protocol).

When the server listens for the TCP connection from a client, it initiates a connection
on port 587.

Python provides a smtplib module, which defines an the SMTP client session object
used to send emails to an internet machine. For this purpose, we have to import
the smtplib module using the import statement.

1. $ import smtplib

The SMTP object is used for the email transfer. The following syntax is used to
create the smtplib object.

1. import smtplib
2. smtpObj = smtplib.SMTP(host, port, local_hostname)

It accepts the following parameters.

o host: It is the hostname of the machine which is running your SMTP server.
Here, we can specify the IP address of the server like
(https://www.javatpoint.com) or localhost. It is an optional parameter.
o port: It is the port number on which the host machine is listening to the
SMTP connections. It is 25 by default.
o local_hostname: If the SMTP server is running on your local machine, we
can mention the hostname of the local machine.

The sendmail() method of the SMTP object is used to send the mail to the desired
machine. The syntax is given below.

1. smtpObj.sendmail(sender, receiver, message)

Example

1. #!/usr/bin/python3
2. import smtplib
3. sender_mail = 'sender@fromdomain.com'
4. receivers_mail = ['reciever@todomain.com']
5. message = """From: From Person %s
6. To: To Person %s
7. Subject: Sending SMTP e-mail
8. This is a test e-mail message.
9. """%(sender_mail,receivers_mail)
10.try:
11. smtpObj = smtplib.SMTP('localhost')
12. smtpObj.sendmail(sender_mail, receivers_mail, message)
13. print("Successfully sent email")
14.except Exception:
15. print("Error: unable to send email")

Sending email from gmail


There are cases where the emails are sent using the Gmail SMTP server. In this
case, we can pass Gmail as the SMTP server instead of using the localhost with the
port 587.

Use the following syntax.

1. $ smtpObj = smtplib.SMTP("gmail.com", 587)

Here, we need to login to the Gmail account using Gmail user name and password.
For this purpose, the smtplib provide the login() method, which accepts the
username and password of the sender.
This may make your Gmail ask you for access to less secure apps if you're using
Gmail. You will need to turn this ON temporarily for this to work.

Consider the following example.

Example

1. #!/usr/bin/python3
2. import smtplib
3. sender_mail = 'sender@gmail.com'
4. receivers_mail = ['reciever@gmail.com']
5. message = """From: From Person %s
6. To: To Person %s
7. Subject: Sending SMTP e-mail
8. This is a test e-mail message.
9. """%(sender_mail,receivers_mail)
10.try:
11. password = input('Enter the password');
12. smtpObj = smtplib.SMTP('gmail.com',587)
13. smtpobj.login(sender_mail,password)
14. smtpObj.sendmail(sender_mail, receivers_mail, message)
15. print("Successfully sent email")
16.except Exception:
17. print("Error: unable to send email")

Sending HTML in email


We can format the HTML in the message by specifying the MIME version, content-
type, and character set to send the HTML.

Consider the following example.

Example

1. #!/usr/bin/python3
2. import smtplib
3. sender_mail = 'sender@fromdomain.com'
4. receivers_mail = ['reciever@todomain.com']
5. message = """From: From Person %s
6. To: To Person %s
7.
8. MIME-Version:1.0
9. Content-type:text/html
10.
11.
12.Subject: Sending SMTP e-mail
13.
14.<h3>Python SMTP</h3>
15.<strong>This is a test e-mail message.</strong>
16."""%(sender_mail,receivers_mail)
17.try:
18. smtpObj = smtplib.SMTP('localhost')
19. smtpObj.sendmail(sender_mail, receivers_mail, message)
20. print("Successfully sent email")
21.except Exception:
22. print("Error: unable to send email")

Python read excel file


Excel is a spreadsheet application which is developed by Microsoft. It is an easily
accessible tool to organize, analyze, and store the data in tables. It is widely used in
many different applications all over the world. From Analysts to CEOs, various
professionals use Excel for both quick stats and serious data crunching.

Excel Documents
An Excel spreadsheet document is called a workbook which is saved in a file
with .xlsx extension. The first row of the spreadsheet is mainly reserved for the
header, while the first column identifies the sampling unit. Each workbook can
contain multiple sheets that are also called a worksheets. A box at a particular
column and row is called a cell, and each cell can include a number or text value.
The grid of cells with data forms a sheet.

The active sheet is defined as a sheet in which the user is currently viewing or last
viewed before closing Excel.

Reading from an Excel file


First, you need to write a command to install the xlrd module.

1. pip install xlrd


Creating a Workbook
A workbook contains all the data in the excel file. You can create a new workbook
from scratch, or you can easily create a workbook from the excel file that already
exists.

Input File

We have taken the snapshot of the workbook.

Code

1. # Import the xlrd module


2. import xlrd
3.
4. # Define the location of the file
5. loc = ("path of file")
6.
7. # To open the Workbook
8. wb = xlrd.open_workbook(loc)
9. sheet = wb.sheet_by_index(0)
10.
11.# For row 0 and column 0
12.sheet.cell_value(0, 0)

Explanation: In the above example, firstly, we have imported the xlrd module and
defined the location of the file. Then we have opened the workbook from the excel
file that already exists.

Reading from the Pandas


Pandas is defined as an open-source library which is built on the top of the NumPy
library. It provides fast analysis, data cleaning, and preparation of the data for the
user and supports both xls and xlsx extensions from the URL.
It is a python package which provides a beneficial data structure called a data
frame.

Example

1. Example -
2. import pandas as pd
3.
4. # Read the file
5. data = pd.read_csv(".csv", low_memory=False)
6.
7. # Output the number of rows
8. print("Total rows: {0}".format(len(data)))
9.
10.# See which headers are available
11.print(list(data))

Reading from the openpyxl


First, we need to install an openpyxl module using pip from the command line.

1. pip install openpyxl

After that, we need to import the module.

We can also read data from the existing spreadsheet using openpyxl. It also allows
the user to perform calculations and add content that was not part of the original
dataset.

Example

1. import openpyxl
2. my_wb = openpyxl.Workbook()
3. my_sheet = my_wb.active
4. my_sheet_title = my_sheet.title
5. print("My sheet title: " + my_sheet_title)

Output:

My sheet title: Sheet

To learn more about openpyxl, visit our complete tutorial Click Here. We have
discussed essential detail in this tutorial.
Python Write Excel File
The Python write excel file is used to perform the multiple operations on a
spreadsheet using the xlwt module. It is an ideal way to write data and format
information to files with .xls extension.

If you want to write data to any file and don't want to go through the trouble of
doing everything by yourself, then you can use a for loop to automate the whole
process a little bit.

Write Excel File Using xlsxwriter Module


We can also write the excel file using the xlsxwriter module. It is defined as a
Python module for writing the files in the XLSX file format. It can also be used to
write text, numbers, and formulas to multiple worksheets. Also, it supports features
such as charts, formatting, images, page setup, auto filters, conditional formatting,
and many others.

We need to use the following command to install xlsxwriter module:

1. pip install xlsxwriter


Note- Throughout XlsxWriter, rows, and columns are zero-indexed. The first cell in
a worksheet is listed as, A1 is (0,0), B1 is (0,1), A2 is (1,0), B2 is (1,1)......,and so
on.

Write Excel File Using openpyxl Module


It is defined as a package which is generally recommended if you want to read and
write .xlsx, xlsm, xltx, and xltm files. You can check it by running type(wb).

The load_workbook() function takes an argument and returns a workbook object,


which represents the file. Make sure that you are in the same directory where your
spreadsheet is located. Otherwise, you will get an error while importing.

You can easily use a for loop with the help of the range() function to help you to
print out the values of the rows that have values in column 2. If those particular
cells are empty, you will get None.

Writing data to Excel files with xlwt


You can use the xlwt package, apart from the XlsxWriter package to create the
spreadsheets that contain your data. It is an alternative package for writing data,
formatting information, etc. and ideal for writing the data and format information to
files with .xls extension. It can perform multiple operations on the spreadsheet.

It supports features such as formatting, images, charts, page setup, auto filters,
conditional formatting, and many others.
Pandas have excellent methods for reading all kinds of data from excel files. We can
also import the results back to pandas.

Writing Files with pyexcel


You can easily export your arrays back to a spreadsheet by using the save_as()
function and pass the array and name of the destination file to the dest_file_name
argument.

It allows us to specify the delimiter and add dest_delimiter argument. You can pass
the symbol that you want to use as a delimiter in-between " ".

Code

1. # import xlsxwriter module


2. import xlsxwriter
3.
4. book = xlsxwriter.Book('Example2.xlsx')
5. sheet = book.add_sheet()
6.
7. # Rows and columns are zero indexed.
8. row = 0
9. column = 0
10.
11.content = ["Parker", "Smith", "John"]
12.
13.# iterating through the content list
14.for item in content :
15.
16. # write operation perform
17. sheet.write(row, column, item)
18.
19. # incrementing the value of row by one with each iterations.
20. row += 1
21.
22.book.close()

Output:

Python Regular Expressions


The regular expressions can be defined as the sequence of characters which are
used to search for a pattern in a string. The module re provides the support to use
regex in the python program. The re module throws an exception if there is some
error while using the regular expression.

The re module must be imported to use the regex functionalities in python.

1. import re

Regex Functions
The following regex functions are used in the python.

SN Function Description

1 match This method matches the regex pattern in the string with the
optional flag. It returns true if a match is found in the string
otherwise it returns false.

2 search This method returns the match object if there is a match


found in the string.

3 findall It returns a list that contains all the matches of a pattern in


the string.

4 split Returns a list in which the string has been split in each
match.

5 sub Replace one or many matches in the string.

Forming a regular expression


A regular expression can be formed by using the mix of meta-characters, special
sequences, and sets.

Meta-Characters
Metacharacter is a character with the specified meaning.

Metacharacte Description Example


r

[] It represents the set of characters. "[a-z]"

\ It represents the special sequence. "\r"

. It signals that any character is present at "Ja.v."


some specific place.

^ It represents the pattern present at the "^Java"


beginning of the string.

$ It represents the pattern present at the end "point"


of the string.

* It represents zero or more occurrences of a "hello*"


pattern in the string.

+ It represents one or more occurrences of a "hello+"


pattern in the string.

{} The specified number of occurrences of a "java{2}"


pattern the string.

| It represents either this or that character is "java|point"


present.

() Capture and group

Special Sequences
Special sequences are the sequences containing \ followed by one of the characters.

Character Description
\A It returns a match if the specified characters are present at the
beginning of the string.

\b It returns a match if the specified characters are present at the


beginning or the end of the string.

\B It returns a match if the specified characters are present at the


beginning of the string but not at the end.

\d It returns a match if the string contains digits [0-9].

\D It returns a match if the string doesn't contain the digits [0-9].

\s It returns a match if the string contains any white space


character.

\S It returns a match if the string doesn't contain any white space


character.

\w It returns a match if the string contains any word characters.

\W It returns a match if the string doesn't contain any word.

\Z Returns a match if the specified characters are at the end of the


string.

Sets
A set is a group of characters given inside a pair of square brackets. It represents
the special meaning.

SN Set Description

1 [arn] Returns a match if the string contains any of the specified


characters in the set.
2 [a-n] Returns a match if the string contains any of the
characters between a to n.

3 [^arn] Returns a match if the string contains the characters


except a, r, and n.

4 [0123] Returns a match if the string contains any of the specified


digits.

5 [0-9] Returns a match if the string contains any digit between


0 and 9.

6 [0-5][0- Returns a match if the string contains any digit between


9] 00 and 59.

10 [a-zA-Z] Returns a match if the string contains any alphabet


(lower-case or upper-case).

The findall() function


This method returns a list containing a list of all matches of a pattern within the
string. It returns the patterns in the order they are found. If there are no matches,
then an empty list is returned.

Consider the following example.

Example

1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.findall("How", str)
6.
7. print(matches)
8.
9. print(matches)

Output:
['How', 'How']

The match object


The match object contains the information about the search and the output. If there
is no match found, the None object is returned.

Example
1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.search("How", str)
6.
7. print(type(matches))
8.
9. print(matches) #matches is the search object

Output:

<class '_sre.SRE_Match'>
<_sre.SRE_Match object; span=(0, 3), match='How'>

The Match object methods


There are the following methods associated with the Match object.

1. span(): It returns the tuple containing the starting and end position of the
match.

2. string(): It returns a string passed into the function.

3. group(): The part of the string is returned where the match is found.

Example
1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.search("How", str)
6.
7. print(matches.span())
8.
9. print(matches.group())
10.
11.print(matches.string)

Output:

(0, 3)
How
How are you. How is everything

You might also like