English
Tamil Nadu Board of Secondary EducationHSC Science Class 12

Write a class with two private class variables and print the sum using a method. - Computer Science

Advertisements
Advertisements

Question

Write a class with two private class variables and print the sum using a method.

Short Note

Solution

class Sample:
def_init_(self,n1,n2):
self._n1=n1
self._n2=n2
def display(self):
print(“class variable 1:”, self._n1)
print(“class variable 2:”, self._n2)
print(“sum self._n1 + self._n2)
s = sample(10, 20)
s.display( )

Output:
class variable 1: 10
class variable 2: 20
sum: 30

shaalaa.com
Public and Private Data Members
  Is there an error in this question or solution?
Chapter 10: Python Classes and objects - Evaluation [Page 188]

APPEARS IN

Samacheer Kalvi Computer Science [English] Class 12 TN Board
Chapter 10 Python Classes and objects
Evaluation | Q 2. | Page 188
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×