내일배움캠프(사전캠프)

[내일배움캠프] 사전캠프 (10일차)

anscodus 2026. 2. 25. 16:58

오늘 한 일

 

  • 파이썬 (1-9 ~ 1-14)

1-9 함수

더보기

함수 : 반복적으로 사용하는 코드들에 이름을 붙여놓은 것

def hello():
		print("안녕!")
    print("또 만나요!")


hello()
hello()

 

조건문에 넣을 값을 바꿔가면서 결과를 확인할 때 쓰면 편리함

def bus_rate(age):
		if age > 65:
		    print("무료로 이용하세요")
		elif age > 20:
		    print("성인입니다.")
		else:
		    print("청소년입니다")

bus_rate(27)
bus_rate(10)
bus_rate(72)

 

출력 뿐만 아니라 결과 값을 돌려주도록 만들 수 있음

def bus_fee(age):
		if age > 65:
		    return 0
		elif age > 20:
		    return 1200
		else:
		    return 0     


money = bus_fee(28)
print(money)

 

 

 

Q.주민등록번호를 입력받아 성별을 출력하는 함수 만들기

def check_gender(pin):
    print('')

my_pin = '200101-3012345'
check_gender(my_pin)

 

A.

def check_gender(pin):
    num = pin.split('-')[1][0]
    if int(num) % 2 == 0:
        print('여성')
    else:
        print('남성')

my_pin = "200101-3012345"
check_gender(my_pin)

 

1-10 파이썬 심화 문법 뽀개기

약간 더 심화된 문법을 배울 예정

 

1-11 튜플, 집합

더보기

튜플 : 리스트와 비슷하지만 불변인 자료형, 순서 존재

a = (1,2,3)

print(a[0])

 아래와 같은 작업은 불가능함.

a = (1,2,3)
a[0] = 99

 -> 딕셔너리 대신 리스트와 튜플로 딕셔너리 비슷하게 만들어 사용해야 할 때 많이 쓰임

a_dict = [('bob','24'),('john','29'),('smith','30')]

 

 

 

집합 : 집합을 구현. 중복이 제거됨

a = [1,2,3,4,5,3,4,2,1,2,4,2,3,1,4,1,5,1]

a_set = set(a)

print(a_set)

 

교집합/합집합/차집합도 구할 수 있다.

a = ['사과','감','수박','참외','딸기']
b = ['사과','멜론','청포도','토마토','참외']

a_set = set(a)
b_set = set(b)

print(a_set & b_set)  # 교집합
print(a_set | b_set)  # 합집합

 

 

Q. A가 들은 수업 중, B가 듣지 않은 수업을 찾아보기

- a, b의 차집합을 구하는 방법 구글링해서 알아보기!

A.

a_set = set(student_a)
b_set = set(student_b)

print(a_set-b_set)

 

1-12 f-string

더보기

ex) for문

scores = [
    {'name':'영수','score':70},
    {'name':'영희','score':65},
    {'name':'기찬','score':75},
    {'name':'희수','score':23},
    {'name':'서경','score':99},
    {'name':'미주','score':100},
    {'name':'병태','score':32}    
]

 

이름, 점수 출력

for s in scores:
    name = s['name']
    score = str(s['score'])
    print(name,score)

 

for s in scores:
    name = s['name']
    score = str(s['score'])
    print(name+'는 '+score+'점 입니다')

 

 

f-string을 이용하면 간단하게 해결 가능

for s in scores:
    name = s['name']
    score = str(s['score'])
    print(f'{name}은 {score}점입니다')

 

1-13 예외처리

더보기

try-except 문 : 에러가 있어도 건너뛰게 할 수 있는 방법

(실제 프로젝트에서 남용 금물. 에러의 원인을 알 수 없다.)

 

ex)

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben', 'age': 27},
    {'name': 'bobby', 'age': 57},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]

 

20세 이상만 출력

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben', 'age': 27},
    {'name': 'bobby', 'age': 57},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]

for person in people:
    if person['age'] > 20:
        print (person['name'])

 

중간에 잘못 들어간 데이터 존재 시 (bobby)

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben', 'age': 27},
    {'name': 'bobby'},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]

for person in people:
    if person['age'] > 20:
        print (person['name'])

 

try except 구문 이용하면 에러를 넘길 수 있다.

for person in people:
    try:
        if person['age'] > 20:
            print (person['name'])
    except:
        name = person['name']
        print(f'{name} - 에러입니다')

 

 

1-14 파일 불러오기

더보기

main_test.py

from main_func import *

say_hi()

 

main_func.py

def say_hi():
	print('안녕!')

 


  • 과제: 반복문과 조건문을 활용하여 '숫자 맞추기 게임' 또는 '구구단 출력기' 로직 짜보기

작성 코드

더보기

 

print('구구단 출력하기')
for i in range(1,10):
    print(i,'단')
    for j in range(1,10):
        multiply = i * j
        print(i,'x',j,'=',multiply)
        j = j+1
    i = i+1

 

range 범위가 살짝 헷갈렸다

실행 결과

더보기

구구단 출력하기
1 단
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
2 단
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
3 단
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
4 단
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
5 단
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
6 단
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
7 단
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
8 단
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
9 단
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81