GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Display the first and last colors from a given list - w3resource

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

Last update on May 17 2025 05:44:01 (UTC/GMT +8 hours)


First and Last Colors

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:

# Create a list called 'color_list' containing color names
color_list = ["Red", "Green", "White", "Black"]
# Print the first and last elements of the 'color_list' using string formatting
# The '%s' placeholders are filled with the values of 'color_list[0]' (Red) and 'color_list[-1]' (Black)
print("%s %s" % (color_list[0], color_list[-1]))

Sample Output:

Red Black

Explanation:

The said code creates a list named "color_list" which includes four different colors, "Red", "Green", "White", and "Black". Then, it utilizes string formatting to print the first (Index 0) and the last (Index -1) color of the list together with a space in between. The output would be "Red Black".

Flowchart:

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


For more Practice: Solve these Related Problems:

  • Write a Python program that prints the second and second-last colors from a given list.
  • Write a Python script that takes a list of colors and prints them in reverse order.
  • Write a Python program that takes a list of colors and finds the longest color name.
  • Write a script to check if a specific color exists in the given list.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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).

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.