How to Append Random Numbers (1–20) to a List in Python
In this tutorial, we will learn how to program "How to Append Random Numbers (1–20) to a List in Python." The objective is to efficiently append random numbers between 1 and 20 into a list. This tutorial will guide you step by step through the entire process of generating and inserting random numbers into a list based on user input or specific requirements. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and enhance your coding skills.
This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program demonstrated will guide you step by step through the process of sorting a hyphen-separated sequence of words in alphabetical order. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- import random
- while True:
- print("\n============= Append Random Numbers (1–20) to a List =============\n")
- array=[]
- n=int(input("Enter a number: "))
- for j in range(n):
- array.append(random.randint(1,20))
- print('Random list : ',array)
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This Python program repeatedly prompts the user to enter a number n, then generates a list of n random integers between 1 and 20 using the random.randint() function. It appends each random number to a list and displays the final list. The user is then given the option to run the program again or exit.
Output:

There you have it we successfully created How to Append Random Numbers (1–20) to a List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language