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. 22.从上往下打印二叉树(python)

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom( ...

  2. SQL limit(分页)

    1.limit使用 limit参数,第一个参数:从哪儿开始查:第二个参数:查几条 i : 为查询结果的索引值(默认从0开始): n : 为查询结果返回的数量  -- 从3开始 取 3 条 SELECT ...

  3. XML 浏览器支持

    几乎所有的主流浏览器均支持 XML 和 XSLT. Mozilla Firefox 从 1.0.2 版本开始,Firefox 就已开始支持 XML 和 XSLT(包括 CSS). Mozilla Mo ...

  4. 举例子说明ubuntu中remove,autoremove,purge区别

    转自:慎用 apt-get autoremove !   apt-get 提供了一个用于下载和安装软件包的简易命令行界面.卸载软件包主要有这3个命令 remove – 卸载软件包autoremove ...

  5. UOJ #395 BZOJ 5417 Luogu P4770 [NOI2018]你的名字 (后缀自动机、线段树合并)

    NOI2019考前做NOI2018题.. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=5417 (luogu) http ...

  6. 页面点击按钮下载excel(原生js)

    let els = document.getElementsByTagName('iframe'); if(els.length > 0){ for(let i = 0;i < els.l ...

  7. es索引基本操作(2)之 索引映射(mappings)管理和索引库配置管理(settings)

    1:索引的映射管理 elasticsearch中的文档等价于java中的对象 , 那么在java对象中有字段(比如string.int.long等): 同理在elasticsearch索引中的具体字段 ...

  8. route Cmd详解

    第一条命令,配置外网网关: route -p add 0.0.0.0 mask 0.0.0.0 192.168.1.1 第二条命令,配置内网网关:route -p add 192.168.0.0 ma ...

  9. htonl(),htons(),ntohl(),ntons()--大小端模式转换函数

    不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...

  10. C语言的AES加密

    C语言的AES加密 稍微封装了几个函数 方便使用 #if 1 #include <stdio.h> #include <stdlib.h> #include <strin ...