리스트

friends = ['Joseph', 'Glenn', 'Sally']
carryon = ['socks', 'shirt', 'perfume']
colors = ['red', ['yellow','blue'], 'black']
emptyList = []
print(colors[0])
# red라고 출력됨
lotto = [2, 14, 26, 41, 63]
print(lotto)
# [2, 14, 26, 41, 63]이 출력됨
lotto[2] = 28
print(lotto)
# [2, 14, 28, 41, 63]이 출력됨

friends = ['Joseph', ' Glenn', 'Sally']

print(len(friends))
# 3으로 출력됨
for i in range(5):
    print(i)

# 0
# 1
# 2
# 3
# 4 로 출력됩니다.

  1. 리스트 병합 - ‘+’ 연산자
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
# [1, 2, 3, 4, 5, 6]로 출력됩니다.
  1. 리스트 슬라이싱 - ‘ : ‘ 이용
t = [9, 41, 12, 3, 74, 15]
print(t[1:3])
print(t[:4])
print(t[3:])
print(t[:])

# [41, 12]
# [9, 41, 12, 3]
# [3, 74, 15]
# [9, 41, 12, 3, 74, 15] 로 출력됩니다.

빈 리스트 만들기 - 항목 추가하기 - 항목 정렬하기 - in을 활용해 'Glenn'이 친구 목록에 있는지 확인하기

friends = list()
friends.append('Joseph')
friends.append('Glenn')
friends.append('Sally')

print(friends)
# ['Joseph', 'Glenn', 'Sally']

friends.sort()
print(friends)
# ['Glenn', 'Joseph', 'Sally']

print('Glenn' in friends)
# True로 출력됩니다.

words2 = 'first;second;third'
stuff2 = words2.split()
print(stuff2)
# ['first;second;third']

stuff2 = words2.split(';')
print(stuff2)
# ['first', 'second', 'third']

활용 예시 - 이메일 주소 추출

line = 'From [email protected] Sat Jan 5 09:14:16 2008'
# line 에 uct.ac.za만 추출하는 방법을 찾아 보도록 하겠습니다.

words = line.split()
print(words[1])
# [email protected]이 출력됩니다.

email = words[1]
address = email.split('@')
print(address)
# ['stephen.marquard', 'uct.ac.za'] 가 출력됩니다.

print(address[1])
# uct.ac.za가 출력됩니다.

딕셔너리