0% found this document useful (0 votes)
42 views12 pages

Introduction to Data Structures in Python

This document provides an introduction to data structures, including their operations, characteristics, and classifications into primitive and non-primitive types, with Python examples. It discusses linear and non-linear data structures, as well as abstract data types (ADTs) and their implementation. Key concepts such as stacks, queues, graphs, and trees are explained, along with their respective functionalities and use cases.

Uploaded by

amanthpanchal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views12 pages

Introduction to Data Structures in Python

This document provides an introduction to data structures, including their operations, characteristics, and classifications into primitive and non-primitive types, with Python examples. It discusses linear and non-linear data structures, as well as abstract data types (ADTs) and their implementation. Key concepts such as stacks, queues, graphs, and trees are explained, along with their respective functionalities and use cases.

Uploaded by

amanthpanchal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 1

INTRODUCTION

● Introduction to Data Structures


● Operations
● Characteristics.
● Classification
● Primitive types – primitive data structures, python examples.
● Non primitive types – Non primitive data structures, python examples.
● Linear and nonlinear data structures – with python examples.
● Abstractions
● Abstract Data Types
● An Example of Abstract Data Type (Student, Date, Employee)
● Defining the ADT, Using the ADT, Implementing the ADT.

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.

Data structures are widely applied in the following areas:


• Compiler design
• Statistical analysis package
• Numerical analysis
• Artificial intelligence
• Operating system
• DBMS
• Simulation
• Graphics

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

GOVERNMENT POLYTECHNIC, KARWAR 1


items. For example, to find the names of all the students who secured 100 marks in
mathematics.
• Inserting It is used to add new data items to the given list of data items. For example,
to add the details of a new student who has recently joined the course.

• 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.

Characteristics of a Data Structure

• Correctness − Data structure implementation should implement its interface correctly.

• Time Complexity − Running time or the execution time of operations of data structure
must be as small as possible.

• Space Complexity − Memory usage of a data structure operation should be as little as


possible.

Classification of Data Structures

Fig.1.1 Classification of Data structure in Python

GOVERNMENT POLYTECHNIC, KARWAR 2


Data structures are generally categorized into two classes: primitive and non-primitive data
structures.

Primitive and Non-primitive Data Structures

• 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.

• Linear and Non-linear 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.

The Primitive Data Structures


Four primitive variable types are defined in Python, and these are as follows:
1. Integers
2. Strings
3. Boolean
4. Float

Integers

We can utilize the integer data type to represent the numeric data. for example, 52, 23, 0, or -8

String

GOVERNMENT POLYTECHNIC, KARWAR 3


Strings are collections of alphabets, words or many other characters. We can create the string
data type in Python by including an order of characters within a pair of single or double-quotes.

For example: 'tutorial', "example", etc.

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

The Non-Primitive Data Structures

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.

GOVERNMENT POLYTECHNIC, KARWAR 4


Lists

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.

GOVERNMENT POLYTECHNIC, KARWAR 5


Let’s take an example of a line at a ticket counter for a ride in an amusement park. The people
are treated according to their arrival sequence. And hence the individual who reaches first is
also the first to leave.

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.

It is represented as G={V,E} Where V-> vertices and E -> Edges

GOVERNMENT POLYTECHNIC, KARWAR 6


Tree:
• Tree is a connected acyclic graph.
• Tree is a non-linear structure.
• Tree is a collection of nodes linked together to simulate hierarchyTrees

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

GOVERNMENT POLYTECHNIC, KARWAR 7


Files are a part of traditional data structures in Python. In the Data Science industry, where big
data appears to be usual, a programming language without the ability to store and recover
formerly stored data or information would barely be convenient.

Some of the fundamental methods and functions that allows one user to interact with files
using Python are shown below:

•The read() method is used to read entire files;


•The open() method is used to open files in the system where filename is the name of
the file to be opened;
•The write() method is used to write a string to a file and also returns the number of
characters written;
•The readline() method is used to read one line at a time; and
•The close() method is used to close the opened file.

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.

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.

GOVERNMENT POLYTECHNIC, KARWAR 8


The Date Abstract Data Type (ADT):

Defining Date ADT

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.

Implementing Date ADT


class date:
def __init__(self,a,b,c):
self.d=a

