本文目录一览:
求python题目解答(初学阶段)
列表lst中有4个元素,看有几个元素,就看逗号就好了,即便是嵌套列表,在两个逗号之间,也算一个元素,你可以使用len(lst)得到结果。
lst[3]的数据类型为列表,列表用[]表示。
lst[3][1][2]=10
lst[-1][-1][1]=9;
lst[-1][-1][3]=12;
lst[-1][-1][-3:]=[9, 10, 12];
lst[-1][-1][-3:][::-1]=[12, 10, 9] #::-1表示列表反转
求Python高手解答基本Python习题
#!/usr/bin/env python
# coding = utf-8
KNOWTREE = dict(
# does it have a backhone?
True = dict(
# does it give birth to live babies
True = "Mammal",
False = dict(
# does it have feathers
True = "Bird",
False = dict(
# does it have gills
True = "Fish",
False = dict(
# does it lay eggs in water
True = "Amphibian",
Flase = "Reptile",
),
),
),
),
False = dict(
# does it have a shell
True = "Mollusc",
False = dict(
# does it have 6 legs
True = "Insect",
False = "Arachind",
),
),
)
def which_animal(ans):
know = KNOWTREE
while isinstance(know, dict):
know = know[repr(ans.pop(0))]
return know
def movie_price(weekday, dayhour):
if weekday == "Tuesday":
return 10.75
elif weekday == "Wednesday":
return 5.75
elif weekday in ("Monday","Thursday","Friday") and dayhour 17:
return 12.75
else:
return 15.75
print which_animal([True,True,True,True,True,True,True,])
print which_animal([False,False,False,False,False,False,False,])
print movie_price("Tuesday", 4)
print movie_price("Saturday", 15)
print movie_price("Friday", 17)
print movie_price("Friday", 16)
Python中基础练习题?
法一:利用set()函数的去重功能,去重后再使用list()函数将集合转换为我们想要的列表
list1 = [11,22,33]
list2 = [22,33,44]
list3 = list(set(list1 + list2))
list3.sort()
print(list3)
-------------
法二:利用if和for,先遍历list1所有元素追加到list3中,然后遍历list2,条件判断list2中当前元素是否在list3中,如果不在则追加到list3中
list1 = [11,22,33]
list2 = [22,33,44]
list3 = []
for ele1 in list1:
list3.append(ele1)
for ele2 in list2:
if ele2 not in list3:
list3.append(ele2)
print(list3)