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))

输出如下

 

posted @ 2019-06-26 16:10  纯情小浩浩  阅读(180)  评论(0编辑  收藏  举报