일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- Python
- json
- mcponaws
- amazon q
- nosql
- Eclipse
- django
- Spark
- ai assistant
- devops
- 툴바안뜸
- AWS
- debug toolbar
- AI
- VPC
- coding with ai
- pyspark
- Transit Gateway
- tgw
- Amazon
- git
- debug_toolbar
- list
- MongoDB
- django-debug-toolbar
- SCALA APP
- AWSKRUG
- ubuntu
- Today
- Total
목록프로그래밍/Python (9)
STACKBASE
1. 이터러블, 이터러블 객체 문자열, 리스트, 튜플, 집합, 딕셔너리 등의 자료형 객체는 Iterable(반복 가능)하다. 원소를 하나씩 꺼내는 구조 iter(), __next__, next() 를 이용하여 원소를 순차적으로 꺼낼 수 있다. 꺼낼 원소가 존재하지 않으면 StopIteration Error를 발생시킨다.
1. 모듈의 의미 하나의 스크립트 프로그램 확장자 .py를 제외한 파일 이름 자체를 모듈 이름으로 사용 2. if __name__ == '__main__': ?? # A.py from typing import Any, Sequence def func(a, b): return a+b if __name__ == "__main__": print(f'값은 {func(3,4)} 입니다.') __name__ : 모듈 이름을 나타내는 변수 A.py가 직접 실행될 때 변수 __name__은 '__main__' A.py가 import 되어 사용될 때는 변수 __name__ == 'A' A.py가 직접 실행 될때 __name__ == "__main__" 참이되므로 print함수 실행 다른 스크립트에서 import 하여 실..
파이썬은 자료형 선언 없이 변수나 함수를 자유롭게 사용할 수 있으나, 이를 프로그래머가 봤을때 명시적으로 해석하기가 어렵다. 따라서 등장한 기능이 annotation(주석달기) 이다. Python3 이상에서 사용 할 수 있다. 강제성은 없으며, 단지 주석 달기일 뿐이다. 코드 자체에 미치는 영향 또한 없다. 함수의 매개변수와 반환값을 나타내는 역할을 한다.
#1. map 함수 # - 사용이유 # - 기존 리스트를 수정해서 새로운 리스트를 만들때 사용 # - 사용방법 # map(함수, 순서가 있는 자료형) print(list(map(int, ['1', '2', '3']))) items = [' logitecmouse', 'epsol keyboard '] # def strip_all(x): # return x.strip() # items = list(map(strip_all, items)) items = list(map(lambda x: x.strip(), items)) print(items) #filter 함수 # - 사용이유 # - 기존 리스트에 조건을 만족하는 요소만 추출 # - 사용방법 # filter(함수, 순서가 있는 자료형) def func(x):..
#기존 함수 def minus_one(a): return a-1 #람다 함수 lambda a:a-1 print((lambda a:a-1)(10)) #람다 함수 호출방법, 변수에 담아서 호출 minus_one_2 = lambda a:a-1 print(minus_one_2(10)) #람다 함수 if/else 구문 사용 print((lambda a: True if a>0 else False)(-1)) #람다 함수 if/else 구문 사용, 변수에 담아서 호출 lamb2 = lambda a: True if a>0 else False print(lamb2(-3)) 기존 함수 호출방법
#위치 가변 매개변수 def print_fruit(*args): print(args) print_fruit('melon', 'cherry', 'apple') #키워드 가변 매개변수 def print_name(**kwargs): for key, value in kwargs.items(): print(f'{key}:{value}') print_name(name='John', content='cena')
#리스트 복사 방식 a = [1,2,3,4,5] b = a.copy() print(id(a)) print(id(b)) #다차원 리스트 복사 x = [[1,2], [3,4,5]] import copy #deep copy y = copy.deepcopy(x) print(id(x)) print(id(y))
1. 에러를 강제로 발생시킬 때 사용 2. 형태 raise 예외("에러 메세지") 3. 에러 만들기 class 예외(Exception): def __init__(self): super().__init__("에러 메세지") 4. 예제 class PositiveError(Exception): def __init__(self): super().__init__("양수 입력 불가!") try: val = int(input('input negative number: ')) if val >= 0: raise PositiveError except PositiveError as e: print("에러 발생", e)