Python Introducing Lists Using Lists Continental

Sehrish Ijaz
Sehrish Ijaz
756 Points

how to retrieve continents starting with letter A?

i don't know the code in for loop to get elements with a specific letter

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here

2 Answers

Sehrish Ijaz
Sehrish Ijaz
756 Points

tinents = [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia', ]

Your code here

for continent in continents: print("*",continent)

Steven Parker
Steven Parker
177,655 Points

You'll still need the code you did for task 1 that creates the loop, you will just add an "if" statement to control when the "print" happens.

You can access individual characters in a string by indexing, using brackets similar to working with a list. For example, since index numbers start at 0, the first character of "mystring" would be "mystring[0]".

You could then compare that to the letter "A" in a conditional expression as part of the "if" statement.

Sehrish Ijaz
Sehrish Ijaz
756 Points

Can you please send me the whole code?

Steven Parker
Steven Parker
177,655 Points

Please post the code you passed task 1 with first, then I can show you what you need to add for task 2.

Steven Parker
Steven Parker
177,655 Points

So between the "for" line and the "print", you could add:

    if continent[0] == "A": 

And be sure to adjust the indenting of the "print" line.