python

[TIL] Python Day 03 - 기본값, return, f-string, 조건문

기마니 2025. 10. 2. 15:11

 

수업 과정: 노마드 코더(Nomad Coders) - Python으로 웹 스크래퍼 만들기
기간: 2025년 9월 29일 - 2025년 10월 13일


 

[Day 03 학습 내용] 기본 매개변수(Default Parameter), return, f-string(formatted string), if-elif-else 조건문

 

1. 기본 매개변수 (Default Parameter): 함수 정의 시 파라미터에 기본값을 설정할 수 있음
  • 아규먼트를 전달하지 않으면 기본값이 사용됨
def say_hello(user_name="홍길동"):
    print("Hello", user_name)

say_hello("kim")  # Hello kim
say_hello() # Hello 홍길동

 

 

2. return: 함수가 값을 반환(return)할 수 있게 해줌
  • print()는 화면에 출력만 하지만, return은 값을 돌려줘서 다른 곳에서 사용 가능
  • return한 값을 변수에 저장하거나 다른 함수의 아규먼트로 전달할 수 있음
def tax_calc(money):
    return money * 0.35

def pay_tax(tax):
    print("thank you for paying", tax)

to_pay = tax_calc(15000000)
pay_tax(to_pay)

 

  • return의 재사용성: return한 값을 여러 함수에 연결해서 사용 가능
def make_juice(fruit):
    return f"{fruit}+🍹"

def add_ice(juice):
    return f"{juice}+🧊"

def add_sugar(iced_juice):
    return f"{iced_juice}+🍭"

juice = make_juice("🍏")
cold_juice = add_ice(juice)
perfect_juice = add_sugar(cold_juice)
print(perfect_juice)  # 🍏+🍹+🧊+🍭

 

 

3. f-string (Formatted String): 문자열 안에 변수나 표현식을 쉽게 삽입할 수 있는 방법
  • 문자열 앞에 f를 붙이고 중괄호 {}로 변수를 감쌈
my_name = "김말이"
print(f"Hello I'm {my_name}")

 

 

4. 조건문 (if-elif-else): 조건에 따라 다른 코드를 실행, 들여쓰기로 코드 블록 구분
  • if - 조건이 True일 때만 코드 실행
if 10 > 5:
    print("Correct!")
  • else - if 조건이 False일 때 실행
password_correct = False

if password_correct:
    print("Here is your money")
else:
    print("Wrong password")
  • elif (else if) - 여러 조건을 순차적으로 검사

      if 조건이 False일 때 다음 조건 확인

      else는 모든 조건이 False일 때 실행

winner = 6

if winner > 10:
    print("Winner is greater than 10")
elif winner < 10:
    print("Winner is less than 10")
else:
    print("Winner is 10")

 

 

Python vs Java 비교
구분 Python Java
기본값 def func(a=0): 메서드 오버로딩으로 구현
return return value return value; (세미콜론)
문자열 포맷팅 f"Hello {name}" String.format() 또는 "Hello " + name
조건문 if condition:
들여쓰기
if (condition) {
중괄호 {}
비교 연산자 ==, !=, >, < (동일)