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

[๋ฉ‹์‚ฌ] AI SCHOOL 5๊ธฐ_ Day 1

by hk713 2022. 3. 14.
๋ฐ์ดํ„ฐ๋ฅผ ์†Œ์œ ํ•˜๊ณ 
์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด
์ผ๋ฐ˜์ธ์„ ๋ฐ”๋ผ๋ณด๋Š” ์‹œ์„ ์€
19์„ธ๊ธฐ ์œ ๋Ÿฝ์ด ์•„ํ”„๋ฆฌ์นด๋ฅผ ๋ณด๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค
- ์ฑ… <ํ˜ธ๋ชจ ๋ฐ์šฐ์Šค> ์œ ๋ฐœ ํ•˜๋ผ๋ฆฌ -

 

๐Ÿšซ DRY(Don't Repeat Yourself) : ์ปดํ“จํ„ฐ๊ฐ€ ํ•  ์ผ์„ ๋‚ด๊ฐ€ ํ•˜์ง€ ๋ง์ž

 

 Random 

์ปดํ“จํ„ฐ๊ฐ€ ๋žœ๋ค์œผ๋กœ ํ•˜๋‚˜๋ฅผ ๋ฝ‘๋„๋ก ํ•  ๋•Œ ์‚ฌ์šฉ!

import random

# ์ˆซ์ž 1-5 ์ค‘์—์„œ ๋žœ๋ค์œผ๋กœ ํ•œ๊ฐœ๋ฅผ ์ถœ๋ ฅ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด?!
print(random.choice([1,2,3,4,5]))

 

 ์ด๋ฆ„ ๋ถ™์ด๊ธฐ 

import random

# ๋žœ๋ค์œผ๋กœ ๋ฝ‘์€ ๊ฐ’์— 'result' ๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  ์‹ถ๋‹ค๋ฉด?!
result = random.choice([1,2,3,4,5])

# 'result' ๊ฐ’์ด ํ•ญ์ƒ 1 ์ด๊ณ  ์‹ถ๋‹ค๋ฉด?!
result = 1

 

 Dictionary 

์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ!

# ์—ฌ๋Ÿฌ ์ฑ…๋“ค์˜ ์ •๋ณด(์ด๋ฆ„๊ณผ ์ €์ž)๋ฅผ ํ•˜๋‚˜๋กœ ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
books = {"์‚ฌ๋ž‘์˜ ๊ธฐ์ˆ ":"์—๋ฆฌํžˆ ํ”„๋กฌ","์ด๊ธฐ์  ์œ ์ „์ž":"๋ฆฌ์ฒ˜๋“œ ๋„ํ‚จ์Šค","์—ฌ๋ฆ„์˜ ์ฑ…":"ํ† ๋ฒ  ์–€์†"}

# '์ด๊ธฐ์  ์œ ์ „์ž'์˜ ์ €์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
print(books.get("์ด๊ธฐ์  ์œ ์ „์ž"))

# ์ฑ…์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
books["์ˆœ์ „ํ•œ ๊ธฐ๋…๊ต"] = "C.S. ๋ฃจ์ด์Šค"

# '์‚ฌ๋ž‘์˜ ๊ธฐ์ˆ ' ์ฑ… ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
del books["์‚ฌ๋ž‘์˜ ๊ธฐ์ˆ "]

# ๋ช‡ ๊ฐœ์˜ ์ฑ… ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด?!
print(len(books))

# ๋ชจ๋“  ์ฑ… ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
books.clear()

 

 List 

์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐ’๋“ค์„ ํ•˜๋‚˜๋กœ ๋ฌถ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ!

# ๋””์ฆˆ๋‹ˆ ์บ๋ฆญํ„ฐ๋“ค์„ ๋ชจ์•„ 'disney'๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  ์‹ถ๋‹ค๋ฉด?!
disney = ["์—˜์‚ฌ","๋ชจ์•„๋‚˜","์ฃผ๋””"]

# ์ฒซ๋ฒˆ์งธ๋กœ ์ €์žฅ๋œ ์บ๋ฆญํ„ฐ์˜ ์ด๋ฆ„์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด?!
print(disney[0])  
# ๋งˆ์ง€๋ง‰์œผ๋กœ ์ €์žฅ๋œ ์บ๋ฆญํ„ฐ์˜ ์ด๋ฆ„์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด?!
print(disney[-1])

# '๋ผํ‘ผ์ ค'์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
disney.append("๋ผํ‘ผ์ ค")

# '์ฃผ๋””'๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
del disney[2]

 

 For 

๋ฐ˜๋ณต๋ฌธ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ!

disney = ["์—˜์‚ฌ","๋ชจ์•„๋‚˜","์ฃผ๋””"]

# 'disney'์˜ ๋ชจ๋“  ์บ๋ฆญํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
for character in disney:
	print(character)


books = {"์‚ฌ๋ž‘์˜ ๊ธฐ์ˆ ":"์—๋ฆฌํžˆ ํ”„๋กฌ","์ด๊ธฐ์  ์œ ์ „์ž":"๋ฆฌ์ฒ˜๋“œ ๋„ํ‚จ์Šค","์—ฌ๋ฆ„์˜ ์ฑ…":"ํ† ๋ฒ  ์–€์†"}

# 'books'์˜ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
for name,author in books.items():
	print(name)
	print(author)

 

 ์ง‘ํ•ฉ 

์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ! ((์ˆœ์„œ ์—†์Œ))

## ์ง‘ํ•ฉ์„ ๋งŒ๋“œ๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•
# 1
disney = ["์—˜์‚ฌ","๋ชจ์•„๋‚˜","์ฃผ๋””"]
set1 = set(disney)

# 2
set2 = set(["์—˜์‚ฌ","๋ชจ์•„๋‚˜","์ฃผ๋””"])

## ์ง‘ํ•ฉ์˜ ํŠน์„ฑ
disney = set(["์—˜์‚ฌ","๋ชจ์•„๋‚˜","์ฃผ๋””","๋ฏธ๋ผ๋ฒจ"])
fixar = set(["์šฐ๋””","๋งˆ์ดํฌ","๋‹ˆ๋ชจ","๋ฏธ๋ผ๋ฒจ"])

# ํ•ฉ์ง‘ํ•ฉ - ์ค‘๋ณต์€ ์ž๋™ ์ œ๊ฑฐ๋จ
character = disney | fixar 

# ๊ต์ง‘ํ•ฉ 
character = disney & fixar

# ์ฐจ์ง‘ํ•ฉ
character = disney - fixar
# 'disney' ์—์„œ '์ฃผ๋””'๋ฅผ ๋นผ๊ณ  ์‹ถ๋‹ค๋ฉด?!
animal = "์ฃผ๋””"
disney = disney - set([animal])

# ์ง‘ํ•ฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
fixar = list(fixar)

 

 ๋งŒ์•ฝ์— 

# ์ปดํ“จํ„ฐ๊ฐ€ 1-5 ์ค‘์—์„œ 1์„ ๋ฝ‘์•˜์„ ๋•Œ "์ •๋‹ต"์„ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด "์˜ค๋‹ต"์„ ์ถœ๋ ฅ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด?!
import random

answer = random.choice([1,2,3,4,5])
if answer == 1:
	print("์ •๋‹ต")
else:
	print("์˜ค๋‹ต")

 

 While 

๋ฌดํ•œ ๋ฃจํ”„ ๋งŒ๋“ค ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ!

# ์ปดํ“จํ„ฐ๊ฐ€ 1-5 ์ค‘์—์„œ 1์„ ๋ฝ‘์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
import random

while True:
	answer = random.choice([1,2,3,4,5])
	if (answer != 1):
		print(answer)
	else:
		print("๋‚˜์™”๋‹ค!!")
		break

 

 Time 

์ปดํ“จํ„ฐ๋ฅผ ๋ช‡ ์ดˆ์”ฉ ์‰ฌ๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ!

import time

# 3์ดˆ๋ถ€ํ„ฐ 1์ดˆ์”ฉ ์นด์šดํŠธ ๋‹ค์šดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?!
for i in range(3,0,-1):
	print(i)
	time.sleep(1)
print("๋•ก๋•ก๋•ก")

 

 ํ•จ์ˆ˜ 

๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ญ‰ํ……์ด๋กœ ๋งŒ๋“ค์–ด ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉ!

# ๊ธฐ๋ณธ ๊ตฌ์กฐ
def ํ•จ์ˆ˜์ด๋ฆ„():
	ํ•จ์ˆ˜ ์ฝ”๋“œ1
	ํ•จ์ˆ˜ ์ฝ”๋“œ2
    	   .
      	   .
      	   .

 

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

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

๋Œ“๊ธ€