DEV Community

Ghazal Abdulraheem
Ghazal Abdulraheem

Posted on

Building a Tip Calculator

Day 2 of #100DaysOfCode — Tip Calculator Project

Today’s project was quite fun and very interesting because it is something very relatable. Imagine going out for a hangout with friends, colleagues, or business associates, and you feel like tipping the waiter or waitress while still splitting the bill equally among everyone involved. That is the problem today’s lesson focused on solving.

The lesson was centered around Data Types, Numbers, and Operators in Python. You may be wondering: What are data types, and how do they work in Python?

In Python, there are four basic data types covered in today’s lesson:

  1. String
  2. Boolean
  3. Integer
  4. Float

Let’s dive deeper into each data type.


String

As mentioned in Day 1, a string is a collection of characters usually wrapped in either single quotes ('') or double quotes ("").

print("Hello")
Enter fullscreen mode Exit fullscreen mode

The double quotes wrapped around "Hello" tell the Python interpreter that the data should be treated as a string.

Strings allow us to perform different kinds of data manipulation such as:

  • Accessing characters by index
  • Counting characters
  • Combining strings
  • Converting other data types into strings

Let’s explore some common string operations.

Substring

This gives us the ability to extract characters from a string by specifying the index position of the character.

print("Hello"[2])
Enter fullscreen mode Exit fullscreen mode

Output:

l
Enter fullscreen mode Exit fullscreen mode

Note: You can get the last character of a string using a negative index.

print("Hello"[-1])
Enter fullscreen mode Exit fullscreen mode

Output:

o
Enter fullscreen mode Exit fullscreen mode

Concatenation

You can combine two strings together using the + operator.

last_name = "Temmy"
first_name = " Olajide"

full_name = last_name + first_name

print(full_name)
Enter fullscreen mode Exit fullscreen mode

Output:

Temmy Olajide
Enter fullscreen mode Exit fullscreen mode

str()

The str() function allows us to convert non-string data into a string.

admission = "ADM" + str(123)

print(admission)
Enter fullscreen mode Exit fullscreen mode

Output:

ADM123
Enter fullscreen mode Exit fullscreen mode

Integer

An integer is a data type used to represent whole numbers without decimal points.

age = 25
Enter fullscreen mode Exit fullscreen mode

If you are working with large numbers and want them to be more readable, Python allows the use of underscores.

amount_spent = 345_829_202
Enter fullscreen mode Exit fullscreen mode

Just like strings, you can also convert non-integer data into integers using the built-in int() function.

print(int("123"))
Enter fullscreen mode Exit fullscreen mode

Output:

123
Enter fullscreen mode Exit fullscreen mode

Float

A float is a data type used to represent numbers with decimal points.

application_fee = 1500.89
Enter fullscreen mode Exit fullscreen mode

Python also provides a built-in float() function to convert integers or strings into floats.

print(float(1500))
Enter fullscreen mode Exit fullscreen mode

Output:

1500.0
Enter fullscreen mode Exit fullscreen mode

type()

The type() function is used to check the data type of a value or variable in Python.

Examples:

print(type("Hello"))   # <class 'str'>
print(type(True))      # <class 'bool'>
print(type(123))       # <class 'int'>
print(type(13.68))     # <class 'float'>
Enter fullscreen mode Exit fullscreen mode

Mathematical Operations

One of the reasons Python is widely used in fields like Data Science, Cybersecurity, and Engineering is because of how easy it is to work with mathematical operations.

Addition

3 + 5
Enter fullscreen mode Exit fullscreen mode

Subtraction

5 - 2
Enter fullscreen mode Exit fullscreen mode

Multiplication

4 * 2
Enter fullscreen mode Exit fullscreen mode

Division

6 / 2
Enter fullscreen mode Exit fullscreen mode

Modulo

The modulo operator returns the remainder after division.

5 % 2
Enter fullscreen mode Exit fullscreen mode

Output:

1
Enter fullscreen mode Exit fullscreen mode

Power

Used for exponents or superscripts.

2 ** 2
Enter fullscreen mode Exit fullscreen mode

Output:

4
Enter fullscreen mode Exit fullscreen mode

Just like regular mathematics, Python follows the PEMDAS rule:

  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

When writing mathematical expressions, it is important to understand operator precedence to avoid unexpected results.


Number Manipulation and F-Strings in Python

Number Manipulation

Python allows shorthand operations when working with numbers.

Instead of writing:

score = 0
score = score + 1
Enter fullscreen mode Exit fullscreen mode

You can write:

score += 1
Enter fullscreen mode Exit fullscreen mode

The same applies to other operators such as:

-= 
*= 
/= 
Enter fullscreen mode Exit fullscreen mode

F-Strings

F-strings are a clean and readable way to insert variables or expressions directly into strings using curly braces {}.

name = "Temmy"

print(f"My name is {name}")
Enter fullscreen mode Exit fullscreen mode

Output:

My name is Temmy
Enter fullscreen mode Exit fullscreen mode

Tip Calculator Project

Now let’s build our Tip Calculator application.

Tip Calculator Algorithm

amount_spent = float(input("Enter the amount spent: $"))
tip = float(input("How much do you want to tip? 5, 10, 12, or 15: "))
num_of_people = float(input("How many people are splitting the bill? "))

tip_calc = (tip / 100) * amount_spent

total_bill = (amount_spent + tip_calc) / num_of_people

print(f"Each person should pay: ${total_bill}")
Enter fullscreen mode Exit fullscreen mode

This simple project calculates:

  1. The total tip amount
  2. Adds it to the original bill
  3. Splits the total equally among everyone involved

A fun and practical beginner-friendly Python project

Top comments (0)