Classes in Python.

Class Definition:

A class is define with the keyword 'class' followed by userdefine class name and end with the colon (:) . All the statement which is present inside the class such as variable declaration of function.

Syntax: class <class_name> : variable declaration method declaration: statement inside method

Every thing in the class is intended just like code in the function , loop, if statement etc.

Any thing not intended is not part of the class.

Write a program for a simple class demostration which print the message "hello" class display: def show(self): print("hello") s=display() s.show() Output: hello write a program to add class addition: a=5 b=6 def add(self): print(a+b) aObj=addition() aObj.add() Output: 11 write a program to fill the details of students like its name, branch and year. class student: def fillsdetails(self,name,branch,year): self.name=name self.branch=branch self.year=year def display(self): print("name:",self.name) print("branch:",self.branch) print("year:",self.year) s1=student() s2=student() s1.fillsdetails("AAA","IT",2) s2.fillsdetails("BBB","CS",2) s1.display() s2.display() Output: AAA IT 2 BBB CS 2

Objects are Mutable

"Ojects are mutable", this statement tells us that this state of object can be change at any pont of time by making changess to its attribute

Example: s1.fillsdetails("AAA","IT",3) s1.display() Output: AAA IT 3

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.