상세 컨텐츠

본문 제목

[PLAYDATA] 데이터 엔지니어링 8월 3주차 8/16

PLAYDATA/PLAYDATA데일리노트

by Na느님 2023. 8. 17. 15:53

본문

  • 8월 16일
두번째 수업날이다. 저번 수업때의 내용을 잠깐 리마인드 하고 다음 진도를 나갔다. 오늘 처음으로 파이썬에서 함수라는 개념이 수업에 나왔었다. 나는 전공자이기도 하고, 따라서 파이썬을 쓴 적이 있어서 함수 개념은 이미 알고 있어서 이해하는데는 무리가 없었다.

 

 

집합 연산
합집합: set.union(set1, set2) (set1 | set2)
교집합: set.intersection(set1, set2) (set1 & set2)
차집합: set.difference(set1, set2) (set1 - set2)
대칭차집합: set.symmetric_difference(set1, set2) (set1 ^ set2)

중복되지 않은 elements를 대입: set1.update(set2) (set1 |= set2)
중복되는 elements만 대입: set1.intersection_update(set2) (set1 &= set2)
특정 elements를 제거: set1.difference_update(set2) (set1 -= set2)
겹치는 요소는 제거하고 겹치지 않으면 추가: set1.symmetric_difference_update(set2) (set1 ^= set2)


Dictionary 연산
dictionary의 접근은 인덱스가 아니라 key값으로 접근한다.
dictionary에 새로운 element 추가: dictionary_name[key] = value
element 제거: del dictionary_name[key]
Dictionary에 다른 dictionary 추가: dict1.update(dict2)
dictionary 요소들 출력: dict1.items()
dictionary 초기화: dict1.clear()

dictionary에서 key값만 추출: dict1.keys()
dictionary에서 value값만 추출: dict1.values()

Packing과 Unpacking
개별 변수들을 하나의 상위 집합 개념의 변수로 합치는 것이 packing이다.
하나의 변수를 하위 단위로 쪼개는 것이 unpacking이다.
Packing과 Unpacking 모두 변수 앞에 *를 붙여서 사용한다.
(Unpacking에서의 *는 list나 tuple같은 컨테이너형 변수를 쪼개는 역할을 한다.)
Packing 예시)
x=123; y=456
z=x, y

list1 = [1, 2, 3, 4]
a, *b = list1

def func(*args):
      print(args)
      print(type(args))
func(1, 2, 3, 4, 5, 6, 'a', 'b')
#이 경우에는 tuple 형태로 묶인다

Unpacking 예시)
def my_print(*contents):
    print(*contents)
my_print("hello")  # hello
my_print("hello", "world")  # hello world


함수
함수의 형식
def func_name(args):
    #codes


조건문
조건문 형식
if condition:
    #codes
elif condition:
    #codes
else:
    #codes


반복문
반복문 형식
for var in array:
    #codes
(array는 list, tuple, set, dictionary, range가 가능하다. dictionary의 경우 var에 key값이 대입된다)

(array에 string을 대입할 경우, var에는 문자 하나(character)가 대입된다!)
range함수 형식
argument 1개 range(end_idx + 1)
arguments 2개 range(start_idx, end_idx + 1)
arguments 3개(step이 양수인 경우) range(start_idx, end_idx + 1, step)
arguments 3개(step이 음수인 경우) range(start_idx, end_idx - 1, step)

for문 팁:
var 갯수가 여러개인 경우: in 뒤에 있는 array의 elements들이 var갯수만큼 elements를 담은 array자료구조 이어야 한다.
array 부분에 숫자 형식의 str (예를들어 "123")이 들어가면, var은 그 숫자의 각 자릿수에 해당한다.

while condition:
    #codes


표준 입출력
입력함수: input(str) (str은 입력이 활성화될 때 화면에 출력할 문자열)

관련글 더보기