Python Data Type

Data Types

Python has various built-in data types categorized into following groups:

Numeric Types

int (Integer)

Whole numbers of unlimited length, positive or negative

 p = 123456789
 n = -987654321
 zero = 0
Note: No size limit in Python 3

float (Floating Point)

Numbers containing decimal points or exponential numbers

 pi = 3.14159
 temperature = -12.5
 scientific = 35.2e3  # 35200.0

complex

Numbers with real and imaginary parts (j suffix)

 c1 = 3+5j
 c2 = complex(4, -2)  # 4-2j

Sequence Types

str (String)

Immutable sequence of Unicode characters

 single = 'Single quoted string'
 double = "Double quoted string"
 multiline = """First line
 Second line"""
Strings cannot be modified after creation (immutable)

list

Mutable ordered collection of items

 numbers = [1, 2, 3, 4]
 mixed = [1, "two", 3.0, True]
Common Operations:
  • Indexing: numbers[0]
  • Slicing: numbers[1:3]
  • Modification: numbers.append(5)

tuple

Immutable ordered collection

 coordinates = (40.7128, -74.0060)
 colors = ('red', 'green', 'blue')
Use tuples for fixed data that shouldn’t change

Mapping Type

dict (Dictionary)

Unordered collection of key-value pairs

 person = {
     "name": "Alice",
     "age": 30,
     "city": "London"
 }
Characteristics:
  • Keys must be unique and immutable
  • Values can be any data type
  • Fast lookups using keys

Set Types

set

Unordered collection of unique elements

 unique_numbers = {1, 2, 3, 3}  # {1, 2, 3}
 vowels = set(['a', 'e', 'i', 'o', 'u'])

frozenset

Immutable version of set

 f_set = frozenset(['apple', 'banana', 'orange'])

Boolean Type

bool

Represents truth values: True or False

 is_valid = True
 is_active = False
Truthy/Falsy Values:
  • 0 → False
  • Empty collection → False
  • None → False

Binary Types

bytes

Immutable sequence of bytes (0-255)

 b = b'binary_data'
 empty_bytes = bytes(4)

bytearray

Mutable sequence of bytes

 ba = bytearray(b'hello')
 ba[0] = 104  # ASCII for 'h'

None Type

None

Special constant representing absence of value

 result = None
 def no_return():
     pass  # Implicitly returns None

Type Conversion

Explicit Conversion Examples
 int("25")    # → 25
 float(3)     # → 3.0
 str(True)    # → "True"
 list("abc")  # → ['a', 'b', 'c']
 tuple([1,2]) # → (1, 2)
 set([1,1,2]) # → {1, 2}

Type Checking

Using type()
 print(type(42))         # <class 'int'>
 print(type(3.14))       # <class 'float'>
 print(type("hello"))    # <class 'str'>
Using isinstance()
 isinstance(5, int)        # True
 isinstance(3.14, float)   # True
 isinstance([1,2], tuple)  # False
Important: Python uses dynamic typing – variables can change type during execution!