
![]() |
@wtf | |
(Student grade tracker) |
||
1
Replies
10
Views
1 Bookmarks
|
![]() |
@wtf | 18 days |
*Classes & Objects in Python* Object-Oriented Programming helps you: - Model real-world entities - Group data + behavior together - Write clean, scalable, and reusable code *What is a Class?* A class is a blueprint for creating objects (instances). It defines attributes (variables) and methods (functions). class Person: def __init__(self, name, age): Constructor self.name = name Attribute self.age = age def greet(self): Method print(fHello, I'm self.name and I'm self.age years old.) *What is an Object?* An object is an instance of a class. p1 = Person(Alice, 25) p1.greet() Output: Hello, I'm Alice and I'm 25 years old. *What is ___init___?* It’s the constructor method in Python. It runs automatically when you create a new object and is used to initialize attributes. *Mini Project: Student Grade Tracker* class Student: def __init__(self, name): self.name = name self.grades = [] def add_grade(self, grade): self.grades.append(grade) def average(self): if self.grades: return sum(self.grades) / len(self.grades) return 0 def report(self): print(fself.name's Average Grade: self.average():.2f) Usage s1 = Student(David) s1.add_grade(85) s1.add_grade(92) s1.add_grade(78) s1.report() *Output:* David's Average Grade: 85.00 *OOP Concepts Practiced* : - Class and object creation - Initializing attributes with __init__ - Using methods to manipulate internal data |
||


