Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Continents Loop starting with "A"

On the task to only show continents that begin with the letter "A", I'm missing the WHY this works. We are asking 'if continent[0] == "A":, how is that not throwing an error since the first item in our list doesn't truly = A, it's equaling 'Asia'. Is it because the for is only looking at that first letter and then as soon as it's hitting 'A' it's printing that string in the list?

My second question is regarding the index. With 'if continent[0] == "A": are we actually identifying the first letter or are we identifying the first list item? For example, what if I wanted to print continents ENDING with the letter 'A', does that become continents[-1]?

Thanks!

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
for continent in continents:
    if continent[0] == 'A':
        print("* " + continent)

1 Answer

Steven Parker
Steven Parker
243,253 Points

While "continent" represents the whole word (the loop assigns one word at a time to it), "continent[0]" represents just the first character of the word.

And you're quite right, and index of -1 will give you the last letter instead of the first.

Awesome, thank you!