Namedtuple - Python Programming Exercise

This Python program is an excellent introduction to using NamedTuples for storing structured data. The program allows users to define 2D points with specific fields, including the x and y coordinates as short integers, and r, g, and b as bytes representing the red, green, and blue color values. By using NamedTuple, the program ensures that each point is stored in a readable and immutable format, making it easier to manage and manipulate. The program prompts the user to input data for two points and then displays the entered values. This exercise demonstrates how to use NamedTuple to store and access related data in a clean and efficient manner. It also highlights the importance of data organization in Python, especially when handling complex data like coordinates and color values. Overall, this task is an excellent way to practice handling structured data in Python while learning how to work with NamedTuples for better code readability and efficiency.

 Category

Arrays, Lists, and Strings

 Exercise

Namedtuple

 Objective

Develop a Python program to store data of 2D points using a NamedTuple. The fields for each point will be:

- x coordinate (short)
- y coordinate (short)
- r (red color, byte)
- g (green color, byte)
- b (blue color, byte)

Write a Python program which creates two "points", asks the user for their data, and then displays their content.

 Example Python Exercise

 Copy Python Code
from collections import namedtuple

# Define the NamedTuple for a 2D point with color data
Point = namedtuple('Point', ['x', 'y', 'r', 'g', 'b'])

# Create the first point
print("Enter the data for the first point:")
x1 = int(input("Enter x coordinate (short): "))
y1 = int(input("Enter y coordinate (short): "))
r1 = int(input("Enter red color value (byte): "))
g1 = int(input("Enter green color value (byte): "))
b1 = int(input("Enter blue color value (byte): "))

point1 = Point(x1, y1, r1, g1, b1)

# Create the second point
print("\nEnter the data for the second point:")
x2 = int(input("Enter x coordinate (short): "))
y2 = int(input("Enter y coordinate (short): "))
r2 = int(input("Enter red color value (byte): "))
g2 = int(input("Enter green color value (byte): "))
b2 = int(input("Enter blue color value (byte): "))

point2 = Point(x2, y2, r2, g2, b2)

# Display the points data
print("\nThe data for the first point is:")
print(f"x: {point1.x}, y: {point1.y}, r: {point1.r}, g: {point1.g}, b: {point1.b}")

print("\nThe data for the second point is:")
print(f"x: {point2.x}, y: {point2.y}, r: {point2.r}, g: {point2.g}, b: {point2.b}")

 Output

Enter the data for the first point:
Enter x coordinate (short): 5
Enter y coordinate (short): 10
Enter red color value (byte): 255
Enter green color value (byte): 100
Enter blue color value (byte): 50

Enter the data for the second point:
Enter x coordinate (short): 20
Enter y coordinate (short): 25
Enter red color value (byte): 0
Enter green color value (byte): 255
Enter blue color value (byte): 200

The data for the first point is:
x: 5, y: 10, r: 255, g: 100, b: 50

The data for the second point is:
x: 20, y: 25, r: 0, g: 255, b: 200

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Array of Namedtuple

    In this exercise, you will develop a Python program that expands the previous exercise (NamedTuple point), allowing up to 1,000 points to be stored using an "array of...

  •  Array of Namedtuple and menu

    In this exercise, you will develop a Python program that expands the previous exercise (array of NamedTuples), so that it displays a menu where the user...

  •  Library Database

    In this exercise, you will develop a Python program to create a small database for storing book data. For each book, you will keep the following information: Title...

  •  Enhanced Triangle V2

    In this exercise, you will develop a Python program that prompts the user for their name and displays a triangle with it, starting with 1 letter and growing until it ...

  •  Enhanced Rectangle V3

    In this exercise, you will develop a Python program that prompts the user for their name and a size, and displays a hollow rectangle with it. This exercise is ...

  •  Symmetrical Triangle

    In this exercise, you will develop a Python program that displays a centered triangle from a string entered by the user. This exercise is perfect for practicin...