You are on page 1of 22

CMCA501/CSET501: Data

Structures using Python

Dr. Mohit Agarwal


Why Data Structures
Data Structure is a branch of Computer Science.
The study of data structure allows us to understand the
organization of data and the management of the data
flow in order to increase the efficiency of any process or
program.
Data Structure is a particular way of storing and
organizing data in the memory of the computer so that
these data can easily be retrieved and efficiently utilized
in the future when required.
Why Data Structures
The data can be managed in various ways, like the logical or
mathematical model for a specific organization of data is known as a
data structure.

Some examples of Data Structures are


 Arrays
 Linked Lists
 Stack
 Queue
 Trees etc.

Data Structures are widely used in almost every aspect of Computer


Science, i.e., Compiler Design, Operating Systems, Graphics, Artificial
Intelligence, and many more.
First python program
print(‘hello world’)

temp = 40
if(temp < 10):
print(‘Cold weather’)
elif(temp < 25):
print(‘Pleasant weather’)
else:
print(‘Hot weather’)
Execution Cycle of Python Program
Compilation
The program is converted into byte code. Byte code is a
fixed set of instructions that represent arithmetic,
comparison, memory operations, etc. It can run on any
operating system and hardware. The byte code
instructions are created in the .pyc file. The .pyc file is
not explicitly created as Python handles it internally but
it can be viewed with the following command:

>python –m py_compile first.py


Execution Cycle of Python Program
>python –m py_compile first.py

-m and py_compile represent module and module name


respectively. This module is responsible to generate .pyc
file. The compiler creates a directory named
__pycache__ where it stores the first.cpython-38.pyc
file.
Execution Cycle of Python Program
Interpreter
The next step involves converting the byte code (.pyc
file) into machine code. This step is necessary as the
computer can understand only machine code (binary
code). Python Virtual Machine (PVM) first understands
the operating system and processor in the computer and
then converts it into machine code. Further, these
machine code instructions are executed by processor
and the results are displayed.
Execution Cycle of Python Program
Python OOPs
Python is an object oriented programming language.
Almost everything in Python is an object, with its
properties and methods.
A Class is like an object constructor, or a "blueprint" for
creating objects.

class MyClass:
x = 5

 Create an object named p1, and print the value of x:


p1 = MyClass()
print(p1.x)
Python OOPs
__init__() function:

 Create a class named Person, use the __init__() function to


assign values for name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)
Python OOPs
Inheritance:

 Python Inheritance
 Inheritance allows us to define a class that inherits all the
methods and properties from another class.
 Parent class is the class being inherited from, also called base
class.
 Child class is the class that inherits from another class, also
called derived class.
Inheritance
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute


the printname method:

x = Person("John", "Doe")
x.printname()
Inheritance
Child class:
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)

Or

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

x = Student("Mike", "Olsen", 2019)


Multiple Inheritance
# Python program to demonstrate
# super()

class Class1:
def m(self):
print("In Class1")

class Class2(Class1):
def m(self):
print("In Class2")
super().m()

class Class3(Class1):
def m(self):
print("In Class3")
super().m()
Multiple Inheritance
class Class4(Class2, Class3):
def m(self):
print("In Class4")
super().m()

obj = Class4()
obj.m()

Output:

In Class4
In Class2
In Class3
In Class1
Access modifiers
class Super:

# public data member


var1 = None

# protected data member


_var2 = None

# private data member


__var3 = None

# constructor
def __init__(self, var1, var2, var3):
self.var1 = var1
self._var2 = var2
self.__var3 = var3
Friend function
There is no option of friend function in python. you have an option to define a
protected variable by using a single underscore.
Run time polymorphism
Runtime polymorphism is nothing but method overriding. In Python, method overriding occurs
when a derived class provides its own implementation of a method that is already defined in its base
class.

class Animal:
def makeNoise(self):
raise NotImplementedError
class Cat(Animal):
def makeNoise(self):
print("Meoooowwwww")
class Dog(Animal):
def makeNoise(self):
print("Woooooof")
Run time polymorphism
a = Cat();
a.makeNoise() #Prints Meeeowwwwww

a = Dog();
a.makeNoise() #Prints WoooooofPythonCopy
Output:

Meoooowwwww
Woooooof
Function polymorphism
x = "Hello World!"

print(len(x))

mytuple = ("apple", "banana", "cherry")

print(len(mytuple))

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

print(len(thisdict))
Class polymorphism
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Drive!")

class Boat:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Sail!")

class Plane:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car class


boat1 = Boat("Ibiza", "Touring 20") #Create a Boat class
plane1 = Plane("Boeing", "747") #Create a Plane class

for x in (car1, boat1, plane1):


x.move()
Inheritance Class polymorphism
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Move!")

class Car(Vehicle):
pass

class Boat(Vehicle):
def move(self):
print("Sail!")

class Plane(Vehicle):
def move(self):
print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car object


boat1 = Boat("Ibiza", "Touring 20") #Create a Boat object
plane1 = Plane("Boeing", "747") #Create a Plane object

for x in (car1, boat1, plane1):


print(x.brand)
print(x.model)
x.move()

You might also like