Skip to main content

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​

CategoryTypesExample
Textstr"hello"
Numericint, float, complex10, 3.5, 2+3j
Sequencelist, tuple, range[1, 2], (1, 2), range(3)
Mappingdict{"a": 1}
Setset, frozenset{1, 2, 3}
BooleanboolTrue
NoneNoneTypeNone

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']