[go: up one dir, main page]

0% found this document useful (0 votes)
6 views34 pages

CDS M4

The document provides an overview of Object-Oriented Programming (OOP) in Python, covering its basic concepts, components, and practical examples. It explains classes, objects, attributes, methods, and includes sample code for creating classes and objects, as well as implementing methods. Additionally, it presents exercises for creating a student class and a fighter class, along with their functionalities.
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)
6 views34 pages

CDS M4

The document provides an overview of Object-Oriented Programming (OOP) in Python, covering its basic concepts, components, and practical examples. It explains classes, objects, attributes, methods, and includes sample code for creating classes and objects, as well as implementing methods. Additionally, it presents exercises for creating a student class and a fighter class, along with their functionalities.
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
You are on page 1/ 34

OOP in Python

Froilan De Guzman
Learning outcomes:
- To discuss the basic concepts of object-oriented
programming
- To develop class in Python and construct objects from it.
- To apply OOP approach in some programming
problems.

2
Object Oriented Programming
“Object-oriented programming (OOP) is a computer
programming model that organizes software design
around data, or objects, rather than functions and
logic. An object can be defined as a data field that has
unique attributes and behavior.”
Source: https://searchapparchitecture.techtarget.com/definition/object-oriented-programming-OOP

3
Basic components of OOP
Class – it is a group of similar objects.
Object – it is an element derived from
the class
Attributes – these are the characteristics
that describes the object.
Instance – It is a specific member of a Source: Lynda.com
class.
Methods – these are tasks or functions
that the object performs.

4
OOP example OBJECT 1
Attributes:
Name: Jenny
Age: 20
Hair Color: Brown
Course: BSIT
WOMAN CLASS
Attributes:
Name: OBJECT 2
Age: Attributes:
Hair Color: Instance Name: Mika
Course: Age: 19
Methods: Hair Color: Yellow
Play() Course: BSCS
Talk()
Read()
OBJECT 3
Attributes:
Name: Trish
Source: Pinterest and Devian Art Age: 21
Hair Color: Gold
5
Course: BSCpE
Example:
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.

6
Example:
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.

num1 = int(input("Enter first integer: "))


num2 = int(input("Enter second integer: "))
print ("Sum: ", num1+num2)
print ("Difference: ", num1-num2)
print ("Product: ", num1*num2)
print ("Quotient: ", float(num1/num2))

7
Example: Using OOP (Create class and initialization)
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2

8
Example: Using OOP (Create methods)
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
return float(self.num1 / self.num2)

9
Example: Using OOP
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic: n1 = int(input("Enter first integer: "))
def __init__(self, num1, num2): n2 = int(input("Enter second integer: "))
self.num1 = num1
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
return float(self.num1 / self.num2)

10
Example: Using OOP (Create objects)
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic: n1 = int(input("Enter first integer: "))
def __init__(self, num1, num2): n2 = int(input("Enter second integer: "))
self.num1 = num1 test = arithmetic(n1, n2)
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
return float(self.num1 / self.num2)

11
Example: Using OOP (Test the methods)
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic: n1 = int(input("Enter first integer: "))
def __init__(self, num1, num2): n2 = int(input("Enter second integer: "))
self.num1 = num1 test = arithmetic(n1, n2)
self.num2 = num2 print ("Sum: ", test.add())
def add(self): print ("Difference: ", test.subtract())
return self.num1 + self.num2 print ("Product: ", test.multiply())
def subtract(self): print ("Quotient: ", test.divide())
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
return float(self.num1 / self.num2)

12
class arithmetic:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
return float(self.num1 / self.num2)
THIS IS NOT SAME INDENTION!!!
n1 = int(input("Enter first integer: "))
n2 = int(input("Enter second integer: "))
test = arithmetic(n1, n2)
print ("Sum: ", test.add())
print ("Difference: ", test.subtract())
print ("Product: ", test.multiply())
13
print ("Quotient: ", test.divide())
Example: Using OOP (without using return)
• Create a console program in Python that will accept 2 integers
and display the sum, difference, product and quotient.
class arithmetic: n1 = int(input("Enter first integer: "))
def __init__(self, num1, num2): n2 = int(input("Enter second integer: "))
self.num1 = num1 test = arithmetic(n1, n2)
self.num2 = num2 test.add()
def add(self): test.subtract()
print ("Sum: ", self.num1 + self.num2) test.multiply()
def subtract(self): test.divide()
print ("Difference: ", self.num1 - self.num2)
def multiply(self):
print ("Product: ", self.num1 * self.num2)
def divide(self):
print ("Quotient: ", float(self.num1 / self.num2))

14
class arithmetic:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def add(self):
print ("Sum: ", self.num1 + self.num2)
def subtract(self):
print ("Difference: ", self.num1 - self.num2)
def multiply(self):
print ("Product: ", self.num1 * self.num2)
def divide(self):
print ("Quotient: ", float(self.num1 / self.num2))

n1 = int(input("Enter first integer: ")) THIS IS NOT SAME INDENTION!!!


