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数据基础类型-新建列表的更多相关文章

  1. Python数据基础类型-列表

    1,列表的创建 list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", &quo ...

  2. Python的基础类型(int,bool,str):

    Python的基础类型(int,bool,str): 1.int -------> 整形:主要用力进行数字计算 2.string ------>字符串:可以保存少量数据并进行相关的操作 3 ...

  3. 2--Python入门--Python数据集合类型--列表

    在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...

  4. 4--Python入门--Python数据集合类型--集合

    在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...

  5. 3--Python入门--Python数据集合类型--元组

    在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...

  6. Python中高级变量类型(列表,元组,字典,字符串,公共方法...)

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  7. Python数据基础--列表、元组、字典、函数

    一.数据结构 列表(List)和元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内 ...

  8. 3、Python 基础类型 -- List 列表类型

    2.更新列表:list.append() 3.删除列表元素 del 

  9. python笔记---数据基础类型

    s = 'laonanHai' s1 = s.capitalize() #首字母大写,其他字母小写 s2 = s.upper() #全部大写 s3 = s.lower() #全部小写 print(s, ...

随机推荐

  1. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  2. codevs 1057 津津的储蓄计划 2004年NOIP全国联赛提高组 x

     时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300元钱,津津会预算这个月的花销,并且总能做到实际花 ...

  3. 【Leetcode】判断平面中1个点是否落在三角形内

    参考资料: 题目: https://blog.csdn.net/dongtinghong/article/details/78657403 符号重载: https://blog.csdn.net/cd ...

  4. PCL智能指针疑云 <二> 使用同一智能指针作为PCL预处理API的输入和输出

    问题介绍: slam构建地图,先进行降采样,再进行可视化或存储.然而经过降采样后,代码没有报错的情况下,点云数据散成一团.将代码和点云数据展示如下, pcl::VoxelGrid<Lidar:: ...

  5. swiper实现滑动到某页锁住不让滑动

    var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', onTouchStart: funct ...

  6. Apache配置详解(最好的APACHE配置教程)

    From: http://aiks.blog.com.cn/archives/2006/1748482.shtml Apache的配置 Apache的配置由httpd.conf文件配置,因此下面的配置 ...

  7. DVWA--CSP Bypass

    0x01看到标题,是否有点疑惑 CPS 是什么东东.简单介绍一下就是浏览器的安全策略,如果 标签,或者是服务器中返回 HTTP 头中有 Content-Security-Policy 标签 ,浏览器会 ...

  8. [BZOJ3990]:[SDOI2015]排序(搜索)

    题目传送门 题目描述 小A有一个1-${2}^{N}$的排列A[1..${2}^{N}$],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1≤i≤N), ...

  9. rollup的学习

    概述(Overview) Rollup 是一个 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码,例如 library 或应用程序.Rollup 对代码模块使用新的标准化格式,这些 ...

  10. HBuilder使用逍遥Android模拟器

    Microvirt HBuilder使用逍遥Android模拟器 1.逍遥模拟器安装 地址: 点我下载 2.连接注意事项 a. 复制adb等文件 HBuilder安装目录中tools文件夹下的三个文件 ...