Article Directory
- 1. Print prime numbers between 100 and 200
- 2. The output of the multiplication tables
- 3. The judgment leap year between 1000 --- 2000
- 4. Given the two values of integer variables, the contents of the two values to be exchanged.
- 5. a text file, each line is a word. There may be repeated. Number of statistics for each word appears.
- 6. selecting the maximum value of 10 integers.
- 7. The bubble sort in descending output
- 8. Enter two digits input, and two numbers
- 9. Create a list contains five values, each value is determined by user input, and calculates the average of five values.
- 10. Calculate 1 / 1-1 / 2 + 1 / 3-1 / 4 + 1 / + 5 ...... 1/99 - 1/100 of the value.
1. Print prime numbers between 100 and 200
for item in range(100,200):
su_shu=True
for i in range(2,item):
if item % i ==0:
su_shu = False
break
if su_shu==True:
print(item,end='\t')
2. The output of the multiplication tables
for _list in range(9):
for _col in range(_list+1):
print(_col+1,'*',_list+1,'=',(_col+1)*(_list+1),end='\t')
print()
3. The judge in 1000 - a leap year between 2000
year=1000
while year<2000:
if (year%4==0 and year%100!=0) or year%400==0:
print(year,end='\t')
year+=1
4. Given the two values of integer variables, the contents of the two values to be exchanged.
a=10
b=20
a,b=b,a
print(a,b)
5. a text file, each line is a word. There may be repeated. Number of statistics for each word appears.
Example file:
aaa
bbb
ccc
aaa
bb
c
aaa
f=open('./test.txt','r')#python语言中一切接对象,print(type(f))可以看对象类型
word={}
for a in f:
a = a.strip()#strip字符串前后的空格抠掉
if a in word:
word[a]+=1
else:
word[a]=1
print(word)
f.close()
6. selecting the maximum value of 10 integers.
a=[98,25,35,56,46,51,12,65,85,66]
max=a[0]
for item in a:
if item>max:
max=item
print(max)
98
7. The bubble sort in descending output
a=[98,25,35,56,46,51,12,65,85,66]
#冒泡排序
for counts in range(0,len(a)-1):
sort=True
for i in range(0,len(a)-1-counts):
if a[i]<a[i+1]:
a[i],a[i+1]=a[i+1],a[i]
sort=False
if sort==True:
break
print(a)
8. Enter two digits input, and two numbers
a=input('请输入第一个数:')
b=input('请输入第二个数')
a=int(a)
b=int(b)
print('a+b=',a+b)
9. Create a list contains five values, each value is determined by user input, and calculates the average of five values.
a=[]
for count in range(5):
x=input('你输入的第%d个数为>>' % (count+1))
x=int(x)
a.append(x)
print(a)
print((a[0]+a[1]+a[2]+a[3]+a[4])/5)
10. Calculate 1 / 1-1 / 2 + 1 / 3-1 / 4 + 1 / + 5 ...... 1/99 - 1/100 of the value.
ret=0
for item in range(1,101):
ret+=((-1)**(item+1))*(1/item)
print(ret)