Write a for loop
that prints each item in the list ['python','perl','java','C++']
Now modify your code so that it prints only the second character in each item (so: y, e, a, +
)
Write a while loop
that prints each item in the list ['python','perl','java','C++']
Modify your script so that it prints each item in the list + which position it has in the list:
python has position 0 in the list
perl has position 1 in the list
java has position 2 in the list
C++ has position 3 in the list
languages = ['python','perl','java','C++'] # Save all languages to a list
for language in languages: # Use a foor loop to go to each item in turn in the list and do something...
print(language) # In this case printing the output
python perl java C++
languages = ['python','perl','java','C++']
for language in languages:
print(language[1]) # language is an iterable item in fruits, so you can use index to access characters
y e a +
languages = ['python','perl','java','C++'] # Save all languages to a list
i = 0 # Set a counter on where to start in the list (0)
while i < len(languages): # Run loop as long as this statement is true.
print(languages[i]) # Use i for index of languages
i = i + 1 # Don't forget to increase i with 1, otherwise you have an infinite loop
python perl java C++
languages = ['python','perl','java','C++']
i = 0
while i < len(languages):
print(languages[i]+' has position '+str(i)+' in the list') # Use the counter i to print the position
i = i + 1
python has position 0 in the list perl has position 1 in the list java has position 2 in the list C++ has position 3 in the list