Python Data Types for AI & ML: Types, Structures, Usage

Master Python data types, structures (lists, dicts), and dynamic typing essential for AI, ML, and data science. Understand and manipulate data efficiently.

Understanding Data in Python: Types, Structures, and Usage

In Python, data refers to the values that are stored, processed, and manipulated during program execution. These values come in various types, such as numbers, text, and collections, and can be organized using structures like lists and dictionaries to represent real-world logic and entities.

Python and Dynamic Typing

Python is a dynamically typed language. This means you do not need to explicitly declare the data type of a variable. The Python interpreter automatically infers the variable's type at runtime based on the value assigned to it.

x = 10        # x is automatically inferred as an integer
x = "Hello"   # Now x is a string

This flexibility allows for rapid development, but it's crucial to understand the types involved for effective programming and to avoid unexpected behavior.

Categories of Data Types in Python

Python data types can be broadly categorized into two main groups:

  • Primitive (Basic) Data Types: The fundamental data types built directly into the language.
  • Non-Primitive (Complex or Derived) Data Types: Data types that allow for grouping multiple values or represent more abstract concepts.

1. Primitive Data Types in Python

These are the building blocks for representing simple values.

TypeDescriptionExample
intInteger numbers (whole numbers)10, -3, 0
floatFloating-point numbers (decimal numbers)3.14, -0.5, 2.718
boolBoolean values (True or False)True, False
strString or textual data"hello", 'world'
NoneTypeRepresents the absence of a value (null)None

2. Non-Primitive Data Types in Python

These types are used to store collections of data or represent more complex information.

TypeDescriptionExampleMutability
listOrdered, mutable sequence of items[1, 2, 3], ["a", "b", "c"]Mutable
tupleOrdered, immutable sequence of items(1, 2, 3), ("a", "b", "c")Immutable
rangeRepresents a sequence of numbersrange(5) produces 0, 1, 2, 3, 4Immutable
dictUnordered collection of key-value pairs{"name": "John", "age": 25}Mutable
setUnordered collection of unique, hashable items{1, 2, 3}Mutable
frozensetImmutable version of a setfrozenset([1, 2, 3])Immutable
bytesImmutable sequence of bytesb'hello'Immutable
bytearrayMutable sequence of bytesbytearray(b'hello')Mutable
memoryviewA view of bytes or bytearray data without copyingmemoryview(b'abc')N/A

Mutability:

  • Mutable types can be changed after creation (e.g., lists, dictionaries, sets).
  • Immutable types cannot be changed after creation (e.g., strings, tuples, numbers, frozensets).

Variables as Data Holders

Variables are named references that store and point to data in Python. As Python is dynamically typed, the type of a variable is determined by the value assigned to it at runtime.

name = "Chinni"         # type: str
age = 21                # type: int
height = 5.7            # type: float
is_student = True       # type: bool

Inspecting Data Types

Python provides built-in functions to check and verify the type of a variable:

  • type(): Returns the type of an object.
  • isinstance(): Checks if an object is an instance of a particular class or a subclass thereof.
print(type(10))         # Output: <class 'int'>
print(type("hello"))    # Output: <class 'str'>
print(type([1, 2, 3]))   # Output: <class 'list'>

print(isinstance(10, int))     # Output: True
print(isinstance("hello", str)) # Output: True
print(isinstance([1, 2], list)) # Output: True
print(isinstance(10, str))     # Output: False

The isinstance() function is often preferred for type checking because it also accounts for inheritance.

Type Conversion in Python

Python supports converting data from one type to another using built-in functions, often referred to as "casting." This is useful when you need data in a specific format for processing or comparison.

  • Integer to String: str(number)
  • String to Integer: int(string_number) (raises ValueError if the string is not a valid integer)
  • Integer to Float: float(integer)
  • Float to Integer: int(float_number) (truncates the decimal part)
  • String to Float: float(string_float) (raises ValueError if the string is not a valid float)
  • To Boolean: bool(value) (e.g., 0 becomes False, non-empty strings/collections become True)
# String to Integer
num_str = "123"
num_int = int(num_str)
print(num_int + 7)  # Output: 130

# Integer to String
count = 42
count_str = str(count)
print("The count is: " + count_str) # Output: The count is: 42

# Float to Integer
pi_float = 3.14159
pi_int = int(pi_float)
print(pi_int)      # Output: 3

# Boolean conversion
print(bool(0))       # Output: False
print(bool(1))       # Output: True
print(bool(""))      # Output: False
print(bool("Python"))# Output: True

Example: Using Complex Data Structures

Python's non-primitive data types make it easy to model real-world data by combining different types and structures.

student = {
    "name": "Chinni",
    "age": 21,
    "is_enrolled": True,
    "courses": ["Python", "Java", "DBMS"],
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    }
}

print(f"Student Name: {student['name']}")
print(f"First Course: {student['courses'][0]}")
print(f"City: {student['address']['city']}")

In this example, the student dictionary effectively organizes various pieces of information:

  • name is a str.
  • age is an int.
  • is_enrolled is a bool.
  • courses is a list of str.
  • address is a nested dict containing str values.

This structure is ideal for managing and accessing related data efficiently.

Summary

  • Python data types are classified into primitive and non-primitive categories.
  • Python employs dynamic typing, meaning variable types are inferred at runtime, not explicitly declared.
  • Variables serve as named references to data, with their types determined by assigned values.
  • Use type() or isinstance() to inspect the data type of a variable.
  • Type conversion allows for transforming data between different types.
  • Complex data structures like lists and dicts are powerful tools for modeling real-world scenarios effectively.

SEO Keywords

  • Python data types
  • Primitive vs non-primitive data Python
  • Dynamic typing in Python
  • Python variable type inference
  • Python type conversion examples
  • Checking data type Python
  • Python list vs tuple
  • Python dictionary usage
  • Python isinstance() function
  • Python complex data structures

Interview Questions

  1. What are the main primitive data types in Python?
  2. Explain the concept of dynamic typing in Python. How does it differ from static typing?
  3. What is the difference between a list and a tuple in Python? When would you use one over the other?
  4. How do you check the data type of a variable in Python?
  5. What is the purpose of the isinstance() function? How is it different from type()?
  6. How can you convert between different data types in Python? Provide examples.
  7. What are Python dictionaries and when would you typically use them?
  8. Explain mutable vs. immutable data types in Python with clear examples.
  9. What are sets and frozensets in Python, and what are their key differences?
  10. How can complex data structures, such as nested dictionaries and lists, be used to model real-world data?