日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python(蟒蛇)三大控制流程语句

發布時間:2024/3/13 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python(蟒蛇)三大控制流程语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章預覽:

  • 1.順序執行
  • 2.分支選擇 (if else)
    • 分支語句之三元運算符
    • 多分支語句
  • 3.循環語句
    • while循環
  • 需求: 輸出數字從0-100
    • for循環
    • 九九乘法表

1.順序執行

從上到下按順序執行

2.分支選擇 (if else)

if 表達式:
    執行代碼

elif 表達式:
    執行代碼

elif 表達式:  
    執行代碼
.  …
  else:
    執行代碼

說明: 只需要滿足一個條件,程序會執行哪個條件下面的代碼, 然后退出分支

python中input接受的是字符串,如果接收整型數,需要通過int轉換成整型數

age = int(input ("請輸入年齡:")) if age >= 18:print ("成年") else:print ("未成年")

分支語句之三元運算符

age = int(input("請輸入年齡:")) print("成年" if age>= 18 else "未成年")

多分支語句

""" 補充pycharm快速復制一行的快捷鍵: ctrl + d 需求: 分數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 滿足第二個條件做什么操作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.循環語句

for i in xxxx:
    print()

while 表達式
    print()

while循環

while循環原理: while 循環的 suite_to_repeat 子句會一直循環執行, 直到 expression 值為布爾假.

需求: 輸出數字從0-100

while循環

""" 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 += 1

while死循環

""" 用戶登錄成功進入系統, 登錄失敗,繼續登錄。 并且統計登錄次數。 """ try_count = 1 # 用戶嘗試登錄的次數 while True:print(f'用戶第{try_count}次登錄系統')try_count += 1 # 用戶嘗試登錄的次數+1name = input("用戶名:")password = input("密碼:")if name == 'admin' and password == 'westos':print(f'用戶{name}登錄成功')exit() # 退出程序else:print(f'用戶{name}登錄失敗')


需求: 輸出數字0-100之間所有的偶數。
方法一:

count = 0 while count<=100:if count % 2 == 0:print(count)count += 1

方法二:

count = 0 while count <= 100:print(count)count += 2

for循環

for循環原理(從生成器上講):
可以遍歷序列成員, 可以用在 列表解析 和 生成器表達式中, 它會自動地調用迭代器的 next()
方法, 捕獲 StopIteration 異常并結束循環(所有這一切都是在內部發生的).

range語法
range(start, end, step =1)返回一個包含所有 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]

# 需求: 輸出數字從0-100 """ range(0, 101) 生成0-100的數值 for num in range(0, 101) - num=0 - num=1 .... - num=100 """ # range(start, end)指的是從start開始倒end-1結束 # range(num)指的是從0開始倒num-1結束 # range(start, end, step), 指的是從start開始倒end-1結束, 步長為step for num in range(0, 101):print(num) # 需求: 輸出數字0-100之間所有的偶數。 for num in range(0, 101, 2):print(num) # 需求: 輸出數字0-100之間所有的奇數。 for循環和range實現。 for num in range(1, 100, 2):print(num) # for循環訪問字符=串 string = 'adreamchaser' # item='w', 'e', 's', 't'... for item in string:print("---", item)

count = 0 while count <= 10:count += 1 # 1print(count)if count == 5:# 遇到break, 循環結束break count = 0 while count <= 10:count += 1 # 1print(count)if count == 5:# 遇到continue跳出本次循環continue

九九乘法表

""" 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()


登錄次數限制

""" 需求: 根據輸入用戶名和密碼,判斷用戶名和密碼是否正確。 為了防止暴力破解, 登陸僅有三次機會, 如果超過三次機會, 報錯提示。 數據庫信息:name='root' passwd='westos' """try_count = 1 # 用戶嘗試登錄的次數 while try_count <= 3:print(f'用戶第{try_count}次登錄系統')try_count += 1 # 用戶嘗試登錄的次數+1name = input("用戶名:")password = input("密碼:")if name == 'root' and password == 'westos':print(f'用戶{name}登錄成功')exit() # 退出程序else:print(f'用戶{name}登錄失敗') else:print("嘗試的次數大于3次")

循環與else結合使用

""" while 條件表達式:滿足條件執行的內容 else:不滿足條件執行的內容for i in range(2):循環時執行的語句 else:沒有for可以遍歷的值時,執行的語句""" try_count = 1 while try_count <= 3:print(f"第{try_count}次開始嘗試登錄")try_count += 1 else:print("嘗試登錄次數大于3次") python內置數據類型
字符串str單引號,雙引號,三引號引起來的字符信息
數組array存儲同種數據類型的數據結構。[1, 2, 3], [1.1, 2.2, 3.3]
列表list打了激素的數組, 可以存儲不同數據類型的數據結構. [1, 1.1, 2.1, ‘hello’]
元組tuple帶了緊箍咒的列表, 和列表的唯一區別是不能增刪改
集合set不重復且無序的(交集和并集)
字典dict{“name”:“westos”, “age”:10}

總結

以上是生活随笔為你收集整理的python(蟒蛇)三大控制流程语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。