Q1.
출생년도에 대한 리스트 year_lists를 만들어보자.
출생년도를 첫번째 요소로 하고 1년씩 증가해 다섯 번째 생일이 되는 해까지의 요소를 넣는다.
예를 들어 1980년에 태어났다면 리스트는
years_list = [1980, 1981, 1982, 1983, 1984, 1985]
일 것이다.
다섯 살도 안됐는데 이 책을 읽고 있다면 뭐라고 말해야 할지 모르겠다.
A.
나는 99년생이기 때문에 나를 기준으로 진행하겠다.
year_lists = [1999, 2000, 2001, 2002, 2003, 2004]
Q2.
years_list의 세번째 생일은 몇년도인가? 기억하자. 첫해는 0살이다.
(오프셋 0은 출생년도).
A.
print(year_lists[3]) # 2002
Q3.
years_list 중 가장 나이가 많을 때는 몇년도인가?
A.
print(year_lists[5]) # 2004
Q4.
"mozzarella", "cinderlla", "salmonella" 세 문자열을 요소로 갖는 things 리스트를 만들어보자.
A.
things = ["mozzarella", "cinderlla", "salmonella"]
Q5.
things 리스트에서 사람 이름의 첫 글자를 대문자로 바꿔서 출력해보자.
그러면 리스트의 요소는 변경되는가?
A.
things = ["mozzarella", "cinderlla", "salmonella"]
print(things[1].capitalize()) # Cinderlla
print(things[1]) # cinderlla
Q6.
things 리스트의 치즈 요소를 모두 대문자로 바꿔서 출력해보자.
A.
things = ["mozzarella", "cinderlla", "salmonella"]
print(things[0].upper()) # MOZZARELLA
print(things[0]) # mozzarella
Q7.
things 리스트에 질병 요소가 있다면 제거한 뒤 리스트를 출력해보자.
A.
things = ["mozzarella", "cinderlla", "salmonella"]
things.remove("salmonella")
for item in things:
print(item)
"""
mozzarella
cinderlla
"""
Q8.
"Groucho", "Chico", "Harpo"
세 문자열 요소를 갖는 surprise 리스트를 만들어보자.
A.
surprise = ["Groucho", "Chico", "Harpo"]
Q9.
surprise 리스트의 마지막 요소를 소문자로 변경하고,
단어를 뒤집은 다음에 첫글자를 대문자로 바꿔보자.
A. 슬라이싱을 활용해서 단어를 뒤집어보자.
surprise = ["Groucho", "Chico", "Harpo"]
last_word = surprise[-1].lower()[::-1]
print(last_word.capitalize())
"""
Oprah
"""
Q10.
리스트 컴프리헨션을 이용하여 range(10)에서 짝수 리스트를 만들어보자.
A.
list = [number for number in range(10) if number % 2 == 0]
print(list)
"""
[0, 2, 4, 6, 8]
"""
Q11.
줄넘기 랩 음악을 만들어보자. 일련의 두 줄 리듬을 출력한다.
프로그램의 시작부분은 다음과 같다.
start1 = ["fee", "fie", "foe"]
rhymes = [
("flop", "get a mop"),
("fope", "turn the rope"),
("fa", "get your ma"),
("fudge", "call the judge"),
("fat", "pet the cat"),
("fog", "walk the dog"),
("fun", "Say we're done")
]
start2 = "Someone better"
A.
start1 = ["fee", "fie", "foe"]
rhymes = [
("flop", "get a mop"),
("fope", "turn the rope"),
("fa", "get your ma"),
("fudge", "call the judge"),
("fat", "pet the cat"),
("fog", "walk the dog"),
("fun", "Say we're done")
]
start2 = "Someone better"
for first, second in rhymes:
line1 = ' '.join([word.capitalize() for word in start1]) + ' ' + first.capitalize()
line2 = f"{start2} {second}"
print(line1)
print(line2 + "\n")
"""
Fee Fie Foe Flop
Someone better get a mop
Fee Fie Foe Fope
Someone better turn the rope
Fee Fie Foe Fa
Someone better get your ma
Fee Fie Foe Fudge
Someone better call the judge
Fee Fie Foe Fat
Someone better pet the cat
Fee Fie Foe Fog
Someone better walk the dog
Fee Fie Foe Fun
Someone better Say we're done
"""