+ 4

While loops and negative list indexing in python

Please help me understand while loops and negative indexing in lists in python.

12th May 2025, 11:45 AM
whirl coder ☕
whirl coder ☕ - avatar
3 Réponses
+ 7
Code , both of your *mentioned topics* are covered also by sololearn tutorials. for better understanding it is recommended to learn from these lessons. also it is crucial to do some execises to get a better understanding for using negative indexing or a mix of positive and negative indexing. > while loops tutorial: introduction to python module: control flow lesson: while loops > negative indexing: tutorial: python developer module: working with lists lesson: indexing, using indexing, slicing, using slicing, advanced slicing and indexing.
12th May 2025, 3:29 PM
Lothar
Lothar - avatar
+ 4
while loops are a type of loop that keeps running as long as a condition set is true. For example: count = 0 while count < 5: print(count) count += 1 This loop's condition is "count < 5", and the loop adds 1 to the count after every iteration. So after running 5 times and printing 0 1 2 3 4, the count reaches 5(and the condition becomes false as 5 is not less than 5, 5==5) and thus the loop stops. Remember to include a counter or another mechanism to make the condition false at some point, if you don't want infinite loop. Negative indexing allows you to extract items from the end of a list. Let's take this list as an example: colors = ["red", "green", "blue", "orange", "yellow"] When we use negative indexing, we decrease the index(so in negative term, -1 to -2 to -3 is decreasing) to go from the right(end) to the left(start) of a list. If we do: print(colors[-1]) This will output the item which is at the end of the list(yellow). If you use -2, it'll print orange, as it goes from right to left.
12th May 2025, 12:56 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 3
While loops? It's used to iterate (repeat) a specifical block of code as long as the condition is true, e.g: I = 34 while I > 0: print ("yes") I-- As for negative indexing, it's used to call a parts of the last strings in a list or word
12th May 2025, 10:15 PM
Coder
Coder - avatar