Advertisement
Cobble5tone

Iterating through list

Aug 18th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. dates = [1982, 1980, 1973, 2000]
  2.  
  3.  
  4. for date in dates:  # Iterates through dates list item by item until reaching 1973
  5.     if date == 1973:
  6.         break  # Exits loop upon finding 1973 value
  7.     else:
  8.         print(date)
  9.  
  10.  
  11. counter = 0
  12. year = 0  # Initialize year value
  13.  
  14. while year != 1973:  # Begins conditional loop
  15.    
  16.     print(dates[counter])  # Uses the counter as an index to print our indexed value in dates list
  17.     year = dates[counter]  # Sets the year value to the current indexed value in the dates list
  18.     counter += 1  # Advances the index 1 value per iteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement