Home » Python programs

Python | Create Employee class with some attributes and method

In this python program, we are creating an employee class with some attributes and methods to set and show employee data.
Submitted by Pankaj Singh, on November 16, 2018

In this program we are creating an Employee Class which contains some attributes and methods to set and show Employee Data.

Program:

# 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    : pankaj
Gender  : male
City    : delhi
Salary  : 55000

Entering data from the use

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:")
        self.__city = input("Enter City\t:")
        self.__salary = int(input("Enter Salary:"))
    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:     pankaj
Enter Gender:   male
Enter City:     delhi
Enter Salary:   55000
Id              : 1
Name    : pankaj
Gender  : male
City    : delhi
Salary  : 55000





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.