则输出结果为:the longest words are:
Python
MATLAB
def proc(strings):
m = 0
lst = []
for i in range(len(strings)): #查找最长的单词
if len(strings[i]) > m:
m = len(strings[i])
for i in range(len(strings)):
if len(strings[i]) == m:
lst.append(strings[i])
return lst
strings = ['cad', 'VB', 'Python', 'MATLAB', 'hello', 'world']
result = proc(strings)
print("the longest words are:")
for item in result:
print("{: >25}".format(item))
while True:
try:
a = eval(input('请输入一个正整数: '))
if a > 0 and type(a) == int:
print(a)
break
else:
print("请输入正整数")
except:
print("请输入正整数")
std = [['张三',90,87,95],['李四',83,80,87],['王五',73,57,55]]
modl = "亲爱的{}, 你的考试成绩是: 英语{}, 数学{}, Python语言{}, 总成绩{}.特此通知."
for st in std:
cnt = 0
for i in range(1,4):
cnt += st[i]
print(modl.format(st[0],st[1],st[2],st[3],cnt))
a, b, c = [eval(x) for x in input().split(',')] #列表推导式:将a、b、c分别赋值
ls = []
for i in range(c) :
ls.append(str(a * (b ** i)))
print(",".join(ls))
等差数列通项公式: a n = a 1 + ( n − 1 ) ⋅ d a_n =a_1+(n−1)⋅d an=a1+(n−1)⋅d
等比数列通项公式: a n = a 1 ⋅ r n − 1 a_n=a_1⋅r^{n−1} an=a1⋅rn−1
import random
random.seed(100) # 此处可多行
s = 0 #
for i in range(3) :
n = random.randint(1, 9)
s += n ** 3 #此处可多行
print(s)
s = input("请输入一组数据: ")
ls = s.split(',')
lt = []
for i in ls:
lt.append(eval(i))
print(max(lt))
import random
s = input("请输入随机种子: ")
ls = []
for i in range(26):
ls.append(chr(ord('a')+i))
for i in range(10):
ls.append(chr(ord('0')+i))
random.seed(eval(s))
for i in range(10):
for j in range(8):
print(ls[random.randint(0,35)],end='')
print()
import turtle as t
import random as r
r.seed(1) #以1为随机数种子
t.pensize(2) #设置画笔宽度
for i in range(3):
length = r.randint(20,80)
x0 = r.randint(-100, 100)
y0 = r.randint(-100, 100)
t.penup() #抬起画笔,移动时不绘制轨迹
t.goto(x0,y0) #将画笔移动到指定坐标(x0,y0)
t.pd() #pd()是pendown()的缩写
for j in range(4):
t.fd(length)
t.seth(90 * (j + 1))
t.done()
def f(n):
s = 0
if n % 2 == 1:
for i in range(1, n + 1, 2):
s += 1/i
else:
for i in range(2, n + 1, 2):
s += 1/i
return s
n = int(input())
print('{:.2f}'.format(f(n)))
import random
n = eval(input('请输入一个整数:'))
random.seed(100)
for i in range(1,11):
if i < 10:
print(random.randint(1, n),end = ",")
else:
print(random.randint(1, n))
def allFactor(n) :
if n <= 1 :
return[n]
if n <= 3 :
return[1, n]
i = 1
rlist = []
while i <= n :
if n % i == 0 :
rlist.append(i)
i += 1
return rlist
try :
n = eval(input("请输入一个正整数:"))
print("整数{}的因子是:{}".format(n, allFactor(n)))
except :
print("输入错误!")
f = open('data1.txt')
ls = []
for line in f:
for c in line:
if c not in ls:
ls.append(c)
f.close()
print(len(ls))
import random as r
r.seed(1)
s = input("请输入三个整数 n,m,k:")
slist = s.split(",")
n, m, k = eval(slist[0]), eval(slist[1]), eval(slist[2])
for i in range(n) :
print(r.randint(k, m))
[表达式 for 变量 in 列表]
[out_exp_res for out_exp in input_list]
或者
[表达式 for 变量 in 列表 if 条件]
[out_exp_res for out_exp in input_list if condition]
a, b, c = [eval(x) for x in input().split(',')] #列表表达式
print('a:{0},b:{1},c:{2}'.format(a,b,c))
#输出结果:a:1,b:2,c:3
实例1:
>>> names = ['Bob','Tom','alice','Jerry','Wendy','Smith']
>>> new_names = [name.upper()for name in names if len(name)>3]
>>> print(new_names)
#['ALICE', 'JERRY', 'WENDY', 'SMITH']
实例2:
>>> multiples = [i for i in range(30) if i % 3 == 0]
>>> print(multiples)
#[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
{键表达式: 值表达式 for 迭代变量 in 可迭代对象}
{ key_expr: value_expr for value in collection }
或
{键表达式: 值表达式 for 迭代变量 in 可迭代对象 if 条件}
{ key_expr: value_expr for value in collection if condition }
实例1:
listdemo = ['Google','Runoob', 'Taobao']
# 将列表中各字符串值为键,各字符串的长度为值,组成键值对
>>> newdict = {key:len(key) for key in listdemo}
>>> newdict
#{'Google': 6, 'Runoob': 6, 'Taobao': 6}
实例2:
>>> dic = {x: x**2 for x in (2, 4, 6)}
>>> dic
#{2: 4, 4: 16, 6: 36}
>>> type(dic)
#
{元素表达式 for 迭代变量 in 可迭代对象}
{ expression for item in Sequence }
或
{元素表达式 for 迭代变量 in 可迭代对象 if 条件}
{ expression for item in Sequence if conditional }
实例1:
>>> setnew = {i**2 for i in (1,2,3)}
>>> setnew
#{1, 4, 9}
实例2:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
#{'d', 'r'}
>>> type(a)
#
(元素表达式 for 迭代变量 in 可迭代对象)
(expression for item in Sequence )
或
(元素表达式 for 迭代变量 in 可迭代对象 if 条件)
(expression for item in Sequence if conditional )
实例:
>>> a = (x for x in range(1,10))
>>> a
# at 0x7faf6ee20a50> # 返回的是生成器对象
>>> tuple(a) # 使用 tuple() 函数,可以直接将生成器对象转换成元组
#(1, 2, 3, 4, 5, 6, 7, 8, 9)