当前位置:首页>python>自学Python--Day3,创建列表、删除、插入数据、简单for、while循环

自学Python--Day3,创建列表、删除、插入数据、简单for、while循环

  • 2026-10-11 07:44:12
自学Python--Day3,创建列表、删除、插入数据、简单for、while循环
用的书上的练习题。
name=['HBY','ZX','LHQ','WTM','XYD','ZY','ZMC']print(name)message=f'these friends are will not attend the party:{name[0]}'print(message)name[0]='LXL'for friend in name:    message=f'come on, join the party:{friend}'    print(message)# The new version of the code uses a for loop to iterate through each friend in the list and print a message for each friend. This ensures that the message is personalized for each friend.name.insert(0,'HBY')name.insert(3,'LQR')name.append('XFY')for friend in name:    message=f'welcome to the party, {friend}!'    print(message)# The new version of the code uses the insert() method to add new friends to specific positions in the list and the append() method to add a friend to the end of the list. The for loop then iterates through the updated list and prints a welcome message for each friend.print('Sorry, the party can only accommodate two guests.')while len(name) > 2:    removed_friend = name.pop()    message=f'sorry, {removed_friend}, you cannot attend the party.'    print(message)print('The party is now full.')# The new version of the code uses a while loop to remove friends from the list until only two guests remain. The pop() method is used to remove the last friend from the list, and a message is printed to inform them that they cannot attend the party. Finally, a message is printed to indicate that the party is now full.for friend in name:    message=f'welcome to the party, {friend}!'    print(message)# The new version of the code uses a for loop to iterate through the remaining friends in the list and print a welcome message for each of them.del name[0]del name[0]print(name)
顺便给大家安利VSC,一个敲代码的工具,里面有AI插件,能够帮你自动补全代码,我敲这些代码的时候都震惊了,他竟然猜出我下一步想干什么,还帮我自动添加了注释。我用的书上的练习题,AI就顺着我的代码,猜到了下一题的内容。初学阶段有这个工具可以让你有参考,但是最好的学习手段依然是多敲,所以明天我将关闭AI

最新文章

随机文章