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

Python Unit5

A python module allows code reuse through importing reusable code segments. Common uses of modules include importing mathematical functions from the Numpy library. Modules can be imported in their entirety or specific objects can be imported. Algorithms like the Sieve of Eratosthenes, Tower of Hanoi, and Merge Sort are then explained in detail through examples and pseudocode.

Uploaded by

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

Python Unit5

A python module allows code reuse through importing reusable code segments. Common uses of modules include importing mathematical functions from the Numpy library. Modules can be imported in their entirety or specific objects can be imported. Algorithms like the Sieve of Eratosthenes, Tower of Hanoi, and Merge Sort are then explained in detail through examples and pseudocode.

Uploaded by

yam639353
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

A python module is a .

py file that contains variables, constants functions, class


and objects and can be reused in other programs. The collection of modules is
called a library which has modules related to specific purpose. For example, the
Numpy library provides modules related to advance mathematics along with
tools to create or manipulate numeric arrays.

How to import modules


Python uses import statement to import a module in a program. This statement
can be used in two ways:

• To import entire module:

Syntax:
import module → To import single module
import module_1, module_2, ……,module_n → To import more than module
Example
import math

Note: After importing the module to access one of the functions one has to
specify the name of the module and name of the function separated by dot. This
is also called dot notation

Syntax:
module.function
Example:
math.sin(value )
Program
• To import selected objects from a module:

Syntax:
1. from module import object → To import single object
2. form module import object_1, object_2,…, object_n → To import more than
one object

3. from module import * → To import all the objects


Example:
from math import sin
or
from math import *

Note: After importing the module to access one of the functions one can
directly write the function name without writing the module name before it

Program
Sieve of Eratosthenes
Sieve of Eratosthenes is an algorithm to find all the prime numbers in a given
range i.e., 2 to n.
Process:
Step 1: We first write down the all the numbers between 2 and n
Step 2: After that we mark all the multiple of 2(the smallest prime number) as
composite number. The multiple of a number n is the number greater than n
and is divisible by n. Then we find the next number that has been not marked as
composite in our case 3 (the next prime number) and mark all multiples of 3.
And this process is repeated until we have processed all the numbers in a row
and the numbers left out are the prime numbers between that range.
Example: To find all the prime numbers between 2 to 50
Step 1: List all the numbers between 2 to 31
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31

Step 2: Begin with 2 and mark all the multiples of 2


2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31

Step 3: look out for the next unmarked number 3 in our case and marked all
the multiples of 3
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31

Step 4: Again, look for the next unmarked number 5 in our case and mark all
the multiples of 5.
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31
In the same way we look for another unmarked number and mark all the
multiples of that number and repeat this process until we have processed all the
numbers. At the end we will get list of unmarked numbers which are prime.
The list of prime numbers obtained between 2 to 31: 2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31.
Tower of Hanoi
It is a mathematical problem where we have three towers and n disk. The puzzle
is to move entire stack of rings from pole A to pole C. Keeping in mind the
following rule:
• Only one disk can be moved at a time
• Disk can only be moved if it is the uppermost disk on the stack
• No disk can be place on the top of smaller disk
Solution:
Base case: when n = 1
Move the ring from A to C using B as a spare
Recursive Case:
Move n-1 rings from A to B using C as spare
Move the one ring left on A to C using B as spare
Move n-1 rings from B to C using A as spare

Illustration

Considering 3 disk: Disk 1, Disk 2 and Disk 3


In terms of length the order of the disk is: Disk3 > Disk 2 > Disk 1
Initial setting:
Disk 1
Disk 2
Disk 3
Tower A Tower B Tower C

Step 1: Move Disk 1 from Tower A to Tower C

Disk 2
Disk 3 Disk 1
Tower A Tower B Tower C
Step 2: Move Disk 2 from Tower A to Tower B

Disk 3 Disk 2 Disk 1


Tower A Tower B Tower C

Step 3: Move Disk 1 from Tower C to Tower B

Disk 1
Disk 3 Disk 2
Tower A Tower B Tower C

Step 4: Move Disk 3 from Tower A to Tower C

Disk 1
Disk 2 Disk 3
Tower A Tower B Tower C

Step 5: Move Disk 1 from Tower B to Tower A

Disk 1 Disk 2 Disk 3


Tower A Tower B Tower C

Step 6: Move Disk 2 from Tower B to Tower C

Disk 2
Disk 1 Disk 3
Tower A Tower B Tower C
Step 7: Move Disk 1 from Tower A to Tower C
Disk 1
Disk 2
Disk 3
Tower A Tower B Tower C

Code:

Merge Sort
Merge sort is one of the sorting algorithms which is based on divide and
conquer, Where the problem is divided into multiple sub problem and these are
solved recursively and finally the solution of the sub-problems is combined to
get the final solution.
In merge sort the list is divided into two halves and these sub lists are divided
again and again into halves until only single element is left in each. After that we
combine the pair of one element list to the two sorting them in the process. The
sorted two element list is combined into four element list and this process keeps
on repeating till we get the sorted list. We use the merge() function for this
process.
Example:
5 4 2 1

5 4 2 1

5 4 2 1

4 5 1 2

1 2 4 5

Code:

You might also like