REPL or Interactive Mode
Python을 가장 쉽게 (또는 naive하게) 사용하는 방법은 Python Shell을 통해 사용하는 것임.
이 경우, Python Interpreter는
- Read: 사용자가 Python Shell의 prompt에 입력한 statements를 읽고,
- Evaluate: 이를 평가하여 실행을 시키고
- Print: 해당 결과를 즉시 사용자에게 출력하고 나서
- Loop: 다시 prompt를 통해 사용자의 입력을 대기함으로서 위 과정을 반복함.
이같은 방식을 REPL (Read-Eval-Print Loop)이라는 약어로 부르거나 interactive mode라고 부른다.
Python code를 statement 단위로 입력받아 수행해서 결과를 출력해주는 프로그램을
Python Interactive Shell 이라고 부름: 한번에 하나의 statement가 수행되는 방식으로 동작.
참고 : 다음 URL에서 statement와 expression의 비교를 통해 개념을 확실히 할 것
https://dsaint31.tistory.com/entry/Python-Expression-vs-Statement
[Python] Expression vs. Statement
Expression (표현식)프로그래밍 또는 컴퓨터 과학 분야에서 Expression은 흔히, function call, identifier, number, operator, literal 등으로 이루어진다.표현식(or 수식) 으로 번역.하나의 value로 reduce 될 수 있는 code
dsaint31.tistory.com
Windows 의 command prompt 나 Linux shell prompt처럼 사용되기 때문에
Interactive Prompt 라고도 불린다.
Python Shell을 실행
Python Shell을 실행하려면, terminal에서 다음의 커맨드로 Python shell을 수행한다.
$ python
$는 terminal에서의 prompt임.python을 입력하고 엔터를 누르면 실행됨.
다음이 실행 결과임.

