Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Lesson 01_Introduction to Objects and Classes

Uploaded by

dohaminta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lesson 01_Introduction to Objects and Classes

Uploaded by

dohaminta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Classes & Objects

Using Python
ASSESSMENTS
• Quiz- 10%
• Class Participation- 10%
• Midterm Exam-30%
• Final Exam-40%
• Project Viva / Presentation-10%
Class / Lab Rules
• Use of internet is not allowed unless explicitly allowed
• Use of mobiles is not allowed unless explicitly allowed
• No late comings (more than 10 minutes) or early outs
• Discipline should be maintained in teacher’s presence
In case of breach of lab
rules vote for marks
deduction or samosa party?
How to get good grades?
• By putting earnest effort-which brings fruitful results
• By having 75% attendance
• By submitting work on time
• By adhering to lab and class rules
Procedural Programming
• Procedural programming simply contains a series of computational
steps, these steps instruct the computer on how to solve a task in
logical steps.
• A procedural program is typically a list of instructions that execute
one after the other starting from the top of the line.
• Procedural programming is best for simple programs.
Procedural Programming
• Make a Sum function that receives a list of integers as an argument
and returns the sum of integer values stored in a list. Print the
returned total sum at the end.
Procedural Programming
Example
def Sum(sample_list):
total = 0
for x in sample_list:
total += x
return total
list1 = [10, 200, 50, 70]
list2 = [3, 26, 33, 13]
print(Sum(list1))
print(Sum(list2))
Advantages of Procedural
Programming
• Best for general-purpose programming.
• Reusability of the code.
• It is easy to trace the flow of the program.
Disadvantages of Procedural
Programming
• The data is exposed (security issues).

• Difficult to solve real-world problems.


Object Oriented Programming
• Object-Oriented Programming are built around objects.

• 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.
Object as Real-World Entity
An object has two characteristics:-
• attributes
• behaviors

For example, an object could represent an employee with attributes


such as name, title, experience, etc., with behaviors like working, on-
leave, underperformed, etc.,
Can you think of objects
in real-world? Give
Examples one by one
Advantages of Object Oriented
Programming
• Object-oriented programming enables you to develop large, modular
programs that can instantly expand over time.
• Object-oriented programs hide the implementation from the end-
user.
• It supports collaborative development, where projects are divided
into groups.
• OOP include code reusability, scalability and efficiency.
Class
• A class is considered as a blueprint of objects.

• Classes can be viewed as factories or templates for generating new


object instances.

• Each object instance takes on the properties of the class from which it
was created.
Classes Have Attributes
But Also Behaviors
ATTRIBUTES BEHAVIORS
Name: Open account
Phone: Buy investments
Email: Sell investments
Purchases: Close account

Image of James curtesy of James Tam


Objects as Instances of Class
Class Creation
Defining Class Methods
Format:
class <classname>:
def <method name> (self, <other parameters>):
Unlike functions, every method of
<method body> a class must have the ‘self’
parameter (more on this later)

Example:
class Person:
name = "I have no name :("
def sayName (self): When the attributes are accessed
print ("My name is...", self.name) inside the methods of a class they
MUST be preceded by the suffix
“.self”
Class Example
# create a class
class Room:
length = 0.0
breadth = 0.0

# method to calculate area


def calculate_area(self):
print("Area of Room =", self.length * self.breadth)
Creating Objects
# create object of Room class
study_room = Room()

# assign values to all the attributes


study_room.length = 42.5
study_room.breadth = 30.8

# access method inside class


study_room.calculate_area()
Complete Code
# create a class
class Room:
length = 0.0
breadth = 0.0

# method to calculate area


def calculate_area(self):
print("Area of Room =", self.length * self.breadth)

# create object of Room class


study_room = Room()

# assign values to all the attributes


study_room.length = 42.5
study_room.breadth = 30.8

# access method inside class


study_room.calculate_area()
Class & Object
Class Example
• Class: Person

Attributes / State: Name, Sex, Profession


Functions / Behavior: Working, Study
Objects of a class
Code the person class on
the pattern of room class
discussed previously

You might also like