#1
a_list1 = [12,13,14,15,16,17,18]
a_list1_len = len(a_list1)
if a_list1_len % 2 == 0:
print('列表中心元素为:%d 和 %d' % (a_list1[a_list1_len // 2], a_list1[a_list1_len // 2 - 1]))
else:
print('列表中心元素为: %d' % a_list1[a_list1_len // 2])
运行结果:列表中心元素为: 15
# 2
a_list2 = [1,2,3,4,5,6]
a_list2_sum = 0
for index in range(len(a_list2)):
a_list2_sum += a_list2[index]
print('元素之和为:', a_list2_sum)
运行结果:元素之和为: 21
# 3
print(a_list1[1::2], end=',')
运行结果:[13, 15, 17],
# 4
print('所有元素中值为奇数:')
for index in range(a_list1_len):
if a_list1[index] % 2:
print(a_list1[index], end=',')
运行结果:所有元素中值为奇数:
13,15,17,
#5
print()
for index in range(a_list1_len):
a_list1[index] = a_list1[index] * 2
print(a_list1, end=',')
运行结果:[24, 26, 28, 30, 32, 34, 36],
# 6
print()
names = ['张三', '李四', '大黄', '张三']
for name in names[:] :
# print(names.count(names[index]))
if names.count(name) >= 2:
names.remove(name)
print(names)
运行结果:['李四', '大黄', '张三']
# 7
a_list3 = [1, 98, 4563, 87, 6534]
for index in range(len(a_list3)):
a_list3[index] = chr(a_list3[index])
print(a_list3)
运行结果:['\x01', 'b', 'ᇓ', 'W', 'ᦆ']
# 8
a_scores = [99,98,97,96,95,94,93]
a_scores.sort()
print(a_scores)
del a_scores[0]
del a_scores[-1]
a_scores_num = 0
for score in a_scores:
a_scores_num += score
print('平均分是:', a_scores_num // len(a_scores))
运行结果:[93, 94, 95, 96, 97, 98, 99]
平均分是: 96
# 9
a_listA = [1,'a', 4,90]
a_listB = ['a', 8, 'j', 1]
a_list_commen = []
for elementA in a_listA:
for elementB in a_listB:
if elementA == elementB:
a_list_commen.append(elementB)
print(a_list_commen)
运行结果:[1, 'a']
# 周末1
age = int(input('请输入年龄:'))
if age <= 14:
print('儿童 ,未成年')
elif age < 18:
print('未成年')
elif age < 60:
print('成年人, 青壮年')
else:
print('老年人, 成年人')
运行结果:请输入年龄:20
成年人, 青壮年
# 2
rpp = 1
for rpp_num in range(1,6):
rpp *= rpp_num
print(rpp)
运行结果:120
# 3
sum1 = 0
rpp = 1
for sum_num in range(1,21):
for rpp_num in range(1,sum_num + 1):
rpp *= rpp_num
sum1 += rpp
rpp = 1
print(sum1)
运行结果:2561327494111820313
# 4
sum1 = 0
rpp = 1
for sum_num in range(1,21):
for rpp_num in range(1,sum_num + 1):
rpp *= rpp_num
sum1 += (1/rpp)
rpp = 1
print(sum1)
运行结果:1.7182818284590455
# 5
sum1 = 0
while True:
input_num = int(input('请输入数字:'))
if input_num == 0:
break
else:
sum1 += input_num
print(sum1)
运行结果:请输入数字:4
请输入数字:5
请输入数字:6
请输入数字:7
请输入数字:0
22
6
a = int(input('请输入a:'))
sum2 = 0
number = int(input('请输入几个数:'))
for i in range(0,number):
for j in range(0,i+1):
sum2 += a * 10 ** j
print(sum2)
运行结果:请输入a:2
请输入几个数:6
246912
# 7
numbers = []
for i in range(3):
numbers.append(input('请输入第%d个数字:' % (i+1)))
numbers.sort()
for num in numbers:
print(num,end=',')
运行结果:请输入第1个数字:15
请输入第2个数字:12
请输入第3个数字:16
12,15,16,
#8-1
n = int(input('请输入一个值:'))
for i in range(1,n+1):
for j in range(1,n-i+2):
print('*',end='')
print('\n')
运行结果:****
***
**
*
#8-2
str = ''
n = int(input('请输入一个奇数:'))
for i in range(1,n+1):
for j in range(1,i+1):
str += '*'
if i % 2:
print(str.center(n, ' '))
print('\n')
str = ''
运行结果:
请输入一个奇数:5
*
***
*****
# 9
for i in range(1,10):
for j in range(1,i+1):
print('%d×%d=%d ' % (j, i, i*j),end='')
print('\n')
运行结果:
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
# 10
for big in range(0,101):
for middle in range(0,101):
for small in range(0,101):
if big * 3 + middle * 2 + small / 2 == 100 and big + small + middle == 100:
print('大马:%d匹;中马:%d匹;小马:%d匹;' % (big, middle, small))
运行结果:
大马:2匹;中马:30匹;小马:68匹;
大马:5匹;中马:25匹;小马:70匹;
大马:8匹;中马:20匹;小马:72匹;
大马:11匹;中马:15匹;小马:74匹;
大马:14匹;中马:10匹;小马:76匹;
大马:17匹;中马:5匹;小马:78匹;
大马:20匹;中马:0匹;小马:80匹;
for i in range(0,101):
for j in range(0,101):
for k in range(0,101):
if i + j + k == 100 and 5 * i + 3 * j + 1 / 3 * k == 100:
print('公鸡:%d只;母鸡:%d只;雏鸡:%d只;' % (i,j,k))
运行结果:
公鸡:0只;母鸡:25只;雏鸡:75只;
公鸡:4只;母鸡:18只;雏鸡:78只;
公鸡:8只;母鸡:11只;雏鸡:81只;
公鸡:12只;母鸡:4只;雏鸡:84只;
12
for i in range(1,101):
for j in range(1,101):
for k in range(1,101):
if 15 * i + 2 *j + 5 * k == 100:
print('洗发水:%d瓶;香皂:%d盒;牙刷:%d只;' % (i, j ,k))
运行结果:
洗发水:1瓶;香皂:5盒;牙刷:15只;
洗发水:1瓶;香皂:10盒;牙刷:13只;
洗发水:1瓶;香皂:15盒;牙刷:11只;
洗发水:1瓶;香皂:20盒;牙刷:9只;
洗发水:1瓶;香皂:25盒;牙刷:7只;
洗发水:1瓶;香皂:30盒;牙刷:5只;
洗发水:1瓶;香皂:35盒;牙刷:3只;
洗发水:1瓶;香皂:40盒;牙刷:1只;
洗发水:2瓶;香皂:5盒;牙刷:12只;
洗发水:2瓶;香皂:10盒;牙刷:10只;
洗发水:2瓶;香皂:15盒;牙刷:8只;
洗发水:2瓶;香皂:20盒;牙刷:6只;
洗发水:2瓶;香皂:25盒;牙刷:4只;
洗发水:2瓶;香皂:30盒;牙刷:2只;
洗发水:3瓶;香皂:5盒;牙刷:9只;
洗发水:3瓶;香皂:10盒;牙刷:7只;
洗发水:3瓶;香皂:15盒;牙刷:5只;
洗发水:3瓶;香皂:20盒;牙刷:3只;
洗发水:3瓶;香皂:25盒;牙刷:1只;
洗发水:4瓶;香皂:5盒;牙刷:6只;
洗发水:4瓶;香皂:10盒;牙刷:4只;
洗发水:4瓶;香皂:15盒;牙刷:2只;
洗发水:5瓶;香皂:5盒;牙刷:3只;
洗发水:5瓶;香皂:10盒;牙刷:1只;