Data Types in Python
Understand Python data types: dynamic typing, built-in types, mutability, type checking, comparisons, and validations.
What Are Data Types?
Data types define what kind of value a variable can hold. Python is dynamically typed, meaning types are determined at runtime.
1. Dynamic Typing
Variables can change their data type.
Built-in Data Types
Python has several built-in data types. Some are immutable and some are mutable.
1. String (str)
Text data.
2. Integer (int)
Whole numbers.
3. Float
Decimal numbers.
4. Boolean (bool)
True or False.
5. NoneType
Represents absence of value.
Collection Data Types
Collection types store multiple values inside a single variable.
1. List
Ordered, mutable collection.
2. Tuple
Ordered, immutable collection.
3. Dictionary (dict)
Key-value pairs.
4. Set
Unordered unique values.
5. Function
Callable object.
Printing the Data Type
Python provides the built-in function `type()` to inspect data types.
1. Using type()
Check the type of a value.
Type Checking & Validation
Use isinstance() for reliable type checking instead of comparing type() directly.
1. Using isinstance()
Preferred type checking method.
2. Checking Lists, Dictionaries, and Functions
Check specific types properly.