GOVERNMENT POLYTECHNIC, KARWAR 9


self.m=b
self.y=c
def day(self):
print("Day = ", self.d)
def month(self):
print("Month = ", self.m)
def year(self):
print("year = ", self.y)
def monthName(self):
months = ["Unknown","January","Febuary","March","April","May","June","July",
"August","September","October","November","December"]
print("Month Name:",months[self.m])
def isLeapYear(self):
if (self.y % 400 == 0) and (self.y % 100 == 0):
print("It is a Leap year")
elif (self.y % 4 == 0) and (self.y % 100 != 0):
print("It is a Leap year")
else:
print("It is not a Leap year")
d1 = date(3,8,2000)
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

Stack Abstract Data Type:

GOVERNMENT POLYTECHNIC, KARWAR 10


Defining Stack ADT
A stack is structured as an ordered collection of items where items are added to and removed
from the end called the “top”.
Stacks are ordered LIFO.
Stack(): creates a new stack that is empty. It needs no parameters and returns an empty stack.
push(item): adds a new item to the top of the stack. It needs the item and returns nothing.
pop(): removes the top item from the stack. It needs no parameters and returns the item. The
stack is modified.
peek(): returns the top item from the stack but does not remove it. It needs no parameters. The
stack is not modified.
isEmpty(): tests to see whether the stack is empty. It needs no parameters and returns a
boolean value.

STACK ADT
class stack:
def __init__(self):
[Link] = []
def isEmpty(self):
return [Link] == []
def push(self, item):
[Link](item)
def pop(self):
return [Link]()
def peek(self):
return [Link][len([Link]) - 1]
def size(self):
return len([Link])
def display(self):

GOVERNMENT POLYTECHNIC, KARWAR 11


return ([Link])

s=stack()
print([Link]())
print("push operations")
[Link](11)
[Link](12)
[Link](13)
print("size:",[Link]())
print([Link]())
print("peek",[Link]())
print("pop operations")
print([Link]())
print([Link]())
print([Link]())
print("size:",[Link]())

GOVERNMENT POLYTECHNIC, KARWAR 12

Common questions

Powered by AI

In Python, stacks and queues are implemented using lists, which provide the necessary operations to adhere to LIFO and FIFO principles. For stacks, operations like 'push' append items to the list, and 'pop' functions remove the last item added, ensuring efficient management and retrieval according to LIFO . Since lists in Python are dynamic, they automatically handle memory reallocation, making stack operations efficient both in time and in terms of space management . Queues, although naturally supported by lists, could be more efficiently managed by using collections like 'deque' for appending and popping left, adhering to FIFO, which optimizes both time complexity and memory usage, as list pop from the beginning can be inefficient . These implementations allow Python to manage data effectively by leveraging built-in capabilities in its list and collections for various stack and queue operations .

Primitive data structures are the basic types of data built into a programming language, such as integers, strings, booleans, and floats . They are generally simpler, more efficient in terms of memory usage, and less versatile than non-primitive data structures. Non-primitive data structures, on the other hand, are more complex structures that are created using primitive data structures. They include lists, arrays, tuples, sets, and dictionaries . These structures enable the storage of multiple values and the organization of data in a manner suitable for various processing needs. Non-primitive data structures offer more functionality and flexibility but can introduce overhead due to their more complex nature . For example, lists are mutable whereas tuples are immutable, affecting how data can be manipulated .

Non-linear data structures like trees and graphs differ significantly from linear data structures such as stacks and queues in both structure and application. Linear structures are sequential; elements are stored in a sequence, and operations track this order. For example, stacks operate on LIFO, and queues on FIFO principles . Non-linear structures like trees, which are hierarchical and acyclic, allow for multiple paths originating from the same node and are used in applications like file systems and hierarchical data representation . Graphs, comprising nodes and edges, further enable modeling of networked data with complex interconnections, making them ideal for social network analysis and shortest path algorithms . While linear structures are more straightforward and efficient for ordered data processing, non-linear structures provide flexibility and efficiency in representing and querying complex, interconnected data sets .

