1、简述普通参数、指定参数、默认参数、动态参数的区别
1)普通参数中,有形参和实参,其中形参是定义函数时写的参数,实参是调用函数时传给函数的参数;
2)默认参数是在定义函数时,参数设定一个默认值;
3)指定参数是在调用有默认参数的函数时,指定了有默认值的参数;
4)动态参数是定义函数中参数是地址,调用此类函数时,可以传地址也可以传数据;
2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
def str_count(s):
i = 0
num_count = 0
alpha_count = 0
space_count = 0
else_count = 0
print s
while i < length:
if s[i].isdigit() :
num_count += 1
elif s[i].isalpha():
alpha_count += 1
elif s[i].isspace():
space_count += 1
else:
else_count += 1
i += 1
print "数字的个数为:", num_count
print "字母的个数为:", alpha_count
print "空格的个数为:", space_count
print "其他的个数为:", else_count
st = raw_input()
length = len(st)
str_count(st)
View Code
3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
#方法一:判断传入对象的总长度(包括格式所占的长度)
def object_length(s):
if s[0] == "(" and s[-1] == ")":
print "传入的是元组,", s
elif s[0] == "[" and s[-1] == "]":
print "传入的是列表,", s
elif s[0] == "{" and s[-1] == "}":
print "传入的是字典,", s
print "长度是:", len(s)
if len(s) > 5:
print "长度超过5"
else:
print "长度没有超过5"
n = raw_input("请输入需要判断长度的对象:")
#object_length(n)
#方法二:判断传入对象的总长度(不包括格式所占的长度)
def element_length(s):
if s[0] == "(" and s[-1] == ")":
print "传入的是元组~"
elif s[0] == "[" and s[-1] == "]":
print "传入的是列表~"
elif s[0] == "{" and s[-1] == "}":
print "传入的是字典~"
else:
print "传入的是字符串~"
e_length = 0
for k, v in enumerate(s, 1):
# 判断是否是列表、元组、字典中格式
if v == "," or v == "(" or v == ")" or v == '''"''' or v == "'" or v == "[" or v == "]" or v == ":" or v == "{" or v == "}":
continue
else:
# print v,
e_length += 1
print s
print "元素的总长度是:", e_length
n = raw_input("请输入需要判断长度的对象:")
element_length(n)
View Code
4、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。
def object_space(s):
if s[0] == "(" and s[-1] == ")":
print "传入的是元组~", s
elif s[0] == "[" and s[-1] == "]":
print "传入的是列表~", s
else:
print "传入的是字符串~", s
e_sapce = 0
for k, v in enumerate(s, 1):
# 判断是否是列表、元组、字典中格式
if v == " ":
e_sapce += 1
else:
continue
print "元素是否含有空内容的个数:", e_sapce
n = raw_input("请输入对象:")
object_space(n)
#12 a3 "3 3" (1 ,"a b",d) [a ,b," 3"] {'k 1': 1,'k2': 3} [(1, 2),{'k 3':3,'k4': 4}, e]
View Code
5、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
def li_list(li):
length = 0
new_li = []
print li
for i in li:
length += 1
if length > 2:
new_li = li[0:2]
print new_li
#print length
return new_li
lis = [12,"afd@3$#","d3",4]
li_list(lis)
View Code
6、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
def odd_element(li):
print li
new_li = []
i = 0
for i in range(1, len(li)):
if i % 2 != 0:
#print li[i],
new_li.append(li[i])
i += 1
return new_li
old_li = ["0a", 1, "2b", 3, "4c", 5, 6, "7d"]
old_tup = ("0a", 1, "2b", 3, "4c", 5, 6, "7d")
print odd_element(old_li)
print odd_element(old_tup)
View Code
7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
PS:字典中的value只能是字符串或列表
def dict_element(dic):
print dic
new_dic = {}
for item in dic:
#print item,dic[item]
if len(dic[item]) > 2:
new_dic[item] = dic[item][0:2]
else:
new_dic[item] = dic[item]
return new_dic
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
print dict_element(dic)
View Code
8、写函数,利用递归获取斐波那契数列中的第 10 个数,并将该值返回给调用者。
def func(n):
if n == 1 or n == 2:
return 1;
#func(n) = func(n - 1) + func(n - 2)
return (func(n-1)+func(n-2))
print func(10)
View Code