728x90
반응형

3장 되새김문제 p.149~p.151

 

# 조건문의 참과 거짓
# 1. 다음 코드의 결괏값은 무엇일까?
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

# 가장 먼저 참이 되는 것

 

 



# 3의 배수의 합 구하기
# 2. while문을 사용해 1부터 1000까지의 자연수 중 3의 배수의 합을 구해 보자.
result = 0
i = 1
while i <= 1000:
    if i % 3 == 0:
        result += i
    i += 1
print(result)

 

 


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

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

 

 

 


# 1 부터 100까지 출력하기
# 4. for문을 사용해 1부터 100까지의 숫자를 출력해 보자.
for i in (range(1,101)):
    print(i)

 

 

 


# 평균 점수 구하기
# 5. 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 / 10 --> total / len(A)
print(average)

 

 

 


# 리스트 컴프리헨션 사용하기
# 6. 다음 소스 코드는 리스트의 요소 중에서 홀수만 골라 2를 곱한 값을 result 리스트에 담는 예제이다.

numbers = [1, 2, 3, 4, 5]
result = []
for n in numbers:
    if n % 2 == 1:
        result.append( n * 2 )
# 이 코드를 리스트 컴프리헨션을 사용하여 표현해 보자.

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




728x90
반응형

'Data Analysis & Engineer > Python' 카테고리의 다른 글

python 입출력: 사용자 입출력  (1) 2024.01.30
python 입출력: 함수  (3) 2024.01.27
python 제어문: 반복문 for  (3) 2024.01.25
python 제어문: 반복문 while  (3) 2024.01.24
python 제어문: 조건문 if문  (0) 2024.01.24

+ Recent posts