오늘은 저번보다 작성한 코드가 정답에 모두 근접했다(휴,,,)

151 ~ 160
.answer {margin-top: 10px;margin-bottom: 50px;padding-top: 10px;border-top: 3px solid LightGray;bo…
wikidocs.net
print("-"*80)
# 2024 03 05 TUE
# 151
# for문을 사용해 리스트의 음수만 출력
list7 = [3, -20, -3, 44]
for minus in list7:
if minus < 0:
print(minus)
# 152
# for문을 사용해 3의 배수만 출력
list8 = [3, 100, 23, 44]
for three in list8:
if three % 3 == 0:
print(three)
# 153
# 리스트에서 20보다 작은 3의 배수를 출력하라
list9 = [13, 21, 12, 14, 30, 18]
for underthree in list9:
if underthree < 20 and underthree % 3 == 0:
print(underthree)
# 154
# 세 글자 이상의 문자만 출력
list10 = ["I", "study", "python", "language", "!"]
for words in list10:
if len(words) >= 3:
print(words)
# 155
# 대문자만 출력
list11 = ["A", "b", "c", "D"]
for cap in list11:
if cap.isupper() == True:
print(cap)
# 156
# 소문자만 출력
for uncap in list11:
if uncap.islower == True:
print(uncap)
# 157
# 이름의 첫 글자를 대문자로 변경해 출력
list12 = ['dog', 'cat', 'parrot']
for animals in list12:
anis = animals[0].upper()+animals[1:]
print(anis)
# 158
# 파일 확장자를 제거하고 파일 이름만 화면에 출력
list13 = ['hello.py', 'ex01.py', 'intro.hwp']
for remove in list13:
re = remove.split(".")
print(re[0])
# 159
# 파일 확장자가 .h인 파일 이름 출력
list14 = ['intra.h', 'intra.c', 'define.h', 'run.py']
for h1 in list14:
h2 = h1.split(".")
if h2[1] == "h":
print(h1)
# 160
# 파일 확장자가 .h나 .c인 파일 이름 출력
for hc in list14:
hcc = hc.split(".")
if hcc[1] == "h" or hcc[1] == "c":
print(hc)

내가 몰랐던 문법
1. isupper() == True
155,156번 문제에서 if 뒤에 오는 조건식 부분이 정답과 달랐다 정답 코드는
리스트 = ["A", "b", "c", "D"]
for 변수 in 리스트:
if 변수.isupper():
print(변수)
isupper() == True를 사용하지 않았다. 조건문에서는 이미 불리언 값을 반환하는 메서드가 사용되었기 때문에 '== True'를 추가로 사용하지 않아도 되고 불리언 표현식 자체가 이미 조건을 평가하기에 충분하다. 따라서 정답 코드와 같이 작성하는 것이 더 깔끔하고 효율적이다.
2. split()
틀리지는 않았으나 split()을 유연하게 사용하는 것은 아직까지 어렵다. 조금 더 split()에 익숙해질 필요가 있는 것 같다.
3. 157번 문제
파이썬의 문자열은 변경이 불가능하기 때문에 첫 글자만 대문자로 바꾸고 뒷 글자들은 그대로 두는 새로운 문자열을 생성해 출력해야 한다. 따라서 단어[0] .upper() + 단어[1:]를 하면 새로운 문자열이 생성되는 것이다.
'PYTHON > 초보자를 위한 파이썬 300제' 카테고리의 다른 글
| 08. 파이썬 반복문 171 ~ 180 (0) | 2024.03.07 |
|---|---|
| 08. 파이썬 반복문 161 ~ 170 (2) | 2024.03.06 |
| 08. 파이썬 반복문 141 ~ 150 (0) | 2024.03.04 |
| 08. 파이썬 반복문 131~140 (0) | 2024.03.03 |
| 07. 파이썬 분기문 121 ~ 130 (0) | 2024.03.02 |