zoukankan      html  css  js  c++  java
  • Python基础(一)

    几个简单的Python练习题

    1.使用while循环输出1到9

    Python代码

    1 counter = 1
    2 while counter < 10:
    3     print(counter)
    4     counter += 1

    输出如下

    2.求出1到100所有数的和

    Python代码

    1 counter = 1
    2 result = 0
    3 while counter < 101:
    4     result += counter
    5     counter += 1
    6 print("sum of 1 to 100:", str(result))

    输出如下

    3.输出1到100内的所有基数

    Python代码

    1 counter = 1
    2 print("odd numbers of 1 to 100:")
    3 while counter < 101:
    4     if counter % 2 == 1:
    5         print(counter)
    6     counter += 1

    输出如下

    4.输出1到100内的所有偶数

    Python代码

    1 counter = 1
    2 print("odd numbers of 1 to 100:")
    3 while counter < 101:
    4     if counter % 2 == 0:
    5         print(counter)
    6     counter += 1

    输出如下

    5.求1-2+3-4+5..+99所有数的和

    Python代码

    1 counter = 1
    2 result = 0
    3 while counter < 100:
    4     if counter % 2 == 1:
    5         result += counter
    6     elif counter %2 == 0:
    7         result -= counter
    8     counter += 1
    9 print("sum of 1-2+3-4...+99:", str(result))

    输出如下

  • 相关阅读:
    Map
    Collection接口之Set
    Collection接口之List、泛型
    简介
    递归
    File类
    转换流InputStreamReader、OutputStreamWriter
    springmvc
    集合
    SpringCloud学习笔记
  • 原文地址:https://www.cnblogs.com/huangwenhao/p/11090495.html
Copyright © 2011-2022 走看看