Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Week 5: Object-Oriented Programming

Computing Programming with


Python
Ronny Estrella
● What is Object-Oriented Programming?
○ Attributes and Methods
○ OOP in python
○ What is python class?
○ When to use classes in python?
○ Abstraction
CLASS
CONTENTS
Introduction

Class Computing Programming with Python

Professor Ronny Estrella (restrella@solbridge.ac.kr;


ronnyestrella@hotmail.com)

Lecture Time Monday/Wednesday 10:30 am - 12:00 pm

Place Online via Zoom


Review Quiz

● What is are examples of loop control statements?


● When I need to use a copy function?
● Why I need to use a copy function?
● What is lambda expression?
Practice Exercise from the previous class

Write a program that reads a CSV file of student grades, calculates the average
grade for each student, and writes the results to a new CSV file. Use the csv module
to read and write CSV files “import csv”, and define functions to calculate the
average grade and format the results.
What is object-oriented programming?
● Object-oriented programming (OOPs) is a programming approach.
● Objects are software bundles like people or car or robot or etc.
● Objects hold data and code together.
● Objects are analogous to real-world objects with properties and behaviors.
● Objects have nature and behavior, such as the remote control of a
television.
● Object have attributes/properties.
● oBjects have behaviours/methods.
Attributes and Methods Attributes → variables
Methods → functions

● Attributes are information that is saved


in a class blueprint and individual
objects have data saved in the
attributes field.
● Behaviors are represented through
methods that carry out operations
specified in the class specification.
● A class represents a template to
create objects with specified attributes
and methods.
● Objects created using a class will have
the attributes and methods contained
within it, differing only from one object
to another.
How we use OOP in
python?
How we use OOP in python?

● OOP is used in Python to create classes, which act as templates for creating
objects with similar attributes and behaviors.
● A class is defined using the 'class' keyword, followed by the class name and a
colon. The class body contains the attributes and methods for the class.
● Attributes are variables that hold data for each object, and methods are
functions that perform operations on the object's data.
● When an object is created, it is called an instance of the class. The 'self'
parameter is used to refer to the instance within the class.
● Objects can interact with each other through their methods, and inheritance can
be used to create new classes that inherit attributes and methods from a parent
class.
What is a Python class?

● A blueprint to create objects


● A class defines what objects look like, and what functions can operate on these
objects
● An object is an item with fields supported by a set of method functions.
○ Can have several fields
○ These fields are accessed or modified by object methods

Syntax: Class Definition


Syntax: Object Definition
class ClassName:
# Statement obj = ClassName()
print(obj.atrr)
Steps to define class
class Person: 1

#defining python class methods 4


Python Class Variables def showName(self):
print(self.name)
#initializing the variables 2
name = “ ”
age = 0 def method_name(self, parameter 1,
parameter 2, …….)
#defining constructor statements……..
def __init__(self, personName, return value (if required)
personAge): 3
self.name = personName
self.age = personAge
When to use classes in Python

● When you need to represent a real-world object, such as a person, car, or


animal.
● When you need to work with complex data structures, such as graphs,
trees, or networks.
● When you need to implement complex algorithms, such as sorting or
searching algorithms.
● When you need to create a library or package that other programmers can
use.
Abstraction

● The concept of hiding, or removing.


● The concept of generalization.
● The concept of idea versus reality

Source: https://thevaluable.dev/abstraction-type-software-example/
Define a dog object
Abstraction of dog object
Abstraction of dog object
Abstraction of dog object
Declaring Objects (Also called instantiating a class)
Practice time

Bank Account Class


Considering the previous Python code for a class for a bank account. Modified the
class to have the following attributes:

Account number (a string)


Balance (a float)
Owner name (a string)

An include the following method:

Print statement: prints a statement showing the account number, owner name, and
current balance.
Practice time
Student Class
Write a Python class for a student. The class should have the following attributes:

Student ID (an integer)


First name (a string)
Last name (a string)
List of grades (a list of integers)
The class should have the following methods:

Add grade: adds a specified grade to the list of grades.


Calculate average grade: calculates and returns the average grade for the student.
Print transcript: prints a transcript showing the student ID, first name, last name, list of grades, and
average grade.
Read for the next class

What are abstractions in software engineering with examples


TAKEAWAYS

● Object-oriented programming (OOP) is a


programming approach that uses the concept of
objects to hold data and code together.
● In Python, classes are used to create objects with
similar attributes and behaviors.
● A class is defined using the 'class' keyword, followed
by the class name and a colon. The class body
contains the attributes and methods for the class.

Q&A ● Attributes are variables that hold data for each


object, and methods are functions that perform
operations on the object's data.
● When an object is created, it is called an instance of
Every question is welcome be the class. The 'self' parameter is used to refer to the
instance within the class.
free to express your thoughts ● Objects can interact with each other through their
methods, and inheritance can be used to create new
classes that inherit attributes and methods from a
parent class.
Gracias!!!
Thank you
감사합니다

You might also like