python 立方体_Python | 创建三个数字列表,分别是正方形和立方体
python 立方體
Take a range i.e. start and end, and we have to create three lists, list1 should contains numbers, list2 should contain squares of the numbers and list3 should contain cubes of the numbers in Python.
取一個范圍,即開始和結束,然后我們必須創建三個列表,list1應該包含數字,list2應該包含數字的平方,list3應該包含數字的立方體。
Example:
例:
Input:Start = 1 End = 10Output:numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]Logic:
邏輯:
Declare three lists.
聲明三個列表。
Define range, here we are defining start with 1 and end with 10.
定義范圍,這里我們定義以1開始,以10結束。
Run a loop with the range as range(start, end+1) and loop counter as count.
運行范圍為range(start,end + 1)的循環,循環計數器為count 。
Append the loop counter count to the list named numbers, append square to the list named squares and append the cube to the list named cubes.
將循環計數器計數追加到名為numbers的列表,將square追加到名為squares的列表,并將多維數據集追加到名為cubes的列表。
Finally, print the lists.
最后,打印列表。
Program:
程序:
# declare lists numbers = [] squares = [] cubes = []# start and end numbers start = 1 end = 10 # run a loop from start to end+1 for count in range (start, end+1) :numbers.append (count)squares.append (count**2)cubes.append (count**3)# print the lists print "numbers: ",numbers print "squares: ",squares print "cubes : ",cubesOutput
輸出量
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}通過定義自己的功能 (By defining own functions )
# define function to add numbers in list def listNumbers(a,b):#define dynamic listlist = []for count in range(a,b+1):list.append(count) #return listreturn list# define function to add squares in list def listSquares(a,b):#define dynamic listlist = []for count in range(a,b+1):list.append(count**2) #return listreturn list# define function to add cubes in list def listCubes(a,b):#define dynamic listlist = []for count in range(a,b+1):list.append(count**3) #return listreturn list# Main code # declare lists numbers = [] squares = [] cubes = []# start and end numbers start = 1 end = 10 # get values in lists numbers = listNumbers(start, end) squares = listSquares(start, end) cubes = listCubes(start, end)# print the lists print "numbers: ",numbers print "squares: ",squares print "cubes : ",cubesOutput
輸出量
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]翻譯自: https://www.includehelp.com/python/create-three-lists-of-numbers-their-squares-and-cubes.aspx
python 立方體
總結
以上是生活随笔為你收集整理的python 立方体_Python | 创建三个数字列表,分别是正方形和立方体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摩尔庄园手游大力商人在哪
- 下一篇: 线性方程组 python_线性方程组的表