본문 바로가기

파이썬/Do it! 점프 투 파이썬

점프 투 파이썬 3장 연습 문제 / 2021.08.02

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

1
2
3
4
5
6
7
= "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")
elseprint("none")
cs


실행 결과 :



2. while문을 사용해 1부터 1000까지의 자연수 중 3의 배수의 합을 구해 보자.

1
2
3
4
5
6
7
8
9
result = 0
= 1
while i <= 1000:
    if i%3 == 0:
        result += i
    i += 1
 
print(result)
 
cs


3. while문을 사용하여 다음과 같이 별을 표시하는 프로그램을 작성해 보자.

1
2
3
4
5
= 0
while True:
    i += 1
    if i > 5: break
    print("*"*i)
cs


4. for문을 사용해 1부터 100까지의 숫자를 출력해보자.

1
2
for i in range(1101):
    print(i)
cs


5. A 학급에 총 10명의 학생이 있다. 이 학생들의 중간고사 점수는 다음과 같다.
[70, 60, 55, 75, 95, 90, 80, 80, 85, 100]
for문을 사용하여 A학급의 평균 점수를 구해보자.

1
2
3
4
5
6
= [706055759590808085100]
total = 0
for score in A:
    total += score
average = total / 10
print(average)
cs


6. 리스트 중에서 홀수에만 2를 곱하여 저장하는 다음 코드가 있다.

1
2
3
4
5
6
7
8
numbers = [12345]
 
result = []
for n in numbers:
    if n % 2 == 1:
        result.append(n*2)
 
print(result)
cs

 

위 코드를 리스트 내포를 사용하여 표현해보자.

1
2
3
4
numbers = [12345]
 
result = [n*2 for n in numbers if (n%2== 1]
print(result)
cs