Unit 5 Notes Python
Unit 5 Notes Python
Python Programming
Unit-5(Iterators and Recursion)
Full Explana on
Explana on:
Iterator –
1. An iterator is an object that contains a countable number of values.
2. An iterator is an object that can be iterated upon, meaning that we can traverse
through all the values.
3. Python iterator, implicitly implemented in constructs like for-loops.
4. Python lists, tuples, dic onary and sets are all examples of in-built iterators.
5. Iterators are implemented using following methods –
a. __iter__: This method is called on ini aliza on of an iterator. This should return an
object that has a next () method.
b. next () (or __next__): The iterator next method should return the next value for the
iterable.
Recursion –
1. In Python, recursion occurs when a func on is defined by itself.
2. When a func on calls itself, directly or indirectly, then it is called a recursive func on
and this phenomenon is known as recursion.
3. Recursion is the process of repea ng something self-similar way.
Topic-2: Fibonacci series.
Explana on:
1. Fibonacci series is a series of numbers formed by the addi on of the preceding
two numbers in the series.
2. It is simply the series of numbers which starts from 0 and 1.
3. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…. and so on.
Python code –
Explana on:
1. Tower of Hanoi is a mathema cal puzzle which consists of three rods and a number of
disks of different sizes, which can slide onto any rod.
2. The puzzle starts with the disks in a neat stack in ascending order of size on one rod,
the smallest at the top, thus making a conical shape.
Explana on:
Topic-5: Searching.
Explana on: Searching refers to determining that a par cular value is present in the given
data structure or not. There are two basic methods to perform searching opera on –
1. Linear Search
2. Binary Search
Linear Search –
1. Linear search is a method for finding a par cular value in a list.
2. Linear (Simple) search is one of the simplest searching algorithms, and the easiest to
understand.
3. This can be used for both sorted and unsorted list.
4. Here we perform searching by one-by-one traversing all the elements of the list from
the start ll the end.
Suppose there is a list named Fruits-
Apple Mango Banana Avocado Papaya
5. Time complexity of Linear Search, Time Complexity basically refers to the me taken
by an algorithm to produce results. There are three cases to analyse the me
complexity of any algorithm –
a. Worst case – When the element which is to be searched is at last posi on- O(n)
b. Average case – When the element which is to be searched is between first and last
posi on. – O(n)
c. Best case – When the element which is to be searched is at first posi on. – O(1)
Binary Search –
1. Binary search follows a divide and conquer approach.
2. It is faster than linear search but requires that the array be sorted before the algorithm
is executed.
3. Binary search looks for a par cular item by comparing the middle most item of the
collec on. If a match occurs, then the index of item is returned.
4. If the middle item is greater than the item, then the item is searched in the sub-array
to the le of the middle item.
5. Otherwise, the item is searched for in the sub-array to the right of the middle item.
6. This process con nues on the
sub-array as well un l the size
of the sub-array reduces to
zero.
7. Time Complexity of Binary Search can be analysed in following ways –
a. Best case – When element which is to be searched present at middle of list- O(1)
b. Worst case – When element which is to be searched do not present in the list- O(n)
c. Average case – When element which is to be searched present in List but not at
middle- O(logn)
Explana on:
Selec on sort –
Merge sort –