Python Program to Generate Random List and Find Position & Occurrences

📅 11 Mar 2026
Python List Program Random Numbers Position Occurrence Example

This Python program generates a list of 20 random numbers and allows the user to enter a number from the keyboard. The program then finds the positions where the number appears in the list and counts its total occurrences. This example helps beginners understand Python lists, loops, and searching techniques.

Python Program: Random List and Find Position & Occurrences

Python list is one of the most commonly used data structures in programming. In this example, we generate a list of 20 random numbers and search for a number entered by the user. The program then displays the positions and the number of occurrences.

Program Objective

  • Generate 20 random numbers in a list
  • Accept a number from the user
  • Find the positions of that number in the list
  • Count how many times it appears

Python Program

import random

numbers = []

for i in range(20):
    numbers.append(random.randint(1,10))

print("Generated List:", numbers)

num = int(input("Enter number to search: "))

positions = []

for i in range(len(numbers)):
    if numbers[i] == num:
        positions.append(i)

if len(positions) > 0:
    print("Number found at positions:", positions)
    print("Total occurrences:", len(positions))
else:
    print("Number not found")

Example Output

Generated List: [3,5,2,5,8,5,1,9,4,5,6,7,5,2,3,5,1,8,5,4]

Enter number to search: 5

Number found at positions: [1,3,5,9,12,15,18]
Total occurrences: 7

Explanation

The program first generates a list of random numbers using the random module. Then it takes input from the user and checks each element in the list using a loop. If the element matches the entered number, its position is stored. Finally, the program prints the positions and total occurrences.

Conclusion

This program is useful for beginners learning Python lists, loops, and searching techniques. It is commonly used in Python practical exams and programming practice.