- 讀程序
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
i = 0, numbers = 12 = 2
i = 1, numbers = 122
i = 2, numbers = 1222
...
i = 19, numbers = 2*20
print(2 * 20)
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
num = 1 <= 100, if False, num += 1, num = 2
num = 2 <= 100, if False, num += 1, num = 3
num = 3 <= 100, if True, summation += 1, summation = 1, num += 1, num = 4
...
print(summation)
- 求1到100所有數(shù)的和、平均值
for 循環(huán):
sum = 0
for x in range(101):
sum += x
print('和:', sum, '平均值:', sum / 100)
sum = 0
x = 0, sum = 0 + 0 = 0
x = 1, sum = 0 +1 =1
x = 2, sum = 1 + 2 = 3
...
x = 100, sum = 0 + 1 + 2 +...+ 100
print(sum, sum / 100)
while循環(huán):
sum = 0
i = 1
while i <= 100:
sum += i
i += 1
print('和:', sum, '平均值:', sum / 100)
i = 1, i <= 100, sum = 0 + 1 = 1, i += 1, i = 2
i = 2, i <= 100, sum = 1 + 2 = 3, i += 1, i = 3
i = 3, i <= 100, sum = 1 + 2 + 3, i += 1, i = 4
...
i = 100, i <= 100, sum = 1+2+...+100, i += 1, i = 101
i = 101, while False, print(sum, sum / 100)
- 計(jì)算1-100之間能3整除的數(shù)的和
sum = 0
for x in range(3, 100, 3):
sum += x
print(sum)
sum = 0
x = 3, sum = 0 + 3
x = 6, sum = 0 + 3 + 6
...
x = 99, sum = 0 + 3 + 6 +...+99
print(sum)
sum = 0
for x in range(1, 100):
if x % 3 != 0:
continue
sum += x
print(sum)
sum = 0
x =1 , if True, continue跳出當(dāng)前循環(huán)
x = 2, if True, continue跳出當(dāng)前循環(huán)
x = 3, if False, sum = 0 + 3
x = 4, if True, continue跳出當(dāng)前循環(huán)
...
x = 99, if False, sum = 0 + 3 + ....99
print(sum)
sum = 0
num = 3
while num <= 100:
if num % 3 == 0:
sum += num
num += 1
print(sum)
sum = 0
num = 3, while True, if True, sum = 0 + 3, num = 4
num = 4, while True, if False, num = 5
...
num = 99, while True, if True, sum = 0 + 3 +... + 99, num = 100
num = 100, while True, if False, num = 101
num = 101, while False, print(sum)
- 求斐波那契數(shù)列列中第n個(gè)數(shù)的值:(0),1兔辅,1驮瞧,2氓扛,3,5论笔,8采郎,13,21狂魔,34....
規(guī)則: 從第二個(gè)數(shù)開始蒜埋,后面的數(shù)的值就是這個(gè)數(shù)前兩個(gè)數(shù)的和
for 循環(huán)
a = 0
b = 1
n = int(input('請(qǐng)輸入一個(gè)數(shù):'))
for x in range(1, n + 1):
a, b = b, a + b
print(a)
a = 0 #定義當(dāng)前數(shù)的前兩個(gè)數(shù)
b = 1 # 定義當(dāng)前數(shù)的前一個(gè)數(shù)
n = int(input) # 從控制臺(tái)輸入要求的斐波那契數(shù)列中的第幾個(gè)數(shù)
x = 1, a = 1, b = 1
x = 2, a = 1, b = 2
x = 3, a = 2, b = 3
....
while 循環(huán):
a = 0
b = 1
c = 1
i = 1
n = int(input())
if n == 1:
print(1)
While i < n:
c = a + b
a = b
b = c
i += 1
print(c)
a = 0 #定義當(dāng)前數(shù)的前兩個(gè)數(shù)
b = 1 # 定義當(dāng)前數(shù)的前一個(gè)數(shù)
c = 0 #定義一個(gè)當(dāng)前數(shù)字變量
i = 1 定義一個(gè)變量
n = int(input) # 從控制臺(tái)輸入要求的斐波那契數(shù)列中的第幾個(gè)數(shù) 暫時(shí)取5
if n == 1, 輸出1
if n != 1,執(zhí)行while循環(huán)最楷。
i = 1, while true, c = 0 +1 = 1, a = 1, b = 1, i =2
i = 2, while true, c= 1+ 1 = 2整份,a = 1, b = 2, i = 3
i = 3, while true, c= 1+ 2 = 3, a = 2, b = 3
i = 4, while true, c = 2 + 3 = 5, a = 3, b = 5, i = 5
i = 5, while False, print(c)
- 判斷101-200之間有多少個(gè)素?cái)?shù)待错,并輸出所有素?cái)?shù)。
素?cái)?shù)(質(zhì)數(shù)): 除了1和它本身皂林,不能再被其他的數(shù)整除
count = 0
for x in range(101, 200):
for i in range(2, x):
if x % i == 0:
break
else:
print(x)
count += 1
print(count)
count = 0
x = 101,
i = 2, if False
i = 3, if False
...
i = 100, if False
print(x), count = 1
x = 102,
i = 2, if True, break
x = 103,
i = 2, if False
I = 3,if False
...
i = 102, if False
print(x), count = 2
...
x = 199,
i = 2, if False
i = 2, if False
...
i = 198, if False
print(x), count += 1
while循環(huán):
count = 0
num = 101
while num < 200:
x = 2
while x < num:
if num % x == 0:
break
x += 1
else:
print(num)
count += 1
num += 1
print(count)
count = 0
num = 101, while True,
x = 2, x < 101, if False, x = 3
x = 3, x < 101, if False, x = 4
...
x = 100, x < 101, if False, x = 101
x = 101, while False, else, print(101),count = 1, num += 1
num = 102, while True,
x = 2, while True,if True, break, num += 1
...
num = 199, while True,
x = 2, x < 200, if False, x =3
x = 3, x < 200, if False, x =4
...
x = 198, x < num, if False, x = 199
x = 199, while False, print(199), count += 1 ,num += 1
num = 200, while False, print(count)
用標(biāo)志True結(jié)束循環(huán):
count = 0
for n in range(101, 200):
is_prime = True
for x in range(2, n):
if n % x != 0:
is_prime = False
break
if is_prime = True:
print(n)
count += 1
print(count)
count = 0
n = 101, is_prime = True,
x = 2, if False,
x = 3,if False
...
x = 100, if False
if True: print(102), count = 1
n = 102, is_prime = True, x = 2, if True, is_prime = False, break. if False
n = 199, is_prime = True,
x = 2, if False
x = 3, if False
...
x = 198, if False
if True: print(199),count += 1
print(count)
- 有一分?jǐn)?shù)序列:(1/1),2/1,3/2,5/3,8/5,13/8,21/13...求出這個(gè)數(shù)列的第20個(gè)分?jǐn)?shù)
分子: 前一個(gè)分?jǐn)?shù)的分子+前一個(gè)分?jǐn)?shù)的分母
分母: 前一個(gè)分?jǐn)?shù)的分子
思路: 用兩個(gè)變量保存前一個(gè)分?jǐn)?shù)的分子和分母朗鸠, 然后根據(jù)規(guī)則不斷更新分子和分母的值
fen_zi = 1
fen_mu = 1
for x in range(20):
fen_zi, fen_mu = fen_zi + fen_mu, fenzi
print(fen_zi + fen_mu, '/', fen_mu)
fen_zi = 1
fen_mu = 1
x = 1, fen_zi, fen_mu = 2, 1
x = 2, fen_zi, fen_mu = 3, 2
...
x= 20, fen_zi, fen_mu = fen_zi + fen_mu, fenzi
print(fen_zi + fen_mu, '/', fen_mu)
7.給一個(gè)正整數(shù),要求:1础倍、求它是幾位數(shù) 2.逆序打印出各位數(shù)字
解題思路: 看一個(gè)正整數(shù)是幾位數(shù)烛占,就看這個(gè)數(shù)整除幾次10以后會(huì)變成0
count = 0
n = int(input('請(qǐng)輸入一個(gè)數(shù)字: ')) #暫定為n = 12456
While True:
print(n % 10)
n //= 10
count += 1
if n == 0:
break
print(count)
count = 0
print(6),n = 1245沟启,count = 1忆家,if False
print(5), n = 124, count = 2, if False
print(4), n = 12, count = 3, if False
print(2), n = 1, count= 4, if False
print(1), n =0, count = 5, if True, break,
print(count)
8.不斷的從控制臺(tái)輸入年齡,根據(jù)年齡輸出不同的提示(例如:老年人德迹,青壯年芽卿,成年人,未成年胳搞,兒童)
輸入'q'卸例,就退出
while True:
value = input('請(qǐng)輸入你的年齡:')
if value == 'q':
break
age = int(value)
if age <= 12:
print('兒童')
elif 12 < age <= 18:
print('未成年')
elif 18 < age <= 50:
print('青壯年')
else:
print('老年')
進(jìn)入循環(huán):
請(qǐng)輸入年齡,如果輸入值='q',break肌毅,不等于'q'
將輸入的值轉(zhuǎn)為整型筷转;判斷age的范圍,輸出相應(yīng)的值
9.計(jì)算5的階乘 5!的結(jié)果
sum = 1
for x in range(1, 6):
sum *= x
print(sum)
sum = 1
x = 1, sum = 1 * 1 = 1
x = 2,sum = 1 * 1 * 2
x = 3, sum = 1* 12 3
...
x = 5, sum = 12345
10.求1+2!+3!+...+20!的和
思路: 1. 先分別取出1~20 2.求每次取出的數(shù)的階乘
sum1 = 0
for x in range(1, 21):
sum2 = 1
for i in range(1, x + 1):
sum2 *= i
sum1 += sum2
print(sum1)
sum1 = 0
x = 1, sum2 = 1. i = 1, sum1 = 1
x = 2, sum2 = 1,
i = 1, sum2 = 1
i = 2, sum2 = 12,
sum1 = 1! + 2!
x = 3, sum2= 1
i =1, sum2 = 1
i =2, sum2 = 12,
i =3, sum2 = 123,
sum1 = 1悬而!+ 2呜舒!+ 3!
...
x = 20, sum2 = 1
i =1, sum2 = 1
i =2, sum2 = 12,
i =3, sum2 = 123,
...
i = 20, sum = 12...*20
sum1 = 1!+ 2笨奠!+ 3!+...20!
print(sum1)
11.循環(huán)輸入大于0的數(shù)字進(jìn)行累加袭蝗,直到輸入的數(shù)字為0,就結(jié)束循環(huán)般婆,并最后輸出累加的結(jié)果
sum =0
while True:
num = int(input('請(qǐng)輸入一個(gè)數(shù)字到腥,輸入0結(jié)束: '))
if num == 0:
break
sum += num
print(sum)
sum = 0
進(jìn)入循環(huán),輸入數(shù)字蔚袍,如果num = 0, 跳出循環(huán)左电。如果不是0,sum = sum+num
再次進(jìn)入循環(huán)页响,直到輸入0跳出循環(huán)
print(sum)
12.求s=a+aa+aaa+aaaa+aa...a的值篓足,其中a是一個(gè)數(shù)字。例如2+22+222+2222+22222(此時(shí)共有5個(gè)數(shù)相加)闰蚕,幾個(gè)數(shù)相加有鍵盤控制栈拖。
解題思路:
2
22 = 210 + 2
222 = 2210+2
2222= 2222*10 + 2
a = int(input('請(qǐng)輸入一個(gè)個(gè)位數(shù):')) #定義a的值 a = 2
n = int(input('請(qǐng)輸入一個(gè)數(shù):')) #決定多少位數(shù)相加,循環(huán)幾次 n = 5
i = 1
sum1 = 0 #保存最后求和的值
sum2 = 0 #保存每一項(xiàng)
while i <= n:
sum2 = sum2*10 + a
sum1 += sum2
i += 1
print(sum1)
a = 2, n = 5, i = 1, sum1 = sum2 = 0
i = 1, while True, sum2 = 0 + 2 = 2, sum1 = 0 +2 = 2, i = 2
i = 2, while True, sum2 = 22, sum1 = 2 + 22, i = 3
i = 3, while True, sum2 = 222, sum1 = 2 + 22 + 222, i = 4
i = 4, while True, sum2 = 2222, sum1 = 2 + 22 + 222 + 2222, i = 5
i = 5, while True, sum2 = 22222, sum1 = 2 + 22 + 222 + 2222 + 22222, i = 6
print(sum1)
13.由小到大輸出x, y, z 中最大的數(shù)
解題思路:先假設(shè)最小的數(shù)為x, 再用x 與其他數(shù)字比較没陡,如果其他數(shù)字比x小涩哟, 則將較小的值賦給min
x = int(input('enter your first number: ')) 3
y = int(input('enter your first number: '))5
z = int(input('enter your first number: '))1
if x > y:
x, y = y, x
if x > z:
x, z = z, x
if y > z:
y, z = z, y
print(x, y, z)
if False, if 3>1, x = 1, z = 3
if 5>3, y = 3, z = 5
- 控制臺(tái)輸出三角形
根據(jù)n的值的不同索赏,輸出相應(yīng)的形狀
''''
**
*
n = 5
for x in range(5,0, -1):
for i in range(x):
print('*', end = '')
print()
n = 5
x = 5, i = 0, *
i = 1, **
...
i = 4, *****
x = 4, i = 0, *
i = 1, **
...
i = 3, ****
...
x = 1, i = 0, *
根據(jù)n的值的不同,輸出相應(yīng)的形狀(n為奇數(shù))
n = 7
for x in range(1, n + 1, 2):
space = int(n - star) / 2
for _ in range(space):
print(' ', end = '')
for _ in range(star):
print('*', end = '')
print()
x = 1, space = 3
*
x = 3, space = 2
***
...
x = 7, space = 0
*******
- 這是經(jīng)典的"百馬百擔(dān)"問題贴彼,有一百匹馬潜腻,馱一百擔(dān)貨,大馬馱3擔(dān)器仗,中馬馱2擔(dān)融涣,兩只小馬馱1擔(dān),問有大精钮,中威鹿,小馬各幾匹?
for small in range(1, 100):
for middle in range(1, 50):
for big in range(1, 33):
if (big + middle + small == 100 )and (big * 3 + middle * 2 + small / 2 == 100):
print('大:', big, '中:', middle, '小:', small)
small = 1, middle = 1, big = 1, if False
small = 1, middle = 1, big = 2, if False
...
16.輸出9*9口訣轨香。 1.程序分析:分行與列考慮忽你,共9行9列,i控制行臂容,j控制列科雳。
左下角輸出
for i in range(1, 10):
for j in range(1, i + 1):
print(j, ' x ', i, '= ',i*j, end = ' ')
print()
i = 1,j = 1, print(1 * 1 = 1)
i = 2,
j = 1, print(1 * 2 = 2)
j = 2, print(2 * 2 = 4)
i = 3,
j = 1, print(1 * 3 = 3)
j = 2, print(2 * 3 = 6 )
j = 3, print(3 * 3 = 9)
...
i = 9,
j = 1, print(1 * 9 = 9)
j = 2, print(2 * 9 = 19)
j = 3, print(3 * 9 = 27)
...
j = 9, print(9 *9 = 81)