
Python Course
Data Types in Python
Every piece of information in a Python program has a type. A number is different from a word. A true/false value is different from a list of items. Python uses data types to keep track of what kind of information each variable holds and what you are allowed to do with it.
Understanding data types is one of the most important foundations in Python. Once you know the types available and when to use each one, writing programs becomes much more natural and predictable.
How to Check a Data Type
Python has a built-in function called type() that tells you what kind of data a variable is holding. You can use this at any point in your code to check or confirm the type.
a = 50
b = 12.9
c = "Hello"
print(type(a))
print(type(b))
print(type(c))
intmeans it is a whole number.floatmeans it is a decimal number.strmeans it is text (string).
Numbers in Python
Python has three numeric types. The two you will use most often are int for whole numbers and float for numbers with decimal points. The third type, complex, is used in advanced mathematics and science.
x = 10 # int — whole number
y = 12.75 # float — decimal number
z = 2 + 5j # complex — advanced numeric type
print(x)
print(y)
print(z)
print(type(x))
print(type(y))
print(type(z))
- Use
intfor things you count — ages, quantities, scores, years. - Use
floatfor things you measure — prices, distances, percentages. complexnumbers appear in scientific and engineering programs. You will not need them often as a beginner.
Text — the String Type
A string is simply text. Any time you store a word, a sentence, a name, or any collection of characters, you are working with a string. Strings must be written inside quotation marks — either single or double.
platform = "Dataplexa"
topic = "Python"
print(platform)
print(topic)
print(platform.upper()) # convert to uppercase
print(len(topic)) # count the characters
upper()converts every letter to uppercase.len()counts how many characters are in the string.- Strings are used for names, messages, addresses, labels, file paths — almost everything that involves text.
True or False — the Boolean Type
A boolean is one of the simplest data types. It can only hold one of two values: True or False. Booleans are used whenever a program needs to make a decision — is the user logged in? Is an item in stock? Is the password correct?
is_logged_in = True
has_access = False
print(is_logged_in)
print(has_access)
print(5 > 3) # comparison returns True
print(10 < 2) # comparison returns False
- Comparisons using
>,<,==always produce a boolean result. - Booleans are the foundation of all decision-making in Python programs.
- The values
TrueandFalsemust start with a capital letter in Python.
Lists — Storing Multiple Items
A list lets you store several values inside a single variable. The items are kept in order and you can add, remove, or change them at any time. Lists are one of the most commonly used data structures in Python.
fruits = ["apple", "banana", "mango"]
print(fruits)
print(fruits[1]) # second item — counting starts from 0
fruits.append("orange") # add a new item to the end
print(fruits)
- List items are numbered starting from 0, so
fruits[0]is "apple" andfruits[1]is "banana". append()adds a new item to the end of the list.- Lists keep their order. The first item you put in stays first.
Tuples — Fixed Collections
A tuple is similar to a list, but once you create it, you cannot change its contents. Tuples are used when the data should stay the same throughout the program — like GPS coordinates, RGB colour values, or a fixed set of settings.
coordinates = (10, 20)
print(coordinates)
print(coordinates[0]) # first item
- Tuples use round brackets
()instead of square brackets. - You can read values from a tuple but cannot modify, add, or remove items.
- Because tuples cannot change, they are slightly faster and safer than lists for fixed data.
Dictionaries — Key and Value Pairs
A dictionary stores information in pairs. Each piece of data has a key and a value. You use the key to find the value instantly, just like looking up a word in a real dictionary to find its meaning.
student = {
"name": "Alex",
"age": 21,
"course": "Python"
}
print(student["name"])
print(student["course"])
- Keys like
"name"and"course"must be unique within the dictionary. - You access a value by writing the key inside square brackets.
- Dictionaries are used everywhere — user profiles, API responses, configuration settings, and more.
Sets — Unique Values Only
A set stores a collection of values where every item must be unique. If you try to add the same value twice, Python quietly ignores the duplicate. Sets are useful when you need to remove duplicates from data or check whether a value exists in a collection.
nums = {10, 20, 30, 20, 10}
print(nums)
print(20 in nums)
print(99 in nums)
- The duplicates 20 and 10 were automatically removed — only one copy of each is kept.
- The
inkeyword checks whether a value exists in the set. - Sets are unordered, so the position of items can vary each time you print them.
Real World Example — Order Summary
Real programs use several data types at the same time. This example shows how a shopping order might be stored using different types working together.
item = "Bluetooth Speaker" # str
price = 1499.99 # float
quantity = 2 # int
in_stock = True # bool
total = price * quantity
print("Item:", item)
print("Quantity:", quantity)
print("In Stock:", in_stock)
print("Total Price:", total)
Why Each Type Was Chosen
itemis a string because product names are text.priceis a float because it has decimal places.quantityis an int because you cannot buy half an item.in_stockis a bool because it is either available or not.totalis calculated from numeric values and becomes a float automatically.
Data Types at a Glance
| Type | Example | Used For |
|---|---|---|
| int | count = 10 | Whole numbers |
| float | price = 49.99 | Decimal numbers |
| complex | z = 2 + 3j | Scientific computing |
| str | name = "Dataplexa" | Text values |
| bool | is_valid = True | True or False logic |
| list | items = [1, 2, 3] | Ordered, changeable collection |
| tuple | point = (10, 20) | Ordered, fixed collection |
| dict | user = {"name":"Alex"} | Key-value pairs |
| set | nums = {1, 2, 3} | Unique values only |
Practice
Which built-in function tells you the data type of a variable?
Which data type can only hold True or False?
Which data type is ordered and can be changed after creation?
Which data type is ordered but cannot be changed after creation?
Which data type automatically removes duplicate values?
Quick Quiz
Which data type should you use for a price like 49.99?
Which type is best for storing a user profile with a name and age?
Which type is best when you need only unique values?