一.猜拳游戏
import random
print("请输入:剪刀(0)、石头(1)、布(2),三种中的任意一个数字!!!")
a=float(input("请输入数字:"))
if a >= 0 and a <= 2:
print("您的输入为:", a)
b = random.randint(0, 2)
print("随机生成数字为:", b)
if a == b:
print("平局")
elif a > b:
print("恭喜您,您赢了!!!")
else:
print("哈哈哈,您输了!!!")
else:
print("请输入正确的值!!!")
二. 拿硬币
coin_list = [10, 8, 5, 3, 27, 99 ]
i = 0
a = 0
for i in coin_list:
if i % 2 == 0:
a += i // 2
else:
a += i // 2 + 1
print(f"最少需要拿{a}次")
三.找100以内的孪生数
m = 2
for i in range(2 ,101):
flage = True
for j in range(2, i // 2 + 1):
if i % j == 0:
flage = False
break
if flage == True:
if i - m == 2:
print(f"({m}, {i})")
m = i
四.给定列表求值
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr_min = arr[0]
arr_max = arr[0]
sum = 0
for i in arr:
if i > arr_max:
arr_max = i
if i < arr_min:
arr_max = i
sum += i
aver = sum / len(arr)
print(f"最大值为{arr_max}")
print(f"最小值为{arr_min}")
print(f"和为{sum}")
print(f"平均值为{aver}")
五.将list中的重复数据去掉
num1 = [1, 2, 3, 3, 4, 4, 5, 1]
num2 = []
for i in num1:
if i not in num2:
num2.append(i)
print(num2)
六.将两个列表进行合并操作
arr1 = ["asd", 123, "hahaha", 45]
arr2 = ["qwe", 147, "lalala", 66]
arr1.extend(arr2)
print(arr1)
七.使用列表判断令一个列表是否在另外一个列表中
um1 = [1, 2, 3, 4, 5, 6, 7]
num2 = [3, 4, 5]
for i in num2:
if i in num1:
print("num2在num1中")
break
else:
print("num2不在num1中")
break
八.reverse和sort
list = [1, 4, 5, 2, 6, 7, 3]
list.reverse()
print(f"反转{list}")
list.sort()
print(f"排序{list}")
九.将0~10随机存入列表
mport random
arr = list()
for i in range(10):
a = random.randint(0,10)
arr.append(a)
print(arr)
十.在列表中找重复值
arr = [1, 2, 6, 4, 6, 5, 3]
res = 0
for i in range(len(arr)):
res ^= i ^ arr[i]
print(res)
十一.水仙花数
for i in range(100,1000):
a=i//100
b= i//10 %10
c=i % 10
if i == a*a*a+b*b*b + c*c*c:
print(i)
print(a,b,c)
标签:练习题,10,arr,num2,python,list,列表,print
From: https://blog.csdn.net/2401_84698721/article/details/142980466