Python Course
Data Types in Python
In Python, every value has a specific data type. A data type tells Python what kind of value it is storing and how that value should behave.
Data types are a foundation topic. Once you understand them, writing Python becomes easier because you know what each kind of data can do.
What Data Types Control
- What operations are allowed (example: you can add numbers, but you cannot add a number to a list).
- How values are stored in memory.
- How Python interprets your code during execution.
- How data behaves inside conditions, loops, and functions.
Checking a Value’s Data Type
Python provides a built-in function called type() to check the data type of a value.
# Checking types using type()
a = 50
b = 12.9
c = "Hello"
print(type(a))
print(type(b))
print(type(c))
<class 'int'> <class 'float'> <class 'str'>
intmeans integer (whole number).floatmeans decimal number.strmeans string (text).
Numeric Data Types
Python supports multiple numeric types. The most common are:
- int for whole numbers
- float for decimal numbers
- complex for scientific use (less common for beginners)
# Numeric types
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))
10 12.75 (2+5j) <class 'int'> <class 'float'> <class 'complex'>
- Use
intfor counts, ages, quantities. - Use
floatfor prices, measurements, percentages. - Complex numbers are mainly used in advanced math and science.
String Data Type (str)
Strings represent text. They must be written inside single quotes or double quotes.
# Strings (text values)
platform = "Dataplexa"
topic = "Python"
print(platform)
print(topic)
print(platform.upper()) # convert to uppercase
print(len(topic)) # count characters
Dataplexa Python DATAPLEXA 6
upper()converts text to uppercase.len()gives the length of a string.- Strings are used everywhere: names, messages, labels, file paths.
Boolean Data Type (bool)
A boolean represents a truth value: True or False. Booleans are heavily used in conditions and decision-making logic.
# Boolean values and comparisons
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
True False True False
- Comparisons like
>and<return booleans. - Booleans help programs choose different paths using if/else.
List Data Type (list)
A list stores multiple values in a single variable. Lists are ordered and mutable (you can change them).
# Lists store multiple items
fruits = ["apple", "banana", "mango"]
print(fruits)
print(fruits[1]) # second item (index starts at 0)
fruits.append("orange") # add a new item
print(fruits)
['apple', 'banana', 'mango'] banana ['apple', 'banana', 'mango', 'orange']
- Lists keep the order of elements.
- Indexing starts at 0, so
fruits[1]is the second element. append()adds an item to the end of the list.
Tuple Data Type (tuple)
A tuple is similar to a list, but it is immutable (cannot be changed). Tuples are used when values should stay constant.
# Tuples are fixed collections
coordinates = (10, 20)
print(coordinates)
print(coordinates[0]) # first item
(10, 20) 10
- Tuples use parentheses
(). - You can read values, but you should not modify them.
- Common usage: coordinates, fixed settings, constant pairs.
Dictionary Data Type (dict)
A dictionary stores data as key-value pairs. You use keys to quickly access the related values.
# Dictionaries store key-value pairs
student = {
"name": "Alex",
"age": 21,
"course": "Python"
}
print(student["name"])
print(student["course"])
Alex Python
- Keys like
"name"and"course"are used to access values. - Dictionaries are common in APIs, JSON, settings, and structured data.
Set Data Type (set)
A set stores unique values. Duplicate values are automatically removed.
# Sets store unique values
nums = {10, 20, 30, 20, 10}
print(nums)
print(20 in nums)
print(99 in nums)
{10, 20, 30}
True
False- Duplicates are removed automatically.
- Sets are useful for membership checks like
value in set. - The order of set output may vary because sets are unordered.
Real-World Example: Order Summary Using Multiple Data Types
Real programs combine multiple data types. This example uses string, float, int, bool, and a calculated total.
# Order information (different data types)
item = "Bluetooth Speaker" # str (text)
price = 1499.99 # float (decimal)
quantity = 2 # int (count)
in_stock = True # bool (true/false)
# Total price calculation
total = price * quantity
# Output
print("Item:", item)
print("Quantity:", quantity)
print("In Stock:", in_stock)
print("Total Price:", total)
Item: Bluetooth Speaker Quantity: 2 In Stock: True Total Price: 2999.98
Why This Example Matters
itemis text, because product names are strings.priceis decimal, so it uses float.quantityis a count, so it uses int.in_stockis a True/False state, so it uses bool.totalis computed using numeric values.
Practice
Which function is used to check a value’s data type in Python?
Which data type stores only 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 stores key-value pairs?
Which data type automatically removes duplicate values?
Quick Quiz
Which data type should be used for a price like 49.99?
Which data type is best for storing a user profile with name and age?
Which type is best when you want only unique values?
Which type is used for storing text?
Data Types Summary Table
| Data 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/False logic |
| list | items = [1, 2, 3] | Ordered, changeable collection |
| tuple | point = (10, 20) | Ordered, fixed collection |
| dict | user = {"name":"Alex"} | Key-value structured data |
| set | nums = {1, 2, 3} | Unique values collection |
Recap:
You learned the major Python data types, how they behave, how to check types using type(),
and how different types work together in real programs.
Next up: Type Conversion and Type Casting.