파이썬 기초문법을 계속 배우고 있는데요 이번에는 if else(conditional), string variables,module 에 대하여 정리하고 본격적인 web scrapping TIL 적겠습니다.
string 변수 이어쓰는 방법에 대하여 정리해보겠습니다.
example 1:
hello hello1은 같은 역할을 하지만 보이시나요? + 를 사용하는것과 앞에 f 를 붙이는 것의 차이?
f-string 이라고 literal string 이라고 정의되고 있네요..
간단하게 찾아보아 이해했을때는 다음 쌍따움표(" "), 따움표(' ') 를 만날때 까지 실행을 안하는것 이라는군요...
순서만 지정해주면 변수 위치 는 상관없다 !!
보통 Case Example 1:
def my_intro(name,age,place,fav_food): return f"Hi my name is {name}, i am {age} years old, I live in {place}, my favorite food is {fav_food}" intro=my_intro("fran",30,"seoul","kimbap",) print(intro)
" Hi my name is fran , i am 30 years old, I live in seoul, my favorite food is kimbap" 이라고 출력된다.
Before:
def menu(a,c,d): return f"봄 메뉴는 {a},가을 메뉴는 {c}이고, 겨울 메뉴는 {d}번이에요" shop_menu= menu(a="카페라떼",c="모카",d="민트초코") print(shop_menu)
After:
#순서가 상관없음 def menu(a,c,d,b): return f"봄 메뉴는 {a}, 여름메뉴는 {b}입니다, 가을 메뉴는 {c}이고, 겨울 메뉴는 {d}번이에요" #대신에 변수를 지정해줘야함 shop_menu= menu(a="카페라떼",c="모카",d="민트초코",b="레몬에이드") print(shop_menu)
https://docs.python.org/3/library/stdtypes.html #비교문 comparison
#chaining is /or
def boy(a,b):
if type(a) is int or type(b) is int:
return print("hello")
else:
a=int(a)
b=int(b)
return print(a+b)
boy("12","10")
boy("12","10")a
String 이니까 return a+b 가 안됨
def can_drink(a):
print( f"you are {a} years old")
if a>=18:
return print("you can drink")
elif a<18:
return print("you are too young")
else :
return print("welcome")
can_drink(18)
Tuple
days = ("Mon", "Tue", "Wed", "Thu","Fri")
for day in days:
print(day)
for day in days:
if day == "Wed":
break
else:
print(day)
for i in days:
print(i)
본인은 i 를 좋아해서 i 를 사용했지만 day, days,helloworld 아무거나 다 됩니다.
Break를 선언 하면 해당 내용이있을때 끝납니다.
파이썬 모듈에 대해서 잠깐 배워봤습니다 Math 기능이 Module 이라고 하네요..
Math Module 활용 예시:
import math print(math.ceil(1.2)) #ceil 반올림 print(math.floor(1.2)) #반내림 a = [.1,.1,.1,.1,.1,.1,.1,.1,.1] print(sum(a)) b= [.1,.1,.1,.1,.1,.1,.1,.1,.1] print(math.fsum(b))
다른 파일의 function 불러오기 예시:
#활용법:from somefile import function as name #calulator 에서 plus 라는 function을 불러오기 from calculator import plus as plus print(plus(1,2))