Python with AI Tutorial · Chapter 6 of 48
Python data types describe what kind of value a variable holds: text (str), whole numbers (int), decimals (float), true/false flags (bool), ordered collections (list, tuple), key-value pairs (dict) and unique items (set). Knowing the type tells you what operations are allowed. This chapter introduces each built-in type with a business example.
The built-in data types at a glance
Python groups its built-in types into a few families. You will meet every one of them in later chapters; here is the map so you know where each fits.
| Family | Type | Example value | Typical business use |
|---|---|---|---|
| Text | str |
"Invoice #1041" |
Names, IDs, addresses |
| Numeric | int |
250 |
Quantities, counts |
| Numeric | float |
19.99 |
Prices, percentages |
| Numeric | complex |
3+4j |
Engineering maths (rare) |
| Boolean | bool |
True |
Paid or unpaid, active flag |
| Sequence | list |
[120, 340, 90] |
Monthly sales, editable rows |
| Sequence | tuple |
(19.5, 77.2) |
Fixed records, coordinates |
| Sequence | range |
range(1, 13) |
Month numbers in a loop |
| Mapping | dict |
{"North": 45000} |
Lookups, JSON from APIs |
| Set | set |
{"Delhi", "Pune"} |
Unique customers, deduplication |
| None | NoneType |
None |
Missing or not-yet-known value |
Checking a type with type()
You never declare types, so type() is how you find out what Python chose. It is the first thing to check when a calculation behaves strangely, because the usual culprit is a number stored as text.
invoice_no = "INV-2041"
amount = 15750
tax_rate = 0.18
is_paid = True
notes = None
for value in (invoice_no, amount, tax_rate, is_paid, notes):
print(repr(value), "is", type(value).__name__)
Output: 'INV-2041' is str 15750 is int 0.18 is float True is bool None is NoneType
Text: str
A string is any text inside single or double quotes. Strings can be joined with +, repeated with *, measured with len() and sliced with square brackets. They are immutable: methods such as .upper() return a new string rather than changing the original.
customer = "Meridian Logistics"
print(len(customer))
print(customer.upper())
print(customer[:8])
print("Ref: " + customer + " / 2026")
Output: 18 MERIDIAN LOGISTICS Meridian Ref: Meridian Logistics / 2026
Numbers: int and float
An int is a whole number with no size limit. A float has a decimal point and follows the same 64-bit rules as Excel, including tiny rounding artefacts. Mixing the two in arithmetic produces a float. Division with / always gives a float; // gives the whole-number part.
units = 7
unit_price = 349.50
subtotal = units * unit_price
print(subtotal, type(subtotal))
print(1000 / 8, 1000 // 8)
print(round(0.1 + 0.2, 2))
Output: 2446.5 <class 'float'> 125.0 125 0.3
Without round(), 0.1 + 0.2 prints 0.30000000000000004. That is not a Python bug; it is how binary floating point works everywhere. The numbers chapter shows the decimal module for exact money maths.
Booleans: bool
A bool is either True or False (capitalised). Comparisons produce booleans, and if statements consume them. Under the hood True equals 1 and False equals 0, which is why sum() over a list of booleans counts how many are true.
overdue = [True, False, True, True, False]
print("Overdue invoices:", sum(overdue))
print(15750 > 10000)
print("paid" == "Paid")
Output: Overdue invoices: 3 True False
Collections: list, tuple, dict and set
Collections hold many values under one name. A list is ordered and changeable, a tuple is ordered and fixed, a dict maps keys to values, and a set keeps only unique items with no order. Each has its own chapter later; here is how they differ in one example.
monthly_sales = [45000, 52000, 48500] # list: can grow
monthly_sales.append(61000)
hq_location = (19.0760, 72.8777) # tuple: fixed pair
region_sales = {"North": 45000, "South": 52000} # dict: key -> value
region_sales["West"] = 48500
cities = {"Mumbai", "Pune", "Mumbai", "Delhi"} # set: duplicates vanish
print(monthly_sales)
print(hq_location[0])
print(region_sales["South"])
print(len(cities))
Output: [45000, 52000, 48500, 61000] 19.076 52000 3
None: the absence of a value
None is Python's way of saying "nothing here yet". A function that does not return anything returns None, and it is the usual placeholder for an optional field such as a discount that has not been set. Always test for it with is None, not == None.
discount = None
if discount is None:
print("No discount applied")
else:
print("Discount:", discount)
Output: No discount applied
Mutable versus immutable
Some types can be changed in place (mutable): list, dict, set. Others cannot (immutable): str, int, float, bool, tuple. This matters when two variables point at the same object. Changing a shared list changes it for both names; changing a string never affects anyone else, because a new string is created.
team_a = ["Asha", "Rahul"]
team_b = team_a # same list, two names
team_b.append("Meera")
print(team_a)
label_a = "Draft"
label_b = label_a
label_b = label_b + " v2" # new string
print(label_a, "|", label_b)
Output: ['Asha', 'Rahul', 'Meera'] Draft | Draft v2
If you want an independent copy of a list, use team_b = team_a.copy() or list(team_a). The lists chapter covers this in depth.
Give the assistant a small dataset as plain text and ask which Python type each field should become and why; this is the exact decision you make when loading real data.
Here is one row from a CSV export of our orders: 10452,"Wireless mouse",3,19.99,TRUE,"2026-03-14","". The columns are order_id, product, quantity, unit_price, shipped, order_date, discount_code. For each column, tell me the best Python data type to store it in (str, int, float, bool, date, None) and explain in one sentence why. Then write Python code that converts this row from strings into those types.
Ask for a side-by-side explanation of list, tuple, dict and set using a scenario from your own work, then verify the code runs.
I manage a small warehouse. Show me the same data (5 products with stock counts) stored as a Python list, a tuple, a dictionary and a set. For each version explain in two sentences what I gain and what I lose, and give one realistic warehouse task where that type is the best choice. Make all code runnable in Python 3.12.
Common mistakes
- Treating
"250"as a number. Check withtype()and convert withint(). - Writing
trueorfalsein lowercase. Python only recognisesTrueandFalse. - Expecting
0.1 + 0.2 == 0.3to beTrue. Useround()or thedecimalmodule for money. - Assuming
team_b = team_acopies a list. It only creates a second name for the same list. - Using a list where the values must never change (for example a fixed set of coordinates). Use a tuple.
Exercise
Create one variable of each type: a product name (str), stock quantity (int), unit cost (float), a flag for whether it is discontinued (bool), a list of three supplier names, a tuple of (min_stock, max_stock) and a dict mapping the product name to its stock. Print the type of each on one line each.
Show answer
product = "Steel bracket"
stock_qty = 420
unit_cost = 3.75
discontinued = False
suppliers = ["Kumar Metals", "Apex Steel", "Navi Forge"]
stock_limits = (100, 1000)
stock_by_product = {product: stock_qty}
for item in (product, stock_qty, unit_cost, discontinued, suppliers, stock_limits, stock_by_product):
print(type(item).__name__)
Output: str int float bool list tuple dict
Related chapters
FAQ
How many data types does Python have?
Python has around a dozen built-in types you use daily: str, int, float, complex, bool, list, tuple, range, dict, set, frozenset, bytes and NoneType. Libraries add more, such as pandas DataFrames and datetime objects.
What is the difference between a list and a tuple?
Both are ordered sequences, but a list can be changed after creation (append, remove, sort) while a tuple is fixed. Use tuples for records that should not change and lists for data you build up or edit.
How do I check the data type of a variable in Python?
Call type(variable). For example type(19.99) returns class float. To test for a type in code, use isinstance(value, float), which also handles subclasses correctly.
Working with spreadsheets too? Ready-made Excel, Google Sheets and Power BI templates are at NextGenTemplates.com.
Chapter 6 of 48 · Python with AI: all 48 chapters



