WjExplor Story

[점프 투 파이썬] 3장 제어문 되새김 문제 Q1 ~ Q6 본문

Python/Python : Code Study

[점프 투 파이썬] 3장 제어문 되새김 문제 Q1 ~ Q6

더블유제이플로어 2025. 9. 11. 00:11

Q1. 조건문의 참과 거짓

다음 코드의 결괏값은 무엇일까?

a = "Life is too short, you need python"

if "wife" in a : print("wife")
elif "python" in a and "you" not in a : print("python")
elif "shirt" not in a : print("shirt")
elif "need" in a : print("need")
else:print("none")

답 ) shirt


Q2. 3의 배수의 합 구하기

while 문을 사용하여 1부터 1000까지의 자연수 중 3의 배수의 합을 구하라.

result =0
i=1
while i<=1000:
    if i%3==0:
        result += i
    i+=1
print(result) # 결과값 166833 출력

Q3. 별 표시하기

i =0
while True:
    i+=1
    if i>5: break
    print(i*'*')
*
**
***
****
*****

Q4. 1부터 100 까지 출력하기 

for i in range(1,101):
    print(i)
    i+=1

Q5. 평균 점수 구하기

 A 학급에 총 10명의 학생이 있다. 이 학생들의 중간고사 점수는 다음과 같다.
[70,60,55,75,95,90,80,80,85,100]
for 문을 사용하여 A 학급의 평균 점수를 구해보자.
a = [70,60,55,75,95,90,80,80,85,100]
total = 0
for score in a:
    total += score
average = total /len(a)
print(average)

Q6. 리스트 컴프리헨션 사용하기

리스트의 요소 중에서 홀수만 골라 2를 곱한 값을 result 리스트에 담는 예시이다.
numbers = [1,2,3,4,5]
result = []
for n in numbers:
    if n%2==1:
        result.append(n*2)
print(result)
numbers = [1,2,3,4,5]
result = [n*2 for n in numbers if n%2==1]
print(result)

https://youtu.be/Sg33k9IVRr8?si=sMEfKkj6PwBWdMQ0