文章目錄
前言
for(用法)
-
例:
for ITEM in range(10):
…1.求0~100加法和 sum = 0 for i in range(1,101,2): sum += i print(sum) 2.100以內偶數和 sum = 0 for i in range(2,101,2): sum +=i 3.100以內奇數和 for o in range(1,101,2): sum +=o print(sum) 4.對輸入的數字進行階乘 num = int(input('num:')) jiecheng = 1 for p in range(1,num+1): jiecheng *= p print(jiecheng)
for(練習)
for(練習1)
有1,2,3,4四個數字
求這四個數字能生成多少個互不相同且無重複數字的三位數(不能含有122,
133這種)
count = 0
for i in range(1,5):
for j in range(1,5):
for x in range(1,5):
if i != j and j != x and i != x:
print(i * 100 + j * 10 + x)
count += 1
print('The count is: %d' %count)
用戶登陸程序需求:
1. 輸入用戶名和密碼;
2. 判斷用戶名和密碼是否正確? (name=‘root’, passwd=‘westos’)
3. 爲了防止暴力破解, 登陸僅有三次機會, 如果超過三次機會,報錯提示;
for i in range(2):
username = input('Please input your username:')
passwd = input('Please input your passwd:')
if username == 'westos' and passwd == 'root':
print('Welcome to linux8.0!!!')
break
print('Please check your username and passwd!')
print('%d chance left' %(2 - i))
username = input('Please input your username:')
passwd = input('Please input your passwd:')
for(練習2)
輸入兩個數值:
求兩個數的最大公約數和最小公倍數.
最小公倍數=(num1*num2)/最大公約數
for(練習3)
模仿命令行
import os
for i in range(1000):
cmd = input('[root@python lujiaxi ~]#')
if cmd:
if cmd == 'exit':
print('logout')
break
else:
print('run %s' %cmd)
os.system(cmd)
else:
continue
while(用法)
-
例:
while 條件:
條件滿足時,做的事情1
條件滿足時,做的事情2
…counter = 1 while counter <= 5: print('hello python') counter = counter + 1 while 1: print('hello')
while(練習)
while(練習1)
計算:0~100之間所有數字的累積求和
python中的計數方法
常見的計數方法有兩種,可以分爲
自然計數法(從1開始) – 更符合人類的習慣
程序計數法(從0開始) – 幾乎所有的程序語言都選擇從0開始計數
因此,大家在編寫程序時,應該儘量養成習慣:除非需求的特殊要求,否則>循環的計數從0開始
i = 0
result = 0
while i <= 100:
result += i
i += 1
print('The result is : %d' %result)
計算奇數和:
u = 0
result2 = 0
while u <= 100:
if u % 2 == 1:
result2 += u
u += 1
print('The result2 is : %d' %result2)
while(練習2)
*
**
***
****
*****
row = 1
while row <= 5:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row +=1
while(練習3)
猜數字遊戲
1. 系統隨機生成一個1~100的數字;
2. 用戶總共有5次猜數字的機會;
3. 如果用戶猜測的數字大於系統給出的數字,打印“too big”;
4. 如果用戶猜測的數字小於系統給出的數字,打印"too small";
5. 如果用戶猜測的數字等於系統給出的數字,打印"恭喜",並且退出循環;
import random
sys = random.randint(1,100)
count = 1
while count <= 5:
user = int(input('Please guess the number:'))
count += 1
if user < sys:
print('too small')
elif user > sys:
print('too big')
# continue
else:
print('congraculations!')
break
print('only %d times left!' %(6-count))
break continue exit()
break:跳出整個循環,不會再循環後面的內容
continue:跳出本次循環,continue後面的代碼不再執行,但是循環依然繼續
exit():結束程序的運行
for i in range(10):
if i ==5:
break
print('hello python break')
print(i)
print('hello word break')
for i in range(10):
if i == 5:
continue
print('hello python break')
print(i)
print('hello word continue')
for i in range(10):
if i == 5:
exit()
print('hello python break')
print(i)
print('hello word exit()')
運行結果:
0
1
2
3
4
hello word break
0
1
2
3
4
6
7
8
9
hello word continue
0
1
2
3
4
其他
range
- 例:
range(stop): 0 ~ stop-1
range(start,stop): start ~ stop-1
range(start,stop,step): start ~ stop
ipython:
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(1,11,2)
[1, 3, 5, 7, 9]
>>> range(2,11,2)
[2, 4, 6, 8, 10]
循環計算
在程序開發中,通常會遇到利用循環重複計算的需求(利用CPU的強大之處完成相應的複雜計算)
遇到這種情況:
1.在while上方定義一個變量,用於存放最終的計算結果
2.在循環體內部,每次循環都用最新的計算結果,更新之前定義的變量