Representative class in Python: This is an example of Python class and item, here we are going to execute an Employee class in Python?
Python – worker class code:
# employee class code in Python
# class definition
class Employee:
__id=0
__name=""
__gender=""
__city=""
__salary=0
# function to set data
def setData(self,id,name,gender,city,salary):
self.__id=id
self.__name = name
self.__gender = gender
self.__city = city
self.__salary = salary
# function to get/print data
def showData(self):
print("Id\t\t:",self.__id)
print("Name\t:", self.__name)
print("Gender\t:", self.__gender)
print("City\t:", self.__city)
print("Salary\t:", self.__salary)
# main function definition
def main():
#Employee Object
emp=Employee()
emp.setData(1,'pankaj','male','delhi',55000)
emp.showData()
if __name__=="__main__":
main()
Output:
Id : 1
Name : kishan
Gender : male
City : Raipur
Salary : 75000
Another Example: Getting values from the capacity:
class Employee:
__id=0
__name=""
__gender=""
__city=""
__salary=0
def setData(self):
self.__id=int(input("Enter Id:\t"))
self.__name = input("Enter Name:\t")
self.__gender = input("Enter Gender:\t")
self.__city = input("Enter City:\t")
self.__salary = int(input("Enter Salary:\t"))
def showData(self):
print("Id\t\t:",self.__id)
print("Name\t:", self.__name)
print("Gender\t:", self.__gender)
print("City\t:", self.__city)
print("Salary\t:", self.__salary)
def main():
#Employee Object
emp=Employee()
emp.setData()
emp.showData()
if __name__=="__main__":
main()
Output:
Enter Id: 1
Enter Name: kishan
Enter Gender: male
Enter City: raipur
Enter Salary: 75000
Id : 1
Name : kishan
Gender : male
City : raipur
Salary : 75000