当前位置:   article > 正文

Python循环语句的应用_a=0,1,2,3,i=len(a-1)

a=0,1,2,3,i=len(a-1)

###梅洁第三天的作业
##倒叙

a=1234

i=0

while a>0:

i=i*10+a%10

a//=10

print(i)

a=1234

i=0

while a>0:

i=i*10+a%10

a//=10

print(i)

a=1234

i=0

while a>0:

i=i*10+a%10

a//=10

print(i)

###\n换行,\t空格

a=‘1\n2\n3\n4’

a=‘1\t2’

print(a)

###索引,len测长度

a=‘abcdef’

print(a[1],a[2],a[3])

print(len(a))

i=0

while i<len(a)-1:

print(“第%d个字符是%c”%(i+1,a[i]))

i+=1

a=‘abcdef’

i=0

while i<len(a)-1:

print(“第%d个字符是%c”%(i+1,a[i]))

i+=1

a=‘abcdef’

i=0

while i<len(a)-1:

print(“第%d个字符是%c”%(i+1,a[i]))

i+=1

###值的互换

a=4

b=3

a,b=b,a

print(a)

print(b)

###切片取值
a=‘123456789’

print(a[3:8])

print(a[3:8:2])

print(a[::-1])

print(a[2:])

print(a[:9])

print(a[-1])

print(a[::2])

print(a.find(‘123’))

print(a.find(‘1233’))

print(a.index(“1233”))

i=len(a)-1

while i>=0:

print(a[i])

i-=1

i=len(a)-1

while i>=0:

print(a[i])

i-=1

i=len(a)-1

while i>=0:

print(a[i])

i-=1

i=len(a)-1

while i>=0:

print(a[i])

i-=1

###replace替换,count总和

a=‘aabcabd’

print(a.count(‘ab’))

print(a.replace(‘ab’,‘12’))

###split分割

i=‘l-o-v-e’

print(i.split(’-’))

print(i.splitlines())

print(i.split(’-’,maxsplit=1))

print(i.partition(’-’))

a=‘123\n45\n67’

print(a.splitlines())

print(a.split(’\n’))

###capitalize首字母大写,title每个字母的首字母大写

a=‘asd’

print(a.capitalize())

i=‘i love you’

print(i.title())

###startswith以…开始,endswith以…结尾

i=‘中公教育’

print(i.startswith(‘中’))

print(i.endswith(‘育’))

###lower将句子中大写子母转换成小写字母,upper将句子中小写子母转换成大写字母

a=’ ABCdef ’

print(a.lower())

print(a.upper())

print(a.strip())

