How can I generate a list of consecutive numbers?

前端 未结 7 627
南旧
南旧 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:24

    You can use list comprehensions for this problem as it will solve it in only two lines. Code-

    n = int(input("Enter the range of the list:\n"))
    l1 = [i for i in range(n)] #Creates list of numbers in the range 0 to n
    if __name__ == "__main__":
        print(l1)
    

    Thank you.

    0 讨论(0)
提交回复
热议问题