(Day 02) Task : Data Types & Tip Calculator Project :-

Table of contents
- Topics Covered :-
- What are Data Types in Python?
- Two Categories of Data Types :-
- What is a TypeError in Python?
- Mathematical (Arithmetic) Operators in Python :-
- Mathematical (Assignment) Operators in Python :-
- Flooring a Number :-
- Rounding a Number :-
- f-Strings :-
- Project: Tip Calculator (Python) :-
- Project :-

Topics Covered :-
Primitive data types in Python:
int
,float
,str
, andbool
.
Type Error & Type Checking with
type()
.Type conversion using
str()
,int()
,float()
.Arithmetic operations (
+
,-
,*
,/
,//
,%
,**
).Number rounding using
round()
, floor division//
.F-strings for formatting output.
What are Data Types in Python?
In Python (and all programming languages), data types are categories of data that tell the interpreter what kind of value is being stored or used.
Just like we organize different things in real life (books, numbers, names), Python organizes data into types.
Two Categories of Data Types :-
Primitive Data Types :-
These are the most basic types of data in Python — simple and not made up of other types.
Primitive types are immutable, meaning once created, their value cannot be changed.
int
– Integer :Represents whole numbers (no decimal point).
Positive or negative.
✅ Example:
int age = 25 int bornyear = 1999
float
– Floating Point Number :Represents decimal numbers (numbers with a point).
Used for more precise values like measurements or money.
✅ Example:
float price = 99.99 float height = 5.8
str
– String :Represents text (a sequence of characters).
Written in quotes (
'single'
or"double"
).✅ Example:
name = "Alice" greeting = 'Hello, world!'
bool
– Boolean :Represents True or False values.
Used in decision making (conditions, comparisons, etc.)
✅ Example:
is_raining = True is_sunny = False
Type | Description | Example |
int | integer | 10 , -3 , 1000 |
float | Decimal number | 3.14 , -0.5 |
str | String (text) | "Hello" , '123' |
bool | Boolean (True/False) | True , False |
Non-Primitive Data Types :-
They can store multiple values and are built using primitive types.
These types help organize, group, or relate data together.
List :
A list is a collection of items that are ordered and changeable (mutable) contains [].
You can store different types of data in a list.
Think of it like a grocery list — you can add, remove, or change items.
✅ Example:
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple fruits.append("orange") # Add item
Tuple :
A tuple is like a list, but it is unchangeable (immutable) and contains parenthesis.
Use it when you want to store a fixed set of items that shouldn’t be modified.
✅ Example:
fruits = ("apple", "banana", "cherry") print(fruits[0]) # Output: apple
Set :
A set is an unordered collection of unique items.
It automatically removes duplicates.
Great for storing things like tags, unique words, etc.
✅ Example:
numbers = {1, 2, 3, 3, 4} print(numbers) # Output: {1, 2, 3, 4} numbers.add(5) # Add new unique item
Dictionary :
A dictionary stores data in key-value pairs.
You use the key to access the value, just like looking up a word in a dictionary.
✅ Example:
student = {"name": "Alice", "age": 21 ,"grade": "A"} print(student["name"]) # Output: Alice
Type | Description | Example |
list | Ordered, mutable collection | [1, 2, 3] , ["a", "b"] |
tuple | Ordered, immutable collection | (1, 2, 3) |
set | Unordered, unique items | {1, 2, 3} |
dict | Key-value pairs | {"name": "John", "age": 25} |
What is a TypeError in Python?
These errors occur when you are using the wrong data type , such as passing an integer to a function that expects a string.
Example :
len(12345)
: This function will show a type error because you can only give the string to len() function , it will refuse to work and give you a TypeError if you give it a number (Integer).
To Cure this Problem(error) :-
1. (Type Checking) —> Check the data types using type()
.
You can check the data type of any value or variable in python using the type() function.
✅ Example: print(type("abc")).
Write out 4 type checks to print all 4 primitive data types :
2. (Type Conversion) —> Change one data type to other data type.
It refers to converting data into different data types using functions like float() , int() or str().
Types of Type Conversion:
Implicit Type Conversion (Python does it automatically) :-
Python automatically converts smaller data types to larger ones during operations.
a = 5 # int b = 2.0 # float result = a + b # Python converts `a` to float print(result) # 7.0 print(type(result)) # <class 'float'>
Explicit Type Conversion (You do it manually):-
You intentionally change the data type using built-in functions.
num = "10" num_int = int(num) # 10 num_float = float(num) # 10.0
Example :-
Mathematical (Arithmetic) Operators in Python :-
Python supports several arithmetic operators to perform basic math calculations. Here’s a breakdown of each:
+
Addition :Adds two numbers.
-
Subtraction :Subtracts the second number from the first.
*
Multiplication :Multiplies two numbers.
/
Division :Divides the first number by the second.
Always returns a float, even if the division is exact.//
Floor Division :Divides and rounds down to the nearest whole number.
%
Modulus (Remainder) :Returns the remainder of division.
**
Exponentiation (Power) :Raises the first number to the power of the second.
x = 10 + 5 print(x) # Output: 15 x = 10 - 3 print(x) # Output: 7 x = 4 * 3 print(x) # Output: 12 x = 10 / 2 print(x) # Output: 5.0 x = 10 // 3 print(x) # Output: 3 x = 10 % 3 print(x) # Output: 1 x = 2 ** 3 # 2 raised to the power 3 print(x) # Output: 8
Mathematical (Assignment) Operators in Python :-
1. =
Basic Assignment :
Assigns the value on the right to the variable on the left.
x = 10 print(x) # Output: 10
2. +=
Add and Assign :
Adds a value to a variable and updates the variable with the new value.
x = 5 x += 3 # same as x = x + 3 print(x) # Output: 8
3. -=
Subtract and Assign :
Subtract value from variable and updates the variable with the new value.
x = 10 x -= 4 # x = x - 4 print(x) # Output: 6
4. *=
Multiply and Assign :
Multiply value to the variable and updates the variable with the new value.
pythonCopyEditx = 6 x *= 2 # x = x * 2 print(x) # Output: 12
5. /=
Divide and Assign :
Divide variable and updates the variable with the new value.
Always results in a
float
.x = 12 x /= 3 # x = x / 3 print(x) # Output: 4.0
6. //=
Floor Division and Assign :
Divides and rounds down to the nearest whole number.
pythonCopyEditx = 10 x //= 3 # x = x // 3 print(x) # Output: 3
7. %=
Modulus and Assign :
Gives the remainder after division.
pythonCopyEditx = 10 x %= 3 # x = x % 3 print(x) # Output: 1
8. **=
Exponentiation and Assign :
Raises the number to the power of another.
pythonCopyEditx = 2 x **= 3 # x = x ** 3 print(x) # Output: 8
Flooring a Number :-
We can floor a number or remove all decimal places using the int() function which converts a floating point number (with decimal places) into an integer (whole number).
int(3.738492) # Becomes 3
Rounding a Number :-
If we want to round a decimal number to the nearest whole number using the traditional mathematical way, where anything over .5 rounds up and anything below rounds down. Then you can use the python
round()
function.round(3.738492) # Becomes 4 round(3.14159) # Becomes 3 round(3.14159, 2) # Becomes 3.14
f-Strings :-
In Python, we can use f-strings to insert a variable or an expression into a string.
age = 12 print(f"I am {age} years old"). # Will output I am 12 years old.
Project: Tip Calculator (Python) :-
What It Does:
Calculates how much each person should pay when splitting a bill, including tip.
We're going to build a tip calculator.
Ex :-
If the bill was $150.00, split between 5 people, with 12% tip.
Each person should pay:
(150.00 / 5) * 1.12 = 33.6
After formatting the result to 2 decimal places = 33.60
Sample Output :-
Welcome to the tip calculator!
What was the total bill? $150
How much tip would you like to give? 10, 12, or 15? 12
How many people to split the bill? 5
Each person should pay: $33.6
Project :-
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS