{"id":8816,"date":"2023-11-04T11:11:39","date_gmt":"2023-11-04T05:41:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=8816"},"modified":"2023-11-10T12:16:22","modified_gmt":"2023-11-10T06:46:22","slug":"python-program-to-generate-random-numbers-from-1-to-20-and-append-them-to-the-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-random-numbers-from-1-to-20-and-append-them-to-the-list\/","title":{"rendered":"Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List"},"content":{"rendered":"

Lists in Python:<\/strong><\/p>\n

Python\u2019s built-in container types are List and Tuple. Objects of both classes can store various additional objects that can be accessed via index. Lists and tuples, like strings, are sequence data types. Objects of different types can be stored in a list or a tuple.<\/p>\n

A list is an ordered collection of objects (of the same or distinct types) separated by commas and surrounded by square brackets.<\/p>\n

Random Numbers in Python:<\/strong><\/p>\n

The random<\/strong> module in Python defines a set of functions for generating and manipulating random integers. Random()<\/strong>, a pseudo-random number generator function that generates a random float number between 0.0 and 1.0, is used by functions in the random module. These functions are used in a variety of games, lotteries, and other applications that need the creation of random numbers.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

given number of elements =30<\/p>\n

Output:<\/strong><\/p>\n

The random number list which contains 30 elements =  [6, 18, 11, 5, 16, 12, 6, 20, 12, 1, 5, 20, 20, 18, 13, 12, 5, 7, \r\n14, 9, 4, 5, 13, 18, 19, 19, 19, 20, 12, 2]<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Enter the total number of elements in the given list =25<\/pre>\n

Output:<\/strong><\/p>\n

The random number list which contains 25 elements = [1, 6, 18, 5, 3, 2, 7, 5, 6, 6, 1, 2, 4, 17, 1, 20, 6, 2, 6, 19, 6, \r\n10, 19, 20, 3]<\/pre>\n

Program to Generate Random Numbers from 1 to 20 and Append Them to the List<\/h2>\n

We will create a program that generates random numbers from 1 to 20 and adds them to a empty list using append() function.<\/p>\n