This is a Python Program to generate random numbers from 1 to 20 and append them to the list.
The program takes in the number of elements and generates random numbers from 1 to 20 and appends them to the list.
1. Import the random module into the program.
2. Take the number of elements from the user.
3. Use a for loop, random.randint() is used to generate random numbers which are them appending to a list.
4. Then print the randomised list.
4. Exit.
Here is source code of the Python Program to generate random numbers from 1 to 20 and append them to the list. The program output is also shown below.
import random a=[] n=int(input("Enter number of elements:")) for j in range(n): a.append(random.randint(1,20)) print('Randomised list is: ',a)
1. The random module is imported into the program.
2. User must enter the number of elements from the user.
3. Using a for loop, random.randint() is used to generate random numbers which are then appended to a list.
4. The arguments given inside the random.randint() function are used to specify the range to print the randomized numbers within.
5. Then the randomized list is printed.
Case 1: Enter number of elements:3 ('Randomised list is: ', [17, 4, 9]) Case 2: Enter number of elements:7 ('Randomised list is: ', [16, 5, 18, 19, 6, 7, 20])
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Information Technology Books
- Check Python Books
- Apply for Programming Internship
- Apply for Python Internship
- Practice Programming MCQs