SlideShare a Scribd company logo
Classes - Object-Oriented Programming
• Allow to implement abstract data type to provide logical
description of
• What a data object looks like (its state)
• What it can do (its methods)
• To build a class for this, one can take advantage of the
abstraction process while providing necessary details to use
the abstraction in a program
1
Class Definition
• class class_name:
def __init__(self, para_name_1, para_name_2, ...)
statement body
def other_method_name(self, other_para_1, ...)
statement body
• Constructor method is always __init__ function
• self is always 1st parameter, used to refer back to the object itself
• E.g., self.var_name can be used to refer to the object’s internally defined
variable var_name (as part of the object’s state) 2
Fraction Class as an Example
• Recall that a fraction such as
3
5
consists of two parts
• Top value, i.e., numerator, can be any integer
• Bottom value, i.e., denominator, can be any positive integer
• Negative fractions have a negative numerator
• Operations/methods for Fraction type allow a Fraction data
object to behave like any other numeric value
• Such as add, subtract, multiply and divide
• Fraction should be displayed in “slash” form (e.g., 3/5)
• All methods should return results in lowest terms (i.e., most
common forms, e.g., 3/5 rather than 6/10)
3
Implementation of Fraction Class
• Class definition and its constructor
class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom
• Fraction state data, i.e., numerator and denominator are denoted
by internal data objects self.num and self.den, respectively
• __init__ initializes the internal state by assigning values of the top
and bottom parameters to self.num and self.den, respectively
4
Create Instance of Fraction Class
• To create a Fraction instance, we use the name of the class,
which invokes constructor and passes actual values for the
necessary state
• Note that we never directly invoke __init__
• Note that 1st parameter self is never given
actual parameter value upon invocation
• E.g., my_fraction = Fraction(3, 5)
invokes constructor and creates a
Fraction instance
3
5
as show on the right
5
Display Fraction
• print function requires object convert itself into string so as
to be written to output
• To tell Fraction class how to convert itself into string, we
need to override standard method __str__, i.e., to redefine
its behavior:
def __str__(self):
return str(self.num) + “/” + str(self.den)
• Examples on when this function is invoked
print(my_fraction)
my_fraction.__str__()
str(my_fraction)
6
“Add” Operation for Fraction
• Allow two Fraction objects to be added together using “+”
• For this, we need to override __add__ standard method
• So that f1 + f2 will call f1.__add__(f2), the overridden method
• Recall two fractions must have same denominator to be added
• Easiest solution is to multiply two denominators as common
denominator, i.e., for two fractions
𝑎
𝑏
and
𝑐
𝑑
, we add them as
𝑎
𝑏
+
𝑐
𝑑
=
𝑎𝑑
𝑏𝑑
+
𝑏𝑐
𝑏𝑑
=
𝑎𝑑+𝑏𝑐
𝑏𝑑
7
“Add” Operation for Fraction (cont.)
• “Add” operation implementation:
def __add__ (self, other_fractioin):
new_num = self.num * other_fraction.den +
self.den * other_fraction.num
new_den = self.den * other_fraction.den
return Fraction(new_num, new_den)
• Considering print(Fraction(1, 4)+Fraction(1, 2)), what is the result?
• 6/8 will be returned but 3/4 is supposed to be the best answer
8
Make Result in Lowest Term
• For this, we need a helper function to reduce fraction
• We use this function to calculate the greatest common divisor
(GCD) between numerator and denominator
• We can then divide both numerator and denominator by the GCD
to reduce the result to lowest term
• Best-known algorithm to find GCD is Euclid’s algorithm
• The GCD between m and n is n if n divides m evenly, otherwise it is the
GCD between n and m%n
• This algorithm only works for positive integer n, which is our case
(negative fraction is represented by negative numerator)
9
Make Result in Lowest Term (cont.)
• Implementation of Euclid’s algorithm:
def gcd(m, n):
while m % n != 0:
old_m = m
old_n = n
m = old_n
n = old_m % old_n
return n
10
Updated “Add” Operation Implementation
• We update __add__ to use gcd function to reduce result:
def __add__ (self, other_fractioin):
new_num = self.num * other_fraction.den +
self.den * other_fraction.num
new_den = self.den * other_fraction.den
common = gcd(new_num, new_den)
return Fraction(new_num // common, new_den // common)
11
Compare Fractions
• By default, two fractions f1 and f2 are equal (i.e., f1 == f2 is
True) only if f1 and f2 are references to same object, which is
called shallow equality
• What we want is f1 == f2 is True even they are different
objects but with same values (numerator and denominator),
which is called deep equality
12
Equality for Fraction
• Note that our Fraction object now has two
very useful methods as shown in the figures
13
Achieve Deep Equality
• For this, we need to override __eq__ standard method:
def __eq__(self, other):
first_num = self.num * other.den
second_num = other.num * self.den
return first_num == second_num
• Note that there are other operators that can be overridden
• E.g., __le__ (for “<=”), __gt__ (for “>”), __ne__ (for “!=”), __sub__ (for “-”),
__truediv__ (for “/”), etc.
14
Exceptions
• Two types of errors may occur when programming
• Syntax errors
• Mistakes made in the structure of a statement or expression
• Can usually be found by Python interpreter and IDEs
• Runtime errors usually caused by logic errors
• Due to errors in underlying algorithm or in translation/implementation of
that algorithm
• E.g., divide by zero or access item with index out of bound of a list
• Typically called exceptions, and can cause program to terminate
15
Exception Handling
• Provide a way to deal with exceptions that allows programmer to
have some type of intervention
• Use try statement to catch raised exception and use except
statement to handle the caught exception
• E.g., try:
print(math.sqrt(a_number))
except:
print(“Bad value for square root”)
print(“Using absolute value instead”)
print(math.sqrt(abs(a_number)))
16
Exception Handling (cont.)
• Programmers can also create and raise their own exceptions if
they detect a situation in the program execution that warrants it
• E.g., try:
if a_number <0:
raise RuntimeError(“You can’t use a negative number”)
else:
print(math.sqrt(a_number))
except RuntimeError as err_instance:
print(str(err_instance), “nUsing absolute value instead”)
print(math.sqrt(abs(a_number)))
17
List Comprehension
• Allow to easily create a list based on some processing or
selection criteria by using iteration and selection constructs
• a_list = [statement for item in collection if condition]
• If value item takes on from collection makes condition to be True, then the
statement is calculated and the result is added to the a_list being constructed
• It is equivalent to but more concise and efficient than
a_list = []
for item in collection:
if condition:
a_list.append(statement)
18
List Comprehension (cont.)
• E.g., to obtain a list of the squares of the first 10 integers that are odd:
sq_list = [x*x for x in range(1, 11) if x % 2 == 1]
It is equivalent to but more concise and efficient than
sq_list = []
for x in range(1,11):
if x % 2 == 1:
sq_list.append(x*x)
• Another example to build a list from a string
a_list = [ch.upper() for ch in “comprehension” if ch not in “aeiou”]
19
Summary
• Computer science is the study of problem solving and uses
abstraction as a tool for representing both processes and data
• Abstract data types (ADTs) help manage complexity of problem
domain by hiding details of data
• Python is object-oriented, powerful, yet easy-to-use
• Lists, tuples, and strings are built in Python sequential collections
• Dictionaries and sets are nonsequential collections of data
• Classes allow programmers to implement ADTs
• One can override standard methods as well as create new methods
20
Some Useful Links for Python 3
• https://docs.python.org/3/tutorial/, useful Python 3 tutorial to
help quickly get familiar with Python programming
• https://www.tutorialspoint.com/python3/, another good Python
3 tutorial to help quickly get familiar with Python programming
• https://docs.python.org/3/library/index.html, library reference
guide, to find libraries and functions needed for programming
• https://docs.python.org/3/, Python 3 documentation providing
detailed documentation about almost every aspect of Python 3
21

More Related Content

Similar to Data Structures and Algorithms in Python (20)

PPTX
Python4HPC.pptx
Sonam Mittal
 
PPTX
1P13 Python Review Session Covering various Topics
hussainmuhd1119
 
PDF
Class 2: Welcome part 2
Marc Gouw
 
PPTX
python ppt
EmmanuelMMathew
 
PPT
Introduction to Python Language and Data Types
Ravi Shankar
 
DOCX
Python Laboratory Programming Manual.docx
ShylajaS14
 
PDF
III MCS python lab (1).pdf
srxerox
 
PDF
python.pdf
SwapnilGujar10
 
PPTX
PPt Revision of the basics of python1.pptx
tcsonline1222
 
PPTX
Python4HPC.pptx
priyam737974
 
PPT
Python-review1 for begineers to code.ppt
freyjadexon608
 
PDF
Python-review1.pdf
paijitk
 
PDF
Python Part 1
Mohamed Ramadan
 
PDF
Python's magic methods
Reuven Lerner
 
PPT
Python-review1.ppt
snowflakebatch
 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
 
PPTX
Programming in python
Ivan Rojas
 
PDF
cel shading as PDF and Python description
MarcosLuis32
 
PPTX
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
PPTX
Python programming - Everyday(ish) Examples
Ashish Sharma
 
Python4HPC.pptx
Sonam Mittal
 
1P13 Python Review Session Covering various Topics
hussainmuhd1119
 
Class 2: Welcome part 2
Marc Gouw
 
python ppt
EmmanuelMMathew
 
Introduction to Python Language and Data Types
Ravi Shankar
 
Python Laboratory Programming Manual.docx
ShylajaS14
 
III MCS python lab (1).pdf
srxerox
 
python.pdf
SwapnilGujar10
 
PPt Revision of the basics of python1.pptx
tcsonline1222
 
Python4HPC.pptx
priyam737974
 
Python-review1 for begineers to code.ppt
freyjadexon608
 
Python-review1.pdf
paijitk
 
Python Part 1
Mohamed Ramadan
 
Python's magic methods
Reuven Lerner
 
Python-review1.ppt
snowflakebatch
 
Programming in python
Ivan Rojas
 
cel shading as PDF and Python description
MarcosLuis32
 
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Python programming - Everyday(ish) Examples
Ashish Sharma
 

Recently uploaded (20)

DOCX
Dissertation_Antony_Musyoka.docx.for presentation
antonykamile
 
PPTX
To Live Is For Christ 06 29 2025.pptx
FamilyWorshipCenterD
 
PPTX
From Hackathon to Real-World Impact: The Story of Sneh Vidhya Sahayog
shubhamsharma994585
 
PPTX
Soft Skills Training for Everybody.pp.pptx
Mayuri Srivastava
 
PPTX
Cynthia Kayle Share 5 Root Causes of Child Trafficking Around the World.pptx
Cynthia Kayle
 
PPTX
Présentation Bruit Sud-Ouest- juin 2025_TG_EN_Final.pptx
Pont Samuel-De Champlain Bridge
 
PDF
Amazon Wholesale Product Research Example
Joseph Juntilla
 
PPTX
Accessibility isn't just for users. Creating engaging technical presentations...
Elizabeth McCready
 
PDF
models-of-communication reading and writing.pdf
TristanNabong
 
PPTX
Présentation Bruit Verdun - juin 2025_TG_EN_Final.pptx
Pont Samuel-De Champlain Bridge
 
PPTX
AI for Empowering Women in AI
Letizia Jaccheri
 
PDF
Performancesonore_sudouest_EN.pdf
Pont Samuel-De Champlain Bridge
 
PPTX
Pastor Bob Stewart Acts 19 06 25 2025.pptx
FamilyWorshipCenterD
 
PDF
Performancesonore_verdun_EN.pdf
Pont Samuel-De Champlain Bridge
 
PPTX
Ludwig van Beethoven Life and Legacy.pptx
aryansnow1304
 
PDF
Rethinking Public–Private Partnerships: From Funding Gaps to Shared Goals
Francois Stepman
 
PPTX
2025-06-29 Abraham 05 (shared slides).pptx
Dale Wells
 
PPTX
organic farm Dr Shashi Jain 19.06.2018.pptx
Pratibha Chauhan
 
PDF
SZWDL denim washing and dry washing industry
te20046
 
DOC
STABILITY INDICATING METHOD DEVELOPMENT AND VALIDATION FOR SIMULTANEOUS ESTIM...
jmkeans624
 
Dissertation_Antony_Musyoka.docx.for presentation
antonykamile
 
To Live Is For Christ 06 29 2025.pptx
FamilyWorshipCenterD
 
From Hackathon to Real-World Impact: The Story of Sneh Vidhya Sahayog
shubhamsharma994585
 
Soft Skills Training for Everybody.pp.pptx
Mayuri Srivastava
 
Cynthia Kayle Share 5 Root Causes of Child Trafficking Around the World.pptx
Cynthia Kayle
 
Présentation Bruit Sud-Ouest- juin 2025_TG_EN_Final.pptx
Pont Samuel-De Champlain Bridge
 
Amazon Wholesale Product Research Example
Joseph Juntilla
 
Accessibility isn't just for users. Creating engaging technical presentations...
Elizabeth McCready
 
models-of-communication reading and writing.pdf
TristanNabong
 
Présentation Bruit Verdun - juin 2025_TG_EN_Final.pptx
Pont Samuel-De Champlain Bridge
 
AI for Empowering Women in AI
Letizia Jaccheri
 
Performancesonore_sudouest_EN.pdf
Pont Samuel-De Champlain Bridge
 
Pastor Bob Stewart Acts 19 06 25 2025.pptx
FamilyWorshipCenterD
 
Performancesonore_verdun_EN.pdf
Pont Samuel-De Champlain Bridge
 
Ludwig van Beethoven Life and Legacy.pptx
aryansnow1304
 
Rethinking Public–Private Partnerships: From Funding Gaps to Shared Goals
Francois Stepman
 
2025-06-29 Abraham 05 (shared slides).pptx
Dale Wells
 
organic farm Dr Shashi Jain 19.06.2018.pptx
Pratibha Chauhan
 
SZWDL denim washing and dry washing industry
te20046
 
STABILITY INDICATING METHOD DEVELOPMENT AND VALIDATION FOR SIMULTANEOUS ESTIM...
jmkeans624
 
Ad

Data Structures and Algorithms in Python

  • 1. Classes - Object-Oriented Programming • Allow to implement abstract data type to provide logical description of • What a data object looks like (its state) • What it can do (its methods) • To build a class for this, one can take advantage of the abstraction process while providing necessary details to use the abstraction in a program 1
  • 2. Class Definition • class class_name: def __init__(self, para_name_1, para_name_2, ...) statement body def other_method_name(self, other_para_1, ...) statement body • Constructor method is always __init__ function • self is always 1st parameter, used to refer back to the object itself • E.g., self.var_name can be used to refer to the object’s internally defined variable var_name (as part of the object’s state) 2
  • 3. Fraction Class as an Example • Recall that a fraction such as 3 5 consists of two parts • Top value, i.e., numerator, can be any integer • Bottom value, i.e., denominator, can be any positive integer • Negative fractions have a negative numerator • Operations/methods for Fraction type allow a Fraction data object to behave like any other numeric value • Such as add, subtract, multiply and divide • Fraction should be displayed in “slash” form (e.g., 3/5) • All methods should return results in lowest terms (i.e., most common forms, e.g., 3/5 rather than 6/10) 3
  • 4. Implementation of Fraction Class • Class definition and its constructor class Fraction: def __init__(self, top, bottom): self.num = top self.den = bottom • Fraction state data, i.e., numerator and denominator are denoted by internal data objects self.num and self.den, respectively • __init__ initializes the internal state by assigning values of the top and bottom parameters to self.num and self.den, respectively 4
  • 5. Create Instance of Fraction Class • To create a Fraction instance, we use the name of the class, which invokes constructor and passes actual values for the necessary state • Note that we never directly invoke __init__ • Note that 1st parameter self is never given actual parameter value upon invocation • E.g., my_fraction = Fraction(3, 5) invokes constructor and creates a Fraction instance 3 5 as show on the right 5
  • 6. Display Fraction • print function requires object convert itself into string so as to be written to output • To tell Fraction class how to convert itself into string, we need to override standard method __str__, i.e., to redefine its behavior: def __str__(self): return str(self.num) + “/” + str(self.den) • Examples on when this function is invoked print(my_fraction) my_fraction.__str__() str(my_fraction) 6
  • 7. “Add” Operation for Fraction • Allow two Fraction objects to be added together using “+” • For this, we need to override __add__ standard method • So that f1 + f2 will call f1.__add__(f2), the overridden method • Recall two fractions must have same denominator to be added • Easiest solution is to multiply two denominators as common denominator, i.e., for two fractions 𝑎 𝑏 and 𝑐 𝑑 , we add them as 𝑎 𝑏 + 𝑐 𝑑 = 𝑎𝑑 𝑏𝑑 + 𝑏𝑐 𝑏𝑑 = 𝑎𝑑+𝑏𝑐 𝑏𝑑 7
  • 8. “Add” Operation for Fraction (cont.) • “Add” operation implementation: def __add__ (self, other_fractioin): new_num = self.num * other_fraction.den + self.den * other_fraction.num new_den = self.den * other_fraction.den return Fraction(new_num, new_den) • Considering print(Fraction(1, 4)+Fraction(1, 2)), what is the result? • 6/8 will be returned but 3/4 is supposed to be the best answer 8
  • 9. Make Result in Lowest Term • For this, we need a helper function to reduce fraction • We use this function to calculate the greatest common divisor (GCD) between numerator and denominator • We can then divide both numerator and denominator by the GCD to reduce the result to lowest term • Best-known algorithm to find GCD is Euclid’s algorithm • The GCD between m and n is n if n divides m evenly, otherwise it is the GCD between n and m%n • This algorithm only works for positive integer n, which is our case (negative fraction is represented by negative numerator) 9
  • 10. Make Result in Lowest Term (cont.) • Implementation of Euclid’s algorithm: def gcd(m, n): while m % n != 0: old_m = m old_n = n m = old_n n = old_m % old_n return n 10
  • 11. Updated “Add” Operation Implementation • We update __add__ to use gcd function to reduce result: def __add__ (self, other_fractioin): new_num = self.num * other_fraction.den + self.den * other_fraction.num new_den = self.den * other_fraction.den common = gcd(new_num, new_den) return Fraction(new_num // common, new_den // common) 11
  • 12. Compare Fractions • By default, two fractions f1 and f2 are equal (i.e., f1 == f2 is True) only if f1 and f2 are references to same object, which is called shallow equality • What we want is f1 == f2 is True even they are different objects but with same values (numerator and denominator), which is called deep equality 12
  • 13. Equality for Fraction • Note that our Fraction object now has two very useful methods as shown in the figures 13
  • 14. Achieve Deep Equality • For this, we need to override __eq__ standard method: def __eq__(self, other): first_num = self.num * other.den second_num = other.num * self.den return first_num == second_num • Note that there are other operators that can be overridden • E.g., __le__ (for “<=”), __gt__ (for “>”), __ne__ (for “!=”), __sub__ (for “-”), __truediv__ (for “/”), etc. 14
  • 15. Exceptions • Two types of errors may occur when programming • Syntax errors • Mistakes made in the structure of a statement or expression • Can usually be found by Python interpreter and IDEs • Runtime errors usually caused by logic errors • Due to errors in underlying algorithm or in translation/implementation of that algorithm • E.g., divide by zero or access item with index out of bound of a list • Typically called exceptions, and can cause program to terminate 15
  • 16. Exception Handling • Provide a way to deal with exceptions that allows programmer to have some type of intervention • Use try statement to catch raised exception and use except statement to handle the caught exception • E.g., try: print(math.sqrt(a_number)) except: print(“Bad value for square root”) print(“Using absolute value instead”) print(math.sqrt(abs(a_number))) 16
  • 17. Exception Handling (cont.) • Programmers can also create and raise their own exceptions if they detect a situation in the program execution that warrants it • E.g., try: if a_number <0: raise RuntimeError(“You can’t use a negative number”) else: print(math.sqrt(a_number)) except RuntimeError as err_instance: print(str(err_instance), “nUsing absolute value instead”) print(math.sqrt(abs(a_number))) 17
  • 18. List Comprehension • Allow to easily create a list based on some processing or selection criteria by using iteration and selection constructs • a_list = [statement for item in collection if condition] • If value item takes on from collection makes condition to be True, then the statement is calculated and the result is added to the a_list being constructed • It is equivalent to but more concise and efficient than a_list = [] for item in collection: if condition: a_list.append(statement) 18
  • 19. List Comprehension (cont.) • E.g., to obtain a list of the squares of the first 10 integers that are odd: sq_list = [x*x for x in range(1, 11) if x % 2 == 1] It is equivalent to but more concise and efficient than sq_list = [] for x in range(1,11): if x % 2 == 1: sq_list.append(x*x) • Another example to build a list from a string a_list = [ch.upper() for ch in “comprehension” if ch not in “aeiou”] 19
  • 20. Summary • Computer science is the study of problem solving and uses abstraction as a tool for representing both processes and data • Abstract data types (ADTs) help manage complexity of problem domain by hiding details of data • Python is object-oriented, powerful, yet easy-to-use • Lists, tuples, and strings are built in Python sequential collections • Dictionaries and sets are nonsequential collections of data • Classes allow programmers to implement ADTs • One can override standard methods as well as create new methods 20
  • 21. Some Useful Links for Python 3 • https://docs.python.org/3/tutorial/, useful Python 3 tutorial to help quickly get familiar with Python programming • https://www.tutorialspoint.com/python3/, another good Python 3 tutorial to help quickly get familiar with Python programming • https://docs.python.org/3/library/index.html, library reference guide, to find libraries and functions needed for programming • https://docs.python.org/3/, Python 3 documentation providing detailed documentation about almost every aspect of Python 3 21