Day_4 · 列表:一串数据 #5

Open
opened 2026-08-22 17:26:34 +08:00 by shimmer · 38 comments
Owner

题目与教程详见仓库对应目录 Day_4/README.md(本仓库已克隆即得)。

预计耗时:20~30 分钟
列表就是一串排好队的数据。以后 ORM 查出来的"一堆结果",本质上就是一串对象。

今日知识点

  • 创建:fruits = ["apple", "banana"]
  • 增:fruits.append("orange") 尾部加;fruits.insert(0, "pear") 指定位置加
  • 删:fruits.remove("banana") 按值删
  • 查:fruits[0] 第一个、fruits[-1] 最后一个、len(fruits) 个数
  • 遍历:for f in fruits: print(f)

先学(教程)

任务(后做)

新建 day4.py,按顺序写并运行:

  1. 创建一个水果列表:
    fruits = ["apple", "banana"]
    print(fruits)
    
  2. 追加两个水果,删除一个,再打印:
    fruits.append("orange")
    fruits.append("grape")
    fruits.remove("banana")
    print(fruits)          # 现在有哪些?
    
  3. 遍历打印 + 长度:
    for f in fruits:
        print(f"我喜欢吃{f}")
    print(f"一共{len(fruits)}种水果")
    
  4. 打印第一个和最后一个:
    print(f"第一个:{fruits[0]}")
    print(f"最后一个:{fruits[-1]}")
    
  5. 进阶(加分):nums = [3, 1, 4, 1, 5],试试 nums.sort() 和 nums.reverse() 后分别打印。

完成标准

  • 四步输出都符合预期(增删改查 + 遍历)
  • 能说出 append 和 remove 的作用
  • (进阶)sort / reverse 结果正确

提交方式:把 day4.py 代码贴在当日工单评论里。

参考解答(先做再看)

fruits = ["apple", "banana"]
fruits.append("orange")
fruits.append("grape")
fruits.remove("banana")
print(fruits)              # ['apple', 'orange', 'grape']

for f in fruits:
    print(f"我喜欢吃{f}")
print(f"一共{len(fruits)}种水果")   # 一共3种水果

print(f"第一个:{fruits[0]}")       # apple
print(f"最后一个:{fruits[-1]}")    # grape

nums = [3, 1, 4, 1, 5]
nums.sort()
print(nums)                # [1, 1, 3, 4, 5]
nums.reverse()
print(nums)                # [5, 4, 3, 1, 1]
> 题目与教程详见仓库对应目录 Day_4/README.md(本仓库已克隆即得)。 > 预计耗时:20~30 分钟 > 列表就是一串排好队的数据。以后 ORM 查出来的"一堆结果",本质上就是一串对象。 ## 今日知识点 - 创建:`fruits = ["apple", "banana"]` - 增:`fruits.append("orange")` 尾部加;`fruits.insert(0, "pear")` 指定位置加 - 删:`fruits.remove("banana")` 按值删 - 查:`fruits[0]` 第一个、`fruits[-1]` 最后一个、`len(fruits)` 个数 - 遍历:`for f in fruits: print(f)` ## 先学(教程) - 菜鸟教程("列表"一节,重点看增删改查和遍历):https://www.runoob.com/python3/python3-list.html - B站黑马教程(看讲"列表"的几集):https://www.bilibili.com/video/BV1qW4y1a7fU/ ## 任务(后做) 新建 `day4.py`,按顺序写并运行: 1. 创建一个水果列表: ```python fruits = ["apple", "banana"] print(fruits) ``` 2. 追加两个水果,删除一个,再打印: ```python fruits.append("orange") fruits.append("grape") fruits.remove("banana") print(fruits) # 现在有哪些? ``` 3. 遍历打印 + 长度: ```python for f in fruits: print(f"我喜欢吃{f}") print(f"一共{len(fruits)}种水果") ``` 4. 打印第一个和最后一个: ```python print(f"第一个:{fruits[0]}") print(f"最后一个:{fruits[-1]}") ``` 5. **进阶(加分)**:`nums = [3, 1, 4, 1, 5]`,试试 `nums.sort()` 和 `nums.reverse()` 后分别打印。 ## 完成标准 - [ ] 四步输出都符合预期(增删改查 + 遍历) - [ ] 能说出 `append` 和 `remove` 的作用 - [ ] (进阶)sort / reverse 结果正确 **提交方式**:把 `day4.py` 代码贴在当日工单评论里。 ## 参考解答(先做再看) ```python fruits = ["apple", "banana"] fruits.append("orange") fruits.append("grape") fruits.remove("banana") print(fruits) # ['apple', 'orange', 'grape'] for f in fruits: print(f"我喜欢吃{f}") print(f"一共{len(fruits)}种水果") # 一共3种水果 print(f"第一个:{fruits[0]}") # apple print(f"最后一个:{fruits[-1]}") # grape nums = [3, 1, 4, 1, 5] nums.sort() print(nums) # [1, 1, 3, 4, 5] nums.reverse() print(nums) # [5, 4, 3, 1, 1] ```
![image.png](/attachments/30ffc0ab-ec33-4843-b48b-99fd171b005b) https://github.com/wyn-netizen/hello-daily-challenge
178 KiB
image.png
<img width="983" alt="image.png" src="attachments/fcef592f-9fad-448e-9d1d-b8e3e9bb2856">
153 KiB

