Python OOPs Concepts 4

Example:4
Python program to create a class named student with attributes as roll and name. Take the input for the details and display them?
Sol:

#class creation
class student:
    roll=101
    name="Amit"
    def input(self):
        self.roll=int(input("Enter roll no "))
        self.name=input("Enter name ")
    def show(self):
        print("roll ",self.roll)
        print("name ",self.name)
        
        

#object creation
s=student()
#to fetch the data elements
print(s.roll)
print(s.name)
#to invode member function
s.show()
#to declare another object
s1=student()
#to take input for data elements
s1.input()
#to display data elements
s1.show()

Output:

101
Amit
roll  101
name  Amit
Enter roll no 1005
Enter name Kishan
roll  1005
name  Kishan
>>> 

Example:5
Python program to create a class named employee with attributes as empno, name and salary. Take input for the details and display them?
Sol:

class employee:
    empno=""
    name=""
    sal=""
    def input(self):
        self.empno=int(input("Enter empno "))
        self.name=input("Enter name ")
        self.sal=float(input("Enter salary "))
    def show(self):
        print("empno ",self.empno)
        print("name ",self.name)
        print("Salay ",self.sal)

#object creating
e=employee()
e.input()
e.show()

Output:

Enter empno 102
Enter name Kapil
Enter salary 45000
empno  102
name  Kapil
Salay  45000.0
>>> 

Example:6
Python program to create a class named bank with attributes as accno, name and bal. Take input for the details and display them?
Sol:

class bank:
    accno=0
    name=""
    bal=0
    def input(self):
        self.accno=int(input("Enter accno "))
        self.name=input("Enter name ")
        self.bal=float(input("Enter balance "))
    def show(self):
        print("accno ",self.accno)
        print("Name ",self.name)
        print("Bal ",self.bal)

#calling
#creating an object
b=bank()
#invoking the method of class
b.input()
b.show()

Output:

Enter accno 1002
Enter name Amit
Enter balance 5000
accno  1002
Name  Amit
Bal  5000.0
>>> 

Example:7
Python program to create a class named student with attributes as roll, name, marks of three subject (m1,m2,m3),total and per. Take input for the details and display them?
Sol:

#class creation
class student:
    roll=0
    name=""
    m1=0
    m2=0
    m3=0
    total=0
    per=0
    def input(self):
        self.roll=int(input("Enter roll no "))
        self.name=input("Enter name ")
        self.m1=float(input("Enter m1 "))
        self.m2=float(input("Enter m2 "))
        self.m3=float(input("Enter m3 "))

    def cal(self):
        self.total=self.m1+self.m2+self.m3
        self.per=self.total/3
        
    def show(self):
        print("roll ",self.roll)
        print("name ",self.name)
        print("m1 ",self.m1," m2 ",self.m2," m3 ",self.m3)
        print("total ",self.total," per ",self.per)
        

#object creation
s=student()
s.show()
s1=student()
s1.input()
s1.cal()
s1.show()

Output:

roll  0
name  
m1  0  m2  0  m3  0
total  0  per  0
Enter roll no 1001
Enter name Amit
Enter m1 98
Enter m2 97
Enter m3 95
roll  1001
name  Amit
m1  98.0  m2  97.0  m3  95.0
total  290.0  per  96.66666666666667
>>> 

Visitor Counter

0051049