• Data Structure can be de ned as the group of data elements which provides an e cient way of storing and organising data in the computer so that it can be used e ciently. • Some examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Introduction to Data Structure • A data structure is a group of data elements that provides the easiest way to store and perform di erent actions on the data of the computer. • Way of organizing data • To reduce the space and time complexities of di erent tasks. • A data structure has also de ned an instance of ADT. Data Structure Classi cation Linear and Nonlinear • Linear structures arrange data in a linear sequence, such as found in an array, list, or queue. • In nonlinear structures, the data doesn’t form a sequence but instead connects to two or more information items, like in a tree or graph. Static and Dynamic • Static structures consist of xed, permanent structures and sizes at compile time. • The array reserves a set amount of reserve memory set up by the programmer ahead of time. • Dynamic structures feature non- xed memory capacities, shrinking or expanding as required by the program and its execution requirements. Homogenous and Non-Homogenous • Homogenous data structures consist of the same data element type, like element collections found in an array. • In non-homogenous structures, the data don’t have to be the same type, such as structures. Di erence Between Data and Information Abstract data type • ADT is de ned by a set of values and set of operation • ADT de nes the logical form of the data type • The process of providing only the essentials and hiding the details is known as abstraction. • ADT is made of primitive data types, but operation logics are hidden • Abstract data types: • List • Stack • Queue • Stack − • isFull(), This is used to check whether stack is full or not • isEmpty(), This is used to check whether stack is empty or not • push(x), This is used to push x into the stack • pop(), This is used to delete one element from top of the stack • peek(), This is used to get the top most element of the stack • size(), this function is used to get number of elements present into the stack • Queue − • isFull(), This is used to check whether queue is full or not • isEmpty(), This is used to check whether queue is empty or not • insert(x), This is used to add x into the queue at the rear end • delete(), This is used to delete one element from the front end of the queue • size(), this function is used to get number of elements present into the queue • List − • size(), this function is used to get number of elements present into the list • insert(x), this function is used to insert one element into the list • remove(x), this function is used to remove given element from the list • get(i), this function is used to get element at position i • replace(x, y), this function is used to replace x with y value ADTS and Classes Constructor A constructor is a special type of method (function) which is used to initialize the instance members of the class. Constructors can be of two types. • Parameterized Constructor • Non-parameterized Constructor Creating the constructor: The method __init__() simulates the constructor of the class. This method is called when the class is instantiated. It accepts the self-keyword as a rst argument which allows accessing the attributes or method of the class It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor. Example Non-Parameterized Constructor • The non-parameterized constructor uses to manipulate the value or the constructor that has only self as an argument Parameterized Constructor • The parameterized constructor has multiple parameters along with the self. Default Constructor • Do not include the constructor in the class or forget to declare it, then that becomes the default constructor. • It does not perform any task but initializes the objects. More than One Constructor in Single class Destructors • The users call Destructor for destroying the object. • Python has a garbage collector whose function is handling memory management automatically. • The __del__() function is used as the destructor function • The user can call the __del__() function when all the references of the object have been deleted, and it becomes garbage collected. Syntax: Example INTRODUCTION TO OOPS • Python is a multi-paradigm programming language. It supports di erent programming approaches. • One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP). • Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects. Oops concepts 1. Class 2. Object 3. Method 4. Inheritance 5. Encapsulation 6. Polymorphism 7. Data Abstraction Class • The class can be de ned as a collection of objects. It is a logical entity that has some speci c attributes and methods. • A class is a blueprint that de nes the variables and the methods (Characteristics) common to all objects of a certain kind. • Every class can have multiple instances • The class de nition typically speci es instance variables, also known as data members, that the object contains, as well as the methods, also known as member functions, that the object can execute Object ● An object (instance) is an instantiation of a class. When class is de ned, only the description for the object is de ned. Methods Inheritance • Inheritance is a way of creating a new class for using details of an existing class without modifying it. • It refers to de ning a new class with less or no modi cation to an existing class. • A class can inherit attributes and behaviour methods from another class called subclass • It speci es that the child object acquires all the properties and behaviors of the parent object. • It provides the re-usability of the code. Types of Inheritance • Single inheritance • Multilevel inheritance • Hierarchical inheritance • Multiple inheritance • Hybrid inheritance Single inheritance Multilevel inheritance Hierarchical inheritance Multiple inheritance Hybrid inheritance Encapsulation • Bundling data and methods within a single unit • A class is an example of encapsulation as it binds all the data members (instance variables) and methods into a single unit. Access Modi ers • Public Member: Accessible anywhere from otside oclass. • Private Member: Accessible within the class • Protected Member: Accessible within the class and its sub- classes Public Member Private Member Public method to access private members Name Mangling Protected Member Polymorphism • Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types). • Implementing same method in di erent context • To de ne methods in the child class with the same name as de ned in their parent class. • Methods with same name implementing in di erent way Overloading Overriding Operator overloading • Operator Overloading means giving extended meaning beyond their prede ned operational meaning. • The same built-in operator or function shows di erent behaviour for objects of di erent classes, this is called Operator Overloading • Implementing operator in di erent ways • Example: string concatenation and addition operator Method overloading • Implementing methods in di erent ways • Method name should be same • Arguments must be same Overriding: • Method name should be same • Number of arguments should be same Example for method overloading Example for method overriding Data abstraction Abstraction is used to hide internal details and show only functionalities. Abstract base class: •A class is called an Abstract class if it contains one or more abstract methods. •An abstract method is a method that is declared, but contains no implementation. •Abstract classes may not be instantiated, and its abstract methods must be implemented by its subclasses. •The 'abc' module in Python library provides the infrastructure for de ning custom abstract base classes. •'abc' works by marking methods of the base class as abstract. •A concrete class which is a sub class of such abstract base class then implements the abstract base by overriding its abstract methods. •The abc module de nes ABCMeta class which is a metaclass for de ning abstract base class. Namespaces • A namespace is a collection of names and the details of the objects referenced by the names. • Used for organizing the names assigned to the objects • Four types of namespaces, Built-in namespaces Global namespaces Local namespaces Enclosing namespaces. Built-in namespace • A built-in namespace contains the names of built-in functions and objects. • It contains the names of built-in data types,exceptions and functions like print() and input(). Global namespaces • Global namespaces are de ned at the program or module level. • It contains the names of objects de ned in a module or the main program. • A global namespace is created when the program starts and exists until the program is terminated by the python interpreter. Local namespace • A local namespace is de ned for a class, a function, a loop, or any block of code. • The names de ned in a block of code or a function are local to it. • The variable names cannot be accessed outside the block of code or the function in which they are de ned. • The local namespace is created when the block of code or the function starts executing and terminates when the function or the block of code terminates. Enclosing namespace • A function or a block of code de ned inside any function can access the namespace of the outer function or block of code. • The outer namespace is termed as enclosing namespace for the namespace of the inner function or block of code. Shallow copy • Constructing a new collection object and then populating it with references to the child objects found in the original. • The copying process does not recurse and therefore won’t create copies of the child objects themselves. • A reference of object is copied in other object. • Any changes made to a copy of object do re ect in the original object. • This is implemented using “copy()” function Deep copy • Deep copy is a process in which the copying process occurs recursively. • Constructing a new collection object and then recursively populating it with copies of the child objects found in the original. • A copy of object is copied in other object. • Any changes made to a copy of object do not re ect in the original object. • This is implemented using “deepcopy()” function