Turning random numbers into a list

If you printed just 10 random numbers and wanted to turn it into a list how would you do that. In the example code it prints 10 numbers but not in a list, how could I change them into a list without changing the variable.

list = 1
for k in range(10):
   list = list += 1
print(list)

2 answers

  • answered 2020-10-01 05:17 user13966865

    probably this is what you looking for

    import random
    list_=[random.randint(1,100) for i in range(10)] 
    #generates 10 random numbers which ranges in between 1,100
    #[34, 15, 3, 18, 23, 32, 49, 65, 3, 35]
    
    list_=list(range(10))
    #generate list [0,1,2,3,4,5,6,7,8,9]
    

  • answered 2020-10-01 05:26 adir abargil

    In case i got you right and believe you didn’t really mean random numbers...

    This is how u get a list:

    my_list = []
    your_number = 10
    for i in range(your_number):
        my_list.append(i)
    print(my_list)