[내일배움캠프] Python 문제 풀이 (1)

셔닝🧸·2025년 6월 2일
0

✅ 문제 1. 변수와 자료형

[문제] 빈칸에 들어갈 코드를 완성하세요.

() = "Hello, Python"
() = 42

print(greeting)
print(age)

[풀이]

(greeting) = "Hello, Python"
(age) = 42

print(greeting)
print(age)

🔻🔻🔻 문제 분석 🔻🔻🔻

  • greeting = "Hello, Python"
    • 변수 greeting에 문자열 저장
  • age = 42
    • 변수 age에 정수 저장
    • age = “42” 는 int가 아닌 str인 것 주의!
      → int(age)로 데이터 타입 변경 필요

✅ 문제 2. 리스트와 인덱스

[문제] "banana"를 출력하세요.

fruits = ["apple", "banana", "cherry"]

print(fruits[]) 

[풀이]

fruits = ["apple", "banana", "cherry"]

print(fruits[1]) 

🔻🔻🔻 문제 분석 🔻🔻🔻

  • fruits = ["apple", "banana", "cherry"]
    • fruits는 리스트 : 리스트에는 값만 들어있음
  • print(fruits[1])
    • 값을 인덱스(값들의 키 값)로 찾음
      *인덱스: 리스트의 위치를 나타내는 번호
    • 파이썬은 0부터 시작

✅ 문제 3. 딕셔너리

[문제] "Alice"를 출력하세요.

student = {"name": "Alice", "age": 20}

print(student[])

[풀이]

student = {"name": "Alice", "age": 20}

print(student["name"])

🔻🔻🔻 문제 분석 🔻🔻🔻

  • student = {"name": "Alice", "age": 20}
    • student는 딕셔너리로 key-value 값으로 구성
  • print(student["name"])
    • name이라는 key 값을 주어 Alice라는 value 값 출력

✅ 문제 4. if 조건문

[문제] 빈칸에 들어갈 코드를 완성하세요.

x = 10
if x > 5:
    print("x is greater than 5")
else:
    print()

[풀이]

x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to x")

🔻🔻🔻 문제 분석 🔻🔻🔻

  • if x > 5:
    • if문 조건
      조건에 맞지 않으면 다음 줄인 else로 넘어감
  • print("x is greater than 5")
    • x가 조건에 맞으면 if 문의 해당 코드 실행 (True)
  • else:
    • 특정 조건이 맞는 True면 else로 아예 안 넘어옴
  • print("x is less than or equal to x")
    • x가 else 조건에 맞으면 else의 해당 코드 실행

✅ 문제 5. for 반복문

[문제] 빈칸에 들어갈 코드를 완성하세요.

colors = ["red", "green", "blue"]
for () in colors:
    print()

[풀이]

colors = ["red", "green", "blue"]
for (color) in colors:
    print(color)

🔻🔻🔻 문제 분석 🔻🔻🔻

  • for color in colors:
    • color에 colors는 리스트에 있는 값 반복 삽입
      color 말고 다른 임의의 변수값도 설정 가능! (예. i, a 등)
  • print(color)
    • 리스트 내 있는 값 하나씩 출력
profile
어떻게든 하겠숴여...❕

0개의 댓글