Computer Science, asked by nabeeharaheel123, 22 days ago

Write down the program to traverse the given list and print each element one by one: colourlist=["red","green","blue","purple","violet","brown", "navy blue", "pink"] I am using Python

Answers

Answered by Equestriadash
2

\tt colourlist\ =\ ["red",\ "green",\ "blue",\ "purple",\ "violet",\ "brown",\\ "navy\ blue",\ "pink"]\\for\ i\ in\ colourlist:\\{\ \ \ \ \ }print(i)

To traverse through the list, we use a for loop, which is an iteration statement used to perform repeated checking. For the loop, the range given is the list 'colourlist'. This means that the loop will keep repeating till it reaches the last element in the list.

The changing variable 'i', traverses through each element and gets them assigned for the print statement.

The first element in the list is 'red'. 'Red' gets assigned to 'i', and as per the next statement, that is to print the value of 'i', it gets printed. Once that's over, it moves to the next element 'green' and the same thing happens. As mentioned earlier, once the loop finishes printing the last element, it will stop.

Similar questions