Are you looking to apply the basic knowledge you've just gained about Python? In that case, this article will help you get started with using Python to solve a simple problem. This tutorial focuses on the fundamentals you need to know to begin using Python to solve problems.

Just as in the English language, when we are asked to interpret a text, I believe that all of us, before even attempting to solve the issue, we first do:

  1. Consult the text.
  2. Read paragraph by paragraph.
  3. Only then proceed to write a more appropriate response.

Because understanding what the text is really about is important.

The same holds in programming. When someone consults you, it's crucial that you at least grasp the basics of what the problem entails before solving it.

Next, we will demonstrate how we can find solutions to one of the problems mentioned earlier. So, let's get started.

Using Python to Create a Salary Calculator

Let's face it; we all know what a salary is, but at the same time, we may not fully understand how it is calculated. Therefore, before creating the program, do some research on what a Salary is.

Tip - Research the subject to understand what it is and get an idea of what to do.

Here's what we found after some internet research about what a salary is:

"Income is the amount of money earned by any person or company for performing a certain task within a specified period."

Income categories:

  1. Remuneration: Remuneration is the amount of money given to employees daily.
  2. Salary: A specific sum of money paid annually to an employee by the employer is called a salary.

Working days in a year:

In a normal year of 365 days, there are 52 weeks. The standard number of hours per day is 8, giving us weekly working hours of 40. So, based on this, we have:

Total working hours in a year = 52 * 40

Total working hours in a year = 2080

Now, if you work 7.5 hours a day, then we have:

Total working hours in a year = 52 * 37.5

Total working hours in a year = 1950

How is Salary Calculated?

If we talk about the standard workweek, an employee works almost 40 hours per week.

Hourly:

If you work 40 hours a week:

Hourly salary = Annual salary / 2080

Daily:

If you work 8 hours a day:

Daily salary = Hourly salary * 8

Weekly:

If you want to calculate the weekly salary using the annual salary, subject to the formula below:

Weekly salary = Annual salary / 52

Monthly:

Subject to the equation below to calculate your monthly salary:

Monthly salary = Gross Annual Salary / 12

Annually:

Here we will calculate the annual income from the monthly salary:

Annual Salary = Monthly Salary * 12

Creating the Program Logic

Now that we know what a Salary is and how to calculate it, we can start creating the program logic.

Note: Begin thinking of a solution to the problem using the acquired information.

Note: This is a simple program that doesn't involve a database. If you had a database, I would advise you to start designing your database first.

Problem Solution

You will use Python to program a solution to your problem. Here's the solution I came up with using the information above:

# Constant values
year = 365
weeks = 52

# User inputs
name = input('What is your full name?: ')
hours_per_day = int(input('How many hours do you work per day?: '))
working_days = int(input('How many days do you work per week?: '))
annual_salary = float(input('What is your Annual Salary?: '))

# Calculate total annual working hours
weekly_working_hours = hours_per_day * working_days
total_working_hours_in_a_year = weeks * weekly_working_hours

# Hourly Salary
hourly_salary = annual_salary / total_working_hours_in_a_year

# Daily Salary
daily_salary = hourly_salary * 8

# Weekly Salary
weekly_salary = annual_salary / 52

# Monthly Salary
monthly_salary = annual_salary / 12

# Annual Salary
# To calculate the annual income from the monthly salary:
annual_salary = monthly_salary * 12

print("\nEmployee Information\n")
print('Name: ' + name)
print('Hours worked per day: ' + str(hours_per_day))
print('Days worked per week: ' + str(working_days))
print("\nSalary Information\n")
print('Hourly Salary: $ {:,.2f}'.format(hourly_salary))
print('Daily Salary: $ {:,.2f}'.format(daily_salary))
print('Weekly Salary: $ {:,.2f}'.format(weekly_salary))
print('Monthly Salary: $ {:,.2f}'.format(monthly_salary))
print('Total Gross Salary: $ {:,.2f}'.format(annual_salary))
print("\n")

Conclusion

Note: Try to create your solution based on the information we obtained above, and remember that you should learn to interpret problems and not simply copy solutions.

Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source

Article link:http://pybeginners.com/article/applying-your-python-knowledge-a-guide-to-solving-programming-problems/