Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update building our first class.py #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Object Oriented Programming/building our first class.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#Today we will learn how to create a class and other attributes of class
#Below is the method how classes are defined
# Define the class with attributes and methods
class Student:
pass

#Below is the method to create object , Here Varun and rohan are two objects of Class Student
Varun = Student()
larry = Student()

# Now after creating objects we can use them to call variables
Varun.name = "Harry"
Varun.std = 12
Varun.section = 1
larry.std = 9
larry.subjects = ["hindi", "physics"]
print(Varun.section, larry.subjects)
def __init__(self, name, std, section=None, subjects=None):
self.name = name
self.std = std
self.section = section
self.subjects = subjects or []

# Create objects and pass values to the constructor
varun = Student("Harry", 12, section=1)
larry = Student("Larry", 9, subjects=["Hindi", "Physics"])

# Access object attributes
print(varun.section, larry.subjects)