廖雪峰官网自学python 切片(附练习题答案)

# -*- coding:utf-8 -*-
L = []
n = 1
while n < 99:
    L.append(n)
    n = n + 2
print(n,L)

def fact(n):
    if n == 1:
        return 1
    return n * fact(n-1)
print(fact(3))

def fact(n):
    return fact_iter(n,1)


def fact_iter(num,product):
    if num == 1:
        return product
    return fact_iter(num -1 ,num * product)

print(fact(4))


L = ['Michael','Bob','yangchun','Kobe','james']
count = 0
for i in L:
    count = count + 1
    if count > 3:
        break
    print(i)

r = []
n = 3
for i in range(n):
    print(L[i])
    r.append(L[i])
print(r)

print(L[0:3])
print(L[:3])
print(L[1:3])
print(L[-2:])
print(L[-1:])
print(L[:-1])
print(L)
L = list(range(100))
print(L)
print(L[:10])
print(L[-10:])
print(L[:10:2])
print(L[1:10:3])
print(L[:])
print(L[::5])
tuplea = tuple(range(10))
print(tuplea)
print(tuplea[:10])
print(tuplea[:5])
str1 = 'abcdefg'
print(str1[:3])
print(str1[::3])
# -*- coding:utf-8 -*-
#s = '  hello world'
#def trim(s):
  #  for i in len(s):
 #       if s[i] != '':
  #          s = s[i:]
  #  return s
#print(trim(s))

str = ' hello wordl '
def trim(str):
    i=0;j=-1;
    if(len(str)==0):
        return str;
    else:
        while(True):
            if str[i]==' ':
                i+=1
                if(i==len(str)):
                    i=0;
                    break
            else:
                break
            while(True):
                if str[j]==' ':
                    j = j-1
                    if((-j-1)==len(str)):
                        j=0
                        break
                    else:
                        j=j+len(str)+1
                        break
        str=str[i:j]
        return str
print(trim(str))

你可能感兴趣的:(python)