Programming Language

Programming Language/Python

[Python] range()를 사용해야하는 이유

range() - range()는 iterable(이터러블)을 생성한다. - 데이터를 메모리에 보관하지 않는다. - range()의 데이터 크기는 메모리와 관계 없다. - range()는 항상 메모리가 작다. - range()는 항상 동일한 크기의 메모리를 사용한다. - range()는 반복문에서 리스트보다 빠르다. 따라서, 숫자와 관련된 반복문을 사용시에는 리스트보다 range()를 쓰자. - (Good) for i in range(5): - (Bad) for j in [0, 1, 2, 3, 4]: range()의 데이터 크기가 달라도 변수가 차지하는 메모리가 같을까? - 같다. %reset -f import sys test_range_1 = range(0, 10) test_range_2 ..

Programming Language/Python

[Python] sqlite3 이용하여 데이터 여러줄 한번에 입력하는 방법

import sqlite3 con = sqlite3.connect(':memory:') cur = con.cursor() cur.execute("CREATE TABLE my_test_table(id int, name varchar, phone_number varchar);") data = [ (11, "Park", "010-1111-2222"), (12, "Kim", "010-1234-5678"), (13, "Oh", "010-8765-4321s"), ] cur.executemany("INSERT INTO my_test_table VALUES (?, ?, ?);", data) result = cur.execute("SELECT * FROM my_test_table;") for row in result: ..

Programming Language/Python

[Python] sqlite3 이용하여 데이터 입력 및 출력하는 방법

import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("CREATE TABLE my_test_table (id int, name varchar, phone_number varchar);") cur.execute("INSERT INTO my_test_table VALUES (1, 'Park', '010-0000-0000');") cur.execute("INSERT INTO my_test_table VALUES (2, 'Kim', '010-1234-5678');") result = cur.execute("SELECT * FROM my_test_table;") for row in result: print(row) cur...

Programming Language/Python

[Python] dictionary를 json으로 변환하는 방법

import json my_dict = {"a": "b", "c": {"d": "e", "f": "g"}} print(type(my_dict), my_dict) my_dict = json.dumps(my_dict) print(type(my_dict), my_dict) my_dict = json.loads(my_dict) print(type(my_dict), my_dict)

Programming Language/Python

[Python] 리스트의 중복값을 제거하는 방법

my_list = [0, 0, 1, 1, 2, 2] print(my_list) my_list = set(my_list) print(my_list) my_list = list(my_list) print(my_list)

Programming Language/Python

[Python] FastAPI 리스트로 입력받는 방법

from enum import Enum from fastapi import FastAPI app = FastAPI() class ModelName(str, Enum): icecream = "아이스크림" snack = "과자" chocolate = "초콜렛" @app.get( path = "/item/price", summary = "Summary", description = "Description", tags = ["Tags"], ) async def read_price(item_name: ModelName): if item_name is ModelName.icecream: price = 3000 elif item_name is ModelName.snack: price = 5000 elif item_na..

Programming Language/Python

[Python] FastAPI tags 나누는 방법

from fastapi import FastAPI app = FastAPI() @app.get( path="/items", summary="아이템 Summary 입니다.", description="아이템 Description 입니다.", tags=['First'] ) async def read_item(item_id: int, item_name: str): return {"item_id": item_id, "item_name": item_name} from fastapi import FastAPI app = FastAPI() @app.get( path="/items", summary="아이템 Summary 입니다.", description="아이템 Description 입니다.", tags=['First..

Programming Language/Python

[Python] FastAPI description, summary 추가하기

from fastapi import FastAPI app = FastAPI() @app.get( path="/items", summary="아이템 Summary 입니다.", description="아이템 Description 입니다.", ) def read_item(item_id: int, item_name: str): return {"item_id": item_id, "item_name": item_name} from fastapi import FastAPI app = FastAPI() @app.get( path="/items", ) def read_item(item_id: int, item_name: str): return {"item_id": item_id, "item_name": item_name}

Programming Language/Python

[Python] FastAPI 데이터 타입 강제하는 방법

from fastapi import FastAPI app = FastAPI() @app.get("/items") def read_item(item_id, item_name): return {"item_id": item_id, "item_name": item_name} from fastapi import FastAPI app = FastAPI() @app.get("/items") def read_item(item_id: int, item_name: str): return {"item_id": item_id, "item_name": item_name}

Programming Language/Python

[Python] FastAPI Return 테스트 해보기

from fastapi import FastAPI app = FastAPI() @app.get("/root") def read_root(): return {"Hello": "World"} from fastapi import FastAPI app = FastAPI() @app.get("/root") def read_root(): return {"Hello": "World", "Read": "Root", "My": "Name"}

박경태
'Programming Language' 카테고리의 글 목록 (3 Page)