1. 選擇結構
任何簡單或複雜的算法都可以由順序結構、選擇結構和循環結構這三種基本結構組合而成
1.1 單分支結構 if
money = 1000
s = int(input('請輸入取款金額'))
# 判斷餘額是否充足
if money >= s:
money = money -s
print('取款成功,餘額為:',money)
1.2 雙分支結構 if-else
#從鍵盤錄入一個整數 判斷奇偶數
num = int(input('請輸入一個整數'))
if num % 2 == 0:
print(num,'是偶數')
else:
print(num,'是奇數')
1.3多分支結構 if-elif-…-else
'''
從鍵盤錄入一個整數成績 判斷等級
90-100 A
80-89 B
70-79 C
60-69 D
0-59 E
'''
score = int(input('請輸入成績:'))
if score >= 90 and score <= 100:
print('A')
elif score >= 80 and score <= 89:
print("B")
elif score >= 70 and score <= 79:
print("C")
elif score >= 60 and score <= 69:
print("D")
elif score >= 0 and score <= 59:
print("E")
else:
print('不在有效範圍内,請輸入0-100的數字')
1.4 嵌套if
'''
會員 >= 200 8折
>= 100 9折
非會員 >=200 9.5折
'''
answer = input('您是會員嗎?y/n')
money = float(input('請輸入您的購物金額:'))
#外層判斷是否為會員 内層打折
if answer == 'y':
if money >= 200:
print('打8折,付款金額:',money*0.8)
elif money >= 100:
print('打9折,付款金額:',money*0.9)
else:
print('不打折,付款金額:'monry)
else: #非會員
if money >= 200:
print('打9.5折,付款金額:',money*0.95)
else:
print('不打折')
1.5 條件表達式
- if…else簡寫
- 文法結構:
- 運算規則
- 如果判斷條件的布爾值為True,傳回x,否則傳回False
# 鍵盤錄入兩個數,比較大小
num_a = int(input('請輸入第一個整數:'))
num_b = int(input('請輸入第二個整數:'))
#正常寫法
'''
if num_a >= num_b:
print(num_a,'大于等于',num_b)
else:
print(num_a,'小于',num_b)
'''
#使用條件表達式
print((num_a,'大于等于',num_b) if num_a >= num_b else (num_a,'小于',num_b))
1.6 pass語句
- 隻是一個占位符,用在文法上需要語句的地方
- 什麼時候使用?
if answer == 'y':
pass
else:
pass
2. 内置函數 range()
- 生成一個整數序列
- 建立的三種港式:
- range(stop) -> [0,stop) 步長為1
- range(start,stop) -> [start,stop) 步長為1
- range(start,stop,step) -> [start,stop) 步長為step
r = range(3) #[0,1,2]
r = range(1,3) #[1,2]
r = range(1,10,2) #[1,3,5,7,9]
3. 循環結構
3.1 while循環
a = 1
sum = 0;
while a<5:
sum += a
a +=1
print('和為',sum) #10
- 選擇結構if和循環結構while差別
- if判斷一次,條件為true
- while判斷N+1次,條件為True執行N次
# 計算1-100偶數和
a = 1
sum = 0
while a <= 100:
if a%2 == 0: # 奇數和 if a%2 :
# 偶數和 if not bool(a%2):
sum += a
print('1-100偶數和:',sum) #2550
3.2 for-in循環
- in 表達從(字元串,序列等)中以此取值,又稱周遊
- for-in 周遊的對象必須為可疊代對象
for item in 'Pyt':
print(item)
'''
P
y
t
'''
#如果循環體中不需要自定義變量,可将自定義變量寫為_
for _ in range(3):
print('python')
'''
python
python
python
'''
#使用for循環,計算1-100偶數和
sum = 0
for item in range(1,101):
if item %2 == 0:
sum += item
print('1-100偶數和:'sum)
# 輸出100-999之間的水仙花數
# 水仙花數:153 = 3*3*3+5*5*5+1*1*1
for item in range(100,1000):
a = item%10 #個位
b = item//10%10 #十位
c = item//100 #百位
if a**3 + b**3 + c**3 == item:
print(item)
3.3 break語句
# 鍵盤錄入密碼,最多錄入三次,正确結束循環
for item in range(3):
pwd = input('請輸入密碼:')
if pwd == '8888':
print('密碼正确')
break
else:
print('密碼不正确')
3.4 continue語句
# 輸出1-50之間所有5的倍數
for item in range(1,51):
if item%5 == 0:
print(item)
# 使用continue
for item in range(1,51):
if item%5 != 0:
continue
print(item)
3.5 else語句
for item in range(3):
pwd = input('請輸入密碼:')
if pwd == '8888':
print('正确')
break
else:
print('不正确')
else:
print('對不起,三次均輸入錯誤')
3.5 嵌套循環
# 輸出一個三行四列的矩形
for i in range(1,4): #三行
for j in range(1,5): #四列
print('*',end='\t')
print() #打行
'''
* * * *
* * * *
* * * *
'''
#輸出九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print(i,'*',j,'=',i*j,end='\t')
print()
'''
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
'''
3.6 二重循環中的break和continue
for i in range(5):
for j in range(1,11):
if j%2 == 0:
break
print(j)
'''
1
1
1
1
1
'''
for i in range(5):
for j in range(1.11):
if j%2 == 0:
continue
print(j,end='\t')
print()
'''
1 3 5 7 9
1 3 5 7 9
1 3 5 7 9
1 3 5 7 9
1 3 5 7 9
'''