1.设计一个函数,只能接受两个参数,却能计算4个数之和
result = lambda a,b:a+b
print(result(5,6))
def fun1(* args) :
sum = sub =0
mul = 1
div = args[0]
for x in args :
sum += x
sub -= x
mul *= x
if x > 0:
div1 = div/x
sub1 = 2*args[0] + sub
print('和是{}'.format(sum))
print('差是{}'.format(sub1))
print('积是{}'.format(mul))
print('商是{}'.format(div1))
fun1(6,3)
def fun1(list) :
for x in list :
print(x)
if list.startswith('http://www.'):
print('是网址')
else:
print('不是网址')
raise Exception('FBI warning')
input1 = input('请输入你的网址')
fun1(input1)
def fun1(*args):
args1=list(map(int,args))
list1 = sorted(args1,key=lambda x:x)
print(list1)
fun1(1,2121,545,855,55,5,4,556,44,8,)
sum = 0
for x in range(1,101):
sum += x
print(sum)
6.计算n的n次方
n = int(input('请输入你的数字'))
print(n**n)
①
for ji in range(0,33):
for tu in range(0,25):
if(2*ji+4*tu==96)and(ji+tu==32):
print('有{}只鸡,有{}条兔'.format(ji,tu))
②
for x in range(1,24):
y = 32 - x
if 4*x + 2*y == 92:
print('兔子有%s只,鸡有%s只'%(x, y))
for a in range(0,34):
for b in range(0,51):
c = 100 - a -b
if c % 2 == 0 and a * 3 + b * 2 + c / 2 == 100:
print('大马%s,中马%s,小马%s' % (a,b,c))
9.有1,2,3,4,四个数字,能组成多少互不相同且无重复数字的三位数?都是多少
num = 0
num1=[]
for x in range(1,5):
for y in range(1,5):
for z in range(1,5):
if x != y and y != z and x !=z:
res = x*100+y*10+z
num1.append(res)
num+=1
print('共有{}种'.format(num),num1)
for x in range(1,10):
for y in range(1,10):
print(y,'x',x,'=',x*y,'\t',end='')
if x == y:
print('')
break
11.打印出1000以内的水仙花数
for x in range(100,1000):
i = x//100
j = x //10%10
k=x%10
if x==i**3+j**3+k**3:
print(x)
num = [1,2,5,74,3,5,6,1,6]
print(set(num))
13.讲一个a种元素复制到b 并正序排序
a = [1,132,32,45,4253,4,23,353,435,43,5,435,2,]
b=a[:]
print(sorted(b))
14.随意输入三个整数,请把三个数由小到大输出
a=[]
for i in range(3):
x = int(input('请输入数字:'))
a.append(x)
a.sort()
print(a[0],a[1],a[-1])
15.输入某月某日,判断这一天是一年的第几天?
dic = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}#用字典的方式来对应月份和天数最合适不过了,不用考虑位置的问题。
x = int(input('请输入月份:',))
y = int(input('请输入日期:',))
r=0
if x in range(1,13) and y in range(1,dic[x]):
for i in range(1, 13):
if i
16.代码实现extend ,将任意容器里面的所有元素放入到另一个容器当中
list =['a','b','c','d']
list1 = [1,2,5,7,8,3]
for x in list:
list1.append(x)
print(list1)
17.不能使用find方法,自己模拟find方法的实现过程判断字符串当中有没有包含指定字符,如果有,返回其在字符串当中的位置如果没有 返回-1
content = 'hello world i love you'
str1 = input('请输入你的字符串')
result = -1
for index,x in enumerate(content):
if str1 == content[index:index+len(str1)]:
# str2.append(index)
result = index
print(index)
break
if result == -1:
print(result)
18.使用不定长参数定义一个函数max_min返回最大值和最小值
def max_min(*args):
i = max(args)
j = min(args)
print('最大值为%s'%i)
print('最小值为%s'%j)
list = [110,25,6655,56,622,462]
max_min(*list)