Python練習五-函數

一、簡述普通參數、指定參數、默認參數、動態參數的區別
1)普通參數中,有形參和實參,其中形參是定義函數時寫的參數,實參是調用函數時傳給函數的參數;
2)默認參數是在定義函數時,參數設定一個默認值;
3)指定參數是在調用有默認參數的函數時,指定了有默認值的參數;
4)動態參數是定義函數中參數是地址,調用此類函數時,能夠傳地址也能夠傳數據;

二、寫函數,計算傳入字符串中【數字】、【字母】、【空格] 以及 【其餘】的個數
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

三、寫函數,判斷用戶傳入的對象(字符串、列表、元組)長度是否大於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

四、寫函數,檢查用戶傳入的對象(字符串、列表、元組)的每個元素是否含有空內容。
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

五、寫函數,檢查傳入列表的長度,若是大於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

六、寫函數,檢查獲取傳入列表或元組對象的全部奇數位索引對應的元素,並將其做爲新列表返回給調用者。
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

七、寫函數,檢查傳入字典的每個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

八、寫函數,利用遞歸獲取斐波那契數列中的第 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
相關文章
相關標籤/搜索