python academy

倒序

def reverse(x):
    length = len(x)
    rev = ''
    for i in range(length-1,-1,-1):
        rev += x[i]
    return rev

reverse("abcd")

去除元音字母

def anti_vowel(x):
    length = len(x)
    strs = ''
    for i in range(length):
        if x[i] not in "aeiouAEIOU":
            strs += x[i]
    return strs
print(anti_vowel("Heay"))

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):
    sums = 0
    for i in word.lower():
        sums += score[i]
    return sums

去除单词并用*替代

def censor(text, word):
    i2 = ""
    tempList = []
    textList = text.split()
    for i in textList :
        if i == word:
            length = len(i)
            i = "*" * length
        tempList.append(i)
    return ' '.join(tempList)

censor("hello world","hello")

median注意2.0

def median(list):
    sorted_list = sorted(list)
    length = len(list)
    if length % 2 == 0:
        mid = (length/2)
        midianNumber = (sorted_list[mid-1] + sorted_list[mid])/2.0
    else :
        mid = int(length/2)
        midianNumber = sorted_list[mid]
    return midianNumber

filter lambda

squares = [x**2 for x in range(1,11)]
print filter (lambda x: x>=30 and x<=70,squares)

for if

threes_and_fives = [x for x in range(1,16) if x%3==0 or x%5==0]
print threes_and_fives
garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = ''
for i in range(0,len(garbled))[::-1] :
    if garbled[i] != 'X':
        message += garbled[i]
print message
garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

message = filter(lambda x: x != 'X',garbled)
print message

继承与派生

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
    def calculate_wage(self,hours):
        self.hours = hours
        return hours * 12.00
    def full_time_wage(self, hours):
        return super(PartTimeEmployee,self).calculate_wage(hours)

milton = PartTimeEmployee("clz")
print milton.full_time_wage(10)

你可能感兴趣的:(python)