Skip to content

Latest commit

History

History
87 lines (70 loc) 路 3.16 KB

Days-7-10.md

File metadata and controls

87 lines (70 loc) 路 3.16 KB

Day 7: Lists

Lists are versatile and commonly used data structures in Python.

  • List Basics:

    • Lists are ordered collections of items.
    • They can contain elements of different data types.
  • Accessing List Elements:

    • You can access elements in a list by their index, starting from 0.

    • Example:

      fruits = ["apple", "banana", "cherry"]
      print(fruits[0])  # Access the first item ("apple").
      print(fruits[1])  # Access the second item ("banana").
  • List Methods:

    • Lists have various built-in methods, such as append(), insert(), remove(), and pop(), to manipulate their contents.

Day 8: Tuples

Tuples are similar to lists, but they have unique characteristics.

  • Tuple Basics:

    • Tuples are also ordered collections of elements.
    • They are immutable, meaning their contents cannot be changed after creation.
  • Accessing Tuple Elements:

    • You can access elements in a tuple using indexing, just like lists.

    • Example:

      coordinates = (3, 4)
      print(coordinates[0])  # Access the first element (3).
      print(coordinates[1])  # Access the second element (4).
  • Use Cases:

    • Tuples are often used for collections of related values, such as coordinates or date and time components.

Day 9: Dictionaries

Dictionaries are powerful data structures for organizing data.

  • Dictionary Basics:

    • Dictionaries consist of key-value pairs.
    • They are unordered, and keys must be unique.
  • Accessing Dictionary Values:

    • You can access values in a dictionary using their keys.

    • Example:

      person = {
          "name": "Alice",
          "age": 25,
          "city": "New York"
      }
      print(person["name"])  # Access the value associated with the "name" key.
      print(person["age"])   # Access the value associated with the "age" key.
  • Dictionary Methods:

    • Dictionaries offer methods like keys(), values(), and items() to work with their keys and values.

Day 10: More on Data Structures

Learn advanced techniques for manipulating data structures.

  • List Manipulation:

    • You can add, remove, and modify elements in lists.

    • Example:

      fruits = ["apple", "banana", "cherry"]
      fruits.append("orange")  # Add an element to the end of the list.
      fruits.remove("banana")  # Remove a specific element from the list.
      fruits[0] = "kiwi"       # Modify an element in the list.
  • Dictionary Manipulation:

    • Dictionaries can be extended, updated, and modified.

    • Example:

      person = {"name": "Alice", "age": 25, "city": "New York"}
      person["country"] = "USA"  # Add a new key-value pair to the dictionary.
      del person["city"]         # Remove a key-value pair from the dictionary.
      person["age"] = 26         # Modify the value associated with the "age" key.

Understanding and effectively using data structures like lists, tuples, and dictionaries is crucial in Python programming. Practice with these examples to gain confidence in managing and organizing data in your Python code.