>>>(three arrows)가 바로 Python shell에서의 (primary) prompt로서- 사용자의
statement입력을 대기하고 있음을 의미한다. - 위와 사진처럼 prompt가 보이면, Python Interactive Session이 시작되었다고 할 수 있다. 해당 shell을 종료하기 까지 객체 및 입력한 statement 등을 Interactive session이 저장 관리한다. 해당 session은 해당 Python Interactive Shell이 종료될 때 끝남.
- 이를 종료하기 위해서는
- exit()를 입력하거나
- Windows에선 CTRL+z (=EOF)이후 Enter,
- Linux에선 CTRL+d (=EOF)를 눌러준다 (WSL인 경우는 Linux에 해당함)
[Term] Prompt 란? : 프롬프트 의 정의.
사용자의 입력을 기다리는 시그널 또는 지시자 Prompt는 (주로 CLI에서) 사용자에게 특정 작업을 수행하도록 지시하는 역할을 수행하는 signal을 가르킴. 명령줄 인터페이스(CLI) 환경에서 Prompt는 사
ds31x.tistory.com
Linux 사용자의 경우, python이 Python 2.x인 경우도 있다. 이 경우 대부분 python3가 Python 3.x를 수행하는 명령어로 되어있다. 가급적 linux system의 기본 python 보다는 conda등을 사용하여 python 관련 환경을 구축하여 사용하길 권한다.
참고로 Windows의 경우,
Python을 설치할 때 IDLE (Integrated Development and Learning Environment)가 같이 설치된다.
(참고로 IDLE는 Python3.5 이상이면 python -m idlelib.idle 로 실행가능함.)
IDLE는 GUI Tkinter 툴킷을 사용한 일종의 IDE 인데...
Learning이 추가로 들어가 있는 이름에서 알 수 있듯이
정말 초보자들이 학습에서나 사용할 수준으로
실제 개발에 쓰기에는 많이 아쉽다 (하라고 하면 하겠지만...).
Linux 사용자라면 vim을 사용하여 개발하는 것보다 못한데 라는 느끼기 쉽다.
개인적으로는 vscode + conda 조합을 권한다.
shell은
- 사용자와 OS (정확히는 kernel) 사이에 위치하면서
- 사용자가 OS를 사용하도록 돕는 프로그램을 가르킨다.
python shell은
- python interpreter(or python virtual machine)와 사용자 사이에 위치하면서
- 사용자가 입력한 statement를 전달하고, interpreter가 수행한 결과를 사용자에게 출력해주는 역할을 하기 때문에
- 마치 OS와 사용자 사이의 shell과 유사하다.
- https://dsaint31.me/mkdocs_site/OS/console_terminal_shell_kernel/#shell
BME
Console, Terminal, and Shell Console: 컴퓨터(주로 서버)에 command를 작성하여 입력하고 그 결과를 확인할 수 있는 (물리적) 입출력 장치 . Terminal과 비슷한 의미를 가지고 있으나 보다 물리적인 측면이 강
dsaint31.me
python shell은 일종의 계산기나 간단한 디버깅 또는 test 용도로 쓸 때 사용하는 수준으로,
30여 line이 넘어가는 code를 사용해야한다면, 대부분.py파일로 저장하여 수행하는 게 훨씬 편하다..py파일을 script file 또는 source code file이라고 부른다.
https://dsaint31.tistory.com/504
[Python] Python Script File 실행해보기
1. Python Script File (=main script file)의 구조다음은 일반적인 Python script file의 전형적인 구조를 간략히 나타냄. (고정된 것은 아님.)이미 구현된 기능 등을 사용하기 위해 외부 library 와 module을 import함.
dsaint31.tistory.com
실제 데이터 처리나 개발 등에선,
기본으로 제공되는 python shell 보다는 IPython Shell과 같이
보다 향상된 기능을 가지고 있는 shell (block 단위 실행)등이 사용된다.
ML이나 데이터 사이언스 등에서는 IPython shell의 웹버전이라고 할 수 있는 Jupyter Notebook 을 사용하는 경우가 많다. 또는 vscode등과 같은 IDE Tool을 이용한다.
[Py] IPython, Jupyter Notebook, and Colab
이 문서는 IPython, Jupyter NoteBook, Google Colab을 간단히 소개함.IPython: IPython은 파이썬의 대화형 인터프리터로, 파이썬 표준 인터프리터보다 향상된 기능을 제공함. 다음과 같은 기능을 제공:Code auto-co
ds31x.tistory.com
간단한 문법을 공부하는 경우등은 python shell로도 충분하다.
하지만, 문법만 배우는 것으론 부족하기 때문에
vscode나 pycharm등의 IDE를 꼭 설치하고 사용법을 같이 익혀나가길 권한다.
주의사항
- Python shell (or Interactive Prompt)에서는 Python command만 수행가능함.
(OS에서 지원하는 command 도 수행시키려면, IPython shell을 사용하거나 os.system을 사용한다.) - multiline statements를 사용하는 경우를 제외하곤 공백문자로 statement를 시작시켜선 안됨.
- compound statements (for 문, if 문 등)의 종료는 blank line으로 처리함.
- Python shell에선 expression을 입력시 evaluation을 통한 값이 그대로 출력(REPL)되기 때문에 굳이 print문을 사용할 필요가 없음.
참고해볼만한 URLs
[Py] IPython, Jupyter Notebook, and Colab
이 문서는 IPython, Jupyter NoteBook, Google Colab을 간단히 소개함.IPython: IPython은 파이썬의 대화형 인터프리터로, 파이썬 표준 인터프리터보다 향상된 기능을 제공함. 다음과 같은 기능을 제공:Code auto-co
ds31x.tistory.com
[Python] IPython shell 에서 shell cmds 사용하기.
IPython 에서 지원하는 shell commandsPython interactive shell에서 OS등이 지원하는 shell commands를 사용하지 못하는 것과 달리, IPython shell에서는 많이 사용되는 shell commands는 다음과 같이 지원함.In [17]: pwdOut[1
ds31x.tistory.com
IPython 사용을 위한 Jupyter notebook (over colab) : https://dsaint31.me/mkdocs_site/CE/colab/hw_spec/
BME
colab ipynb ipython jupyter notebook Colab이란 Google Colaboratory = Google Drive + Jupyter Notebook 구글 계정 전용의 가상 머신 지원 – GPU 및 TPU를 제공 Google drive 문서와 같이 링크만으로 접근 가능: 공동작업 가능 J
dsaint31.me
Jupyter and the future of IPython — IPython
IPython provides a rich architecture for interactive computing with: A powerful interactive shell. A kernel for Jupyter. Support for interactive data visualization and use of GUI toolkits. Flexible, embeddable interpreters to load into your own projects. E
ipython.org
https://bpython-interpreter.org/
Home - bpython
Home bpython is a fancy interface to the Python interpreter for Linux, BSD, OS X and Windows (with some work). bpython is released under the MIT License. It has the following (special) features: In-line syntax highlighting Readline-like autocomplete with s
bpython-interpreter.org
[Python] Terminal, WSL, Conda, and VSCode
Terminal and Console (+ Shell)https://dsaint31.me/mkdocs_site/OS/console_terminal_shell_kernel/ BME228Console, Terminal, and Shell Console: 컴퓨터(주로 서버)에 command를 작성하여 입력하고 그 결과를 확인할 수 있는 (물리적) 입
ds31x.tistory.com
'Programming' 카테고리의 다른 글
| [Python] Function Call (함수호출) (0) | 2023.06.10 |
|---|---|
| [Python] Function Definition, Call and Arguments (0) | 2023.06.10 |
| [Python] Python Script File 실행해보기 (0) | 2023.06.09 |
| [Programming] Application Programming Interface (API) (0) | 2023.06.08 |
| [Python] range and enumerate (0) | 2023.06.07 |