{"id":9269,"date":"2021-10-01T11:00:26","date_gmt":"2021-10-01T05:30:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=9269"},"modified":"2021-11-22T18:33:27","modified_gmt":"2021-11-22T13:03:27","slug":"python-program-to-create-a-list-of-tuples-with-the-first-element-as-the-number-and-second-element-as-the-square-of-the-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-create-a-list-of-tuples-with-the-first-element-as-the-number-and-second-element-as-the-square-of-the-number\/","title":{"rendered":"Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number"},"content":{"rendered":"
Tuples in Python:<\/strong><\/p>\n 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.<\/p>\n Lists in Python:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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:<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nEnter some random lower limit =5\r\nEnter some random lower limit =13<\/pre>\n
The list of Tuples are : [(5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100), (11, 121), (12, 144), (13, 169)]<\/pre>\n
Enter some random lower limit =23\r\nEnter some random lower limit =69<\/pre>\n
The list of Tuples are : [(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961), \r\n(32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), \r\n(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401), (50, 2500), (51, 2601),\r\n(52, 2704), (53, 2809), (54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481), (60, 3600), (61, 3721),\r\n(62, 3844), (63, 3969), (64, 4096), (65, 4225), (66, 4356), (67, 4489), (68, 4624), (69, 4761)]<\/pre>\n
Make a list of Tuples with the first element being the number and the second element being the square of the number in Python<\/h2>\n
\n
Method 1:Using list comprehension (Static input)<\/h3>\n
\n
# given lower limit range as static input\r\nlowerlimit = 5\r\n# given upper limit range as static input\r\nupperlimit = 13\r\n# Using list comprehension, generate a list of tuples with the first element being the number within\r\n# the range and the second element being the square of the number.\r\nlistOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]\r\n# printing the list of tuples\r\nprint('The list of Tuples are : ', listOfTuples)\r\n<\/pre>\n
The list of Tuples are : [(5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100), (11, 121), (12, 144), (13, 169)]<\/pre>\n
Method 2:Using list comprehension (User input)<\/h3>\n
\n
# Scan lower limit range as user input\r\nlowerlimit = int(input(\"Enter some random lower limit =\"))\r\n# Scan upper limit range as user input\r\nupperlimit = int(input(\"Enter some random lower limit =\"))\r\n# Using list comprehension, generate a list of tuples with the first element being the number within\r\n# the range and the second element being the square of the number.\r\nlistOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]\r\n# printing the list of tuples\r\nprint('The list of Tuples are : ', listOfTuples)<\/pre>\n
Enter some random lower limit =11\r\nEnter some random lower limit =49\r\nThe list of Tuples are : [(11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361), \r\n(20, 400), (21, 441), (22, 484), (23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961),\r\n (32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), \r\n(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401)]<\/pre>\n
Method 3:Using for loop and append() function (Static input)<\/h3>\n
\n
# given lower limit range as static input\r\nlowerlimit = 23\r\n# given upper limit range as static input\r\nupperlimit = 69\r\n# Taking a empty list\r\nlistOfTuples = []\r\n# using for loop\r\nfor value in range(lowerlimit, upperlimit+1):\r\n # adding number and square of the iterator value\r\n listOfTuples.append((value, value**2))\r\n\r\nlistOfTuples = [(k, k**2) for k in range(lowerlimit, upperlimit+1)]\r\n# printing the list of tuples\r\nprint('The list of Tuples are : ', listOfTuples)\r\n<\/pre>\n
The list of Tuples are : [(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841), (30, 900), (31, 961), \r\n(32, 1024), (33, 1089), (34, 1156), (35, 1225), (36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681), \r\n(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401), (50, 2500), (51, 2601),\r\n(52, 2704), (53, 2809), (54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481), (60, 3600), (61, 3721),\r\n(62, 3844), (63, 3969), (64, 4096), (65, 4225), (66, 4356), (67, 4489), (68, 4624), (69, 4761)]<\/pre>\n
\n