print(a.lstrip(),end=’ ')

print()

print(a.rstrip())

####isalpha判断a内是否全是字母,结果True;isdigit判断a内是否全是数字,结果True;isalnum#判断a内是否全是字母或数字,结果True

i=‘abc’

j=‘a2’

print(i.isalpha(),j.isalpha())

a=‘123’

b=‘12a’

print(a.isdigit(),b.isdigit())

m=‘123axc’

n=‘12sd_’

print(m.isalnum(),n.isnumeric())

###列表中可以增,删,改

a=[11,12,13,14]

a[1]=8

del a[2]

a.append(66)

a.insert(3,7)#嵌入

print(a)

##依次输出a中的数

i=0

while i<=len(a)-1:

print(“第%d个数是%d”%(i+1,a[i]))

i+=1

i=0

while i<=len(a)-1:

print(“第%d个数是%d”%(i+1,a[i]))

i+=1

i=0

while i<=len(a)-1:

print(“第%d个数是%d”%(i+1,a[i]))

i+=1

i=0

while i<=len(a)-1

print(“第%d个数是%d”%(i+1,a[i]))

i+=1

###拓展题,用while和for语句分别实现

a=[1,2,3,4,5,6,7,8,9]

start=int(input(“请输入起始值:”))

end=int(input(“请输入结束值:”))

bc=int(input(“请输入步长:”))

i=a[star?bc]

print(i)

##while输出

i=start

while i<end:

print(a[i],end=’ ')

i+=bc

i=start

while i<end:

print(a[i],end=’ ')

i+=bc

i=start

while i<end:

print(a[i],end=’ ')

i+=bc

i=start

while i<end:

print(a[i],end=’ ')

i+=bc

i=start

while i<end:

print(a[i],end=’ ')

i+=bc

##for语句输出

for i in range(start,end,bc):

print(a[i],end=’ ')

for i in a:

print(i)

###找出一下集合里的大小写字母,下划线,数字和符号,分别用while语句和for语句

a=“abLMG12_@@”

i=0

xzm=0

dzm=0

sz=0

xhx=0

fh=0

##while语句

while i<=len(a)-1:

if ‘a’<=a[i]<=‘z’:

print(i,“小写字母是”,a[i])

xzm+=1

elif ‘A’<=a[i]<=‘Z’:

print(i,“大写字母是”,a[i])

dzm+=1

elif ‘0’<=a[i]<=‘9’:

print(i,“数字是”,a[i])

sz+=1

elif a[i]==’_’:

print(i,“下划线是”,a[i])

xhx+=1

elif a[i]==’@’:

print(i,“符号是”,a[i])

fh+=1

i+=1

while i<=len(a)-1:

if ‘a’<=a[i]<=‘z’:

print(i,"",a[i])

xzm+=1

elif ‘A’<=a[i]<=‘Z’:

print(i,"",a[i])

dzm+=1

elif ‘0’<=a[i]<=‘9’:

print(i,"",a[i])

sz+=1

elif a[i]==’_’:

print(i,"",a[i])

xhx+=1

elif a[i]==’@’:

print(i,"",a[i])

fh+=1

##for语句

for zf in a:

if ‘a’<=zf<=‘z’:

print(“小写字母是”,zf)

xzm+=1

elif ‘A’<=zf<=‘Z’:

print(“大写字母是”,zf)

dzm+=1

elif ‘0’<=zf<=‘9’:

print(“数字是”,zf)

sz+=1

elif zf==’_’:

print(“下划线是”,zf)

xhx+=1

elif zf==’@’:

print(“符号是”,zf)

fh+=1

for zf in a:

if ‘a’<=zf<=‘z’:

print("",zf)

xzm+=1

elif ‘A’<=zf<=‘Z’:

print("",zf)

dzm+=1

elif ‘0’<=zf<=‘9’:

print("",zf)

sz+=1

elif zf==’_’:

print("",zf)

xhx+=1

elif zf==’@’:

print("",zf)

fh+=1

print(“小写字母的个数是:”,xzm)

print(“大写字母的个数是:”,dzm)

print(“数字的个数是:”,sz)

print(“下划线的个数是:”,xhx)

print(“符号的个数是:”,fh)

###extend扩展,pop弹出,remove移除

a=[1,2,3]

b=[4,5,6]

a.extend(b)

a.pop()

a.remove(3)

print(a)

###sort排序

a=[2,3,5,1,4]

a.sort(reverse=True)#reverse反转

print(a)

###倒序

a=[1,2,3,4,5,0]

i=0

j=len(a)-1

while i<j:

t=a[i]

a[i]=a[j]

a[j]=t

i+=1

j-=1

print(a)

a=[1,2,3,4,5,0]

i=0

j=len(a)-1

while i<j:

t=a[i]

a[i]=a[j]

a[j]=t

i+=1

j-=1

print(a)

a=[1,2,3,4,5,0]

i=0

j=len(a)-1

while i<j:

t=a[i]

a[i]=a[j]

a[j]=t

i+=1

j-=1

print(a)

###扩展题

a=[1,2,3,4,5,6]

t=a[0]

i=0

while i<len(a)-1:

a[i]=a[i+1]

i+=1

a[len(a)-1]=t

print(a)

a=[1,2,3,4,5,6]

t=a[0]

i=0

while i<len(a)-1:

a[i]=a[i+1]

i+=1

a[len(a)-1]=t

print(a)

a=[1,2,3,4,5,6]

t=a[0]

i=0

while i<len(a)-1:

a[i]=a[i+1]

i+=1

a[len(a)-1]=t

print(a)

a=[1,2,3,4,5,6]

t=a[0]

i=0

while i<len(a)-1:

a[i]=a[i+1]

i+=1

a[len(a)-1]=t

print(a)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/561702
推荐阅读
相关标签
  

闽ICP备14008679号