{"id":9038,"date":"2021-09-30T14:00:49","date_gmt":"2021-09-30T08:30:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=9038"},"modified":"2021-11-22T18:34:35","modified_gmt":"2021-11-22T13:04:35","slug":"python-program-to-generate-a-dictionary-that-contains-numbers-between-1-and-n-in-the-form-x-xx","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-a-dictionary-that-contains-numbers-between-1-and-n-in-the-form-x-xx\/","title":{"rendered":"Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x, x*x)."},"content":{"rendered":"
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/p>\n Dictionaries in Python:<\/strong><\/p>\n In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science).<\/p>\n Each key denotes a value and is separated by a colon (:).<\/p>\n Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.<\/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 generate a dictionary which contains numbers from 1 to n in the form ( x, x*x ) some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/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 Explanation :<\/strong><\/p>\n Here we assigned square of iterator value to the key\u00a0 of the dictionary instead of using dictionary comprehension<\/p>\n","protected":false},"excerpt":{"rendered":" Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples Dictionaries in Python: In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map …<\/p>\ngiven number = 17<\/pre>\n
Enter some random number = 17\r\nprinting the resultant dictionary :\r\nkey = 1 value = 1\r\nkey = 2 value = 4\r\nkey = 3 value = 9\r\nkey = 4 value = 16\r\nkey = 5 value = 25\r\nkey = 6 value = 36\r\nkey = 7 value = 49\r\nkey = 8 value = 64\r\nkey = 9 value = 81\r\nkey = 10 value = 100\r\nkey = 11 value = 121\r\nkey = 12 value = 144\r\nkey = 13 value = 169\r\nkey = 14 value = 196\r\nkey = 15 value = 225\r\nkey = 16 value = 256\r\nkey = 17 value = 289<\/pre>\n
given number=11<\/pre>\n
printing the resultant dictionary :\r\nkey = 1 value = 1\r\nkey = 2 value = 4\r\nkey = 3 value = 9\r\nkey = 4 value = 16\r\nkey = 5 value = 25\r\nkey = 6 value = 36\r\nkey = 7 value = 49\r\nkey = 8 value = 64\r\nkey = 9 value = 81\r\nkey = 10 value = 100\r\nkey = 11 value = 121<\/pre>\n
Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x, x*x) in Python<\/h2>\n
\n
Method #1: Using Dictionary Comprehension (User Input)<\/h3>\n
\n
# given number\r\nnumb = int(input(\"Enter some random number = \"))\r\n# using dictionary comprehension\r\nresultdict = {k: k*k for k in range(1, numb+1)}\r\n# printing the resultant dictionary\r\nprint('printing the resultant dictionary :')\r\nfor key,value in resultdict.items():\r\n print(\"key =\",key,\"value =\",value)\r\n<\/pre>\n
Enter some random number = 17\r\nprinting the resultant dictionary :\r\nkey = 1 value = 1\r\nkey = 2 value = 4\r\nkey = 3 value = 9\r\nkey = 4 value = 16\r\nkey = 5 value = 25\r\nkey = 6 value = 36\r\nkey = 7 value = 49\r\nkey = 8 value = 64\r\nkey = 9 value = 81\r\nkey = 10 value = 100\r\nkey = 11 value = 121\r\nkey = 12 value = 144\r\nkey = 13 value = 169\r\nkey = 14 value = 196\r\nkey = 15 value = 225\r\nkey = 16 value = 256\r\nkey = 17 value = 289<\/pre>\n
\n
Method #2: Using Dictionary Comprehension (Static Input)<\/h3>\n
\n
# give the number as static\r\nnumb = 11\r\n# using dictionary comprehension\r\nresultdict = {k: k*k for k in range(1, numb+1)}\r\n# printing the resultant dictionary\r\nprint('printing the resultant dictionary :')\r\nfor key, value in resultdict.items():\r\n print(\"key =\", key, \"value =\", value)\r\n<\/pre>\n
printing the resultant dictionary :\r\nkey = 1 value = 1\r\nkey = 2 value = 4\r\nkey = 3 value = 9\r\nkey = 4 value = 16\r\nkey = 5 value = 25\r\nkey = 6 value = 36\r\nkey = 7 value = 49\r\nkey = 8 value = 64\r\nkey = 9 value = 81\r\nkey = 10 value = 100\r\nkey = 11 value = 121<\/pre>\n
Method #3:Using for loop (Static Input)<\/h3>\n
\n
# give the number as static\r\nnumb = 13\r\n# Take a empty dictionary\r\nresultdict = {}\r\n# using for to iterate from 1 to numb\r\nfor p in range(1, numb+1):\r\n resultdict[p] = p*p\r\n# printing the resultant dictionary\r\nprint('printing the resultant dictionary :')\r\nfor key, value in resultdict.items():\r\n print(\"key =\", key, \"value =\", value)\r\n<\/pre>\n
printing the resultant dictionary :\r\nkey = 1 value = 1\r\nkey = 2 value = 4\r\nkey = 3 value = 9\r\nkey = 4 value = 16\r\nkey = 5 value = 25\r\nkey = 6 value = 36\r\nkey = 7 value = 49\r\nkey = 8 value = 64\r\nkey = 9 value = 81\r\nkey = 10 value = 100\r\nkey = 11 value = 121\r\nkey = 12 value = 144\r\nkey = 13 value = 169<\/pre>\n