பைதான்: பட்டியல் செயல்பாடுகள் மற்றும் முறைமைகள்

Feb 25, 2025

Python ல் List Operations and Methods

List என்றால் என்ன?

  • Ordered மற்றும் mutable collection.
  • Multiple data types தாங்கலாம்:
    • Numbers (int)
    • Strings
    • Floats

Basic List Operations

List Creation

  • Syntax: my_list = [1, 2, 3, 'apple', 5.5]

Accessing Elements

  • Indexing: Elements are accessed via their index (starting from 0).
    • Example: my_list[0] gives 1
  • Slicing: Accessing a range of elements.
    • Example: my_list[0:3] gives [1, 2, 3]

Modifying a List

  • Lists are mutable, meaning we can change their values.
  • Example:
    • Original List: numbers = [10, 20, 30, 40]
    • Modify: numbers[1] = 25

List Concatenation

  • Concatenating two lists:
    • Example: list1 = [1, 2, 3]
    • list2 = [4, 5, 6]
    • Result: list1 + list2 gives [1, 2, 3, 4, 5, 6]

Checking Membership

  • Checking if an element exists in a list:
    • Example: banana in fruits
    • grape not in fruits

Iterating Over a List

  • Using a loop to print each element:
    • Example:
      for number in numbers:
          print(number)
      

List Methods

1. Append

  • Adding an element to the end of the list:
    • Example: fruits.append('cherry')

2. Insert

  • Inserting an element at a specific index:
    • Example: fruits.insert(1, 'mango')

3. Remove

  • Removing first occurrence of a value:
    • Example: fruits.remove('banana')

4. Pop

  • Removing and returning an element:
    • Example: number = numbers.pop(1) removes and returns the element at index 1.

5. Index

  • Finding the index of an element:
    • Example: fruits.index('banana')

6. Count

  • Counting occurrences of an element:
    • Example: fruits.count('apple')

7. Sort

  • Sorting the list:
    • Example: numbers.sort()

8. Reverse

  • Reversing the list:
    • Example: numbers.reverse()

9. Copy

  • Copying a list:
    • Example: copied_list = original_list.copy()

10. Clear

  • Clearing the list:
    • Example: numbers.clear()

Other Useful Functions

  • len(numbers): Length of the list.
  • min(numbers): Minimum value.
  • max(numbers): Maximum value.
  • sum(numbers): Sum of elements.
  • sorted(numbers): Returns sorted list.

Conclusion

  • List operations and methods are crucial in Python for data manipulation.
  • It is essential to understand these concepts for effective programming.