#!/usr/bin/env python3
n = 100
sum = 0
coun = 1
while coun <= n:
sum = sum + coun
coun += 1
print("1 到 %d 之和为: %d" % (n,sum))
#输出
1 到 100 之和为: 5050
#!/usr/bin/python3
count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")
# 输出
0 小于 5
1 小于 5
2 小于 5
3 小于 5
4 小于 5
5 大于或等于 5
a="asdfgjk"
for i in a:
print(i)
#输出
a
s
d
f
g
j
k
#!/usr/bin/python3
for let in 'abcde':
if let == 'b':
break
print ('当前字母为 :', let)
#输出
当前字母为 : a
var = 10
while var > 0:
print ('当期变量值为 :', var)
var = var -1
if var == 5:
break
print ("拜拜!")
#输出
当期变量值为 : 10
当期变量值为 : 9
当期变量值为 : 8
当期变量值为 : 7
当期变量值为 : 6
拜拜!
for i in range(1,5):
for j in range(1,i+1):
print('{1}*{0}={2}'.format(i,j,i * j),end=" ")
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
#!/bin/python
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if i != j and i!=k and j!=k:
print(i,j,k)
for i in range(100,1000):
j=i//100
k=(i-j*100)//10
l=(i-j*100)%10
if j**3 + k**3 + l**3 == i:
print(i)
#!/bin/python
for i in ['x','y','z']:
for j in ['x','y','z']:
for k in ['x','y','z']:
if i != j and j !=k and i != k:
if i != 'x' and k != 'x' and k != 'z':
print("a vs %s,b vs %s,c vs %s"%(i,j,k))
练习2
#! /bin/python
f=[]
n=int(input("please input int:"))
for i in range((n//2+1)):
for k in range(2,n):
m=n%k
if m == 0:
f.append(k)
n=n//k
break
if len(f) == 0:
print('该数字没有任何质因数。')
else:
f.append(n)
print(f)
#! /bin/python
sum=1
a=1
for i in range(0,9):
a=(a+1)*2
sum=sum+a
print(sum,a)