本帖最后由 WoShiXXX 于 2021-11-3 17:32 编辑
由于楼主最近时间有点紧,所以今天不更新太多新内容,处理之前的练习。楼主为八年级学生,想要冲冲编程竞赛,但无奈自己智商不够,没法撸各种算法题,只好先从比较简单的Python开始,开一些帖子记录一下。由于我有前端基础,所以我并不是0基础,而是0.2基础。我随时可能拖更,如果我长时间拖更,欢迎催更!我选用的是Python编程:从入门到实践(第2版).pdf这本书当作教材,这本书前30页都是废话。
练习3-1至3-7
[Python] 纯文本查看 复制代码 names = ["Zhang San", "Li Si", "Wang Wu"]
for name in names:
print(name)
for name in names:
print(f"Hello, my friend {name}")
print("Would you like to come to my home for dinner?")
print(f"But {names[0]},you can't come,so I'll remove you in the list.")
names.remove("Zhang San")
names.append("Zhao Liu")
print(f"I invited {names[2]}")
print("Oh, I found a bigger table.I'll invite more people!")
names.insert(0,"Grandpa")
names.insert(3,"Father")
names.append("Mother")
print(names)
print(f"I invited {len(names)} people.")
练习4-3:
[Python] 纯文本查看 复制代码 for value in range(1,21):
print(value)
练习4-4、4-5:
[Python] 纯文本查看 复制代码 numbers = range(1,1_000_001)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
练习4-6至4-10
[Python] 纯文本查看 复制代码 print(list(range(1,21,2)))
print(list(range(3,31,3)))
nums = list(range(1,11))
newNums = []
for num in nums:
newNums.append(num ** 3)
print(newNums)
Nums2 = [value ** 3 for value in range(1,11)]
print(Nums2)
print(f"The first three items are:{nums[0:3]}")
print(f"Three items from the middle of the list are:{nums[3:6]}")
print(f"The last three items are:{nums[7:11]}")
元组
元组与列表类似,不过其中的值不能修改,用圆括号而不是方括号括起来表示。[Python] 纯文本查看 复制代码 dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
但当只有一个元素时,需要用逗号分隔,否则会产生歧义。
[Python] 纯文本查看 复制代码 my_t = (3,)
我们不能修改元组的值,但可以给元组变量一个新的元组(这也印证了之前说过的:只是一个指针,而不是一个存储值的盒子)
[Python] 纯文本查看 复制代码 dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
练习4-13我的答案:
[Python] 纯文本查看 复制代码 foods = ("Hamburger", "Pizza", "Chips", "Fried chicken", "Tomato soup")
for food in foods:
print(food)
foods = ("Hamburger", "Pizza", "Gongbao chicken", "Fried chicken", "Roasted duck")
for food in foods:
print(food)
|