Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sequence = ['A','B','C','D','E','F']
- print('The sequence is', sequence , '\n')
- # The enumerate function maps a number to each list item,
- # and does not need a separate counter variable.
- # It also allows some clever things.
- # ie. example enumerate(sequence,1) labels
- # each element starting at 1 rather than 0.
- print("The entire list.\n")
- for position, item in enumerate(sequence,1):
- print(item, " is at position ", position,".", sep="")
- print('\n\n')
- print('Using a while loop to search for the \'C\'.')
- #Example 1 - A while loop
- position = 0
- while position < len(sequence):
- if (sequence[position] == 'C'):
- print(sequence[position], " is at position ", str(position + 1),".", sep="")
- position += 1
- print('\n')
- print('Using a for loop to search for the \'F\'.')
- #Example 2 - A for loop over a range
- for position in range(len(sequence)):
- if (sequence[position] == 'F'):
- print(sequence[position], " is at position ", str(position + 1),".", sep="")
- print('\n')
- print('Using the Python enumerate function to search for \'B\'.')
- #Example 3 - The enumerate function
- for position, item in enumerate(sequence,1):
- if (item == 'B'):
- print(item, " is at position ", position,".","\n\n", sep="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement