1. 程式人生 > >Python基礎之for循環

Python基礎之for循環

pre code string for 字典 對象 基礎 val pri

for循環:用戶按照順序循環可叠代對象的內容

1. for循環字符串

msg = "string"
for i in msg:
    print(i)

執行結果為:

s
t
r
i
n
g

2. for循環列表

lst = ["a", "b", "c"]
for i in lst:
    print(i)

執行結果為:

a
b
c

3. for循環字典

dic = {
    "name": "yang",
    "age": 18,
    "sex": "male"
}
for key, value in dic.items():
    print(key, value)

執行結果為:

name yang
age 18
sex male

Python基礎之for循環