https://prefer-all.tistory.com/64
1-1 Basic computer class for newbies
운영체제: 우리의 프로그램을 동작할 수 있는 구동 환경
Software (Applications -> OS) <-> Hardware(CPU + Memory)
Python은 운영체제에 독립적
Interpreter은 운영체제에 의존적
파일 시스템: OS에서 파일을 저장하는 트리구조 저장 체계
파일: 기기에서의 의미있는 정보를 담는 논리적인 단위
디렉토리(폴더): 파일과 다른 디렉토리 포함 가능
터미널: 마우스가 아닌 키보드로 명령을 입력하는 프로그램 실행
Graphical User Interface(GUI)가 아닌 Command Line Interface(CLI)
각 터미널에서는 프로그램을 작동하는 shell이 존재하고 shell마다 다른 명령어를 사용함
1-2 파이썬 개요
플랫폼 독립적, 인터프리터 언어, 객체 지향, 동적 타이핑 언어, 처음 c로 구현
2-3 Conditions & Loops
trapezium_area.py 라는 파일
def addition(x, y):
return x+y
def multiplication(x, y):
return x*y
def divided_by_2(x):
return x/2
콘솔 창에서 불러오기
>>> import trapezium_area as ta #trapezium_area 파일을 ta라는 이름으로 부름
>>> ta.addition(10,5)
15
1) trapezium_area_test.py 라는 파일
if __name == "__main__": 을 사용하면 아무 일도 일어나지 않음
def addition(x, y):
return x+y
def multiplication(x, y):
return x*y
def divided_by_2(x):
return x/2
def main():
print(15 == addition(10,5))
print(addition(10,100) == 110)
if __name=="__main__":
main()
2) if~ 문 없이 main이면
main()이 실행됨
2-4 String & Advanced function concept
- string은 1 byte의 크기로 한 글자씩 메모리 공간이 할당됨
- 각 타입 별로 메모리 공간을 할당 받은 크기가 다름
- 메모리 공간에 따라 표현할 수 있는 숫자범위가 다름
예) 4byte = 32bit = 232 = 4,294M = -2,147M ~ + 2.147M 까지 표시
raw string
특수 기호인 \ 의 줄 넘기기 기능을 무시하고 그대로 출력
>>> raw_string = r"이제 파이썬 강의 그만 만들고 싶다. \n 레알"
>>> print(raw_string)
이제 파이썬 강의 그만 만들고 싶다. \n 레알
Type Hinting
파이썬 코드를 작성할 때 타입에 대한 메타 정보를 제공하는 것
num: int = 1
def repeat(message: str, times: int = 2) -> list:
return [message] * times
repeat(num) #[1,1]
Function docstring
파이썬 함수에 대한 상세스펙을 사전에 작성
세 개의 따옴표로 docstring 영역 표시
def isPrime(n):
'''
n이 소수인지 판별하는 함수
'''
3-1 Python Data Structure
Tuple
- 선언 시 “[ ]” 가 아닌 “( )”를 사용
>>> t = (1) # 일반정수로 인식
1
>>> t = (1, ) # 값이 하나인 Tuple은 반드시 "," 를 붙여야 함
(1,)
Set
- 값을 순서없이 저장하고 같은 원소의 중복을 허용하지 않음
.add()는 원소 한 개만 추가 / update는 여러 개 한 번에 추가
s = set([1,2,3])
s.update([1,4,5])
s #{1, 2, 3, 4, 5}
s.discard(3)
s #{1, 2, 4, 5}
s.clear() #모든 원소 삭제
합집합은 .union() , |
교집합은 .intersection(), &
차집합은 .difference(), -
>>> s1 = set([1,2,3,4,5])
>>> s2 = set([3,4,5,6,7])
>>> s1.union(s2) # s1 과 s2의 합집합
{1, 2, 3, 4, 5, 6, 7}
>>> s1 | s2 # set([1, 2, 3, 4, 5, 6, 7])
{1, 2, 3, 4, 5, 6, 7}
>>> s1.intersection(s2) # s1 과 s2의 교집합
{3, 4, 5}
>>> s1 & s2 # set([3, 4, 5])
{3, 4, 5}
>>> s1.difference(s2) # s1 과 s2의 차집합
{1, 2}
>>> s1 - s2 # set([1, 2])
{1, 2}
Dict
country = {} #country = dict() 도 가능
country = {"US":1, "Korea": 82}
country.items() #dict_items([('US', 1), ('Korea', 82)])
country.keys() #dict_keys(['US', 'Korea'])
country.values() #dict_values([1, 82])
for k,v in country.items():
print("key", k)
print("value", v)
"Korea" in country.keys()
'''
key US
value 1
key Korea
value 82
True
'''
Deque
append vs extend
import collections
# list
lst = ['a', 'b', 'c','d']
lst2 = ['a', 'b', 'c', 'd']
lst2.append('ef') # append()
lst.extend('ef') # extend()
print("lst.extend('ef') >> ", lst)
print("lst2.append('ef') >>", lst2)
'''
결과
lst.extend('ef') >> ['a', 'b', 'c', 'd', 'e', 'f']
lst2.append('ef') >> ['a', 'b', 'c', 'd', 'ef']
'''
#출처: https://excelsior-cjh.tistory.com/entry/collections-모듈-deque [EXCELSIOR:티스토리]
extendleft
import collections
deq = collections.deque(['a', 'b', 'c'])
deq.extendleft('de')
print(deq)
'''
결과
deque(['e', 'd', 'a', 'b', 'c'])
'''
rotate(n)
element를 n만큼 회전
n이 음수이면 왼쪽으로, n이 양수이면 오른쪽으로 회전
import collections
deq = collections.deque(['a', 'b', 'c', 'd', 'e'])
deq.rotate(1)
print('deq >>', ' '.join(deq))
deq2 = collections.deque(['a', 'b', 'c', 'd', 'e'])
deq2.rotate(2)
print('deq2 >>', ' '.join(deq2))
'''
결과
deq >> e a b c d
deq2 >> d e a b c
'''
'AI TECH' 카테고리의 다른 글
[2주차] Pytorch 프로젝트 (0) | 2022.09.26 |
---|---|
[2주차] PyTorch (0) | 2022.09.26 |
CNN의 역전파 (0) | 2022.09.22 |
[1주차] Python&Math : Generator, Asterisk, 가변인자 (0) | 2022.09.20 |
Logistic Regression (0) | 2022.07.27 |