2018-01-25 Python if/while/for 及习题

python 缩进

if  

if 判断条件:

 执行语句

elif 判断条件:

 执行语句

else:

 执行语句

while

while 判断条件:

 执行语句

a=100
while a>1:
    print (a)
    a-=1
break 跳出循环

a=100
while a>1:
    print (a)
    a-=1
    if a==50:
        break

continue 进入下一次循环

a=100
while a>1:
    print (a)
    a-=1
    if a==50:
        break
    if a==55:
        print ('55555555')
        continue


for item in sequence:

 执行语句

l=['a','b','c','d','e']
print (l[:])
print(l[0:4])
print(l[0:-1])
for x,y in enumerate(l):
    print (x,y)
数学题

输入一行字符,分别统计出其中英文字母,空格,数字和其他字符 的个数。

status =1
while status:
    strings=input("please input a string('quit'will exit): ")
    if strings=="quit":
        exit(1)
    digit =pha=space=other=0
    for i in strings:
        if i.isdigit():
            digit+=1
        elif i.isalpha():
            pha+=1
        elif i.isspace():
            space+=1
        else:
            other+=1
    print('num{0},zimu{1},kongge{2},qita{3}'.format(digit,pha,space,other))


abcd*9=dcba ,a= b=,c= d=


for a in range(1,10):
    for b in range(0,10):
        for c in range(0,10):
            for d in range(1,10):
                if (a*1000+b*100+c*10+d)*9==(d*1000+c*100+b*10+a):
                    print('a={0}'.format(a))
                    print('b={0}'.format(b))
                    print('c={0}'.format(c))
                    print('d={0}'.format(d))
                    print('{0}*9={1}'.format(a * 1000 + b * 100 + c * 10 + d, d * 1000 + c * 100 + b * 10 + a))
1!+2!...n!

def one(n):
    total=1
    if n==1or n==0:
        total=1
    for i in (1,n+1):
        total=total*i
    return total

status=1
while status:
    sum=0
    n=input('input a num(n>1):')
    for i in n:
        if not i.isdigit():
            print('no')
            exit(1)
    if int(n)< 0:
        print ('number is wrong ')
        break
    else:
        for i in range(0,int(n)+1):
            sum=sum+one(i)
    print('0!+1!+2!+..n!={0}'.format(sum))

乘法口诀

for i in range(1,10):
    for j in range(1,i+1):
        print('{0}*{1}={2} '.format(j,i,i*j),end="")
        if i==j:
            print('')
1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值