python把数字逐一存入列表_python实现将range()函数生成的数字存储在一个列表中...
说明
同学的代码中遇到一个数学公式牵扯到将生成指定的数字存储的一个列表中,那个熊孩子忽然懵逼的不会啦,,,给了博主一个表现的机会,,,哈哈哈好嘛,虽然很简单但还是记录一下吧,,,嘿嘿
一 代码
# coding=utf-8
"""
@author: jiajiknag
程序功能:
"""
# 方法一
lifts = []
for n in range(1,13):
# lift = 1 +6 * np.sin(np.pi * n/12)
lift = 1 + n/12
lifts.append(lift)
print(lifts)
# 方法二
print("------------------------------------")
squares = [1 +i/12 for i in range(1,5)]
print(squares)
二 结果
好嘛,,,有没有很神奇的节奏!
补充知识:Python 通过range初始化list set 等
啥也不说了,还是直接看代码吧!
"""
01:range()函数调查
02:通过help()函数调查range()函数功能
03:Python中的转义字符
04:使用start、step、stop的方式尝试初始化list、tuple、set等
05:使用len()获取list、set、tuple的长度
"""
help(range)
tempRange = range(1,100,2)
print("type(tempRange): " + str(type(tempRange)))
print("tempRange: " + str(tempRange))
tempStr = ""
for i in range(5): # 注意 输出0到4,包括0和4但不包括5.
tempStr += (" " + str(i) + " ")
print("for i in range(5) " + tempStr) #for i in range(5) 0 1 2 3 4
tempStr = ""
for i in range(20,0,-2):
tempStr += (" " + str(i) + " ")
# 注意看输出不包括0
print("for i in range(20,0,-2) " + tempStr)
"""
for i in range(20,0,-2) 20 18 16 14 12 10 8 6 4 2
"""
tempStr = ""
for i in [1,2,3]:
tempStr += (" " + str(i) + " ")
# for i in [1,2,3] 1 2 3
print("for i in [1,2,3] " + tempStr)
tempStr = ""
for i in "Hello world!":
tempStr += (" " + str(i) + " ")
# for i in "Hello world!" H e l l o w o r l d !
print("for i in \"Hello world!\" " + tempStr)
print(list(range(5,10))) # 默认步长1,输出:[5, 6, 7, 8, 9]不包括10
print(list(range(0,10,2))) #输出:[0, 2, 4, 6, 8]
print(list(range(10,0,2))) #输出:[]
print(list(range(10,0,-2))) #输出:[10, 8, 6, 4, 2]
# 尝试使用start、step、stop的方式尝试初始化list、tuple、set等
# print(list(1,9,1)) # TypeError: list() takes at most 1 argument (3 given)
# print(set(1,9,1)) # TypeError: set expected at most 1 arguments, got 3
# print(tuple(1,9,1)) # TypeError: tuple() takes at most 1 argument (3 given)
tempList = list(range(0,10,1));
print("list(range(0,10,1)): " + str(tempList))
tempSet = set(range(0,10,1))
print("list(set(0,10,1)): " + str(tempSet))
tempTuple = tuple(range(0,10,1))
print("list(tuple(0,10,1)): " + str(tempTuple))
tempDic = {"num":1}
print("len(list) :" + str(len(tempList))) # len(list) :10
print("len(set) :" + str(len(tempSet))) # len(set) :10
print("len(tuple) :" + str(len(tempTuple))) # len(tuple) :10
print("len(dic) :" + str(len(tempDic))) # len(dic) :1
# list.append [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'b']
tempList.append('b')
print("list.append " + str(tempList))
# set.add {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a'}
tempSet.add('a')
print("set.add " + str(tempSet))
以上这篇python实现将range()函数生成的数字存储在一个列表中就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
本文标题: python实现将range()函数生成的数字存储在一个列表中
本文地址: http://www.cppcns.com/jiaoben/python/305331.html
python把数字逐一存入列表_python实现将range()函数生成的数字存储在一个列表中...相关推荐
- python输入一组数字存到列表_Python如何使用输入传递多个值并将它们存储在一个列表中(简单版本),python,怎么,input,传入,储存,到...
Python如何使用输入传递多个值并将它们存储在一个列表中(简单版本),python,怎么,input,传入,储存,到 发表时间:2020-08-20 当传入多个值时,需要使用split()函数来切割 ...
- python将一些朋友的姓名存储在一个列表中,访问该列表中的每个元素,从而将每个朋友的姓名都打印出来...
1 """ 2 3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为 names . 3 依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. 4 &q ...
- 编写一个程序,用户使用for循环输入5个数字,所有这些数字将存储在一个数组中。之后,程序将添加这五个数字并显示结果。程序必须支持运算符重载的概念。
Write a program in which users enter 5 numbers using for loop and all these numbers will store in an ...
- python内置对象是什么_Python内置对象类型之数字类型
Python中有6种内置对象类型 整数.浮点数–Number 字符串–String 列表–List 元组–Tuple 字典–Dictionary 集合–Set 不可变类型:Number.String. ...
- python计算2的平方代码_Python练习实例46 | 求输入数字的平方,如果平方运算后小于 50 则退出。...
这个问题,其实有两种计算的思路,第一种就是需要对结果进行判断,无非就是两种结果,一种结果为真,一种结果为假,将这两种结果分别赋值给1和0,然后创建一个while循环即可. 小黄人.jpg 实现的方式大 ...
- python程序退出后保存变量_Python将变量存储在一个列表中,每次程序重新启动时都会保存这些变量...
我正为我的频道开发一个Python Twitch IRC Bot,它似乎很简单,但我却被困在一个看似简单的任务上.我有一个积分系统,我认为它是有效的,但我发现每次我重新启动程序,包含用户余额的列表都会 ...
- python输入多个数据存入列表_python怎么把input的值储存到一个列表
python怎么把input的值储存到一个列表?下面给大家带来具体的方法: 通常输入的字符串需要分割处理,python使用split()函数进行分割. 例子:>>> x=input( ...
- python整数类型进制表示_Python二进制、八进制、十六进制数字的表示和转换
除了常用的十进制表示法,Python 也支持二进制.八进制和十六进制表示法.这些只是表示方法不同,但是它们表示的含义是一样的,如二进制中的 11 和十进制数中的 3 是一样的. 1. Python 二 ...
- python如何把数字转化成字符_python 定义一个dictpython如何将数字转化为字符串
python如何将数字转化为字符串?下面给大家介绍一下基本步骤: 第一步:在菜单栏里输入idle,打开shell; 第二步:新建文件并命名: 相关推荐:<python视频教程> W WW. ...
最新文章
- 清华学长免费分享Java基础核心知识大总结(1)
- TensorFlow预训练模型在新图中权重部分加载
- mysql/mariadb应该使用utf8mb4而不是utf8
- 机器学习算法平台alink_机器学习-逻辑回归算法
- Alibaba代码规约插件使用IDEA
- 笔记———No.1 使用matlab画圆
- python中id 是什么意思_python中的id函数是什么意思?
- Spark的搭建及实现单词统计
- 超详细的fiddler教程,从小白到精通(六)❤️
- MySQL执行-SQL执行顺序
- 详解js对称加密和非对称加密
- fatfs读写csv文件
- WINDOWS的常用软件点评(1)------系统必备软件篇
- 优盘格式化工具,U盘修复格式化卡死问题
- 在宝塔面板中创建免费的ddns(这里用了dynu.com)
- 大数据之flink教程
- CSR8670--初步认识
- 逃不掉的职场中年危机?
- vue实战--vue+elementUI实现多文件上传+预览(word/PDF/图片/docx/doc/xlxs/txt)
- nodejs npm下载使用包