Python数据基础类型-新建列表
1,遍历列表
遍历列表的所有元素,对每个元素执行相同的操作。可使用for循环
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
运行结果
alice
david
carolina
magicians = ['alice','david','carolina']
for magician in magicians:
#print(magician)
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n") print("Thank you, everyone. That was a great magic show!")
运行结果
Alice, that was a great trick!
I can't wait to see your next trick, Alice. David, that was a great trick!
I can't wait to see your next trick, David. Carolina, that was a great trick!
I can't wait to see your next trick, Carolina. Thank you, everyone. That was a great magic show!
2,创建数字列表
一,列表非常适合用于存储数字集合,函数range()能够轻松生成一系列的数字。
for value in range(1,5):
print(value)
运行结果
1
2
3
4
二,使用range()创建数字列表。可使用函数list()将range()的结果直接转换成列表。还可指定步长。
numbers = list(range(1,6))
print(numbers)
运行结果
[1, 2, 3, 4, 5]
三、列表解析
列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。
语法:描述性的列表名 = 【表达式(用于存储到列表中的值) for循环】
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
result
squares = [value**2 for value in range(1,11)]
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
result
四、切片
1,处理列表的部分元素-称之为切片
players = ['charles','martina','michael','florence','eli']
print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])
print(players[:-3])
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
['charles', 'martina']
result
2,遍历切片
players = ['charles','martina','michael','florence','eli']
print("\nHere are the first three players on my team:")
for player in players[:3]:
print(player.title())
运行结果:
Here are the first three players on my team:
Charles
Martina
Michael
result
3,复制列表
3,元组
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
如下实例:
dimensions = (200,50)
#dimensions[0] = 250 修改无组值报错
print(dimensions[0])
print(dimensions[1]) #遍历元组中的所有值,for循环
for dimension in dimensions:
print(dimension)
@修改元组变量
dimensions = (250,50)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
运行结果:
200
50
200
50 Modified dimensions:
250
50
result
Python数据基础类型-新建列表的更多相关文章
- Python数据基础类型-列表
1,列表的创建 list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", &quo ...
- Python的基础类型(int,bool,str):
Python的基础类型(int,bool,str): 1.int -------> 整形:主要用力进行数字计算 2.string ------>字符串:可以保存少量数据并进行相关的操作 3 ...
- 2--Python入门--Python数据集合类型--列表
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- 4--Python入门--Python数据集合类型--集合
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- 3--Python入门--Python数据集合类型--元组
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- Python中高级变量类型(列表,元组,字典,字符串,公共方法...)
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- Python数据基础--列表、元组、字典、函数
一.数据结构 列表(List)和元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内 ...
- 3、Python 基础类型 -- List 列表类型
2.更新列表:list.append() 3.删除列表元素 del
- python笔记---数据基础类型
s = 'laonanHai' s1 = s.capitalize() #首字母大写,其他字母小写 s2 = s.upper() #全部大写 s3 = s.lower() #全部小写 print(s, ...
随机推荐
- UNIX标准C - socket套接字
一.计算机网络 1.计算机网络的功能 a.数据通信 b.资源共享 c.提高系统的可靠性 d.分布式网络处理和负载均匀. 2.计算机网络的组成 1.通信子网:由网卡.线缆.集线器.中继器.交换器.路由器 ...
- FZU 2203 单纵大法好 (二分 && 贪心)
题意 : 老S最近喜欢上某个搜集战舰的游戏,这个游戏中很重要的一个内容是能编排自己的战舰,通过出击完成任务来获取资源或新的战舰.大家都说老S是一个“直男”,所以他喜欢把战舰排成一条直线.目前老S正准备 ...
- 洛谷P3943 星空——题解
一道很好的锻炼思维难度的题,如果您能在考场上直接想出来的话,提高组450分以上就没问题了吧.(别像作者一样看了好几篇题解才勉强会) 先提取出题目大意:给定一个长度n<=40000的01串,其中1 ...
- 【转载】opencv 二值化函数——cv2.threshold
https://blog.csdn.net/weixin_38570251/article/details/82079080 threshold:固定阈值二值化, ret, dst = cv2.thr ...
- HDU1575--Tr A(矩阵快速幂)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- JDBC API访问数据库的基本步骤。
JDBC本质:官方定义了一套操作所有关系型数据库的规则(接口),各个数据库厂商实现这个接口,提供数据库驱动jar包. 我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类. 任 ...
- java中的char,short,int,long占几个字节
1:“字节”是byte,“位”是bit : 2: 1 byte = 8 bit : char 在java中是2个字节.java采用unicode,2个字节(16位)来表示一个字符. short 2个字 ...
- sklearn—LinearRegression,Ridge,RidgeCV,Lasso线性回归模型简单使用
线性回归 import sklearnfrom sklearn.linear_model import LinearRegression X= [[0, 0], [1, 2], [2, 4]] y = ...
- windows7如何用键盘模拟鼠标操作
windows7如何用键盘模拟鼠标操作 https://jingyan.baidu.com/article/6dad5075104907a123e36e38.html 听语音 37453人看了这个视频 ...
- windows 把ps/2 鼠标当成ps/2键盘了
真坑口阿 https://zhidao.baidu.com/question/425134865713508932.html 电脑的PS/2鼠标接口认成键盘了 电脑主板技嘉,只有一个PS/2接口.开始 ...