Data Structures Using Python
What Are Data Structures?
• A data structure is a storage that is used to store and organize
data. It is a way of arranging data on a computer so that it can be
accessed and updated efficiently.
• A data structure is not only used for organizing the data. It is also
used for processing, retrieving, and storing data. There are
different basic and advanced types of data structures that are
used in almost every program or software system that has been
developed.
1.Linear Data Structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements, is called a linear data structure.
Example: Array, Stack, Queue, Linked List, etc.
2.Static Data Structure: Static data structure has a fixed memory size. It is easier
to access the elements in a static data structure.
Example: array.
3.Dynamic Data Structure: In dynamic data structure, the size is not fixed. It can
be randomly updated during the runtime which may be considered efficient
concerning the memory (space) complexity of the code.
Example: Queue, Stack, etc.
4.Non-Linear Data Structure: Data structures where data elements are not
placed sequentially or linearly are called non-linear data structures. In a non-
linear data structure, we can’t traverse all the elements in a single run only.
Examples: Trees and Graphs.
Why Use Python for Data Structures?
➢ Built-in powerful data types
➢Simple syntax
➢ No need for manual memory management
➢Rich libraries for advanced structures
Python vs C – Data Structures
Feature C Language Python
➢ Memory Manual Automatic
➢ Syntax Verbose Clean & Readable
➢ Built-in Structs Arrays Only List, Dict, Set, Tuple
➢ Pointers Required Not Needed
➢ Performance Fast Slightly Slower
Feature Structured (SOP) Object-Oriented (OOP)
Approach Top-down Bottom-up
Main Building Block Functions Objects
Data encapsulated in
Data Handling Global data shared
objects
Reusability Limited High (via inheritance)
Abstraction Not supported Supported
Security Low High (data hiding)
Examples C Python, Java, C++
Classes and Objects in Python
What is Object-Oriented Programming (OOP)?
• OOP is a programming style that uses "objects" to represent real-world things.
• Each object can have:
o Properties (data) → e.g., name, petals, price
o Actions (methods) → e.g., display the flower's details
Key Concepts in the Program
1. Class
A class is like a blueprint or template.
It defines what data and functions (called methods) an object will have.
Example in the program:
class Flower:
This defines a class named Flower.
2. Object
An object is a real example created from the class.It holds actual data.
Example:
a = Flower("lotus", 20, 98.00)
This creates an object a of class Flower with:
name = "lotus"
petals = 20
price = 98.00
Constructor (__init__ method)
The __init__ method is a special function that runs automatically when you create an
object.
It is used to initialize (set) values.
Example:
def __init__(self, name, petals, price):
self.name = name
self.petals = petals
self.price = price
self refers to the current object being created.
4. Instance Variables
These are variables inside the object.
Example: self.name, self.petals, self.price are instance variables.
They hold unique values for each object.
5. Method (Function inside a class)
A method is a function that belongs to a class.
It can access and use the data of the object.
Example:
def my_function(self)
print(f"Name of the flower : {self.name}")
print(f"Number of petals : {self.petals}")
print(f"Price of the flower: {self.price:.2f}")
This function prints the flower's details.
6. Calling the Method
After creating the object, we call the method to perform an action:
•a.my_function()
Final Output
This will print:
Name of the flower : lotus
Number of petals : 20
Price of the flower: 98.00
class Flower:
This line defines a class named Flower.
A class is like a blueprint for creating objects (similar to a struct in C, but with both data and
functions).
This is the constructor method in Python.
It is automatically called when you create a new object of the class.
•self refers to the current object (like this in C++).
•name, petals, and price are input values (parameters) given while creating a flower object.
Stores the value of name (passed during object creation) in the object's variable
self.name.
Stores the number of petals in self.petals.
Defines a function (called my_function) inside the class.
This function will display the details of the flower object.
Prints the name of the flower using formatted string (f-string).
{self.name} is replaced by the actual name value.
Creates an object a of the class Flower.
It passes "lotus" as name, 20 as petals, and 98.00 as price.
This calls the __init__ constructor
Class Definition:
• class Flower: — This defines a class called Flower.
• Inside the class:
o __init__ is a constructor method that initializes each new object with a
name, number of petals, and price.
o self.name, self.petals, and self.price are instance variables used to
store the respective values.
Method:
• my_function(self) is a method used to print the attributes of the flower object.
Object Creation & Method Call:
python
a = Flower("lotus", 20, 98.00)
a.my_function()
• a = Flower("lotus", 20, 98.00) creates an object a of the class Flower and
assigns:
o name = "lotus"
o petals = 20
o price = 98.00
• a.my_function() calls the method to print the flower’s details.
Name of the flower : lotus
Number of petals : 20
Price of the flower: 98.00
Concept Description
Class Flower
Constructor __init__ method
Object Created a = Flower("lotus", 20, 98.00)
Method Used my_function()
Purpose Print details about the flower