개발/Python
-
[Python] - Function , Lambda개발/Python 2022. 7. 27. 14:37
1. Function A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. A function Object is mutable(User classes are considered mutable) - https://stackoverflow.com/questions/12076445/are-user-defined-classes-mutable # create function def test(): return "test" # call function test() 1-1) *args ..
-
[Python] - Powerful Function개발/Python 2022. 7. 27. 14:12
1. Range range는 range(start, end, step) Option range's result start_number ~ end-1 collection list(range(5,10)) > [5, 6, 7, 8, 9] list(range(10,20,2)) > [10, 12, 14, 16, 18] 2. Enumerate Use repeat Loop we need to check times Loop return tuple( index , element ) for i, v in enumerate(t): print("index : {}, value: {}".format(i,v)) """ index : 0, value: 1 index : 1, value: 5 index : 2, value: 7 in..
-
[Python] - Set개발/Python 2022. 7. 27. 13:05
Set set은 수학에서 이야기하는 집합과 비슷합니다. Not order, unique value mutable Object 1. Syntax s = set() s = set([1,3,5,7]) similar dictionary, but no key . Only value value is not immutable s = {"1", 3, 5, (1,3)} # Error s = {"1", 3, 5, [1,3]} s = {"1", 3, 5, {1,3}} s = {"1", 3, 5, frozenset([1,3,4])} 2. Change Element k = {100, 105} # insert value k.add(50) # multiple value insert k.update([3, 4, 5]) # delet..
-
[Python] - Dictionary개발/Python 2022. 7. 27. 12:17
Dictionary type is immutable key and mutable value mapping not sorted set No index , so access the key Syntax # Default type {"a" : 1, "b":2} e = {} f = dict() newdict = dict( alice = 5, bob = 20, tony= 15, suzy = 30) Key is not allowed mutable key like list , set , dict # Error case a = { {1, 3}: 5, {3,5}: 3} #set a = {[1,3]: 5, [3,5]: 3} #list a = { {"a":1}: 5, "abc": 3} #dict # Key immuttable..
-
[Python] - Tuple개발/Python 2022. 7. 27. 11:59
Tuple immutable Object (순서가 존재) List와 유사하지만 한번 생성되면 값 변경 X 1. 기본 연산 #list와 마찬가지로 다양한 타입이 함께 포함될 수 있습니다. t = (1, "korea", 3.5, 1) # 순서가 있기때문에 인덱스로 접근 가능 t[0] > 1 # '+' 연산으로 tuple(튜플)을 추가 t = t + (3 ,5) > (1, 'korea', 3.5, 1, 3, 5) # '*' 연산으로 tuple(튜플)을 반복 t * 2 > (1, 'korea', 3.5, 1, 3, 5, 1, 'korea', 3.5, 1, 3, 5) 2. 함수 연산 #함수에서 여러 값을 한꺼번에 리턴 def minmax(items): return min(items), max(items) minm..
-
[Python] - List개발/Python 2022. 7. 27. 01:28
List 원소들이 연속적으로 저장되는 형태의 자료형입니다. mutable -> reallocate 할 필요 X 1. 원소 추가 # List 끝에 element 추가 / O(1) list.append(x) # List 끝에 iterable 추가 / O(len(iterable)) list.extend(iterable) # 주어진 i 위치에 항목에 삽입 / O(N) list.insert(i, x) 2. 원소 제거 # delete x's first element / 없다면, ValueError # O(N) list.remove(x) # delete index element / return Value # pop() = O(1) pop(i) = O(N) list.pop([i]) # delete List all el..
-
삼항 연산자(Ternary operator)개발/Python 2022. 7. 18. 22:48
1. 3항 연산자 if-else와 같지만 한 줄로 표현 2. 3항 연산자 Syntax 기존의 if else 문을 통하면 아래와 같습니다. a, b = 100, 200 if a > b: max_num = a else: max_num = b Ternary operator를 사용하여 구현하면 조금 더 직관적으로 구현이 됩니다. # [true] if [expression] else [false] a, b = 100, 200 max_num = a if a > b else b Reference https://www.geeksforgeeks.org/data-types/ Ternary Operator in Python - GeeksforGeeks A Computer Science portal for geeks. It ..