✨ChatGPT Prompt:
- 파이썬을 복습하기 위해 파이썬의 핵심 개념들을 모두 포함한 코드를 작성해줘.
- 각 코드별로 어떤 개념이 사용되었는지 주석을 한글로 달아줘.
- 전체 코드는 500줄 이하로 작성해줘.
x = 10
name = "Alice"
pi = 3.14
is_happy = True
my_list = [1, 2, 3, 4, 5]
my_tuple = (10, 20, 30)
my_list.append(6)
my_list.remove(3)
my_dict = {"name": "Bob", "age": 25}
my_set = {1, 2, 3, 4}
my_dict["city"] = "Seoul"
my_dict["age"] = 26
my_set.add(5)
my_set.discard(3)
if x > 5:
print("x는 5보다 큽니다.")
elif x == 5:
print("x는 5입니다.")
else:
print("x는 5보다 작습니다.")
for num in my_list:
print(num)
count = 0
while count < 3:
print("Count:", count)
count += 1
def greet(name):
"""이름을 받아서 환영 메시지를 출력"""
return f"Hello, {name}!"
message = greet("Charlie")
print(message)
class Person:
def __init__(self, name, age):
"""생성자 함수"""
self.name = name
self.age = age
def introduce(self):
"""자기소개 메서드"""
return f"제 이름은 {self.name}이고, 나이는 {self.age}살입니다."
person1 = Person("David", 30)
print(person1.introduce())
with open("example.txt", "w") as file:
file.write("Hello, Python!\n")
with open("example.txt", "r") as file:
content = file.read()
print(content)
try:
result = 10 / 0
except ZeroDivisionError as e:
print("0으로 나눌 수 없습니다.", e)
finally:
print("예외 처리 완료.")
import math
print(math.sqrt(16))
square = lambda x: x**2
print(square(4))
nums = [1, 2, 3, 4]
squared_nums = list(map(lambda x: x**2, nums))
print(squared_nums)
even_nums = list(filter(lambda x: x % 2 == 0, nums))
print(even_nums)
squares = [x**2 for x in range(1, 6)]
print(squares)
def my_decorator(func):
def wrapper():
print("함수 실행 전 작업")
func()
print("함수 실행 후 작업")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
def my_generator():
yield 1
yield 2
yield 3
for value in my_generator():
print(value)
import itertools
perms = itertools.permutations([1, 2, 3])
print(list(perms))
combs = itertools.combinations([1, 2, 3], 2)
print(list(combs))
import numpy as np
array = np.array([[1, 2], [3, 4]])
print(array.sum())
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
point = Point(1, 2)
print(point)
from typing import List, Tuple
def add_numbers(numbers: List[int]) -> int:
return sum(numbers)
print(add_numbers([1, 2, 3, 4]))
import asyncio
async def say_after(delay, message):
await asyncio.sleep(delay)
print(message)
async def main():
await asyncio.gather(
say_after(1, "Hello"),
say_after(2, "World")
)
asyncio.run(main())