[Python] Python Interactive Shell (or Python Interactive Prompt)

2023. 6. 9. 23:27·Programming
728x90
728x90

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 될 수 있는 c

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에 해당함)

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
 

BME228

Console, Terminal, and Shell Console: A physical device that typically comprises a monitor, keyboard, and mouse. It is used to interact with a computer system and can be found in server rooms or data centers where computers are managed remotely. The consol

dsaint31.me

 

python shell은 일종의 계산기나 간단한 디버깅 또는 test 용도로 쓸 때 사용하는 수준으로,
30여 line이 넘어가는 code를 사용해야한다면, 대부분 .py파일로 저장하여 수행하는 게 훨씬 편하다.
.py파일을 script 파일 또는 source code file이라고 부른다.

 

실제 데이터 처리나 개발 등에선,
기본으로 제공되는 python shell 보다는 IPython Shell과 같이
보다 향상된 기능을 가지고 있는 shell (block 단위 실행)등이 사용된다.

 

ML이나 데이터 사이언스 등에서는 IPython shell의 웹버전이라고 할 수 있는 Jupyter Notebook 을 사용하는 경우가 많다. 또는 vscode등과 같은 IDE Tool을 이용한다.

간단한 문법을 공부하는 경우등은 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

https://ds31x.tistory.com/101

 

[Python] IPython shell 에서 shell cmds 사용하기.

Python interactive shell에서 OS등이 지원하는 shell commands를 사용하지 못하는 것과 달리, IPython shell에서는 많이 사용되는 shell commands는 다음과 같이 사용가능함. In [17]: pwd Out[17]: '/home/dsaint31' In [18]: ls l

ds31x.tistory.com

https://ipython.org/

 

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

IPython 사용을 위한 Jupyter notebook (over colab) : https://dsaint31.me/mkdocs_site/CE/colab/hw_spec/#jupyter-notebook

 

BME228

Colab이란 Google Colaboratory = Google Drive + Jupyter Notebook 구글 계정 전용의 가상 머신 지원 – GPU 및 TPU를 제공 Google drive 문서와 같이 링크만으로 접근 가능 :공동작업 가능 Jupyter Notebook 웹 브라우저에

dsaint31.me


 

'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
'Programming' 카테고리의 다른 글
  • [Python] Function Call (함수호출)
  • [Python] Function Definition, Call and Arguments
  • [Python] Python Script File 실행해보기
  • [Programming] Application Programming Interface (API)
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (740)
      • Private Life (13)
      • Programming (56)
        • DIP (104)
        • ML (26)
      • Computer (119)
        • CE (53)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (351)
        • Signals and Systems (103)
        • Math (172)
        • Linear Algebra (33)
        • Physics (42)
        • 인성세미나 (1)
      • 정리필요. (54)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (1)
        • PET Study 2009 (1)
        • 방사선 장해방호 (4)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    signals_and_systems
    SS
    function
    SIGNAL
    numpy
    signal_and_system
    Programming
    random
    opencv
    Term
    math
    fourier transform
    linear algebra
    검사
    인허가제도
    Probability
    Convolution
    Python
    Vector
    Optimization
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[Python] Python Interactive Shell (or Python Interactive Prompt)
상단으로

티스토리툴바