Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

Tuples in Python:

Tuples are comma-separated sequences or collections of things. In many aspects, they are identical to lists, except that the elements cannot be modified after they are created. Tuples in Python, unlike lists, are immutable objects. They also employ parentheses rather than square brackets.

Lists in Python:

A list is exactly what it sounds like: a container for various Python objects such as integers, words, values, and so on. In other programming languages, it is equal to an array. It is denoted with square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, which means it can be changed or altered, as opposed to tuples, which are immutable.

Given lower limit range and upper limit range the task is to Make a list of Tuples with the first element being the number and the second element being the square of the number.

Examples:

Example1:

Input:

Enter some random lower limit =5
Enter some random lower limit =13

Output:

The list of Tuples are :  [(5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100), (11, 121), (12, 144), (13, 169)]

Example2:

Input:

Enter some random lower limit =23
Enter some random lower limit =69

Output:

The list of Tuples are :  [(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961), 
(32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), 
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401), (50, 2500), (51, 2601),
(52, 2704), (53, 2809), (54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481), (60, 3600), (61, 3721),
(62, 3844), (63, 3969), (64, 4096), (65, 4225), (66, 4356), (67, 4489), (68, 4624), (69, 4761)]

Make a list of Tuples with the first element being the number and the second element being the square of the number in Python

There are several ways to create a list of tuples with the first element being the number and the second element being square of the given number some of them are:

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Method 1:Using list comprehension (Static input)

Approach:

  • Give the lower limit range and upper limit range as static input.
  • Using list comprehension, generate a list of tuples with the first element being the number within the range and the second element being the square of the number.
  • The list of tuples will be printed.
  • Exit of program.

Below is the implementation:

# given lower limit range as static input
lowerlimit = 5
# given upper limit range as static input
upperlimit = 13
# Using list comprehension, generate a list of tuples with the first element being the number within
# the range and the second element being the square of the number.
listOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]
# printing the list of tuples
print('The list of Tuples are : ', listOfTuples)

Output:

The list of Tuples are :  [(5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100), (11, 121), (12, 144), (13, 169)]

Explanation:

List comprehension must be used to produce a list of tuples in which the first element is the supplied range’s number and the second element is a square of the first number.

Method 2:Using list comprehension (User input)

Approach:

  • Scan the lower limit range and upper limit range as user input using int(input()) which converts string to integer.
  • Using list comprehension, generate a list of tuples with the first element being the number within the range and the second element being the square of the number.
  • The list of tuples will be printed.
  • Exit of program.

Below is the implementation:

# Scan lower limit range as user input
lowerlimit = int(input("Enter some random lower limit ="))
# Scan upper limit range as user  input
upperlimit = int(input("Enter some random lower limit ="))
# Using list comprehension, generate a list of tuples with the first element being the number within
# the range and the second element being the square of the number.
listOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]
# printing the list of tuples
print('The list of Tuples are : ', listOfTuples)

Output:

Enter some random lower limit =11
Enter some random lower limit =49
The list of Tuples are : [(11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361), 
(20, 400), (21, 441), (22, 484), (23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961),
 (32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), 
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401)]

Method 3:Using for loop and append() function (Static input)

Approach:

  • Give the lower limit range and upper limit range as static input.
  • Take a empty list.
  • Iterate from lower limit range to upper limit range using for loop
  • Add iterate value and square of iterator value to the list as tuple using append() function.
  • The list of tuples will be printed.
  • Exit of program.

Below is the implementation:

# given lower limit range as static input
lowerlimit = 23
# given upper limit range as static input
upperlimit = 69
# Taking a empty list
listOfTuples = []
# using for loop
for value in range(lowerlimit, upperlimit+1):
    # adding number and square of the iterator value
    listOfTuples.append((value, value**2))

listOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]
# printing the list of tuples
print('The list of Tuples are : ', listOfTuples)

Output:

The list of Tuples are :  [(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961), 
(32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), 
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401), (50, 2500), (51, 2601),
(52, 2704), (53, 2809), (54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481), (60, 3600), (61, 3721),
(62, 3844), (63, 3969), (64, 4096), (65, 4225), (66, 4356), (67, 4489), (68, 4624), (69, 4761)]

Related Programs: