w3resource

Python: Display the first and last colors from a given list

Python Basic: Exercise-8 with Solution

Write a Python program to display the first and last colors from the following list.

color_list = ["Red","Green","White" ,"Black"]

Pictorial Presentation:

Display the first and last colors from a given list

Sample Solution:-

Python Code:

color_list = ["Red","Green","White" ,"Black"]
print( "%s %s"%(color_list[0],color_list[-1]))

Sample Output:

Red Black

Flowchart:

Flowchart: Display the first and last colors from a given list.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to accept a filename from the user and print the extension of that.
Next: Write a Python program to display the examination schedule. (extract the date from exam_st_date).

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]