decorator def decor(func): def wrap(): print("============") func() print("============") return wrap @decor def print_text(): print("Hello world!") print_text(); #============ #Hello world! #============ 어떤 function을 미리 정의된 function으로 wrap할 수 있다. 길/Python 2021.02.12
Generators def numbers(x): for i in range(x): if i % 2 == 0: yield i print(list(numbers(11))) 위 코드는 0 ~ 10 사이의 짝수를 리스트로 만들어 프린트하게 된다. Generator의 이점은 메모리를 적게 차지하는 것이라고 한다. 길/Python 2021.02.12
List Functions nums = [55, 44, 33, 22, 11] if all([i > 5 for i in nums]): print("All larger than 5") if any([i % 2 == 0 for i in nums]): print("At least one is even") for v in enumerate(nums): print(v) 길/Python 2021.02.11
String Functions print(", ".join(["spam", "eggs", "ham"])) #prints "spam, eggs, ham" print("Hello ME".replace("ME", "world")) #prints "Hello world" print("This is a sentence.".startswith("This")) # prints "True" print("This is a sentence.".endswith("sentence.")) # prints "True" print("This is a sentence.".upper()) # prints "THIS IS A SENTENCE." print("AN ALL CAPS SENTENCE".lower()) #prints "an all caps sentence".. 길/Python 2021.02.11
dic.get(key [,default]) 나중에 유용하게 써먹을 수 있을 것 같아서 적는다. error부분에 만약 dictionary안에 키에 해당하는 것이 없을 때, default 인자에 들어온 것을 return함. 길/Python 2021.02.11
try / except / finally / assert try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what") try는 오류가 생길 것으로 예상되는 코드를 집어넣고 except는 except 오른쪽에 정의된 오류가 발생했을 시에 실행된다. 혹은 except의 값을 정의하지 않으면 모든 에러가 발생했을 때, except의 코드를 실행한다. finally는 try 하단의 코드, 혹은 에러가 있을 시에는 except까지 모두 시행한 이후에, 그 내부의 코드를 실행한다. try: print(1 / 0) except ZeroDivisionError: raise ValueError 해.. 길/Python 2021.02.11
List Functions max(list): Returns the list item with the maximum value min(list): Returns the list item with minimum value list.count(item): Returns a count of how many times an item occurs in a list list.remove(item): Removes an object from a list list.reverse(): Reverses items in a list. 길/Python 2021.02.10
not sth in list / sth not in list nums = [1, 2, 3] print(not 4 in nums) #True print(4 not in nums) #True print(not 3 in nums) #False print(3 not in nums) #False 길/Python 2021.02.10
True or False and True print(False and True or False) 이런 것은 기본적으로 순서대로 시행되는 것으로 보임. 길/Python 2021.02.10