본문 바로가기

반응형

Python

(18)
[Python] 함수 (function) / 람다(lambda) 함수 선언 및 정의 : def func1(parameter1, parameter2): //To-Do return value return 값이 없을 때, None return (Python에선 None 이 null 과 같음) def func1(a, b): a+b print(func1(5,7)) -->None - *args 매개변수를 n개 받을 때 사용 입력한 값들이 튜플로 변환되어 함수에서 사용됨 def func1(param, *args): for i in args: print(param) print(i) func1('hi',1,2,3) # 'hi' 는 param, (1,2,3) 은 *args 로 튜플로 인식 ----> hi 1 hi 2 hi 3 - 키워드 파라미터 (**kwargs) n개의 key - v..
[Python] 반복문 loop (while, for, break, continue) while : 해당 조건 내에 반복 while condition: //To-Do index를 하나씩 늘려 List 내 값을 사용하며 반복 for value in List: //To-Do List 자료형이 아닌 n 1 ELSE 2 3 a == 1 일 때, ELSE 출력 a == 2 일 때, continue 에 의해 ELSE 출력 없이 다음 iteration 진행 a == 3 일 때, break 에 의해 while 조건이 a
[Python] 조건문 (if / elif / else) Python의 경우 조건문은 if / elif / else 구조로 되어있다. if condition1: print(1) elif condition2: print(2) else: print(3)
[Python] 딕셔너리(Dictionary) 자료형 Key - Value 구조의 자료형 dic[key] = value 모양으로 key-value 자료를 dictionary 에 저장 dic[key] or dic.get(key) 로 value 사용 # {} --> dictionary 의미 dic = {} #dic[key] = value dic[1] = 43 dic['name'] = 'python' dic[key] dic.get(key)
[Python] 튜플(Tuple) 자료형 List 와 비슷하나, Tuple 내 값을 추가, 변경, 삭제 불가 a = (1, 'h', [1, 2]) # 원소가 1개 있으면 끝에 , 필수 # ,를 붙이지 않으면 int 1로 인식 b = (1,) # () 생략 가능 c = 1, 2, 3 a[2] = 'h' # 에러 발생 --> tuple 내 값 수정 불가 a[2][1] = '2' # 사용 가능 --> a[2] 에 저장된 list의 주소값은 그대로 a = a + (1,) --> a == (1, 'h', [1, 2], 1)
[Python] 리스트(List) 자료형 다양한 data type을 저장할 수 있는 자료형 a = ['a', 1, 'hi', [2, 5, 'python']] - 인덱싱 (indexing) List 내 특정 문자를 가리키는 것 arr[n] 은 n번째 문자 n 이 음수일 경우, 뒤에서 부터 indexing - 슬라이싱 (Slicing) List내 특정 문자열을 가리키는 것 arr[n : m] 은 n [1, 'hi', [1, 2], 2, [10, 20], 'hello'] [1, 'hi', [1, 2], 1, 'hi', [1, 2], 1, 'hi', [1, 2]] - append(a) List내 특정 문자열을 가리키는 것
[Python] 문자열 (String) ' ' 혹은 " " 둘 다 사용 가능 String 내에 작은따옴표 혹은 큰따옴표가 있을 때, 서로 다른 따옴표로 String 감싸야 함. print('hello "MY" python') print("hello 'MY' python") 줄바꿈을 포함한 문자열 (\n) multiline의 경우, ''' ''' 혹은 """ """ 으로 사용 가능 print('''hello "MY" python''') ---> hello "MY" python - 연산자 + 두 String Concatentation String + String 만 가능 - 연산자 * String n번 Concatentation String * int 만 가능 print('hi' * 3) --->hihihi - 인덱싱 (indexing) Stri..
[Python] 연산자 - ** 연산자 : x ** y = x의 y제곱 print(3 ** 4) --> 81 - // 연산자 : 나눗셈 몫을 반환 print(7 // 4) --> 1 - 논리 연산자 # and 연산자 a == 100 and b == 200 # or 연산자 a == 100 or b == 200 # not 연산자 not (a == 100 and b == 100)

반응형