博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 立方体_Python | 创建三个数字列表,分别是正方形和立方体
阅读量:2532 次
发布时间:2019-05-11

本文共 2522 字,大约阅读时间需要 8 分钟。

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 = 10    Output:    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 listsnumbers = []squares = []cubes = []# start and end numbersstart = 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 listsprint "numbers: ",numbersprint "squares: ",squaresprint "cubes  : ",cubes

Output

输出量

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]

通过定义自己的功能 (By defining own functions )

# define function to add numbers in listdef listNumbers(a,b):	#define dynamic list	list = []	for count in range(a,b+1):		list.append(count)		#return list	return list	# define function to add squares in listdef listSquares(a,b):	#define dynamic list	list = []	for count in range(a,b+1):		list.append(count**2)		#return list	return list# define function to add cubes in listdef listCubes(a,b):	#define dynamic list	list = []	for count in range(a,b+1):		list.append(count**3)		#return list	return list	# Main code	# declare listsnumbers = []squares = []cubes = []# start and end numbersstart = 1 end = 10 # get values in listsnumbers = listNumbers(start, end)squares = listSquares(start, end)cubes   = listCubes(start, end)# print the listsprint "numbers: ",numbersprint "squares: ",squaresprint "cubes  : ",cubes

Output

输出量

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]

翻译自:

python 立方体

转载地址:http://hbazd.baihongyu.com/

你可能感兴趣的文章
python安装教学
查看>>
JQuery制作简单的网页导航特效
查看>>
操作系统简述
查看>>
设计模式大总结2-结构型模式
查看>>
【Python】不定期更新学习小问题整理
查看>>
Zico源代码分析:执行启动过程分析和总结
查看>>
Android之Http通信——1.初识Http协议
查看>>
hdu5044(二分)
查看>>
静态路由、缺省路由的作用
查看>>
linux快捷键绝对路径相对路径讲解
查看>>
又漏一次
查看>>
dede模板中plus文件路径使用方法
查看>>
xml解析demo使用
查看>>
python使用模板手记
查看>>
No result defined for action com.nynt.action.ManageAction and result input问题
查看>>
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
查看>>
洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
查看>>
c++ 连接数据库
查看>>
函数指针与回调函数
查看>>
你所不知道的 CSS 滤镜技巧与细节
查看>>