#直接用while循环去除.ad l=['xuyutao.ad','wangjun.ad','liuchao.ad','liuyan1.ad'] n=0 L=[] while n < 4: A=l[n][:-3] L.insert(n,A) n=n+1 print(L)
#while+定义函数去除.ad def xad(x): if x[-3:] in '.ad': a=x[:-3] return a else: return L=['xuyutao.ad','liuchao6.ad','liuyan1.ad','wagnjun.ad'] l=[] n=0 while n<4: l.insert(n,xad(L[n])) n=n+1 print(l)
# 直接用while列出1×1,2×2,3×3,...,10×10的值 x=0 n=0 l=[] while x<=10: l.insert(n,x*x) x=x+1 n=n+1 print(l)
#定义函数+while列出1×1,2×2,3×3,...,10×10的值 def squ(x): y=x*x return y l=[] n=0 while n < 10: l.insert(n,squ(n+1)) n=n+1 print(l)
#去除字符首尾的空格 def trim(x): if x[:1] ==' ': return x[1:] elif x[-1:] ==' ': return x[:-1] else: return x print(trim('xinghen ')) print(trim(' xinghen')) print(trim('xinghen'))
注:Python表示“等于”要用==,表示空格要用' '