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:
- String
- Boolean
- Integer
- 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")
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])
Output:
l
Note: You can get the last character of a string using a negative index.
print("Hello"[-1])
Output:
o
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)
Output:
Temmy Olajide
str()
The str() function allows us to convert non-string data into a string.
admission = "ADM" + str(123)
print(admission)
Output:
ADM123
Integer
An integer is a data type used to represent whole numbers without decimal points.
age = 25
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
Just like strings, you can also convert non-integer data into integers using the built-in int() function.
print(int("123"))
Output:
123
Float
A float is a data type used to represent numbers with decimal points.
application_fee = 1500.89
Python also provides a built-in float() function to convert integers or strings into floats.
print(float(1500))
Output:
1500.0
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'>
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
Subtraction
5 - 2
Multiplication
4 * 2
Division
6 / 2
Modulo
The modulo operator returns the remainder after division.
5 % 2
Output:
1
Power
Used for exponents or superscripts.
2 ** 2
Output:
4
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
You can write:
score += 1
The same applies to other operators such as:
-=
*=
/=
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}")
Output:
My name is Temmy
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}")
This simple project calculates:
- The total tip amount
- Adds it to the original bill
- Splits the total equally among everyone involved
A fun and practical beginner-friendly Python project
Top comments (0)