Day 14: Python Data Types and Data Structures

DevOps Learning

Data Types:

  1. What Are Data Types?

    • Data types classify or categorize data items.

    • They determine what operations can be performed on specific data.

  2. Python’s Data Types:

    • Numeric Types:

      • Integer (whole numbers)

      • Complex (imaginary numbers)

      • Float (decimal numbers)

    • Sequential Types:

      • String (text)

      • Lists (ordered collection of data)

      • Tuples (immutable collection of Python objects)

    • Boolean Type:

      • Represents True or False values
    • Set Type:

      • Unordered collection of unique elements
    • Dictionaries:

      • Unordered key-value pairs (used for optimization)
  3. Example:

    • Imagine a toolbox:

      • Numeric tools (measuring tape, calculator)

      • Sequential tools (to-do list, shopping list)

      • Boolean tool (light switch: on/off)

      • Set tool (deck of cards)

      • Dictionary tool (phone book)

Data Structures:

  1. Organizing Data Efficiently:

    • Data structures help organize data for efficient access.

    • They form the foundation of any program.

  2. Examples:

    • Lists: Ordered collection of data (items can be of different types).

    • Tuples: Immutable collection of Python objects (similar to lists but unchangeable).

    • Dictionaries: Unordered key-value pairs (used for optimization).

Let's use this knowledge in our own practice.

Task 1: Exploring Numeric Types

Objective: Understand numeric data types and perform basic operations.

  1. Create a Python Script:

    • Open your favorite code editor (e.g., Visual Studio Code).

    • Create a new Python script (e.g., numeric_types.py).

  2. Explore Numeric Types:

    • In your script, define variables for different numeric types:

        my_integer = 42
        my_float = 3.14
        my_complex = 2 + 3j
      
  3. Perform Operations:

    • Add, subtract, multiply, and divide these numeric values.
  4. Run Your Script:


Task 2: Working with Strings

Objective: Explore string data type and its methods.

  1. Create a New Script:

    • In the same directory, create another Python script (e.g., string_operations.py).
  2. Define a String:

    • In your script, define a string variable:

        my_string = "Hello, Python!"
      
  3. String Methods:

    • Use string methods to:

      • Convert to uppercase.

      • Replace “Python” with “World.”

      • Check if it starts with “Hello.”

  4. Print the Modified String:

    • Print the modified string after applying the methods.
  5. Run Your Script:


Task 3: Lists and List Operations

Objective:

Learn about lists and perform list operations.

  1. Create a New Script:

    • Create a Python script (e.g., list_operations.py).
  2. Define a List:

    • In your script, define a list of your favorite fruits:Python

        my_fruits = ["apple", "banana", "orange"]
      
  3. List Operations:

    • Append “grape” to the list.

    • Print the length of the list.

    • Access the second fruit (“banana”).

    • Sort the list alphabetically.

  4. Print the Modified List:

    • Print the list after performing the operations.
  5. Run Your Script:


Task 4: Tuples and Immutability

Objective:

Explore tuples and understand their immutability.

  1. Create a New Script:

    • Create a Python script (e.g., tuple_immutable.py).
  2. Define a Tuple:

    • In your script, define a tuple of your favorite colors:

        my_colors = ("red", "green", "blue")
      
  3. Access Tuple Elements:

    • Print the second color (“green”).
  4. Try to Modify the Tuple:

    • Tries to modify the tuple (e.g., my_colors[0] = "yellow").
  5. Run Your Script:


Task 5: Dictionaries and Key-Value Pairs

Objective: Learn about dictionaries and their key-value pairs.

  1. Create a New Script:

    • Create a Python script (e.g., dictionary_key_value.py).
  2. Define a Dictionary:

    • In your script, define a dictionary of book genres:

        book_genres = {
            "fantasy": "Harry Potter",
            "mystery": "Sherlock Holmes",
            "science fiction": "Dune"
        }
      
  3. Access Dictionary Values:

    • Print the book title for the “fantasy” genre.
  4. Run Your Script:

Thank you for reading😉.