본문 바로가기
Python_기초 (Python)

[파이썬_제어문] 반복문 (Loop - for)

by moveho 2022. 9. 2.

조건을 만족하는 동안 작업을 반복적으로 수행하는 반복문이다.

직접 실행시켜본 결과이다.

names = ['egoing', 'basta', 'blackdew', 'leezche']
for name in names:
    print('Hello, '+name+' . Bye, '+name+'.')

Hello, egoing . Bye, egoing.
Hello, basta . Bye, basta.
Hello, blackdew . Bye, blackdew.
Hello, leezche . Bye, leezche.

 

persons = [
    ['egoing', 'Seoul', 'Web'],
    ['basta', 'Seoul', 'IOT'],
    ['blackdew', 'Tongyeong', 'ML'],
]
print(persons[0][0])
 
for person in persons:
    print(person[0]+','+person[1]+','+person[2])
 
person = ['egoing', 'Seoul', 'Web']
name = person[0]
address = person[1]
interest = person[2]
print(name, address, interest)
 
name, address, interest = ['egoing', 'Seoul', 'Web']
print(name, address, interest)
 
for name, address, interest in persons:
    print(name+','+address+','+interest)

egoing
egoing,Seoul,Web
basta,Seoul,IOT
blackdew,Tongyeong,ML
egoing Seoul Web

 

person = {'name':'egoing', 'address':'Seoul', 'interest':'Web'}
print(person['name'])
 
for key in person:
    print(key, person[key])
 
persons = [
    {'name':'egoing', 'address':'Seoul', 'interest':'Web'},
    {'name':'basta', 'address':'Seoul', 'interest':'IOT'},
    {'name':'blackdew', 'address':'Tongyeong', 'interest':'ML'}
]
 
print('==== persons ====')
for person in persons:
    for key in person:
        print(key, ':', person[key])
    print('-----------------')

egoing
name egoing
adress Seoul
interest Web
==== persons ====
name : egoing
adress : Seoul
interest : Web
-----------------
name : basta
adress : Seoul
interest : IOT
-----------------
name : blackdew
adress : Tongyeong
interest : ML
-----------------

 

 

list 는 대괄호로 묶음

사전형 dictionary type 연관배열 해쉬는 { } 중괄호를 통해서

댓글