Python

Python Data Types

Data types define the type of value a variable can store. Python is a dynamically typed language, meaning data types are assigned at runtime. Understanding data types is essential because they determine what operations you can perform on the stored data.

1. Categories of Python Data Types

Python has several built-in data types categorized as follows:

  • Primitive Types
  • Collection Types
  • Boolean Type
  • None Type
  • Binary Types

2. Primitive / Basic Data Types

2.1 Integer (int)

Represents whole numbers (positive, negative, or zero). Python supports arbitrarily large integers.

x = 10
        y = -42
        

2.2 Floating Point (float)

Represents real numbers with decimal values.

pi = 3.14
        value = 10.0
        

2.3 Complex Numbers (complex)

Used in mathematics and scientific calculations. A complex number has a real and imaginary part.

z = 2 + 3j
        

2.4 String (str)

Represents a sequence of characters. Strings in Python are immutable, indexed, and sliceable.

name = "Talha"
        name[0]      # T
        name[1:3]    # al
        

3. Collection Data Types

3.1 List (list)

Ordered, mutable collection that can store different types of values.

fruits = ["apple", "banana", "mango"]
        fruits.append("orange")
        

3.2 Tuple (tuple)

Ordered and immutable. Ideal for fixed collections of data.

point = (10, 20)
        

3.3 Set (set)

Unordered collection of unique items. Very fast for membership tests.

nums = {1, 2, 3, 2}   # {1, 2, 3}
        

3.4 Dictionary (dict)

Stores data in key-value pairs. Mutable, fast, and extremely powerful.

user = {
            "name": "Talha",
            "role": "Android + Python Dev"
        }
        

4. Boolean Type (bool)

Represents truth values: True or False.

is_active = True
        is_admin = False
        

5. None Type (NoneType)

Represents the absence of a value. Commonly used for default parameters or empty placeholders.

value = None
        

6. Binary Types

  • bytes
  • bytearray
  • memoryview
b = b"hello"
        

7. Type Conversion (Casting)

Implicit Casting

x = 10       # int
        y = 2.5      # float
        z = x + y    # float
        

Explicit Casting

int("5")      # 5
        float("3.14") # 3.14
        str(100)      # "100"
        list("abc")   # ['a', 'b', 'c']
        

8. Checking Data Types

x = 10
        type(x)          # 
        isinstance(x, int)  # True
        

9. Do We Have Arrays in Python?

Python does not have native fixed-type arrays like C/Java, but offers alternatives:

  • list – Most common
  • array.array – C-style typed arrays
  • numpy.ndarray – High-performance scientific arrays

10. Interview Questions

Q1. Difference between list and tuple?

List: Mutable
Tuple: Immutable

Q2. Why are sets fast?

Because sets use hash tables internally.

Q3. Is string mutable?

No, strings are immutable in Python.

Q4. What is None used for?

Represents absence of a value (similar to null).