Class and Object in Python

#----------------------------- 1 - creating a Class ---------------------------------

# syntax : class class_name:

class news: # class property
    sent ="Today no special news"
#----------------------------- 2 - creating a Object for class------------------------

# syntax: object_name.class_name() , to invoke the class 

class news: # class property
    sent ="Today no special news"  # property


obj = news()  # object  
print(obj.sent)# accessing the property of  the class 
#----------------------------- 3 -  __init__() function in a class--------------------

class blog:
  def __init__(self, name, year): # __init__() s an built in function 
    #__init__() function is used  to assign values to object properties
    # when __init__() 
is created, the class is called when new object is created

    self.name = name
    self.year = year

post = blog("Python", 2023) # assigning class to variable post

print(post.name)
print(post.year)
#----------------------------- 4 -  __str__() function in a class--------------------

class blog:
    def __init__(self, name, year, duration): # __init__() s an built in function 
        #__init__() function is used  to assign values to object properties

        self.name = name
        self.year = year
        self.duration =duration
    def __str__(self):
        return f"{self.name} - {self.year} - {self.duration}" 

post = blog("Python", 2023,'1 month') # assigning class to variable post

print(post)  # automatically invoke the class 


#----------------------------- 5 -  calling function/method in a class----------------

# syntax: object_name.function_name() , to invoke the function 

class blog:
    def __init__(self, name, year, duration): # __init__() s an built in function 
        #__init__() function is used  to assign values to object properties

        self.name = name
        self.year = year
        self.duration =duration
    def course(self):  # defining a function
        print("We provide easy way of learning for",self.name," in ",self.duration) 

post = blog("Python", 2023,'1 month.') # assigning class to variable post

post.course()  # function is  called from the class 

#----------------------------- 6 -  Updating  values in a class --------------------

class blog:
    def __init__(self, name, year, duration): # __init__() s an built in function 
        #__init__() function is used  to assign values to object properties

        self.name = name
        self.year = year
        self.duration =duration
    def course(self):
        print("We provide easy way of learning for",self.name," in ",self.duration) 

post = blog("HTML", 2023,'1 month.') # assigning class to variable post
post.year = 2022
post.duration='15 days'

post.course()  # function is  invoked from the class 

#----------------------------- 7 -  deleting values in a class --------------------

class blog:
    def __init__(self, name, year, duration): # __init__() s an built in function 
        #__init__() function is used  to assign values to object properties

        self.name = name
        self.year = year
        self.duration =duration
    def __str__(self):
        return f"{self.name} - {self.year} - {self.duration}" 

post = blog("Python", 2023,'1 month') # assigning class to variable post

print(post)  # automatically invoke the class 

# to delete an element in a class 'del' keyword is used
# Syntax : del object_name.function_name() / del object_name.variable()

del post.duration # will delete duration  attribute from the class

print(post)  # will return a error because duration has been deleted so it can't be displayed

Join our team, where career growth knows no limits.

For More Details

Our internship program guarantees personalized attention for optimal career development in the tech industry.

+91 97879 68375

-> You should bring your own laptop during the internship

Who can apply?

Internships are open for students who: B.E / B.Tech / M.E / M.Tech / B.Sc / M.Sc / M.C.A / B.C.A / Diploma.

  • Ongoing Students Only
  • days, 5days/ 10days or Based on Student Preference
  • Limited 1 to 10 students per slot
  • Registration fees : 1000 INR
  • Slots are filling quickly only fewer slots are available