Array of Namedtuple - Python Programming Exercise

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 NamedTuple". This exercise is perfect for practicing the use of NamedTuple and arrays in Python, as well as enhancing your skills in data manipulation and user interaction. By prompting the user for data for the first two points and then displaying them, you will gain hands-on experience in handling user input and output in Python. This exercise not only reinforces your understanding of NamedTuple and arrays but also helps you develop efficient coding practices for managing large datasets.

 Category

Arrays, Lists, and Strings

 Exercise

Array Of Namedtuple

 Objective

Develop a Python program that expands the previous exercise (NamedTuple point), so that up to 1,000 points can be stored, using an "array of NamedTuple". Prompt the user for data for the first two points and then display them.

 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 an array to store up to 1000 points
points = []

# Prompt user for data for the first point
print("Enter the data for point 1:")
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): "))

# Store the first point in the array
points.append(Point(x1, y1, r1, g1, b1))

# Prompt user for data for the second point
print("Enter the data for point 2:")
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): "))

# Store the second point in the array
points.append(Point(x2, y2, r2, g2, b2))

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

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

 Output

Enter the data for point 1:
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 point 2:
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 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...

  •  Urban Database

    In this exercise, you will develop a Python program to create a database for storing information about urban areas. In the first approach, you will store only the nam...