How can I generate a list of consecutive numbers?

前端 未结 7 724
南旧
南旧 2020-11-27 04:38

Say if you had a number input 8 in python and you wanted to generate a list of consecutive numbers up to 8 like

[0, 1, 2, 3, 4, 5,          


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 05:07

    Note :- Certainly in python-3x you need to use Range function It works to generate numbers on demand, standard method to use Range function to make a list of consecutive numbers is

    x=list(range(10))
    #"list"_will_make_all_numbers_generated_by_range_in_a_list
    #number_in_range_(10)_is_an_option_you_can_change_as_you_want
    print (x)
    #Output_is_ [0,1,2,3,4,5,6,7,8,9]
    

    Also if you want to make an function to generate a list of consecutive numbers by using Range function watch this code !

    def  consecutive_numbers(n) :
        list=[i for i in range(n)]
        return (list)
    print(consecutive_numbers(10))
    

    Good Luck!

提交回复
热议问题