Python Data Types
Every value in Python has a data type that determines what kind of data it stores and which operations can be performed on it. Because Python assigns types dynamically, you do not need to declare data types explicitly when creating variables.
Common Built-in Data Types
- Text Type:
str(strings such as"Hello"or'Python'). - Numeric Types:
int(whole numbers),float(decimal numbers), andcomplex(complex numbers like2 + 3j). - Sequence Types:
list(ordered, mutable),tuple(ordered, immutable), andrange(number sequences). - Mapping Type:
dict(key-value pairs). - Set Types:
set(unordered collection of unique items) andfrozenset(immutable set). - Boolean Type:
bool(TrueorFalse). - None Type:
NoneType(None, representing the absence of a value).
Examples of Core Data Types
text_val = "Python" # str
int_val = 25 # int
float_val = 99.5 # float
bool_val = True # bool
list_val = ["Python", "Java", "C"] # list
tuple_val = (10, 20, 30) # tuple
set_val = {1, 2, 3} # set
dict_val = {"course": "Python", "level": "Beginner"} # dict
empty_val = None # NoneType
Checking a Value's Data Type
Use the type() function to see the class (data type) of any object or variable:
items = ["pen", "notebook"]
price = 49.99
print(type(items)) # <class 'list'>
print(type(price)) # <class 'float'>
Tip: Choose the data type that matches how you plan to use the data—use
list when items change, tuple for fixed records, and dict when looking up values by key.