“笨办法”学Python 3 ——练习 32 循环和列表

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

练习32 源代码

the_count = [1,2,3,4,5] #列表内容可以是数字
fruits = ['apples', 'oranges', 'pears','apricots']#列表内容为字符串
change = [1, 'pennies',2, 'dimes', 3, 'quarters'] #列表内容可以为字符串和数字

# this first kind of for-loop goes through a list
for number in the_count: #for循环的值the_count列表中变量number依次遍历列表中的内容
    print(f"This is count {number}") #打印表中内容
    
# same as above
for fruit in fruits:  #原理同上
    print(f"A fruit of type: {fruit}")
    
# also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it
for i in change: #原理同上
    print(f"I got {i}")
    
# we can also build lists, first start with an empty one
elements = [] #新建空列表变量

# then use the range function to do 0 to 5 counts
for i in range(0, 6): # (0,6)是指0<=i<6循环循环6次
    print(f"Adding {i} to the list.")
# append is a function that lists understand
    elements.append(i) #向列表elements添加元素
    
# now we can print them out too
for i in elements:
    print(f"Elements was: {i}")

输出结果

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Elements was: 0
Elements was: 1
Elements was: 2
Elements was: 3
Elements was: 4
Elements was: 5

知识点

1. 列表
列表是个序列第一项索引为 0内容可为不同数据列表里可以有列表二维列表
创建列表时用 变量 = [内容内容] 格式即可。
2. for—loopfor循环
for循环可以遍历任何序列的项目如一个列表或者一个字符串。
语法
for 变量 in 序列列表/字典/字符串等
代码块操作语句
例子
输入

for letter in 'Python':     # 第一个实例
   print("当前字母: %s" % letter)

输出

当前字母: P
当前字母: y
当前字母: t
当前字母: h
当前字母: o
当前字母: n

3.append函数可以向列表末尾添加元素
语法
list.append( element )
element任何类型的元素
示例

c = [1,2,4,5]
c.append(6)
print(c)
c.append("num")
print(c)

输出

[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6, 'num'] #含字符串和数字

附加练习

1.如何使用 range 的。查阅上面的 range 函数并理解掌握。
range函数可创建一个整数列表一般用在for循环
语法range(start,stop[,step])
strat:计数从start开始。默认从0开始例如range(5)等于range0,5。
stop计数到 stop 结束。但不包括 stop。例如range0 5是[0, 1, 2, 3, 4]没有5。
step步长。默认为1。例如range0 5 等价于 range(0, 5, 1)
示例
range(9) # 从 0 开始到 9 [0, 1, 2, 3, 4, 5, 6, 7, 8]
range(1, 10) # 从 1 开始到 10 [1, 2, 3, 4, 5, 6, 7, 8, 9,]
range(0, 30, 6) # 步长为 6 [0, 6, 12, 18, 24]
range(0, -10, -1) # 负数 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0) #为空[ ]
range(1,0) #为空[ ]

2. 能在第 22 行不使用 for-loop而是直接把 range(0, 6) 赋给 elements 吗
可以。
代码如下

elements = range(0,6)

for i in elements:
    print(f"elements was: {i}")

输出结果


elements was: 0
elements was: 1
elements was: 2
elements was: 3
elements was: 4
elements was: 5

3. 找到 Python 文档关于列表的部分然后读一读。看看除了 append你还能对列表做哪些操作
列表是最常用的Python数据类型它可以作为一个方括号内的逗号分隔值出现与字符串的索引一样列表索引从0开始。列表可以进行截取、组合等。列表的数据项不需要具有相同的类型创建一个列表只要把逗号分隔的不同的数据项使用方括号括起来即可。
列表的操作有很多详见https://www.runoob.com/python/python-lists.html
举例
1 list.append(obj) 在列表末尾添加新的对象
2list.count(obj) 统计某个元素在列表中出现的次数
3list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值用新列表扩展原来的列表
代码

a = [1,2,3,5]
b = [6,8,9,0]
a.extend(b)

print("a + b = ",a)

输出

a + b =  [1, 2, 3, 5, 6, 8, 9, 0]

4 list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
5 list.insert(index, obj) 将对象插入列表
6 list.pop([index=-1]) 移除列表中的一个元素默认最后一个元素并且返回该元素的值
7 list.pop([index=-1]) 移除列表中的一个元素默认最后一个元素并且返回该元素的值
8 list.sort(cmp=None, key=None, reverse=False) 对原列表进行排序。

常见问题

1.如何创建一个二维列表
可以用这种列表中的列表[[1,2,3],[4,5,6]]
2. 列表lists和数组arrays难道不是一个东西吗
这取决于语言以及实现方法。在传统术语中列表和数组的实现方式不同。在 python 中都叫做 lists。所以我们就把这些叫做列表吧。
列表和数组使用的都是方括号[ ]区别如下
1创建方式不同在python之中列表是最基础的数据类型它可以直接使用list()函数或者是方括号来创建空或者有值的列表。但是数组则不同在python之中是没有数组这个数据类型的。它是由python内的第三方库科学计算库numpy中的函数所生成的
2元素不同列表和数组的最大区别就是列表内可以存储任意类型的元素、不论是数字还是字符串、哪怕是集合和字典都能够存储在列表之中。但是数组只能够用来存储单一类型的数据如果为整数那数组内的元素就必须都为整数
3运算方式不同当数组和数组之间进行加减乘除四则运算的时候是会对其中的每一个值就行运算的得出的结果就是运算后的结果。而列表只能使用加号进行拼接两个列表拼接后会返回一个新的列表。
3. 为什么 for-loop 可以用一个没有被定义的变量
变量在 for-loop 开始的时候就被定义了它被初始化到了每一次 loop 迭代时的当前元素中。
4. element.append() 的作用是什么
详情见知识点3

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python