Python Data Types — Notes
1. Overview
- A data type defines the kind of value a variable holds.
- Python is dynamically typed — no need to declare the type explicitly.
Use
type()to check a variable’s type:x = 10 print(type(x)) # <class 'int'>
2. Basic Data Types
a) Numeric Types
Integer (
int)- Whole numbers (positive, negative, zero)
Example:
a = 5 b = -10
Floating Point (
float)- Numbers with decimal points
Example:
pi = 3.14
Complex Numbers (
complex)- Numbers with a real and imaginary part
Example:
c = 2 + 3j
b) Text Type
String (
str)- Sequence of characters inside quotes
Example:
name = "Python"
c) Boolean Type
Boolean (
bool)- Represents
TrueorFalse Example:
is_active = True
- Represents
3. Collection Data Types
a) List
- Ordered, changeable (mutable)
- Allows duplicate values
Example:
fruits = ["apple", "banana", "cherry"]
b) Tuple
- Ordered, unchangeable (immutable)
Example:
coordinates = (10.0, 20.0)
c) Set
- Unordered, unique items
Example:
unique_nums = {1, 2, 3}
d) Dictionary
- Key-value pairs, unordered
Example:
person = {"name": "Alice", "age": 25}
4. None Type
Nonerepresents absence of value
data = None
5. Type Conversion (Casting)
x = int("10") # str → int
y = float(5) # int → float
z = str(3.14) # float → str
6. Checking Type
print(type(5)) # <class 'int'>
print(isinstance(5, int)) # True