Variable and Data Types
A variable is a name that refers to a value. Python is dynamically typed, so you don't declare a type: the type belongs to the value.
name = "PrepVerse" # str
year = 2025 # int
pi = 3.14159 # float
is_ready = True # bool
Built-in data types
| Category | Types | Example |
|---|---|---|
| Text | str | "hello" |
| Numeric | int, float, complex | 10, 3.5, 2+3j |
| Sequence | list, tuple, range | [1, 2], (1, 2), range(3) |
| Mapping | dict | {"a": 1} |
| Set | set, frozenset | {1, 2, 3} |
| Boolean | bool | True |
| None | NoneType | None |
Checking a type
x = 42
print(type(x)) # <class 'int'>
print(isinstance(x, int)) # True
Mutable vs immutable
- Immutable:
int,float,str,tuple,frozenset,bool - Mutable:
list,dict,set
a = [1, 2, 3]
b = a # same list object
b.append(4)
print(a) # [1, 2, 3, 4]
Type conversion
int("10") # 10
str(10) # "10"
float("3.5") # 3.5
list("abc") # ['a', 'b', 'c']
Loading Comments...