The 'isLeapYear' function in the Date Abstract Data Type (ADT) is significant because it accurately determines whether a given year is a leap year, which is crucial for handling date-related computations in software applications . The function implements logic to check for leap year conditions according to the Gregorian calendar rules: years divisible by 4 are leap years, except the years divisible by 100 are not, unless they are also divisible by 400 . Handling leap years correctly is vital for ensuring the accuracy of date calculations, impacts scheduling systems, calendar applications, and any software that must manage billing cycles or seasons . By abstracting this complexity within the function, developers can rely on the Date ADT for consistent and correct date management without delving into the intricacies of calendar systems .

The LIFO (Last-In-First-Out) principle applied in stacks means that the most recently added item is the first to be removed. This principle is useful in situations where the order of processing requires reversing compared to the order of addition, such as in function call management or expression evaluations . On the other hand, the FIFO (First-In-First-Out) principle of queues ensures that the first item added is the first to be removed, which is ideal for managing resources in a fair manner, akin to real-world lines or queues, such as scheduling processes in an operating system or handling tasks in a printer spooler . These principles significantly affect how data is accessed and manipulated, optimizing various computational processes as per the need of the application .

In Python, lists are mutable, allowing changes to their size and content, which makes them suitable for collections of data that need to be modified over time . This mutability facilitates operations like adding, removing, or altering elements, making lists ideal for dynamic data storage. Tuples, in contrast, are immutable; once defined, they cannot be altered, ensuring that data remains consistent and unchanged, which is beneficial for fixed data sets or when thread safety is a concern . Sets are also mutable, although they only hold unique elements, making them ideal for operations involving de-duplication or membership tests . The handling of mutability thus affects the choice of data structure based on the needs of data consistency, performance overhead from resizing, and data validation requirements, which in turn influences how data structures are used in code design and management .

Abstraction in data structures allows developers to focus on the operations and functionality of a data type without needing to concern themselves with its implementation details . This separation of concerns simplifies software development, making it easier to update or change implementations without affecting the rest of the system. Abstract Data Types (ADTs) capitalize on this concept by specifying a set of data values and associated operations, independent of their implementation . This focus on the interface allows for more modular and manageable code, as developers can implement changes to the ADT internals without altering how other parts of the program interact with it .

Dictionaries in Python are non-primitive data structures that store data in key-value pairs, enabling efficient retrieval based on unique keys, which makes them particularly suitable for applications requiring fast lookups, like database indexing or configuration settings . Unlike lists and tuples, where data is accessed by numerical indices, dictionaries provide a more intuitive way to manage data with arbitrary key access, accommodating more structured data storage and retrieval . Lists are mutable and used for keeping an ordered collection of items with potential duplicates, facilitating indexing, slicing, and insertion operations . Tuples, on the other hand, are immutable, providing a static structure suited for fixed data that shouldn't change throughout the session or program . The choice between these structures typically depends on the specific needs: dictionaries for associative arrays, lists for ordered collections, and tuples for static, reliable datasets .

Python's file operations such as read, write, and close integrate with data structures to streamline file management in applications by allowing structured data to be efficiently stored and retrieved from files . The 'read()' method enables entire files to be read into data structures like lists, which can then be processed or analyzed in-memory . Conversely, writing structures to files using 'write()' ensures that complex data, such as dictionaries or nested lists, can be serialized for storage or later retrieval . The 'close()' method ensures that file resources are properly released, preventing file locks and ensuring data integrity, which is critical when multiple operations may access file-based data simultaneously . These operations, integrated with Python's robust data structure handling, ensure that file management is both reliable and efficient, supporting diverse application needs from database processing to configuration management .

The efficiency of handling and organizing data in a computer system is profoundly influenced by various operations on data structures. Traversing allows for processing each data item, which is fundamental for accessing dataset contents systematically . Searching for data items that satisfy given conditions is crucial for retrieval efficiency, impacting algorithms in database management and information retrieval systems . Insertion and deletion operations must be handled correctly to maintain data integrity and efficiency, particularly in dynamic datasets where elements are frequently added and removed . Sorting improves data order, facilitating faster search and retrieval operations but can be computationally expensive, especially for large datasets . Finally, merging combines sorted datasets into a single ordered structure, essential for consolidating data across systems but also requiring efficient handling to avoid bottlenecks, particularly in distributed computing environments . Each operation comes with its computational cost in terms of time and space complexity, necessitating careful selection and implementation to optimize system performance .

You might also like