A data type defines:
- The type of value stored in a variable
- The operations that can be performed on it
a = 10Here:
a→ variable10→ integer valueint→ data type
Python is a dynamically typed language. This means:
- You do not declare data types explicitly
- A variable can be reassigned to a different type
a = 10
print(a)
a = 10.5
print(a)✅ Valid in Python ❌ Not allowed in statically typed languages like C or C++
Use the built-in type() function.
a = 10
print(type(a)) # <class 'int'>
a = 10.5
print(type(a)) # <class 'float'>Python stores objects in memory, and variables reference them.
To check the memory address, use id().
a = 10
print(id(a))📌 This helps understand immutability in Python.
Python has 14 built-in data types:
- int
- float
- complex
- bool
- str
- bytes
- bytearray
- range
- list
- tuple
- set
- frozenset
- dict
- NoneType
Integers are whole numbers (positive, negative, or zero).
a = 10 # ✅ int
a = -100 # ✅ int
a = 10.5 # ❌ not int- Python 2 had
intandlong - Python 3 has only
int - Large numbers are handled automatically
a = 1234293864389456348756348745896745879
print(type(a)) # <class 'int'>Python supports integers in four number systems:
| System | Base | Allowed Digits | Prefix |
|---|---|---|---|
| Binary | 2 | 0, 1 | 0b |
| Decimal | 10 | 0–9 | (default) |
| Octal | 8 | 0–7 | 0o |
| Hexadecimal | 16 | 0–9, A–F | 0x |
a = 0b1010
print(a) # 10a = 0o1010
print(a) # 520a = 0xFACE
print(a) # 64206| Function | Converts to |
|---|---|
bin() |
Binary |
oct() |
Octal |
hex() |
Hexadecimal |
a = 0xFACE
print(bin(a))
print(oct(a))
print(hex(a))
print(a) # Decimal (default)a = -15
print(bin(a))A float is a number with a decimal point.
a = 1.45
print(type(a)) # <class 'float'>a = 14
print(type(a)) # int
b = 34.56
print(type(b)) # floatPython supports exponential notation using e or E.
a = 1.2e3
print(a) # 1200.0
print(type(a)) # floatMeaning:
1.2e3 = 1.2 × 10³
Floats cannot be represented in binary, octal, or hexadecimal form.
a = 1.5
bin(a) # ❌ TypeError❌ Also invalid:
a = 0b1.1
a = 0o1.1
a = 0x1.1Next: Complex, Boolean, String, Type Casting & Immutability in Python