keip104

Introduction to Lists

  • Definition: Lists in Python are mutable ordered sequences consisting of elements of one or more data types.

  • Characteristics:

    • Enclosed in square brackets [].

    • Elements are separated by commas.

    • Can contain mixed data types (e.g., integers, floats, strings, tuples, other lists).

  • Example:

    # List of even numbers
    list1 = [2, 4, 6, 8, 10, 12]
    print(list1)  # Output: [2, 4, 6, 8, 10, 12]

Accessing Elements in a List

  • Indexing:

    • Lists are indexed starting from 0.

    • Use square brackets [] with the index to access elements.

    • Example:

      list1 = [2, 4, 6, 8, 10, 12]
      print(list1[0])  # Output: 2  (first element)
      print(list1[3])  # Output: 8  (fourth element)
  • Negative Indexing: Access from the end of the list using negative indices.

Lists are Mutable

  • Meaning: Contents of a list can be changed after creation.

  • Example:

    list1 = ['Red', 'Green', 'Blue', 'Orange']
    list1[3] = 'Black'
    print(list1)  # Output: ['Red', 'Green', 'Blue', 'Black']

List Operations

1. Concatenation

  • Combine lists using the + operator.

  • Example:

    list1 = [1, 3, 5, 7, 9]
    list2 = [2, 4, 6, 8, 10]
    list3 = list1 + list2  # Output: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

2. Repetition

  • Use * for replicating list elements.

  • Example:

    list1 = ['Hello']
    print(list1 * 4)  # Output: ['Hello', 'Hello', 'Hello', 'Hello']

3. Membership

  • Check presence using in and not in operators.

  • Examples:

    list1 = ['Red', 'Green', 'Blue']
    print('Green' in list1)  # Output: True
    print('Cyan' not in list1)  # Output: True

4. Slicing

  • Create new lists from existing lists by slicing.

  • Example:

    list1 = ['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow']
    print(list1[2:5])  # Output: ['Blue', 'Cyan', 'Magenta']

Traversing a List

  • Use loops to access list elements.

  • For Loop Example:

    for item in list1:
        print(item)

List Methods and Built-in Functions

  • Common Methods:

    • len(): Returns number of elements.

    • append(): Add items to the end.

    • extend(): Add multiple items.

    • insert(): Insert at specific position.

    • remove(): Remove first occurrence.

    • pop(): Remove item at specified position.

    • sort(): Sort elements in place.

    • reverse(): Reverses the list order.

Example of List Method Usage


  • myList = [10, 20, 30]
    myList.append(40)
    myList.sort()  # Sorts the list
    print(myList)  # Output: [10, 20, 30, 40]

Introduction to Dictionaries

  • Definition: Dictionaries are mutable mappings of key-value pairs.

  • Characteristics:

    • Unordered collection of items.

    • Keys must be unique and immutable.

    • Values can be of any data type.

  • Creating a Dictionary:

dict1 = {'Mohan': 95, 'Ram': 89}

- **Accessing Items**: Access values using their corresponding keys.
- **Example**:
  ```python
dict1['Mohan']  # Output: 95

Manipulating Dictionaries

  • Adding items:

dict1['Suhel'] = 92

- Modifying existing items:
  ```python
dict1['Ram'] = 90