1. Introduction
Every value in Python has a data type that determines what operations can be performed on it and how it behaves in memory. Python provides a rich set of built-in data types out of the box — numbers, text, sequences, mappings, sets, booleans, and a special 'no value' type — so you rarely need to build your own basic containers.
Cricket analogy: Every player on a scorecard has a defined role type—batsman, bowler, all-rounder, wicketkeeper—just as every Python value has a type (int, str, list, dict) that determines what "moves" it can make.
Understanding data types is essential because it affects everything from arithmetic and comparisons to how data is stored, copied, and iterated. Choosing the right data type for a job (e.g., a set for uniqueness checks, a dict for lookups) is a core Python skill.
Cricket analogy: Choosing a set to track "players who've scored a century this season" avoids duplicate entries, just as a scorer uses a tally sheet (dict) for quick lookups of runs per batsman rather than scanning the whole scorecard.
2. Syntax
integer_num = 10 # int
float_num = 10.5 # float
complex_num = 2 + 3j # complex
string_val = "Python" # str
bool_val = True # bool
list_val = [1, 2, 3] # list
tuple_val = (1, 2, 3) # tuple
dict_val = {"a": 1} # dict
set_val = {1, 2, 3} # set
none_val = None # NoneType3. Explanation
Numeric Types
int represents whole numbers of arbitrary precision, float represents decimal numbers using double precision, and complex represents numbers with a real and imaginary part (a + bj). Arithmetic between an int and a float automatically produces a float.
Cricket analogy: A run tally (int) stays whole, but a batting average like 47.83 (float) has decimals, and just as adding a whole run to a fractional strike rate yields a float, mixing int and float in Python always upgrades to float.
Sequence Types
str, list, and tuple are ordered sequences that support indexing and slicing. Strings and tuples are immutable — once created, their contents cannot change — while lists are mutable and support in-place modification such as append() or item assignment.
Cricket analogy: A fixed batting lineup announced before the toss is like a tuple—unchangeable once set—while a bowling rotation (list) can be adjusted mid-over with a substitution, just as lists support append() and tuples don't.
Mapping and Set Types
dict stores key-value pairs for fast lookup by key, and is mutable. set stores an unordered collection of unique, hashable values, useful for membership tests and removing duplicates; it is also mutable, though its elements must themselves be immutable.
Cricket analogy: A dict mapping player names to jersey numbers gives instant lookup, while a set of "teams that have won the World Cup" ensures no team is listed twice, both mutable and updatable each season.
Boolean and None
bool has only two values, True and False, and is technically a subclass of int (True == 1, False == 0). NoneType has a single value, None, representing the intentional absence of a value.
Cricket analogy: A "won toss" flag being True (which secretly equals 1) is like a scorer treating "yes" as literally 1 run added to a boolean tally, while an unscheduled match shows None—no result recorded at all, not even zero.
Use type(obj) to check an object's exact type, or isinstance(obj, cls) when you want to allow subclasses too (e.g., isinstance(True, int) returns True because bool subclasses int).
Common gotcha: sets and dict keys must contain only hashable (generally immutable) values — trying to put a list inside a set raises TypeError: unhashable type: 'list', because lists are mutable and cannot be hashed.
4. Example
integer_num = 10
float_num = 10.5
complex_num = 2 + 3j
string_val = "Python"
bool_val = True
list_val = [1, 2, 3]
tuple_val = (1, 2, 3)
dict_val = {"a": 1, "b": 2}
set_val = {1, 2, 3}
none_val = None
for value in [integer_num, float_num, complex_num, string_val, bool_val,
list_val, tuple_val, dict_val, set_val, none_val]:
print(value, "->", type(value))5. Output
10 -> <class 'int'>
10.5 -> <class 'float'>
(2+3j) -> <class 'complex'>
Python -> <class 'str'>
True -> <class 'bool'>
[1, 2, 3] -> <class 'list'>
(1, 2, 3) -> <class 'tuple'>
{'a': 1, 'b': 2} -> <class 'dict'>
{1, 2, 3} -> <class 'set'>
None -> <class 'NoneType'>6. Key Takeaways
- Python's core built-in types are int, float, complex, str, bool, list, tuple, dict, set, and NoneType.
- str, tuple, and frozenset are immutable; list, dict, and set are mutable.
- bool is a subclass of int, so True behaves like 1 and False like 0 in arithmetic.
- Only hashable (typically immutable) objects can be set elements or dict keys.
- Use type() for exact type checks and isinstance() when subclassing matters.
- None is the single value of NoneType, representing 'no value'.
Practice what you learned
1. Which of these built-in types is mutable?
2. What is the type of the value produced by `2 + 3j`?
3. What does `type(True)` return in Python?
4. Why does `{[1, 2]: "a"}` raise a TypeError?
5. What is the result of `True + True` in Python?
6. Which type represents an ordered, immutable sequence of items?
Was this page helpful?
You May Also Like
Variables in Python
Learn how Python variables work as dynamically-typed name bindings, how to create and reassign them, and the rules for naming them correctly.
Type Casting in Python
How to convert values between Python data types explicitly with functions like int(), float(), and str(), and how implicit coercion works.
Lists in Python
A mutable, ordered sequence type in Python used to store collections of items, with a deep dive into mutation, aliasing, and common list operations.
Dictionaries in Python
Python's key-value mapping type, covering hashable keys, insertion order guarantees (3.7+), common dict methods, and typical gotchas.
Strings in Python
Python's immutable text sequence type, covering string creation, indexing, slicing, immutability, and formatting.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics