본문 바로가기
728x90
반응형
SMALL

개념/혼자 공부하는 파이썬30

python) datetime package import datetime now = datetime.datetime.now() print(now.year, now.month, now.day, now.hour, now.minute, now.second); This code snippet is a straightforward example of how to use the `datetime` module in Python to get the current date and time, and then print specific components of it such as the year, month, day, hour, minute, and second. Let's break it down: 1. **`import datetime`**: This line .. 2024. 11. 5.
python) Explanation of the Flatten Functions def flatten(data): output = [] for item in data: if type(item) == list: output += item else: output.append(item) return output def flatten2(data): output=[] for item in data: if type(item) == list: output +=flatten2(item) else: output.append(item) return output example = [[1,2,3],[4,[5,6]],[7,[8,9]]] print("origin : ",example) print("flattened : ",flatten(example)) print("flattened2 : ",flatten2.. 2024. 11. 4.
python) Memoization dictionary = {1 : 1, 2 : 1} def fibonacci(n): if n in dictionary: return dictionary[n] else: output = fibonacci(n-1) + fibonacci(n-2) dictionary[n] = output return output number = int(input()) print("fibonacci({}) : {} ".format(number , fibonacci(number))) ### Code Breakdown - **`dictionary = {1: 1, 2: 1}`**: Initializes a dictionary with the first two Fibonacci numbers. The keys are the positio.. 2024. 11. 3.
python) Understanding Custom Exception Handling in Python class Custom(Exception): def __init__(self, message, value): super().__init__() self.message = message self.value = value def __str__(self): return self.message def print(self): print("warning Error") print("message : ", self.message) print("value : ", self.value) try: raise Custom("betzni eiina",273) except Custom as e: e.print() ### Understanding Custom Exception Handling in Python This code s.. 2024. 11. 2.
728x90
반응형
LIST