python(蟒蛇)三大控制流程语句
文章預(yù)覽:
- 1.順序執(zhí)行
- 2.分支選擇 (if else)
- 分支語句之三元運(yùn)算符
- 多分支語句
- 3.循環(huán)語句
- while循環(huán)
- 需求: 輸出數(shù)字從0-100
- for循環(huán)
- 九九乘法表
1.順序執(zhí)行
從上到下按順序執(zhí)行
2.分支選擇 (if else)
if 表達(dá)式:
執(zhí)行代碼
elif 表達(dá)式:
執(zhí)行代碼
elif 表達(dá)式:
執(zhí)行代碼
. …
else:
執(zhí)行代碼
說明: 只需要滿足一個(gè)條件,程序會執(zhí)行哪個(gè)條件下面的代碼, 然后退出分支
python中input接受的是字符串,如果接收整型數(shù),需要通過int轉(zhuǎn)換成整型數(shù)
age = int(input ("請輸入年齡:")) if age >= 18:print ("成年") else:print ("未成年")分支語句之三元運(yùn)算符
age = int(input("請輸入年齡:")) print("成年" if age>= 18 else "未成年")多分支語句
""" 補(bǔ)充pycharm快速復(fù)制一行的快捷鍵: ctrl + d 需求: 分?jǐn)?shù)score1). 90=<score<=100, grade=A2). 80=<score<90, grade=B3). score<80, grade=C """ score = int(input('成績:')) if 90<=score<=100:print("等級是A") elif 80<=score<90: # elif 滿足第二個(gè)條件做什么操作print("等級是B") else:print("等級是C") """ 代碼需求:1. 用戶輸入用戶名和密碼2. 判斷用戶名和密碼是否正確(用戶名=admin, 密碼=westos)3. 如果正確: "用戶admin登錄成功"3. 如果不正確: "用戶admin登錄失敗" """name = input("用戶名:") password = input("密碼:") if name == 'admin' and password == 'westos':print(f'用戶{name}登錄成功') else:print(f'用戶{name}登錄失敗')3.循環(huán)語句
for i in xxxx:
print()
while 表達(dá)式
print()
while循環(huán)
while循環(huán)原理: while 循環(huán)的 suite_to_repeat 子句會一直循環(huán)執(zhí)行, 直到 expression 值為布爾假.
需求: 輸出數(shù)字從0-100
while循環(huán)
""" count=0, while 0<=100, print(0) count+=1 count=1, while 1<=100, print(1) count+=1 .... count=99 while 99 <=100, print(99) count+=1 count=100 while 100 <=100, print(100) count+=1 count=101 while 101 <=100(x) """ count = 0 while count <= 100:print(count) # 0# count = count + 1count += 1while死循環(huán)
""" 用戶登錄成功進(jìn)入系統(tǒng), 登錄失敗,繼續(xù)登錄。 并且統(tǒng)計(jì)登錄次數(shù)。 """ try_count = 1 # 用戶嘗試登錄的次數(shù) while True:print(f'用戶第{try_count}次登錄系統(tǒng)')try_count += 1 # 用戶嘗試登錄的次數(shù)+1name = input("用戶名:")password = input("密碼:")if name == 'admin' and password == 'westos':print(f'用戶{name}登錄成功')exit() # 退出程序else:print(f'用戶{name}登錄失敗')
需求: 輸出數(shù)字0-100之間所有的偶數(shù)。
方法一:
方法二:
count = 0 while count <= 100:print(count)count += 2for循環(huán)
for循環(huán)原理(從生成器上講):
可以遍歷序列成員, 可以用在 列表解析 和 生成器表達(dá)式中, 它會自動(dòng)地調(diào)用迭代器的 next()
方法, 捕獲 StopIteration 異常并結(jié)束循環(huán)(所有這一切都是在內(nèi)部發(fā)生的).
range語法:
range(start, end, step =1)返回一個(gè)包含所有 k 的列表, start <= k < end , k每次遞增 step
eg:
range(3) – [0, 1, 2]
range(1, 4) – [1, 2, 3]
range(0, 6, 2) – [0, 2, 4]
range(1, 6, 2)–[1, 3, 5]
range(-3, 1)–[ -3, -2, -1, 0]
range(4, 1, -1)–[4, 3, 2]
九九乘法表
""" i j 1 1 2 1,2 3 1, 2, 3 ... 9 1, 2, 3, ...9 """ # 如何讓print不換行呢? print('xxx', end=' ') for i in range(1, 10):for j in range(1, i + 1):print(f"{j}*{i}={j*i}", end=' ')# i=1, i=2, i=3, 開始換行print()
登錄次數(shù)限制
循環(huán)與else結(jié)合使用
""" while 條件表達(dá)式:滿足條件執(zhí)行的內(nèi)容 else:不滿足條件執(zhí)行的內(nèi)容for i in range(2):循環(huán)時(shí)執(zhí)行的語句 else:沒有for可以遍歷的值時(shí),執(zhí)行的語句""" try_count = 1 while try_count <= 3:print(f"第{try_count}次開始嘗試登錄")try_count += 1 else:print("嘗試登錄次數(shù)大于3次")| 字符串str | 單引號,雙引號,三引號引起來的字符信息 |
| 數(shù)組array | 存儲同種數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu)。[1, 2, 3], [1.1, 2.2, 3.3] |
| 列表list | 打了激素的數(shù)組, 可以存儲不同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu). [1, 1.1, 2.1, ‘hello’] |
| 元組tuple | 帶了緊箍咒的列表, 和列表的唯一區(qū)別是不能增刪改 |
| 集合set | 不重復(fù)且無序的(交集和并集) |
| 字典dict | {“name”:“westos”, “age”:10} |
總結(jié)
以上是生活随笔為你收集整理的python(蟒蛇)三大控制流程语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【超详细附参考】阿里云部署spring项
- 下一篇: python 编程题 有n个整数、使其前