Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
What do these three lines show? Say it before you run it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. index() gives the position of the first item that matches. count() says how many times it occurs.
letters.index("d"): the list is b (0), a (1), d (2), a (3), c (4). So d is at position 2.letters.index("a"): the first a is at position 1. The second a at position 3 is not reported.letters.count("a"): two items are a, so the answer is 2.Check. Run the program. If you ask for letters.index("z"), Python stops with a ValueError, because z is not in the list.
Answer: 2, 1 and 2.
Log in to join the discussion.
Loading…