Python is a dynamically typed language, meaning variables do not need explicit type declarations. Instead, the type is assigned automatically based on the value assigned to a variable. Python provides several built-in data types, categorized broadly into numeric, sequence, set, mapping, boolean, and binary types.


Numeric Types

Numeric data types store numerical values and support mathematical operations.

int (Integer)

  • Stores whole numbers (both positive and negative);
  • No limit to its size (except memory constraints);
  • Operations: +, -, *, // (floor division), % (modulus), ** (exponentiation);
x = 10
y = -200
z = 0

float (Floating Point Number)

  • Stores decimal numbers (floating-point values);
  • Operations: Same as int, but / always returns a float;
a = 3.14
b = -25.67
c = 1.0  # Even though it looks like an integer, it's a float.

complex (Complex Numbers)

  • Used for mathematical computations involving imaginary numbers;
  • Defined using j as the imaginary unit;
  • Operations: Supports addition, subtraction, multiplication, division, and exponentiation;
num1 = 2 + 3j
num2 = complex(4, -5)  # Equivalent to 4 - 5j

Sequence Types

Sequences store ordered collections of items.

str (String)

  • Immutable sequence of Unicode characters;
  • Defined using single ('), double ("), or triple (''' or """) quotes;
  • Operations:
    Concatenation (+): "Hello" + "World" → "HelloWorld"
    Repetition (*): "Hi" * 3 → "HiHiHi"
    Indexing (s[0]): First character
    Slicing (s[1:4]): Substring extraction
    String methods (upper(), lower(), split(), etc.)
s1 = "Hello, World!"
s2 = 'Python'
s3 = """This is a multi-line string"""

list (List)

  • Ordered, mutable collection of items (can hold different data types);
  • Defined using square brackets [];
  • Operations:
    Append (append()), insert (insert()), remove (remove()), and pop (pop()).
    Slicing (lst[1:3]).
    Sorting (sort() for homogeneous data).
lst = [10, "apple", 3.14, [1, 2, 3]]

tuple (Tuple)

  • Ordered, immutable collection of items;
  • Defined using parentheses ();
  • Operations:
    Indexing and slicing (similar to lists).
    Cannot modify (tpl[0] = 5 → Error).
    Useful for fixed data (e.g., coordinates, database records).
tpl = (1, 2, "Python", 3.5)

range (Range)

  • Generates sequences of numbers;
  • Commonly used in loops;
r = range(5)       # 0, 1, 2, 3, 4
r2 = range(1, 10, 2)  # 1, 3, 5, 7, 9

Set Types

Sets store unique, unordered items.

- set (Set)

  • Unordered collection of unique elements;
  • Defined using {} or set();
  • Operations:
    Add (add()), remove (remove() or discard()).
    Set operations: Union (|), intersection (&), difference (-).
my_set = {1, 2, 3, 4, 4, 2}  # {1, 2, 3, 4}

frozenset (Immutable Set)

  • Like set, but immutable;
  • Defined using frozenset();
  • Cannot modify elements;
fs = frozenset([1, 2, 3, 4])

Mapping Types

Mapping types store key-value pairs.

dict (Dictionary)

  • Stores key-value pairs;
  • Defined using {key: value};
  • Operations:
    Access value (my_dict["name"]).
    Add/modify (my_dict["age"] = 26).
    Remove (del my_dict["city"]).
    Dictionary methods (keys(), values(), items()).
my_dict = {"name": "Alice", "age": 25, "city": "New York"}

Boolean Type

  • Represents True or False (subtype of int, where True = 1, False = 0);
  • Used in conditional statements (if, while, etc.);
a = True
b = False

Binary Types

Binary data types store raw binary data.

bytes (Immutable Binary Data)

  • Immutable sequence of bytes;
b = b"Hello"

bytearray (Mutable Binary Data)

  • Mutable version of bytes;
ba = bytearray([65, 66, 67])

memoryview (Memory Sharing Object)

  • Provides memory-efficient handling of binary data;
mv = memoryview(b"abc")

NoneType

  • Represents the absence of a value (None);
  • Often used as a default value in functions;
x = None

Type Checking & Conversion

Python provides functions to check and convert data types.

Checking Data Type (type())

print(type(10))  # <class 'int'>
print(type("hello"))  # <class 'str'>

Type Conversion (Casting)

x = int("10")  # String to int
y = float("3.14")  # String to float
z = str(100)  # Int to string
l = list("abc")  # String to list → ['a', 'b', 'c']

Python provides a rich set of built-in data types, each suited for different scenarios. Understanding these types helps in writing efficient and bug-free code.