Week1
Week1
INTRODUCTION
Introduction
A data structure is basically a group of data elements that are put together under one name,
and which defines a particular way of storing and organizing data in a computer so that it can
be used efficiently.
Operations
The different operations that can be performed on the various data structures are:
• Traversing It means to access each data item exactly once so that it can be
processed. For example, to print the names of all the students in a class.
• Searching It is used to find the location of one or more data items that satisfy the given
constraint. Such a data item may or may not be present in the given collection of data
• Deleting It means to remove (delete) a particular data item from the given collection of
data items. For example, to delete the name of a student who has left the course.
• Sorting Data items can be arranged in some order like ascending order or descending
order depending on the type of application. For example, arranging the names of
students in a class in an alphabetical order, or calculating the top three winners by
arranging the participants’ scores in descending order and then extracting the top three.
• Merging Lists of two sorted data items can be combined to form a single list of sorted
data items.
• Time Complexity − Running time or the execution time of operations of data structure
must be as small as possible.
• Primitive data structures are the fundamental data types which are supported by a
programming language. Some basic data types supported by python are integer, string,
real, and boolean.
• Non-primitive data structures are those data structures which are created using
primitive data structures. Examples of such data structures include list,
arrays,tuple,set,file, dictionary.
• List data structures can further be classified into two categories: linear and non-linear
data structures.
• If the elements of a data structure are stored in a linear or sequential order, then it
is a linear data structure. Examples include stacks, and queues.
•
• If the elements of a data structure are not stored in a sequential order, then it is a
non-linear data structure. The relationship of adjacency is not maintained between
elements of a non-linear data structure. Examples include trees and graphs.
Integers
We can utilize the integer data type to represent the numeric data. for example, 52, 23, 0, or -8
String
Boolean
The Boolean is a built-in data type used to return the values: True and False, which can often
be interchangeable with the integers, 0 or 1. These are pretty useful in comparison and
conditional expressions.
Float
The Float is also a built-in data type that stands for 'floating point number'. These can be used
for representing rational numbers that usually ends with a decimal figure, for example, 3.14,
2.05 or 12.34
Non-Primitive data Structures act as the complex components of the data structures family.
Instead of storing a value, these data structures have a collection of values in different formats.
Non-primitive data structures are further classified into multiple categories:
• Arrays
• Lists
• Files
Array
Array is a collection of elements with similar data type. It holds all elements in a sequential
order.
Lists are the data structures used to store a collection of heterogeneous items in Python. Lists
are mutable, which indicates that their content can be changed by modifying their identity.
The lists can be represented by the square brackets: [ ], which helps hold the elements,
divided by a comma ‘,’.
List data structure can be further classified into two sub-categories: Linear Data Structures
and Non-Linear Data Structures. The Linear data structures consist of Stacks and Queues,
whereas the Non-Linear data structures consist of Graphs and Trees.
Stacks
A container of objects where objects are removed and inserted according to the LIFO (Last-In-
First-Out) principle is known as Stack.
Let’s take an example where there is a stack of plates at a dinner party. These plates are
always removed from or added to the top of the pile. The same concept is opted in computer
science to evaluate expressions and parse syntax, scheduling algorithms or routines and many
more.
Queue
A container of objects where objects are removed and inserted according to the FIFO (First-In-
First-Out) concept is known as Queue.
Graphs
In Mathematics and Computer Science, the networks consist of vertices (also called nodes) is
known as a graph. These nodes may or may not be connected. The path or the line that helps
in connecting two nodes is known as an edge. The graph is said to be directed if the edge has
a particular flow direction, where the direction edge is known as an arc. At the same time, the
graph is said to be undirected if no directions are specified.
Various sectors depend on the graph and its theory principles such as social networks, maps,
molecular studies in biology and chemistry, recommended system and many more.
Tuples
Tuples are one of the standard sequence data structures. However, tuples differ from lists as
tuples are immutable, which implies that they cannot be deleted, added or edited once they are
defined.
Dictionary
Dictionaries are comprised of key-value pairs. The key is used to identify the item, whereas the
value is holding the item's value. Thus, the telephone directory has a key (contact name) and
the value (contact number) assigned to that key.
Sets
The Set data structure is used to represent a collection of diverse (unique) objects. The Sets
play a significant role in creating lists holding unique values only in the datasets. It is an
unordered collection but a mutable one.
Files
Some of the fundamental methods and functions that allows one user to interact with files
using Python are shown below:
Abstractions
An abstraction is a mechanism for separating the properties of an object and restricting the
focus only on relevant data. There are two types of abstraction procedural abstraction and
Data abstraction.
Procedural abstraction is the use of a function or method knowing what it does but ignoring
how it is accomplished.
Data Abstraction is the separation of the properties of a data type (its values and operations)
from the implementation of that data type.
Likewise in Object-oriented programming, abstraction is a process of hiding the
implementation details from the user, only the functionality will be provided to the user.
A date represents a single day in the Gregorian calendar in which the first day starts on
November 24, 4713 BC.
1. Date( month, day, year ): Creates a new Date instance initialized to the given Gregorian
date which must be valid. Year 1 BC and earlier are indicated by negative year components.
2. day(): Returns the Gregorian day number of this date.
3. month(): Returns the Gregorian month number of this date.
4. year(): Returns the Gregorian year of this date.
5. monthName(): Returns the Gregorian month name of this date.
6. isLeapYear(): Determines if this date falls in a leap year and returns the appropriate boolean
value.
STACK ADT
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()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
def display(self):
s=stack()
print(s.isEmpty())
print("push operations")
s.push(11)
s.push(12)
s.push(13)
print("size:",s.size())
print(s.display())
print("peek",s.peek())
print("pop operations")
print(s.pop())
print(s.pop())
print(s.display())
print("size:",s.size())