Python OOPs Concepts| Classes and Objects

Example:3
Python program to create a class named bank with attributes as accno, name and bal. initialize the data elements and display them?
Sol:

#class creation
class bank:
    accno=1001
    name="Amit"
    bal=5000
    def show(self):
        print("accno ",self.accno)
        print("name ",self.name)
        print("balance ",self.bal)
        

#object creation
e=bank()
print("Fetching Data Elements")
print(e.accno)
print(e.name)
print(e.bal)
print("Calling Memeber Function")
e.show()

Output:

Fetching Data Elements
1001
Amit
5000
Calling Memeber Function
accno 1001
name Amit
balance 5000
>>>

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
>>>