方法一:
解析:先生成50个随机整数,然后进行判断是否为整数,是则加入列表result[],然后排序输出
import random
ls = []
for i in range(50):
num = random.randint(1,100)
ls.append(num)
ls.sort()
print(ls)
result = []
for i in ls:
if i % 2 == 0:
result.append(i)
result.sort()
print(result)
方法二:
import random
ls = []
for i in range(50):
num = random.randint(1,100)
ls.append(num)
ls.sort()
print(ls)
for i in range(len(ls)- 1, -1, -1):
if ls[i] % 2 != 0:
del ls[i]
ls.sort()
print(ls)
x = input("请输入:")
n = len(str(x))
sum,i=0,0
for i in range(n):
sum+=int(x[i])**n
i+=1
if sum == int(x):
print("{0}是水仙花数".format(x))
else:
print("{0}不是水仙花数".format(x))
##字符串分段组合
str = input("请输入:")
s = str.split("-")
print(s[0]+'+'+s[-1])
##回文数判断
num = input("请输入:")
a = num[::-1]
if num==a:
print("是回文数")
else:
print("不是回文数")
while True:
try:
p=3.14159
r = float(input("请输入半径:"))
if r < 0:
print("输入半径不能小于0")
continue
else:
v = (4/3)*p*pow(r,3)
print("球体体积为:{}".format(v))
except:
print("输入有误")
for a in range(10):
for b in range(10):
for c in range(10):
for d in range(10):
n1 = pow(a,4)+pow(b,4)+pow(c,4)+pow(d,4)
n2 = a*1000+b*100+c*10+d
if n1==n2 and len(str(n1))==4:
print(n1)
输出四位玫瑰花数的最大数值:
ls=[]
for a in range(10):
for b in range(10):
for c in range(10):
for d in range(10):
n1 = pow(a,4)+pow(b,4)+pow(c,4)+pow(d,4)
n2 = a*1000+b*100+c*10+d
if n1==n2 and len(str(n1))==4:
ls.append(n1)
ls.sort()
print(ls[-1])
描述 获得用户的任何可能输入,将其中的英文字符进行打印输出,程序不出现错误。
alpha = []
for i in range(26):
alpha.append(chr(ord('a') + i))
alpha.append(chr(ord('A') + i))
s = input()
for c in s:
if c in alpha:
print(c, end="")
描述 获得用户输入的一个数字,可能是浮点数或复数,如果是整数仅接收十进制形式,且只能是数字。对输入数字进行平方运算,输出结果。
s = input()
try:
if complex(s) == complex(eval(s)):
print(eval(s)**2)
except:
print("输入有误")
ls=[]
for i in range(3):
num = input("请输入:")
ls.append(num)
ls.sort()
print(ls)
10.随机密码生成,以整数17作为随机数种子,获取用户输入作为密码长度,产生三个随机密码,密码的每一位为一个数字
##随机密码生成
import random
random.seed(17)
len = int(input("输入整数:"))
a = pow(10,len-1)
b = pow(10,len)-1
for i in range(3):
num = random.randint(a,b)
print(num)
描述
补充编程模板中代码,完成如下功能:获得用户输入数字N,计算并输出从N开始的5个质数,单行输出,质数间用逗号,分割。
注意:需要考虑用户输入的数字N可能是浮点数,应对输入取整数;最后一个输出后不用逗号。
##连续质数计算
def prime(m):
for i in range(2,m):
if m % i == 0:
return False
return True
n1 = eval(input("请输入:"))
n2 = int(n1)
n2 = n2+1 if n2 < n1 else n2
count = 5
while count > 0:
if prime(n2):
if count > 1:
print(n2, end=",")
else:
print(n2, end="")
count -= 1
n2 += 1
##数字不同数之和
sum = 0
num = set(input("请输入:"))
print(num)
for i in num:
sum+=eval(i)
print(sum)
##文本的平均行数
f = open("1.txt","r",encoding="utf-8")
s = 0
char = 0
for line in f:
line = line.strip('\n')
if len(line)==0:
continue
s+=1
print(s)
##csv格式清洗与转换
f = open("data.csv","w+",encoding="utf-8")
lines = f.readlines()
lines.reverse()
for line in f:
line = line.strip("\n")
ls = line.split(",")
ls = ls[::-1]
print(",".join(ls))
f.close()
##统计文件数据中字母e出现的次数(不区分大小写)
with open('1.txt', 'r', encoding='utf-8') as f:
res = f.read()
print('字符串e/E的个数为', res.count('e')+res.count('E'))
##编写程序生成一个10*10的矩阵,并保存为csv文件最后打开查看是否正确
import numpy as np
from numpy import random
a = random.randint(1,100,size=(10,10))
print(a)
f = open("data.csv","w+",encoding="utf-8")
for row in a:
f.write(",".join('%s' %id for id in row)+"\n")
f.close()
##编写程序检测二维数据中是否有重复数据,如果没有重复返回则函数返回为真
import numpy as np
def check():
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
u = np.unique(a)
if len(u)<len(a):
return False
else:
return True
print(check())
str1="1,2,3"
print(str1)
str1=str1.split(",")
print(str1)
ls = ['1','s','d','f','l']
print(ls)
lt=ls.copy()
print(lt)