study with Q - 파이썬
-
55 - (파이썬) 메모리 구조: global 키워드study with Q - 파이썬 2024. 6. 15. 12:25
# 전역 스택 (global stack): 파이썬은 함수를 호출할 때 함수 내부 코드를 실행하기 위해 함수 내부의 변수를 저장할 스택을 추가로 만든다. 그 중에서도 가장 외각에 있는 스택을 전역 스택(global stack)이라고 한다. ...그래서# 전역 위치에서 x, y라는 변수를 생성x = 0y = [1,1,2,3,5,8,13]def function(): # 함수 내부에서 x, y라는 변수를 생성 x = 100 y = [90, 80, 70, 60, 50] print(x) #100 print(y) #[90, 80, 70, 60, 50]function()print(x) print(y) >>>100[90, 80, 70, 60, 50]0[1, 1, 2, 3, 5, 8, 13] 그런데 만약 함수 ..
-
54 - (파이썬) 메모리 구조 : 스택과 힙, 함수와 결합할 때study with Q - 파이썬 2024. 6. 15. 11:05
# 메모리 관리에서의 스택과 힙 현대적인 프로그래밍 언어에서는 자료를 크게 2가지로 구분1) 기본 자료형 (마치 내 월급처럼) 작고 간단한 것 숫자, 문자열, 불 등 (문자열은 크기가 매우 커질 수 있지만 사용적인 관점에서 기본적이라서 기본 자료형에 포함시키는 편이다.)크기가 작고 고정되어 있으므로 → (통장에) 상자에 넣어서 옆에 차곡차곡 보관해욥이렇게 기본 자료형들이 정리되어 있는 공간을 스택(stack)이라고 한다.2) 복합 자료형(우리 레오처럼) 크고 무거운 것 리스트, 딕셔너리, 객체 등 (아무래도 고양이는 액체니까) 크기가 정해져있지 않고 → 다른 창고에 보관을 하고, 이 위피를 상자에 넣어서 차곡차곡 보관해욥이렇게 객체 자료형이 저장되어 있는 거대한 창고를 힙(heap)이라고 한다. 그리..
-
53 - (파이썬) 함수 리턴과 5-1절 확인 문제study with Q - 파이썬 2024. 6. 10. 22:11
def f(x) : #값을 들고 돌아가슈 return x+1print(f(10))print(f(20))>>>1121 def f(x) : #return 뒤에 아무 값도 넣지않으면 return # None을 들고 돌아가게 됨print(f(10))print(f(20))>>>NoneNone #함수 사용해보기: 요론 모양def 함수(매개변수): 변수 = 초기화 #처리 #처리 처리 #함수 처리 return 변수 start부터 end까지 더하는 함수 def sum_all(start, end): output = 0 for i in range(start, end+1): output += i return outputprint(sum_all(1, 10))print(sum_all(1, 100)..
-
52 - (파이썬) 기본 매개변수study with Q - 파이썬 2024. 6. 10. 20:54
#기본 매개변수def test (v = 10): print(v)test()test(30)test(v = "str")>>>1030str #키워드 매개변수 / 기본 매개변수: 기본 매개변수가 2개 이상일 때를 생각해보면 def test (v = 10, i = 15): print(v,i)test()test(10, 50)>>> 10 1510 50...이럴거면 변수 v를 지정해둔 의미가 없잖여그래서 변수를 바꾸고자 하는 키워드만 바꿔서 입력 def test (v = 10, i = 15): print(v,i)test()test(i = 50)>>>10 1510 50 ※ 기본 매개변수는 마지막에 입력해야 한다.: 기본 매개변수를 먼저 입력해야 하는 상황이 오기 때문에def test (v = 10, i): pr..
-
51 - (파이썬) 가변 매개변수 함수study with Q - 파이썬 2024. 6. 3. 20:57
어제 봤지만 또 새로운 개념...# parameterdef prt(str, times): for i in range(times) : print(str)# argumentprt("독도는 한국 땅",5)>>>독도는 한국 땅독도는 한국 땅독도는 한국 땅독도는 한국 땅독도는 한국 땅→ 매개변수를 함수 정의 때 넣은 변수를 n개 만들었다면 함수 호출 때에도 n개의 매개변수를 전달해야 함# 가변 매개변수 함수: 매개변수가 가변적으로 변하는 함수(가변 매개변수는 함수를 정의할 때, 매개변수의 개수를 고정하지 않고, 필요에 따라 유동적으로 매개변수를 추가할 수 있게 만들어주는 기능입니다. 이 기능을 사용하면 함수 호출 시 매개변수의 개수를 다양하게 지정할 수 있어서, 함수의 재사용성을 높이고 코드의 유연성을 증..
-
50 - 함수, 매개변수 기본study with Q - 파이썬 2024. 6. 2. 18:15
# 외워...! ~기본적인 함수~def p_3_ts(): print("hey") print("hey") print("hey")# 그리고 함수를 호출(call)하면p_3_ts()>>>heyheyhey # 파이썬에서 매개변수 : 함수의 괄호 안에 넣는 변수 def f_str(str): print(str) print(str) print(str)f_str("seven")>>>sevensevenseven # parameter(매개변수) : 함수 정의 때 넣은 변수 (위에서 str)# argument(인수) : 함수 호출 때 넣은 값 (위에서 seven) # 문자열과 횟수도 넣을 수 있어요# parameterdef prt(str, times): for i in range(times) : print..
-
49 - 프로그램, 루틴, 프로시저, 메서드, 함수study with Q - 파이썬 2024. 6. 2. 17:58
# 프로그램(program): 미리 작성한 것 코드 전체에서 - 프로그램(program), 루틴(routine)디른 의미로 변경 - 절차(procedure), 방법(method) 예시) 케이크를 먹는 프로그램 한 입 냠)print("400g의 케이크가 있다.")# 케이크를 먹는 프로그램, 루틴cake = { "name":"dankge", "left": 400}# 케이크를 먹는 서브프로그램, 서브루틴print("케이크를 80g 자른다")print("냠")cake["left"] -= 80print(f"남은 케이크의 무게는 {cake['left']}이다.")>>>400g의 케이크가 있다.케이크를 80g 자른다냠남은 케이크의 무게는 320g이다. #서브 프로그램, 서브 루틴 전용 문법, def print(..
-