for 循環:
for 迴圈用於迭代序列(即列表、元組、字典、集合或字串)。
文法:
對於序列中的變數
範例:
txt = '1234' for num in txt: print(num,end=' ') output: 1 2 3 4
如果條件:
If 語句的工作原理是檢查表達式以查看是否滿足條件,並根據獲得的輸出傳回一個值。
syntax: if condition: if condition is True else: if condition is False
範例
x=20 if x>=20: print(x is within 20) else : print(x is above the limit) output: x is within 20
txt = '12a4' for num in txt: if num>='0' and num<='9': print(num,end=' ') else: print('Not Decimal',end=' ') Output: 1 2 Not Decimal 4 above code is the example of combines if and for.
name = input("請輸入您的名字:")
印刷(姓名)
名稱中的字母:
print(字母表, end='*')
輸出:
R*A*J*A
程式碼檢查每個索引並列印輸出。
name1 = input("Enter the first name: ") name2 = input("Enter the second name: ") name3 = input("Enter the third name: ") name4 = input("Enter the fourth name: ") name = [name1, name2, name3, name4] for letter in name: if letter[0]=='G': print(letter) else: continue for alphabet in name: if alphabet[-1]=='a': print(alphabet) else: continue for alpha in name: for i in alpha: if i==' ': print(alpha) else: continue for character in name: if len(character)>9: print(character) else: continue output: Enter the first name: Guhanraja Enter the second name: Lakshmi Pritha Enter the third name: Guru Prasanna Enter the fourth name: Varatharajan Guhanraja Guru Prasanna Guhanraja Lakshmi Pritha Guru Prasanna Lakshmi Pritha Guru Prasanna Lakshmi Pritha Guru Prasanna Varatharajan
以上是python session day-t payilagam for迴圈和if條件的詳細內容。更多資訊請關注PHP中文網其他相關文章!