Q20: How does the Python enumerate function work, and what is its significance in iterating over sequences? Provide an example to illustrate its usage.
A20:
Working of
enumerate:- The
enumeratefunction in Python is used to iterate over a sequence (such as a list, tuple, or string) while keeping track of the index of each element. It returns pairs of index and element as tuples.
- The
Significance in Iterating Over Sequences:
enumeratesimplifies the process of iterating over sequences by providing both the index and the corresponding element, eliminating the need to manually manage indices.
enumerate is used to iterate over the fruits list, providing both the index and the corresponding fruit in each iteration.
ไม่มีความคิดเห็น:
แสดงความคิดเห็น