Program to demonstrate the basic use of Class, Object and Module
Develop a program that reads the basic salary, various allowances and tax deduction from user and then calculate gross salary. Create a class TotalSalary stored in a module name totalsalary. Objects that are created from this class will simulates calculation of gross salary.
totalsalary.py
## # The TotalSalary class simulates calculation of gross salary. class TotalSalary: # The __init__ method accepts an argument for # the basic salary. def __init__(self, basic): self.__basic = basic # Calculate Total Salary def total_salary(self, amount): self.__basic += (self.__basic * amount)/100 # Deduction of tax on total salary def total_deduction(self, value): self.__basic -= (self.__basic * value)/100 # The __str__ method returns a string # indicating the object's state. def __str__(self): return 'Your Gross Salary is $' + format(self.__basic, ',.2f')salary.py
## # Python's program to calculate Gross Salary using OOPS. # This program demonstrates the totalsalary class. # with the __str__ method added to it. import totalsalary def main(): try: # Get the Basic Salary from user. basic_salary = float(input('Enter your Basic Salary: ')) # Create a TotalSalary object. gross_salary = totalsalary.TotalSalary(basic_salary) # Get the various allowance percent from user da_percent = float(input('Enter Dearness Allowances[DA] %: ')) ta_percent = float(input('Enter Travelling Allowance[TA] %: ')) hra_percent = float(input('Enter House Rent Allowance[HRA] %: ')) total_allowance = da_percent + ta_percent + hra_percent gross_salary.total_salary(total_allowance) # Get the tax deduction from user tax = float(input('Enter Professional Tax %: ')) gross_salary.total_deduction(tax) print(gross_salary) except ValueError: print("Error: Do not enter special character or string.") print("Only positive integer is allowed.\nAll values are required.") main()
The __init__ method has two parameter variables: self and basic. The basic parameter will accept the basic salary as an argument.
You do not directly call the __str__ method. It gets automatically called when you pass an object as an argument to the print function.
You do not directly call the __str__ method. It gets automatically called when you pass an object as an argument to the print function.
Sample output of above program.
C:\Python\programs>pep8 --first salary.py
C:\Python\programs>pep8 --first totalsalary.py
C:\Python\programs>python salary.py
Enter your Basic Salary: 60000
Enter Dearness Allowances[DA] %: 42
Enter Travelling Allowance[TA] %: 34
Enter House Rent Allowance[HRA] %: 20
Enter Professional Tax %: 19
Your Gross Salary is $95,256.00
C:\Python\programs>python salary.py
Enter your Basic Salary: 152000
Enter Dearness Allowances[DA] %: 5%
Error: Do not enter special character or string.
Only positive integer is allowed.
All values are required.
C:\Python\programs>
C:\Python\programs>pep8 --first totalsalary.py
C:\Python\programs>python salary.py
Enter your Basic Salary: 60000
Enter Dearness Allowances[DA] %: 42
Enter Travelling Allowance[TA] %: 34
Enter House Rent Allowance[HRA] %: 20
Enter Professional Tax %: 19
Your Gross Salary is $95,256.00
C:\Python\programs>python salary.py
Enter your Basic Salary: 152000
Enter Dearness Allowances[DA] %: 5%
Error: Do not enter special character or string.
Only positive integer is allowed.
All values are required.
C:\Python\programs>
2017-11-28T05:23:49+05:30
2017-11-28T05:23:49+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution