诚意
诚意如你,当一诚的态度对待

导航

 

一:写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

法一:

 1 def jisu(con):
 2     content = []
 3     if type(con) == list or type(con) == tuple:
 4         for i in range(len(con)):
 5             if i %2 == 0:
 6                 content.append(con[i])
 7         return content
 8     else:
 9         return '您打印的不是元祖或列表'
10 li={1,2,3}
11 print(jisu(li))

法二:

1 def func(lst):
2     return  lst[0::2]
3 li=[1,2,3]
4 print(func(li))
二:写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。

法一:

 1 def length(con):
 2     #判断用户传入的对象(字符串、列表、元组)长度是否大于5
 3     count = 0
 4     if type(con) == list or type(con) == tuple or type(con) == str:
 5         for i in con :
 6             count += 1
 7         return  count
 8     else :
 9         return "您输入的对象不是字符串或列表或元组"
10 
11 li =23
12 print(length(li))

法二:

1 def func(a):
2     return  len(a)>5

三:写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者

法一:

 1 def length(con):
 2     count = 0
 3     if type(con) == list :
 4         for i in con :
 5             count += 1
 6             if count > 2:
 7                 return con[0],con[1]
 8         else:
 9             return  "列表的长度小于等于2"
10     else :
11         return "您输入的对象不是列表"
12 
13 li =[1,2,4]
14 print(length(li))

法 二:

1 def func(a) :
2     if len(a) > 2:
3         return a[0:2]

四:写函数,接收两个数字参数,返回比较大的那个数字。

法一:

 1 def max(a,b):
 2     #接收两个数字参数,返回比较大的那个数字
 3     if type(a)== int and type(a)== int :
 4         if a > b :
 5             return a
 6         elif a < b:
 7             return b
 8         else:
 9             print("两个数相等")
10     else:
11         print("您输入的不是数字")
12 
13 c = 2
14 d = 4
15 print(max(c,d))

法二:

1 def func(a,b):
2     return  a if a > b else b

 

posted on 2018-08-09 17:34  诚意  阅读(168)  评论(0)    收藏  举报