Python 入门教程 14 ---- Practice Makes Perfect

来源:互联网 发布:网络管理吧 编辑:程序博客网 时间:2024/06/07 20:59


 第一节

     1 介绍了Python的一种内置方法type(x),用来判断x的类型,比如type(5)是int,type("asf")是string等等

     2 练习:写一个函数为is_int,x作为参数,判断x是否为整数,但是要注意的是如果x的小数点全部为0那么x也认为是整数比如7.0

def is_int(x):    if type(x) == int or x == int(x):        return True    else:        return False


 第二节

     1 练习:写一个函数为digit_sum(),n作为参数,求n所有位数的和

def digit_sum(n):    sum = 0    while n > 0:        sum += n%10        n /= 10    return sum


 第三节

     1 练习:写一个函数factorial,参数为x,求x的所有位数的乘积

def factorial(x):    sum = 1    while x > 0:        sum *= x        x -= 1    return sum


 第四节

     1 写一个函数is_prime,参数是x,判断x是否为质数

def is_prime(x):    if x <= 1:        return False    for i in range(2,x):        if x%i == 0:            return False    return True


 第五节

     1 首先介绍了我们可以使用[::-1]来反转一个列表,一个字符串,比如my_list = [1,2,3,4],那么my_list[::-1]就是为[4,3,2,1]

     2 练习:写一个函数为reverse,参数是一个字符串text,把text逆向的串输出,比如"abcd"应该输出为"dcba"

def reverse(text):    length = len(text)    str = ""    while length > 0:        length -= 1        str += text[length]    return str


 第六节

     1 练习:写一个函数为anti_vowel,参数是字符串text,要求把所有是元音的字母全部去掉然后返回

def anti_vowel(text):    str = ""    length = len(text)    for i in range(length):        x = text[i].lower()        if x != "a" and x != "i" and x != "o" and x != "u" and x != "e":            str += text[i]    return str


 第七节

     1 练习:写一个函数为scrabble_score参数为字符串word,根据提供的字典,求出word的得分

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,          "x": 8, "z": 10}def scrabble_score(word):    sum = 0    length = len(word)    for i in range(length):        x = word[i].lower()        sum += score[x]    return sum


 第八节

     1 练习:写一个函数为censor,参数为两个字符串text和word,要求把text里面出现的word换成对应长度的"*"串

                    比如text为"hack is hack"word为"hack",那么输出为"**** is ****"

def censor(text , word):    len_text = len(text)    len_word = len(word)    str = ""     i = 0        while i < len_text:        isOk = True        pos = 0        for j in range(i,i+len_word):            if j >= len_text or text[j] != word[pos]:                isOk = False                break            else:                pos += 1        if isOk:            for j in range(len_word):                str += "*"            i += len_word        else:            str += text[i]               i += 1    return str


 第九节

      1 练习:写一个函数为purify,参数是一个列表,要求把列表中的所有奇数删除,然后返回这个列表

def purify(my_list):    while True:        isOk = False;        for num in my_list:            if num%2:                my_list.remove(num)                isOk = True                break        if isOk == False:            break    return my_list


 第十节

      1 练习:写一个函数remove_duplicates,参数是一个列表,要求把所有的相同的元素去掉,返回一个新的列表

def remove_duplicates(my_list):    my_list.sort()    res = []    length = my_list    i = 0    for num in my_list:        if (num in res):           continue        else:           res.append(num)    return res