Data Types and Structures in Python

Overview

This lesson dives into the data types and structures that Python offers, enabling you to store, manipulate, and access data efficiently. By the end of this lesson, you will understand the different data types, their characteristics, and how to use built-in data structures like lists, tuples, sets, and dictionaries.

Introduction

Python supports various data types, classified into mutable and immutable types. Understanding these types and their corresponding data structures is crucial for effective data manipulation and storage in Python.

Basic Data Types

  • Integer (int): Represents whole numbers, positive or negative, without decimals. Example: x = 5
  • Float (float): Represents real numbers, containing one or more decimals. Example: y = 3.14
  • String (str): Represents sequences of Unicode characters, enclosed in single, double, or triple quotes. Example: name = "John Doe"
  • Boolean (bool): Represents logical values, either True or False.

Data Structures

  1. List
    • Ordered and mutable collection of items.
    • Items can be of different data types.
    • Created using square brackets []. Example: fruits = ['apple', 'banana', 'cherry']
  2. Tuple
    • Ordered and immutable collection of items.
    • Once a tuple is created, you cannot change its values.
    • Created using parentheses (). Example: coordinates = (10.0, 20.0)
  3. Set
    • Unordered collection of unique items.
    • Mutable but does not allow mutable items.
    • Created using curly braces {} or the set() function. Example: colors = {'red', 'green', 'blue'}
  4. Dictionary
    • Unordered collection of key-value pairs.
    • Keys must be unique and immutable types, whereas values can be of any type.
    • Created using curly braces {} with key-value pairs. Example: person = {'name': 'John', 'age': 30}

Operations on Data Structures

  • Accessing Elements: Use indexing for lists and tuples (fruits[0]), keys for dictionaries (person['name']), and iteration for sets.
  • Modifying Elements: Lists and dictionaries can be modified (fruits[0] = 'strawberry', person['age'] = 31), but tuples and sets cannot be directly modified (though you can add or remove items from sets).
  • Iterating Over Data Structures: Use loops to iterate over elements (for fruit in fruits: print(fruit)).

Comprehensions

Python supports a powerful feature called comprehensions, which provides a concise way to create lists, sets, and dictionaries from existing iterables.

  • List Comprehension: [x for x in range(10)] creates a list of numbers from 0 to 9.
  • Set Comprehension: {x for x in 'hello world' if x not in 'aeiou'} creates a set of consonants in the string “hello world”.
  • Dictionary Comprehension: {x: x**2 for x in range(5)} creates a dictionary where each key is a number and its value is the square of the number.

Conclusion

Understanding Python’s data types and structures is fundamental for programming tasks involving data storage, manipulation, and retrieval. By mastering lists, tuples, sets, and dictionaries, you’ll be well-equipped to handle complex data-centric challenges in Python.

Additional Resources