美文网首页
简单的Python小程序

简单的Python小程序

作者: 传棋Jaking | 来源:发表于2018-03-22 18:45 被阅读0次

    简单的Python小程序

    1.打印数字1-10到屏幕

    #!/usr/bin/env python
    #print 1-10
    i = 1
    while i<=10:
        print i
        i += 1
    print('--------------------')
    j = 1
    while j<=10:
        print j,
        j += 1
    

    2.求1-100的和

    #!/usr/bin/env python
    #sum 1-100
    num = 1
    total = 0
    while num<=100:
        total += num
        num += 1
    print(total)
    
    

    3.求1-100的奇数和

    #!/usr/bin/env python
    #odd number sum 1-100 
    num = 1
    total = 0
    while num<=100:
        total += num
        num += 2
    print(total)
    

    4.求1-100的偶数和

    #!/usr/bin/env python
    #even number sum 1-100
    num = 2
    total = 0
    while num<=100:
        total += num
        num += 2
    print(total)
    

    5.打印1-100的奇数到屏幕上

    #!/usr/bin/env python
    #print odd number 1-100
    num = 1
    while num<=100:
        print num
        num += 2
    

    6.打印1-100的偶数到屏幕上

    #!/usr/bin/env python
    #print even number 1-100
    num = 2
    while num<=100:
        print num
        num += 2
    

    7.求1-2+3-4+5...99的和

    #!/usr/bin/env python
    #sum 1-2+3-4+5...99
    num = 0
    for i in range(100):
        if i % 2 == 0:
            num = num -i
        else:
            num = num + i
    print num
    

    相关文章

      网友评论

          本文标题:简单的Python小程序

          本文链接:https://www.haomeiwen.com/subject/hafzqftx.html