1.列表切片
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
c = a[:7]
print(c) # 输出: [0, 1, 2, 3, 4, 5, 6]
2.生成列表
v=[7,11,13]
print(max(v))
print(min(v))
print(sum(v))
print("%.2f" %float(sum(v)/len(v)))
print(list(range(4)))
print(list(range(7,11)))
print(list(range(1,14,3)))
print(list(range(15,2,-4)))
print(list(range(-20,20,6)))
print(list(range(-1,-22,-7)))
3.列表推导生成52张扑克牌
suits=['红','梅','黑','方']
ranks=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
cards=[(suit+rank) for suit in suits for rank in ranks]
print(cards)
4.求列表中最大值、最小值、求和
v=[7,11,13]
print(max(v))
print(min(v))
print(sum(v))
print("%.2f" %float(sum(v)/len(v)))
5.将fruits列表中的元素按字符串长度进行排序并输出
fruits=['grape','pear','apple','water melon']
fruits.sort(key=len)
print("sorted fruits by len:",fruits)
6.列表倒序
fruits=['grape','pear','apple','water melon']
fruits.reverse()
print("reversed fruits:",fruits)
7.列表将元素插入到制定位置
lst.insert(0,tmp)
列表删除指定位置的元素
lst.pop(-1)
8.判断是否是闰年
def lepyear(x):
if x%400==0:
return True
elif x%4==0 and x%100!=0:
return True
else:
return False
9.统计出该字符串英文字符、空格、数字和其他字符的个数
string=input()
sum1=0
sum2=0
sum3=0
sum4=0
for x in string:
print(x)
if 'a'<=x<='z' or 'A'<=x<='Z':
sum1+=1
elif x==' ':
sum2+=1
elif '0'<x<'9':
sum3+=1
else:
sum4+=1
print(sum1,sum2,sum3,sum4)
10.统计字符串中每个字母出现的次数
strlist=eval(input())
Count={}
for str in strlist:
for char in str:
Count[char]=Count.get(char,0)+1
for i in sorted(Count.keys()):
print("%s,%d"%(i,Count[i]))
11.水仙花数
num = int(input())
i=0
for x in list(range(100,num+1)):
if int(str(x)[0])**3+int(str(x)[1])**3+int(str(x)[2])**3 == x:
print(x)
i=1
else:
pass
if i == 0:
print("none")
12.对文件内每个字符进行大小写转换处理并输出!
with open("data.txt") as f:
for line in f.readlines():
tmp = ""
for x in line:
if 'a'<=x<='z' or 'A'<=x<='Z':
tmp+=x.upper()
else:
tmp+=x
print(tmp)