dic = {"human":"사람", "dog" : "강아지", "carrot" : "당근"}

oddnums = (1, 3, 5, 7, 9)
evennums = [6, 8, 10, 22, 50]
str = "Hello goorm!"

for i in oddnums :
    print(i, end = ' ')
print()
    
for i in evennums :
    print(i, end = ' ')
print()

for i in str :
    print(i , end = ' ')
print()

for key, val in dic.items() :
    print(key, val, end = ' ')
print()
1 3 5 7 9 
6 8 10 22 50 
H e l l o   g o o r m ! 
human 사람 dog 강아지 carrot 당근
  1. print( ) 함수 안에서 end = ' '를 작성하면 출력시 개행하지 않고 end의 키워드인 공백을 삽입한 뒤 데이터를 출력한다.
  2. for key, value in dic.items( )를 통해 변수를 여러개 사용할 수 있다. 딕셔너리를 사용할 때는 items라는 객체로 접근해야한다.
nums1 = [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
nums2 = [(1,2), (3, 4)]

for i, j, k in nums1 :
    print(i, j, k)

print()

for i, j in nums2 :
    print(i, j)
  1. 딕셔너리는 key, value 쌍으로 있을 뿐 두 값이 튜플, 리스트처럼 묶여있지 않아 dic.items( )로 딕셔너리를 아이템 객체로 변환해 key, value로 묶은 뒤 key, value로 접근해야한다.
fruits = {"apple": "red", "banana": "yellow", "grape": "purple", "melon": "green"}

for color in fruits.values():
    print(color, end=" ")
print()

for fruit in fruits.keys():
    print(fruit, end=" ")
print()

for key, value in fruits.items():
    print("%s의 색상은 %s" % (key, value), end=", ")
red yellow purple green 
apple banana grape melon 
apple의 색상은 red, banana의 색상은 yellow, grape의 색상은 purple, melon의 색상은 green,
  1. 딕셔너리의 key, value를 동시에 접근 할 수 있고
  2. key, value 따로 접근할 수 있다.

+ Recent posts