본문 바로가기
python/Python으로 웹 스크래퍼 만들기

1. Theory_2

by 2cong 2020. 3. 24.

1.8 Code Challenge!

계산기 만들기

  • 방법 1: 숫자 argument로 입력
def plus(a,b):
    return int(a)+int(b)

def minus(a,b):
    return int(a)-int(b)

def times(a,b):
    return int(a)*int(b)


def division(a,b):
    return int(a)/int(b)


print (plus(5,4)) # 9
print (minus(5,4)) # 1
print (times(5,4)) # 20
print (division(5,4)) # 1.25
  • 방법 2 : 숫자 input으로 입력
def plus():
    a=int(input('숫자를 입력하세요 : '))
    b=int(input('숫자를 입력하세요 : '))
    return(a+b)

def minus():
    a=int(input('숫자를 입력하세요 : '))
    b=int(input('숫자를 입력하세요 : '))
    return(a-b)

def times():
    a=int(input('숫자를 입력하세요 : '))
    b=int(input('숫자를 입력하세요 : '))
    return(a*b) 

def division():
    a=int(input('숫자를 입력하세요 : '))
    b=int(input('숫자를 입력하세요 : '))
    return(a/b)


print (plus())
    #    숫자를 입력하세요 : 5
    #    숫자를 입력하세요 : 4
    #    9
print (minus())
    #    숫자를 입력하세요 : 5
    #    숫자를 입력하세요 : 4
    #    1
print (times())
    #    숫자를 입력하세요 : 5
    #    숫자를 입력하세요 : 4
    #    20       
print (division())
    #    숫자를 입력하세요 : 5
    #    숫자를 입력하세요 : 4
    #    1.25

1.9 Conditionals part One

조건에 따라 함수 실행시키기

def function():
    if Condition:
       Result

만약 Condition 이 True면 그 아래의 Result 실행

  • and vs or
    • a and b : a와 b 둘 다 True면 True
    • a or b : a 또는 b 중 하나라도 True면 True

예시)

def plus(a,b):
       if type(a)==str or type(b)==str:
               return '숫자만 입력가능'
       else :
               return a+b

위에서 a 또는 b 둘 중 하나라도 type이 string이면 '숫자만 입력가능' 반환
둘 다 string이 아닌 경우 연산!

print (plus("3",4)) # 숫자만 입력가능
print (plus(3.14,4)) # 7.14

1.10 if else and or

img ref) https://www.concretepage.com/python/python-boolean-operators

  • if 가 True면 그 문장 실행 False면 다음문장으로 넘어감
  • elif 가 True면 그 문장 실행 False면 다음문장으로 넘어감

예시)

def drink(age):
    print (f"""A : How old are you?
B : I am {age} years old""") 
    if  0<=int(age)<=19 : 
        print ('A : You can\'t drink beer')
    elif int(age)<0 :  
        print ('A : What?')
    else :
        print ('A : You can drink beer')  
  • f 문자열 포매팅 이용하여 age 입력
  • """ ~ """ 이용하여 여러줄의 문장 출력
  • int(age) 이용하여 문자열로 숫자가 들어와도 오류가 나지않도록 함


결과)

drink (18)
    #   A : How old are you?
    #   B : I am 18 years old
    #   A : You can't drink beer
drink (-3)
    #   A : How old are you?
    #   B : I am -3 years old
    #   A : What?
drink ("20") 
    #   A : How old are you?
    #   B : I am 20 years old
    #    A : You can drink beer

1.11 for in

  • for 변수 in 컨테이너(리스트 또는 튜플, 문자열) :
    result

    list = [1,2,3,4,5,6,7]
    for i in list:
      print (i)
  • break

    • for문 강제 종료
list = [1,2,3,4,5,6,7]
for i in list:
    if i ==5 :
        break
    print (i)    

1.12 Modules

  • 기능의 집합

  • Module 사용  :  import 모듈이름

  • import module_name
    print(module_name.func_name()   < -- 이런식으로 사용

뒤의 func_name()은 모듈에 포함되어있는 함수 이름

 

예시) module 이름 > math // 포함된 함수 이름 > factorial()

import math
print (math.factorial(4))  # 24
  • Module 내에서 특정 함수만 import하기

    • import module 사용 : 모듈 내의 모든 함수를 import ---> 비효율적
    • 사용할 함수만 import하는 것이 효율적
    • from module_name import func_name 사용

    from module_name import func_name
    print(func_name()) 과 같이 사용

예시) module 이름 > math // 포함된 함수 이름 > factorial(), ceil()
                                        * ceil - 올림 함수

  from math import ceil,factorial

  print (ceil(2.3)) # 3
  print (factorial(4)) # 24
  • Module 내의 특정함수 이름 바꾸기

    from module_name import func_name as new_name

예시)

from math import ceil as 올림, trunc as 버림

  print (올림(2.3)) # 3
  print (버림(4.8)) # 4

'python > Python으로 웹 스크래퍼 만들기' 카테고리의 다른 글

2. Building a Job Scrapper_4  (0) 2020.03.25
2. Building a Job Scrapper_3  (0) 2020.03.25
2. Building a Job Scrapper_2  (0) 2020.03.24
2. Building a Job Scrapper_1  (0) 2020.03.24
1. Theory_1  (0) 2020.03.20

댓글