n2 = int(input("Enter second integer: "))
test = arithmetic(n1, n2)
test.add()
test.subtract()
test.multiply()
15
test.divide()
Example:
• Create a Python class for student with the following attributes
and methods:
• Attribute:
• Name
• Age
• Year Level
• Course
• Methods:
• AdvanceYear()
• ShiftCourse()
• CelebBday()
• Display() 16
Code: Create initialization for student class
class student:
def __init__(self, studentname, age, yr, course):
self.name = studentname
self.age = age
self.yrlevel = yr
self.course = course

17
Code: Create advanceyear() method
class student:
def __init__(self, studentname, age, yr, course):
self.name = studentname
self.age = age
self.yrlevel = yr
self.course = course
def advanceyear(self):
self.yrlevel += 1

18
Code: Create shiftcourse() method
class student:
def __init__(self, studentname, age, yr, course):
self.name = studentname
self.age = age
self.yrlevel = yr
self.course = course
def advanceyear(self):
self.yrlevel += 1
def shift_course(self, course):
self.course = course

19
Code: Create celebbday() method
class student:
def __init__(self, studentname, age, yr, course):
self.name = studentname
self.age = age
self.yrlevel = yr
self.course = course
def advanceyear(self):
self.yrlevel += 1
def shift_course(self, course):
self.course = course
def celebbday(self):
self.age +=1

20
Code: Create display() method
class student:
def __init__(self, studentname, age, yr, course):
self.name = studentname
self.age = age
self.yrlevel = yr
self.course = course
def advanceyear(self):
self.yrlevel += 1
def shift_course(self, course):
self.course = course
def celebbday(self):
self.age +=1
def display(self):
print (self.name)
print (self.age)
print (self.yrlevel)
21
print (self.course)
Create objects from student class
s1 = student("JUAN", 21, 2, "IT")
s2 = student("LUCAS", 13, 1, "CS")

22
Perform methods
s1 = student("JUAN", 21, 2, "IT")
s2 = student("LUCAS", 13, 1, "CS")

s1.display()
s1.advanceyear()
s1.celebbday()
s1.shift_course("CS")
s1.display()
if s1.age>10:
print ("OK“)
s2.display()

23
Sample Output:
s1 = student("JUAN", 21, 2, "IT");
s2 = student("LUCAS", 13, 1, "CS");

s1.display();
s1.advanceyear();
s1.celebbday();
s1.shift_course("CS");
s1.display();
if s1.age>10:
print "OK";
s2.display();

24
Example: Fighter class
class fighter:
def __init__(self, name, lives, heart, rank, level):
self.name = name
self.lives = lives
self.heart = heart
self.rank = rank
self.level = level

25
Example: Fighter class
class fighter:
def __init__(self, name, lives, heart, rank, level):
self.name = name
self.lives = lives
self.heart = heart
self.rank = rank
self.level = level
def attack(self, xp):
self.rank += xp
if self.rank >= 100:
self.level += 1
self.rank -=100

26
Example: Fighter class
class fighter:
def __init__(self, name, lives, heart, rank, level):
self.name = name
self.lives = lives
self.heart = heart
self.rank = rank
self.level = level
def attack(self, xp):
self.rank += xp
if self.rank >= 100:
self.level += 1
self.rank -=100
def damage(self, hurt):
self.heart -= hurt
if self.heart <= 0:
self.lives -=1
self.heart = 100
27
self.gameover()
Example: Fighter class (cont)
def dead(self):
self.lives -= 1
self.heart = 100
self.gameover()

28
Example: Fighter class (cont)
def dead(self):
self.lives -= 1
self.heart = 100
self.gameover()
def gameover(self):
if self.lives == 0:
print ("Game Over")
del (self)

29
Example: Fighter class (cont)
def dead(self):
self.lives -= 1
self.heart = 100
self.gameover()
def gameover(self):
if self.lives == 0:
print ("Game Over")
del (self)
def display(self):
print (self.name)
print (self.lives)
print (self.heart)
print (self.rank)
print (self.level)

30
Testing the fighter class
f1 = fighter("Karrie", 5, 100, 0, 1)
f2 = fighter("Claude", 5, 100, 0, 1)

def attackf2(num):
f1.attack(num)
f2.damage(num)
def attackf1(num):
f2.attack(num)
f1.damage(num)
def displayF():
f1.display()
f2.display()
displayF()
attackf1(30)
attackf2(60)
displayF()
31
Testing the fighter class
f1 = fighter("Karrie", 5, 100, 0, 1)
f2 = fighter("Claude", 5, 100, 0, 1)

def attackf2(num):
f1.attack(num)
f2.damage(num)
def attackf1(num):
f2.attack(num)
f1.damage(num)
def displayF():
f1.display()
f2.display()
displayF()
attackf1(30)
attackf2(60)
displayF()
32
Prelim Quiz 3
• Create a pet class using console in Python. Consider the following attributes:
• Name: Pet name
• Type: Cat, Dog, Fish, Etc.
• Age: In years
• Food: 1 (lowest) to 100 (highest)
• Skills: 1 (default) to positive integer
• Consider the following methods:
• EelebBirthday() – increase age by 1
• Eat(num) – increase food by num (10 to 20 values only)
• Exercise(num) – increase skill by num but decrease food (5 to 10)
• Dead() – food reached 0 or negative value, delete the object
• Use at least two objects to simulate.

33
References:
- https://searchapparchitecture.techtarget.com/definition/object-oriented-programming-OOP
- Pinterest and Devian Art
- Lynda.com

33

You might also like