image.png

![image.png](/attachments/2198df31-54ee-4c8c-9518-3346e44b0956)
170 KiB

image.png

![image.png](/attachments/c1a635ca-bedc-4b54-9ea5-f5e712bd0c84)
image.png
<img width="424" alt="image.png" src="attachments/ac734c8e-ec16-4b18-a629-e8475c8e6692">
image.png
<img width="752" alt="image.png" src="attachments/f896f7c8-6bc2-4dbe-beb0-f34929a11e08">
image.png
<img width="781" alt="image.png" src="attachments/6d1df343-c1f5-4bd1-80d1-15d74ba4b437">
250 KiB
image.png
<img width="303" alt="image.png" src="attachments/80517bf7-b483-4d36-99fb-f8338c9189b4">
屏幕截图 2026-09-14 225151.png
<img width="1418" alt="屏幕截图 2026-09-14 225151.png" src="attachments/d04171c5-0143-49b9-828d-1afd0d593a19">
image.png
<img width="1525" alt="image.png" src="attachments/440365e6-fd03-4aa4-80df-c96c05793174">
229 KiB

QQ_1789446068993.png

![QQ_1789446068993.png](/attachments/07251c1b-a9a9-42bd-a240-aa3ce338280e)
image.png
<img width="1600" alt="image.png" src="attachments/c9c7d7ed-ff38-4a09-9eee-6ad946d58c57">
280 KiB
image.png
<img width="1280" alt="image.png" src="attachments/efc2b1f0-26e5-46c2-bb33-09fd6c860f43">
520 KiB
image.png
<img width="295" alt="image.png" src="attachments/6ab3abfe-5622-441d-900a-c53d34e62b14">
100 KiB
屏幕截图 2026-09-15 223841.png
<img width="1277" alt="屏幕截图 2026-09-15 223841.png" src="attachments/c3d0cfc0-82cb-4411-8146-d078aa481aee">
Owner

image.png

![image.png](/attachments/2d616780-2388-4ed0-a057-5aaa6457ab0c)
201 KiB

王士宇

王士宇
image.png
<img width="1152" alt="image.png" src="attachments/9b916a72-3cd5-4890-a010-8a90a6fff820">
156 KiB
36437587c94a36cfd8e28a05efd5e683.png
<img width="879" alt="36437587c94a36cfd8e28a05efd5e683.png" src="attachments/39296eb3-f93b-497d-a2f2-0ab054438c1a">

day4完成

day4完成

image.png

# day4.py
fruits = ["apple", "banana"]
print(fruits)

fruits.append("orange")
fruits.append("grape")
fruits.remove("banana")
print(fruits)          # ['apple', 'orange', 'grape']

for f in fruits:
    print(f"我喜欢吃{f}")
print(f"一共{len(fruits)}种水果")

print(f"第一个:{fruits[0]}")
print(f"最后一个:{fruits[-1]}")

nums = [3, 1, 4, 1, 5]
nums.sort()
print(nums)
nums.reverse()
print(nums)
![image.png](/attachments/36938afa-9854-4baa-95cf-4b48e0ac25bb) ```python # day4.py fruits = ["apple", "banana"] print(fruits) fruits.append("orange") fruits.append("grape") fruits.remove("banana") print(fruits) # ['apple', 'orange', 'grape'] for f in fruits: print(f"我喜欢吃{f}") print(f"一共{len(fruits)}种水果") print(f"第一个:{fruits[0]}") print(f"最后一个:{fruits[-1]}") nums = [3, 1, 4, 1, 5] nums.sort() print(nums) nums.reverse() print(nums) ```
133 KiB

image.png

![image.png](/attachments/643ef487-09c7-44f6-a728-377266df7acf)
260 KiB
image.png
<img width="688" alt="image.png" src="attachments/8233323d-fa09-4ef4-9184-33227fd8278c">
155 KiB

image.png

![image.png](/attachments/2a062c58-c55f-4f84-b0f7-0075cf06b0e8)
327 KiB

image.png

![image.png](/attachments/1ac568c2-7c54-4f66-b68c-bc139a67ba8f)
367 KiB

image.png

![image.png](/attachments/915eff67-b649-4084-8d07-f874ccb82574)

fruits = ["apple", "banana"] print(fruits) fruits.append("orange") fruits.append("grape") fruits.remove("banana") print(fruits) for f in fruits: print(f"我喜欢吃{f}") print(f"一共{len(fruits)}种水果") print(f"第一个:{fruits[0]}") print(f"最后一个:{fruits[-1]}") #append:用于向列表中添加元素 #remove:用于从列表中移除元素

`fruits = ["apple", "banana"] print(fruits) fruits.append("orange") fruits.append("grape") fruits.remove("banana") print(fruits) for f in fruits: print(f"我喜欢吃{f}") print(f"一共{len(fruits)}种水果") print(f"第一个:{fruits[0]}") print(f"最后一个:{fruits[-1]}") #append:用于向列表中添加元素 #remove:用于从列表中移除元素`

Day_4 · 列表(增删、遍历、排序)

day4.png

Day_4 · 列表(增删、遍历、排序) ![day4.png](https://cdn.jsdelivr.net/gh/xu675693517-hue/hello-daily-challenge@main/screenshots/day4.png)
Sign in to join this conversation.