简单的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
网友评论