Python数字序列 [英] Python Sequence of Numbers
本文介绍了Python数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我决定不浪费我的暑假,而是开始学习python.我以为我会开始学习循环技术,所以我想从一个基本的数字列表开始,也就是写一个for循环来生成数字1-10.
I've decided not to waste my summer and start learning python. I figured I'd start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate the numbers 1 - 10.
这就是我所拥有的:
def generateNumber(num):
i=0
for i in range(num):
return i
return i
,该代码不起作用.我想在这样的列表中获得输出:
and the code doesn't work. I want to get an output in a list like this:
>>> generateNumber(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
推荐答案
尝试与您第一次尝试的内容保持一致,您可以执行以下操作
Trying to be consistent with what you first tried, you could do something like this
def generateNumber(num):
mylist = []
for i in range(num+1):
mylist.append(i)
return mylist
x = generateNumber(10)
但是,您可以说,
x = range(10+1) # gives a generator that will make a list
或
x = list(range(10+1)) # if you want a real list
不过,一般来说,您应该基于输入数字10保留此列表,因此它是[0 ... 9]而不是[0 ... 10].
In general though, you should keep this list based on inputting the number 10 so it is [0...9] and not [0...10].
这篇关于Python数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文