23 Jan Prog 2 (Vrushali)

 

1. Function Definition

  1. def main():
    • Defines the main() function where the logic of the program resides.

2. Creating Lists

  1. fruits = ["apple", "banana", "cherry", "date"]
    • Creates a list fruits containing four string elements: "apple", "banana", "cherry", and "date".
  2. print(f"Original List: {fruits}")
    • Prints the original list using an f-string for formatting.

3. Accessing Elements

  1. print(f"First element: {fruits[0]}")
    • Accesses and prints the first element of the list using index 0.
  2. print(f"Last element: {fruits[-1]}")
    • Accesses and prints the last element of the list using negative index -1.

4. Slicing Lists

  1. print(f"First two fruits: {fruits[:2]}")
    • Slices the first two elements of the list (from index 0 to 1) and prints them.
  2. print(f"Fruits from index 1 to end: {fruits[1:]}")
    • Slices the list from index 1 to the end and prints the result.

5. Adding Elements

  1. fruits.append("elderberry")
    • Adds "elderberry" to the end of the list using the append() method.
  2. print(f"List after appending 'elderberry': {fruits}")
    • Prints the list after adding "elderberry".
  3. fruits.insert(2, "fig")
    • Inserts "fig" at index 2 using the insert() method.
  4. print(f"List after inserting 'fig' at index 2: {fruits}")
    • Prints the list after inserting "fig" at index 2.

6. Removing Elements

  1. fruits.remove("banana")
    • Removes the first occurrence of "banana" from the list using the remove() method.
  2. print(f"List after removing 'banana': {fruits}")
    • Prints the list after removing "banana".
  3. popped_fruit = fruits.pop()
    • Pops (removes and returns) the last element from the list using the pop() method and stores it in popped_fruit.
  4. print(f"Popped fruit: {popped_fruit}")
    • Prints the fruit that was popped from the list.
  5. print(f"List after popping last element: {fruits}")
    • Prints the list after removing the last element.

7. Searching in a List

  1. print(f"Is 'apple' in the list? {'Yes' if 'apple' in fruits else 'No'}")
    • Checks if "apple" is present in the list using the in operator and prints the result as "Yes" or "No".

8. Sorting Lists

  1. fruits.sort()
    • Sorts the list fruits in ascending order using the sort() method.
  2. print(f"Sorted list: {fruits}")
    • Prints the sorted list.
  3. fruits.sort(reverse=True)
    • Sorts the list fruits in descending order by setting reverse=True in the sort() method.
  4. print(f"Reverse sorted list: {fruits}")
    • Prints the list sorted in reverse (descending) order.

9. Reversing a List

  1. fruits.reverse()
    • Reverses the order of elements in the list using the reverse() method.
  2. print(f"Reversed list: {fruits}")
    • Prints the list after it has been reversed.

10. Iterating Through a List

  1. print("Iterating through the list:")
    • Prints a message before iterating through the list.
  2. for fruit in fruits:
    • Starts a for loop to iterate through each element of the list fruits.
  3. print(fruit, end=", ")
    • Prints each element of the list followed by , (without moving to a new line after each print).
  4. print()
    • Prints a new line after finishing the iteration to improve the output format.

11. List Comprehension

  1. lengths = [len(fruit) for fruit in fruits]
    • Uses a list comprehension to create a new list lengths containing the length of each string in fruits.
  2. print(f"Lengths of each fruit name: {lengths}")
    • Prints the list lengths, which contains the lengths of the names of the fruits.

12. Combining Lists

  1. vegetables = ["carrot", "broccoli", "spinach"]
    • Creates a list vegetables containing three elements: "carrot", "broccoli", and "spinach".
  2. combined = fruits + vegetables
    • Combines the fruits list and vegetables list using the + operator to create a new list combined.
  3. print(f"Combined list of fruits and vegetables: {combined}")
    • Prints the combined list of fruits and vegetables.

13. Advanced Example: Flattening a Nested List

  1. nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
    • Creates a nested list nested_list containing sublists of integers.
  2. flattened_list = [item for sublist in nested_list for item in sublist]
    • Uses a nested list comprehension to flatten the nested list nested_list into a single list flattened_list.
  3. print(f"Flattened list: {flattened_list}")
    • Prints the flattened list, which contains all elements of the sublists combined into one list.

14. Counting Occurrences

  1. repeated_list = ["apple", "apple", "banana", "apple"]
    • Creates a list repeated_list containing multiple occurrences of "apple".
  2. count_apple = repeated_list.count("apple")
    • Counts how many times "apple" appears in repeated_list using the count() method and stores the result in count_apple.
  3. print(f"Number of 'apple' in the list: {count_apple}")
    • Prints the count of "apple" in the list.

15. Running the Main Function

  1. if _name_ == "_main_":
    • Checks if the script is being executed directly (not imported as a module).
  2. main()
    • Calls the main() function to execute the program.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe