[Python] for 문

전보·2020년 6월 4일
0

for문

enumerate

for i, fruit in enumerate(['apple','banana','orange']):
    print(i, fruit)
    
#결과
0 apple
1 banana
2 orange

zip

days = ['Mon', 'Tue', 'Wed']
fruits = ['apple','banana','orange']
drinks = ['coffee','tea','beer']

for day, fruit, drink in zip(days, fruits, drinks):
    print(day, fruit, drink)
    
#결과
Mon apple coffee
Tue banana tea
Wed orange beer

사전의 for 문

dic = { 'x' : 100, 'y' : 200 }
print(dic.items()) #결과 -> dict_items([('x',100), ('y', 200)])

for k, v in dic.items():
    print(k, ":" ,v) 
profile
엉망진창

0개의 댓글