23 Jan Prog 2 (Vrushali)
1. Function Definition
def main():
- Defines the
main()
function where the logic of the program resides.
- Defines the
2. Creating Lists
fruits = ["apple", "banana", "cherry", "date"]
- Creates a list
fruits
containing four string elements:"apple"
,"banana"
,"cherry"
, and"date"
.
- Creates a list
print(f"Original List: {fruits}")
- Prints the original list using an f-string for formatting.
3. Accessing Elements
print(f"First element: {fruits[0]}")
- Accesses and prints the first element of the list using index
0
.
- Accesses and prints the first element of the list using index
print(f"Last element: {fruits[-1]}")
- Accesses and prints the last element of the list using negative index
-1
.
- Accesses and prints the last element of the list using negative index
4. Slicing Lists
print(f"First two fruits: {fruits[:2]}")
- Slices the first two elements of the list (from index
0
to1
) and prints them.
- Slices the first two elements of the list (from index
print(f"Fruits from index 1 to end: {fruits[1:]}")
- Slices the list from index
1
to the end and prints the result.
- Slices the list from index
5. Adding Elements
fruits.append("elderberry")
- Adds
"elderberry"
to the end of the list using theappend()
method.
- Adds
print(f"List after appending 'elderberry': {fruits}")
- Prints the list after adding
"elderberry"
.
- Prints the list after adding
fruits.insert(2, "fig")
- Inserts
"fig"
at index2
using theinsert()
method.
- Inserts
print(f"List after inserting 'fig' at index 2: {fruits}")
- Prints the list after inserting
"fig"
at index2
.
- Prints the list after inserting
6. Removing Elements
fruits.remove("banana")
- Removes the first occurrence of
"banana"
from the list using theremove()
method.
- Removes the first occurrence of
print(f"List after removing 'banana': {fruits}")
- Prints the list after removing
"banana"
.
- Prints the list after removing
popped_fruit = fruits.pop()
- Pops (removes and returns) the last element from the list using the
pop()
method and stores it inpopped_fruit
.
- Pops (removes and returns) the last element from the list using the
print(f"Popped fruit: {popped_fruit}")
- Prints the fruit that was popped from the list.
print(f"List after popping last element: {fruits}")
- Prints the list after removing the last element.
7. Searching in a List
print(f"Is 'apple' in the list? {'Yes' if 'apple' in fruits else 'No'}")
- Checks if
"apple"
is present in the list using thein
operator and prints the result as"Yes"
or"No"
.
- Checks if
8. Sorting Lists
fruits.sort()
- Sorts the list
fruits
in ascending order using thesort()
method.
- Sorts the list
print(f"Sorted list: {fruits}")
- Prints the sorted list.
fruits.sort(reverse=True)
- Sorts the list
fruits
in descending order by settingreverse=True
in thesort()
method.
- Sorts the list
print(f"Reverse sorted list: {fruits}")
- Prints the list sorted in reverse (descending) order.
9. Reversing a List
fruits.reverse()
- Reverses the order of elements in the list using the
reverse()
method.
- Reverses the order of elements in the list using the
print(f"Reversed list: {fruits}")
- Prints the list after it has been reversed.
10. Iterating Through a List
print("Iterating through the list:")
- Prints a message before iterating through the list.
for fruit in fruits:
- Starts a
for
loop to iterate through each element of the listfruits
.
- Starts a
print(fruit, end=", ")
- Prints each element of the list followed by
,
(without moving to a new line after each print).
- Prints each element of the list followed by
print()
- Prints a new line after finishing the iteration to improve the output format.
11. List Comprehension
lengths = [len(fruit) for fruit in fruits]
- Uses a list comprehension to create a new list
lengths
containing the length of each string infruits
.
- Uses a list comprehension to create a new list
print(f"Lengths of each fruit name: {lengths}")
- Prints the list
lengths
, which contains the lengths of the names of the fruits.
- Prints the list
12. Combining Lists
vegetables = ["carrot", "broccoli", "spinach"]
- Creates a list
vegetables
containing three elements:"carrot"
,"broccoli"
, and"spinach"
.
- Creates a list
combined = fruits + vegetables
- Combines the
fruits
list andvegetables
list using the+
operator to create a new listcombined
.
- Combines the
print(f"Combined list of fruits and vegetables: {combined}")
- Prints the combined list of fruits and vegetables.
13. Advanced Example: Flattening a Nested List
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
- Creates a nested list
nested_list
containing sublists of integers.
- Creates a nested list
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 listflattened_list
.
- Uses a nested list comprehension to flatten the nested list
print(f"Flattened list: {flattened_list}")
- Prints the flattened list, which contains all elements of the sublists combined into one list.
14. Counting Occurrences
repeated_list = ["apple", "apple", "banana", "apple"]
- Creates a list
repeated_list
containing multiple occurrences of"apple"
.
- Creates a list
count_apple = repeated_list.count("apple")
- Counts how many times
"apple"
appears inrepeated_list
using thecount()
method and stores the result incount_apple
.
- Counts how many times
print(f"Number of 'apple' in the list: {count_apple}")
- Prints the count of
"apple"
in the list.
- Prints the count of
15. Running the Main Function
if _name_ == "_main_":
- Checks if the script is being executed directly (not imported as a module).
main()
- Calls the
main()
function to execute the program.
- Calls the
Comments
Post a Comment