๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL๐Ÿ”ฅ/๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ_AI School 5๊ธฐ

[๋ฉ‹์‚ฌ] AI SCHCOOL 5๊ธฐ_ Day2

by hk713 2022. 3. 15.

 int() 

์ˆซ์ž ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ!

python
# input์œผ๋กœ ๋ฐ›์€ ๊ฑด ๊ธ€์ž ํ˜•์‹์ž„
number = input("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ")

# ์ˆซ์ž ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
number = int(number)

 

 List 

# ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ๊ฐ๊ฐ์˜ data๋ฅผ ์š”์†Œ(element)๋ผ๊ณ  ๋ถ€๋ฆ„
colors = ["๋นจ๊ฐ•","ํŒŒ๋ž‘","๋…ธ๋ž‘"]

# ์š”์†Œ์˜ index(์ธ๋ฑ์Šค)๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘
print(colors[0])  # "๋นจ๊ฐ•"

# ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€1 - ๋ฆฌ์ŠคํŠธ ๋งจ ๋’ท์ž๋ฆฌ์— ์ถ”๊ฐ€
colors.append("๋ณด๋ผ")

# ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€2 - ํŠน์ • index์— ์ถ”๊ฐ€
colors.insert(1,"๋ถ„ํ™")

# ๋ฐ์ดํ„ฐ ์‚ญ์ œ1 - index๋ฒˆํ˜ธ๋กœ ์‚ญ์ œ
del colors[0]  

# ๋ฐ์ดํ„ฐ ์‚ญ์ œ2 - ์š”์†Œ ์ž์ฒด๋ฅผ ์ž…๋ ฅํ•ด ์‚ญ์ œ
colors.remove("๋นจ๊ฐ•")

# ๋ฆฌ์ŠคํŠธ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ
len(colors)
numbers = [5, 10, 7, 13, 20]

# ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์ˆซ์ž ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
ํ•ฉ๊ณ„ = sum(numbers)

# ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์ˆซ์ž ํ‰๊ท  ๊ตฌํ•˜๊ธฐ - ํ‰๊ท =์ „์ฒดํ•ฉ/์ „์ฒด๊ฐœ์ˆ˜
ํ‰๊ท  = sum(numbers) / len(numbers)

# ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ์ž‘์€ ๊ฐ’ ์ฐพ๊ธฐ
์ตœ๋Œ“๊ฐ’ = max(numbers)
์ตœ์†Ÿ๊ฐ’ = min(numbers)

 

 Dictionary 

age = {"๋ฝ€๋กœ๋กœ": 6, "๋ฃจํ”ผ": 8, "ํฌ๋กฑ": 4}

# ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
age["์—๋””"] = 5

# ๋ฐ์ดํ„ฐ ๊ฐ’ ๋ณ€๊ฒฝ
age["๋ฃจํ”ผ"] = 7

# ๋ฐ์ดํ„ฐ ์‚ญ์ œ
del age["ํฌ๋กฑ"]

 

 ์—ฐ์‚ฐ์ž 

a == b           # ๊ฐ™๋‹ˆ?

a != b            # ๋‹ค๋ฅด๋‹ˆ?

a < b             # ์ž‘๋‹ˆ?

a > b             # ํฌ๋‹ˆ?

a <= b           # ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ˆ?

a >= b           # ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ˆ?

⇒ ์กฐ๊ฑด์€ True ๋˜๋Š” False ๋กœ ๋‹ตํ•จ!

 

IF + List

colors = ["๋นจ๊ฐ•","ํŒŒ๋ž‘","๋…ธ๋ž‘"]

color = input("์ข‹์•„ํ•˜๋Š” ์ƒ‰๊น”์ด ๋ญ์•ผ? ")

# input ๊ฐ’์ด List ์•ˆ์— ์žˆ๋‹ค๋ฉด?!
if color in colors:
	print("๋‚˜๋„ ๊ทธ ์ƒ‰ ์ข‹์•„ํ•ด!!")
else:
	print("์–ด?! ๊ทธ๋ ‡๊ตฌ๋‚˜")

 

IF + Dictionary

books = {"์‚ฌ๋ž‘์˜ ๊ธฐ์ˆ ": 12000, "์ˆœ์ „ํ•œ ๊ธฐ๋…๊ต": 8000, "์ด๊ธฐ์  ์œ ์ „์ž": 9000}

book = input("์ฑ… ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”: ")

