Python Variable & Data Type
Python Data Types
Learn Python Data Types with clear explanations, Python examples, and practical programming guidance.
What are data types in Python?
A data type describes the kind of value an object represents and the operations that can be performed on it. Python is dynamically typed, so you do not declare a variable's type before assigning a value. Python determines the type at runtime.
value = 10
print(value)
print(type(value))Output
<class 'int'>
In this example, value refers to an integer object. The type() function reports the object's class.
Built-in Python data types
Python includes built-in types for numbers, text, collections, Boolean values, binary data, and missing values:
| Category | Types | Beginner-friendly use |
|---|---|---|
| Numeric | int, float, complex | Whole numbers, decimal values, and complex calculations. |
| Sequence | str, list, tuple, range | Ordered text, collections, fixed records, and number sequences. |
| Set | set, frozenset | Unique values and set operations such as union and intersection. |
| Mapping | dict | Key-value data such as user profiles or configuration. |
| Boolean | bool | Truth values used in conditions. |
| Binary | bytes, bytearray, memoryview | Files, network data, and other raw bytes. |
| Special | NoneType | The absence of a value, represented by None. |
In Python, everything is an object. A variable is a name that refers to an object, and that object has a type.
Numeric types
int stores whole numbers, float stores decimal values, and complex stores a real part and an imaginary part. Python integers can grow as large as available memory allows.
whole = 42
decimal = 3.14
complex_value = 2 + 5j
print(type(whole))
print(type(decimal))
print(type(complex_value))Output
<class 'float'>
<class 'complex'>
Sequence types
Sequence types store items in an order. Lists are mutable, tuples are immutable, strings contain text, and ranges represent a sequence of numbers without storing every number separately.
items = ["Python", 3, True] # list
coordinates = (10, 20) # tuple
language = "Python" # string
numbers = range(1, 4) # range
print(items)
print(coordinates)
print(language)
print(list(numbers))Output
(10, 20)
Python
[1, 2, 3]
Use a list when values need to change, and use a tuple when the group of values should remain fixed.
Set types
A set stores unique values and can be changed. A frozenset also stores unique values, but it cannot be changed after creation. Sets do not support indexing, and their display order should not be relied upon.
languages = {"Python", "Java", "Python"}
fixed_languages = frozenset(["Python", "Java"])
print(len(languages))
print(type(fixed_languages))Output
<class 'frozenset'>
Dictionary type
A dictionary stores data as key-value pairs. Keys must be unique and hashable. Dictionaries preserve insertion order in modern Python versions and are useful for named data.
student = {
"name": "Asha",
"age": 20,
"course": "Python"
}
print(student["name"])
print(type(student))Output
<class 'dict'>
Boolean and None types
bool has only two values: True and False. The special value None means that a value is absent or not available. These values are commonly used in conditions and function results.
is_logged_in = True
profile = None
print(is_logged_in)
print(profile is None)Output
True
Binary data types
bytes is an immutable sequence of integers from 0 to 255. bytearray is its mutable counterpart, while memoryview provides access to binary data without making an unnecessary copy.
data = bytes([65, 66, 67])
mutable_data = bytearray(data)
mutable_data[0] = 90
print(data)
print(mutable_data)Output
bytearray(b'ZBC')
Checking and comparing types
Use type() when you need the exact type. Use isinstance() when you want to check whether an object belongs to a type or one of its subclasses.
age = 21
print(type(age) is int)
print(isinstance(age, int))Output
True
Type conversion
Type conversion creates a value of another type. Python performs some conversions automatically, such as converting an integer to a float during mixed arithmetic. You can request a conversion explicitly with functions such as int(), float(), str(), list(), and tuple().
price = 19
decimal_price = float(price)
number_text = str(price)
print(decimal_price)
print(number_text, type(number_text))Output
19 <class 'str'>
Conversions can fail or lose information. For example, int(7.9) removes the fractional part, and int("hello") raises ValueError.
Key points for beginners
- Python assigns types at runtime; variable declarations do not need a type keyword.
- Lists, dictionaries, sets, and bytearrays are mutable; strings, tuples, bytes, and frozensets are immutable.
- Use
isinstance()for readable type checks in application code. - Use
Noneto represent an intentionally missing value and compare it withis None. - Choose a data type based on the operations and behavior your program needs.
Further Reading
Continue learning with these related Python tutorials:
Continue learning
