读程序,总结程序的功能:
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
程序执行过程:
numbers=1;i=0;true;numbers=12=2; print2;
numbers=2; i=1; true; numbers=22=4; print4;
numbers=4; i=2; true; numbers=42=8; print8;
...
number=262144, i=19; true; numbers=2621442=524288;print524288;
number=262144, i=20; false
执行结束;
功能:求2的19次方的值
summation=0
num=1
while num<=100:
if (num%3==0 or num%7==0) and num%21!=0:
summation += 1
num+=1
print(summation)
执行过程:
summation=0; num=1; 1<=100; true; 1%3==0; false; 1%7==0; false; or语句; false; and语句; false; num+=1; num=2;
summation=0; num=2; 2<=100; true; 2%3==0; false; 2%7==0; false; or语句; false; and语句; false; num+=1; num=3;
summation=0; num=3; 3<=100; ture; 3%3==0; true; or语句; true; 3%21!=0; true; and语句; true; summation=1; num+=1; num=4;
...
summation=0; num=100; 100<=100; true; 100%3==0; false; or语句; false; and语句; false; num+=1; num=101;
summation=0; num=101; 101<=100; false;
执行结束;
功能:求1-100中能被3或者7整除,且同时不能被21整除的数的个数
编程实现(for和while各写一遍):
- 求1到100之间所有数的和、平均值
sum = 0
count = 1
for sum1 in range(1, 101):
sum += sum1
avg = sum / count
count += 1
print(sum)
print(avg)
sum1 = 0
sum = 1
count = 1
while sum < 101:
sum1 += sum
vag = sum1 / count
sum += 1
count += 1
print(sum1)
print(vag)
2. 计算1-100之间能3整除的数的和
sum = 0
for x in range(0,101,3):
sum += x
print(sum)
sum = 0
x = 1
while x < 101:
if x % 3 == 0:
sum += x
x += 1
print(sum)
3. 计算1-100之间不不能被7整除的数的和
sum = 0
for x in range(1, 101):
if x % 7 != 0:
sum += x
print(sum)
sum = 0
x = 1
while x < 101:
if x % 7 != 0:
sum += x
x += 1
print(sum)