01 列表
Q1:Python2与Python3的区别
num = input().split()
num_list = list(num)
print(len(num_list))
Tips: 注意Python2与Python3的区别。
Q2: append函数
# name = input().split()
# namelist = name.append('Allen')
# print(namelist)
list1= input().split(" ")
list1.append('Allen')
print(list1)
头部插入
list1.insert(0,"Allen")
print(list1)
Q3:del函数
name = input().split()
del name[0]
print(name)
Q4: remove函数
# name = input().split()
# name2 = input()
# name.remove(name2)
# print(name)
# list=input().split()
# name=input()
# list.pop(list.index(name))
# #list= [i for i in list if i!=name]
# #list.remove(name)
# print(list)
#新建一个列表存储输入的好友
list1 = input()
#列表中字符串以空格间隔
list2 = list1.split(" ")
#创建一个对象存储输入删除的好友
name = input()
#从列表中删除
list2.remove(name)
print(list2)
Q5: pop函数
name = input().split()
for i in range(3):
name.pop()
print(name)
Q6: sort与sorted的区别
# my_list = ['P'、'y'、't'、'h'、'o'、'n']
# sorted(my_list)
# print(my_list)
# sort(my_list)
# print(my_list)
my_list=['P','y','t','h','o','n']
print(sorted(my_list))
print(my_list)
my_list.sort(reverse=True)
print(my_list)