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, ...
随机推荐
- vue的transition的name作用
记录一下今天在vue的transition中遇到的坑 <!DOCTYPE html> <html> <head> <title>Vue中CSS动画原理& ...
- EF大数据插入
_April给出代码: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotati ...
- VSCode支持jsx自动补全
点击settings.json中编辑, 把这段话加上去就可以了 "emmet.includeLanguages": { "javascript": " ...
- [NOIP2012] 同余方程(第三次考试大整理)
1265. [NOIP2012] 同余方程 输入文件:mod.in 输出文件:mod.out 简单对比 时间限制:1 s 内存限制:128 MB [题目描述] 求关于 x 的同余方程 ax ...
- CCA(典型相关分析)
https://www.cnblogs.com/pinard/p/6288716.html
- 手写ORM
利用ORM把mysql中的数据封装成对象,通过对象点语法来获取mysql中的数据,所以自己手写一个ORM,方便我们操作数据 一.ORM:对象关系映射 类 >>> 数据库的一张表 对象 ...
- vue key的作用
key的作用是什么? 简单来说: key是给每一个vnode的唯一id,可以依靠key,更准确, 更快的拿到oldVnode中对应的vnode节点. 1. 更准确 因为带key就不是就地复用了,在sa ...
- Mysql中 BLOB字段转String的方法
转:https://www.cnblogs.com/renjie0520/p/5242350.html 1.通过sql直接转换 select CONVERT (*** USING utf8) AS u ...
- 大数据笔记(二十五)——Scala函数式编程
===================== Scala函数式编程 ======================== 一.Scala中的函数 (*) 函数是Scala中的头等公民,就和数字一样,可以在变 ...
- tomcat8.5部署管理控制台
1.修改 conf/tomcat-users.xml 文件 <role rolename="manager-gui"/> <role rolename=" ...