T. M. Palayam, Coimbatore-641 105
(Approved by AICTE, New Delhi and Affiliated to Anna University, Chennai)
Accredited by NAAC “A+”, Recognized by UGC under Section 2(f) and 12(B)
NBA ACCRDIATED COURSES: AERO&CSE
UNIT – I
PYTHON PREVIEW
Building data structures and algorithms requires that we communicate detailed instructions to
a computer. An excellent way to perform such communications is using a high-level
computer language, such as Python.
The Python Interpreter
▪ Python is formally an interpreted language. Commands are executed through a piece of
software known as the Python interpreter. The interpreter receives a command, evaluates
that command, and reports the result of the command. While the interpreter can be used
interactively (especially when debugging), a programmer typically defines a series of
commands in advance and saves those commands in a plain text file known as source code
or a script. (e.g., demo.py).
EXAMPLE:
a=10
b=20
sum= a+b
Print(sum)
OUTPUT:
sum=30
EXAMPLE:
A Python program that computes a grade-point average (GPA).
print( Welcome to the GPA calculator. )
print(“Please enter all your letter grades, one per line.”)
print(“Enter a blank line to designate the end. “)
# map from letter grade to point value
,points = { A+ :4.0, A :4.0, A- :3.67, B+ :3.33, B :3.0, B- :2.67,
C+ :2.33, C :2.0, C :1.67, D+ :1.33, D :1.0, F :0.0}
num courses = 0
total points = 0
done = False
while not done:
grade = input( ) # read line from user
if grade ==“ “: # empty line was entered
done = True
elif grade not in points: # unrecognized grade entered
print("Unknown grade ’{0}’ being ignored”. Format (grade))
else:
num courses += 1
total points += points[grade]
if num courses > 0: # avoid division by zero
print(“Your GPA is {0:.3}” .format(total points / num courses))
ADTs and Classes
Abstract Data Type (ADT)
• An abstract data type (ADT) is a programmer defined data type that specifies a set of
data values and a collection of well-defined operations that can be performed on those
values. Abstract Data Type (ADT) are defined independent of their implementation,
allowing us to focus on the use of the new data type instead of how it is
implemented.
▪ By hiding the implementation details, we can work with an abstraction and focus on what
functionality the Abstract Data Type (ADT) provides instead of how that functionality is
implemented.
▪ The set of operations can be grouped into four categories:
1. Constructors: create and initialize new instances of the ADT.
2. Assessors: return data contained in an instance without modifying it.
3. Mutators: modify the contents of an Abstract Data Type (ADT) instance.
, 4. Iterators: process individual data components sequentially
Stack ADT
A stack is a LIFO (last in, first out) list with the following operations: Push,
Pop, Create (Init), Top, IsEmpty, Size. (Such a list is called a Signature or the Interface to the
ADT.)
1. Stack() creates a new stack that is empty. It needs no parameters and returns an empty stack.
2. push(item) adds a new item to the top of the stack. It needs the item and returns
nothing.
3. pop() removes the top item from the stack. It needs no parameters and returns the item.
The stack is modified.
4. top() returns the top item from the stack but does not remove it. It needs no
parameters. The stack is not modified.
5. isEmpty() tests to see whether the stack is empty. It needs no parameters and
returns a boolean value.
6. size() returns the number of items on the stack. It needs no parameters and returns an
integer.
EXAMPLE:
class Stack: def
init (self):
self.items = []
def IsEmpty(self):
return self.items == []def
push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
QUEUE ADT:
A Queue is a FIFO (first in, first out) list with the following operations:Enqueue, Dequeue, Size,
Font.
1. Queue() creates a new queue that is empty. It needs no parameters and returns an empty
queue.
2. Enqueue(item) adds a new item to the rear of the queue. It needs the item and returns
nothing.
3. Dequeue() removes the item from the front of the queue. It needs no
parameters and returns the item. The queue is modified.