# input ๊ฐ’์ด Dictionary ์•ˆ์— ์žˆ๋‹ค๋ฉด?!
if book in books:
	print(book,"์ฑ… ๊ฐ’์€",books[book],"์ž…๋‹ˆ๋‹ค.")
else:
	print("ํ•ด๋‹น ์ฑ… ๊ฐ’์˜ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")

 

 Range() 

range(์‹œ์ž‘์ˆซ์ž, ์ข…๋ฃŒ์ˆซ์ž, ์ฆ๊ฐ์ˆซ์ž) ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ!

# "Good Morning!"์„ 5๋ฒˆ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
for i in range(5):
	print("Good Morning!")

# 10๋ถ€ํ„ฐ 1๊นŒ์ง€ ์นด์šดํŠธ ๋‹ค์šด์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
for i in range(10,0,-1):
	print(i)

 Web Crawling 

ํฌ๋กค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด ์›น ํŽ˜์ด์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•ด ๋‚ด๋Š” ํ–‰์œ„!

 

 ๋ชจ๋“ˆ(Module) 

์ž์ฃผ ์“ฐ๋Š” ํ•จ์ˆ˜๋“ค์„ ๋ชจ์•„๋†“์€ ํŒŒ์ผ!

 

 ์š”์ฒญ๊ณผ ์‘๋‹ต 

๐Ÿ™‹‍โ™€๏ธClient - - ์š”์ฒญ(requests) - - > Server ๐Ÿ–ฅ

๐Ÿ™‹‍โ™€๏ธClient < - - ์‘๋‹ต(response) - - Server ๐Ÿ–ฅ

 

 BeautifulSoup 

# bs4 ๋ผ๋Š” ๋ชจ๋“ˆ์—์„œ BeautifulSoup ๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ์˜ด!
from bs4 import BeautifulSoup  
import requests

url = "http://www.naver.com/"
response = requests.get(url)
 
# ์ถœ๋ ฅ1
print(response.text)

# ์ถœ๋ ฅ2
print(BeautifulSoup(response.text,'html.parser'))

 '์ถœ๋ ฅ1' ๊ณผ '์ถœ๋ ฅ2' ์˜ ์ฝ˜์†”์ฐฝ ๊ฒฐ๊ณผ๋Š” ๋˜‘๊ฐ™๋‹ค

๊ทธ๋Ÿฐ๋ฐ

→ type์„ ํ™•์ธํ•ด๋ณด๋ฉด...

‘์ถœ๋ ฅ1’ ์€ <class ‘str’>: ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด ๋ฉ์–ด๋ฆฌ

‘์ถœ๋ ฅ2’ ๋Š” <class ‘bs4.BeautifulSoup’>: ๋ฌธ์ž์—ด ๋ฉ์–ด๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋–ผ์„œ(parsing) BeautifulSoup ํ†ต์— ๋”ฐ๋กœ ๋„ฃ์–ด์ค€ ๊ฒƒ

 

 Datetime 

from datetime import datetime

# '2022๋…„ 03์›” 15์ผ' ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ 
print(datetime.today().strftime("%Y๋…„ %m์›” %d์ผ")

 

 ํŒŒ์ผ 

  • r : read. ๋‚ด์šฉ์„ ์“ฐ๊ฑฐ๋‚˜ ๋”ํ•˜์ง€ ๋ชปํ•จ
  • w : write. ๊ธฐ์กด ๋‚ด์šฉ์— ๋ฎ์–ด์”€
  • a : append. ๊ธฐ์กด ๋‚ด์šฉ์— ์ถ”๊ฐ€ํ•ด์„œ ์”€

 

 ํฌ๋กค๋ง ์ฐจ๋‹จ ์‹œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• 

ํฌ๋กค๋ง์„ ์ฐจ๋‹จํ•˜๋Š” ์‚ฌ์ดํŠธ์— ๋Œ€ํ•ด์„œ๋Š” header์— ์ •๋ณด๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค

# ์˜ˆ์‹œ
import requests

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
url = "ํฌ๋กค๋งํ•  URL"
response = requests.get(url,headers=headers)

 

โš ๏ธ ํ•ด๋‹น ๊ธ€์€ ๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ AI SCHOOL ๊ต์œก ๋‚ด์šฉ์„ ๋ณต์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค

โš ๏ธ ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ๋ณ€ํ˜•ํ–ˆ์Šต๋‹ˆ๋‹ค

๋Œ“๊ธ€