【1】a=[8,13,11,6,26,19,24]
1)请输出列表a中的奇数项
2)请输出列表a中的奇数
解:1)
1 a=[8,13,11,6,26,19,24] 2 print a[::2]
Result:
>>>
[8, 11, 26, 24]
2)
1 a = [8,13,11,6,26,19,24] 2 b = [] 3 for item in a: 4 if item%2 !=0: 5 b.append(item) 6 else: 7 continue 8 print b
Result:
>>>
[13, 11, 19]
【2】st = ‘Hello Python DuShuSir’请输出st字符串中偶数位上的字符
解:方法一:
1 st = 'Hello Python DuShuSir' 2 sr ='' 3 i=0 4 while i<len(st): 5 if i%2!=0: 6 sr +=st[i] 7 i +=1 8 print sr
方法二:
1 st = 'Hello Python DuShuSir' 2 print st[1::2]
Result:
>>>
el yhnDSui
【3】已知字符串 a = “dUsHUsIR6cOM6”,要求 :
1)请将a字符串改为小写或改为大写
2)将a字符串中的小写改为大写、大写改为小写
解:1)
1 a = "dUsHUsIR6cOM6" 2 print a.upper() 3 print a.lower()
Result:
>>>
DUSHUSIR6COM6
dushusir6com6
2)
1 a = "dUsHUsIR6cOM6" 2 b = "" 3 i=0 4 while i<len(a): 5 if a[i].isupper(): 6 b +=a[i].lower() 7 elif a[i].islower(): 8 b +=a[i].upper() 9 elif a[i].isdigit(): 10 b +=a[i] 11 i +=1 12 print b
Result:
>>>
DuShuSir6Com6
源自:DuShuSir