A Byte of Python - First Steps Python


First Steps



이제 전통적인 "헬로우 월드" 프로그램을 파이썬에서 실행한다. 이는 어떻게 파이썬 프로그램을 작성, 저장 실행하는지 알려준다.

프로그램을 실행하기 위해 파이썬을 사용하는 두가지 방법이 있다. 인터렉티브 인터프리터 프롬프트나 소스코드 파일을 사용하는 것이다. 어떻게 이들 양쪽 메소드를 사용하는지 보여준다.

인터프리터 프롬프트 사용하기

운영체제에서 터미널을 열고 python3을 실행한다.
파이썬이 시작되면 >>> 를 볼 수 있다. 이를 파이썬 인터프리터 프롬프트라고 한다.

파이썬 인터프리터 프롬프트에서 다음을 입력한다.

 print("Hello World")

엔터를 입력하면 화면에 Hello World 가 출력된다.

인터프리터 프롬프트에서 나가는 방법

exit() 를 입력하고 엔터를 누른다.

에디터 선택하기

항상 인터프리터 프롬프트에서 프로그램을 타이핑할 순 없다. 파일로 저장해 몇번이고 프로그램을 실행할 수 있어야 할 것이다.

파이썬 소스파일을 작성하려면, 입력하고 저장할 수 있는 에디터 소프트웨어가 필요하다. 

기본적으로 필요한 기능은 문법 하이라이팅이다. 어떤 것부터 시작할지 모르겠으면 PyCharm Educational Edition 을 추천한다. 

PyCharm

파이참 교육용 에디션은 파이썬 프로그램을 작성하는데 사용할 수 있는 무료 에디터이다. 파이참을 열면 다음을 볼 수 있는데 Create New Project를 선택한다
When you open PyCharm

Pure Python 을 선택한다

PyCharm New Project
프로젝트 위치를 untiltled 에서 helloworld 로 바꾼다


PyCharm project details

Create 를 클릭한다.
사이드 바의 helloworld 를 우클릭해 New -> Python File 을 선택한다

New -> Python File">

이름을 물어보는데 hello를 입력한다

PyCharm New File dialog box

이제 연 파일을 볼 수 있다.

PyCharm hello.py file

보여지는 라인을 제거하고 다음을 입력한다

print("Hello World")

오른 클릭하고 헬로우를 실행한다

PyCharm Run 'hello'

You should now see the output (what it prints) of your program:

PyCharm output

Phew! That was quite a few steps to get started, but henceforth, every time we ask you to create a new file, remember to just right-click on helloworld on the left -> New -> Python File and continue the same steps to type and run as shown above.

You can find more information about PyCharm in the PyCharm Quickstart page.

Vim

  1. Install Vim
    • Mac OS X users should install macvim package via HomeBrew
    • Windows users should download the "self-installing executable" from Vim website
    • GNU/Linux users should get Vim from their distribution's software repositories, e.g. Debian and Ubuntu users can install the vim package.
  2. Install jedi-vim plugin for autocompletion.
  3. Install corresponding jedi python package : pip install -U jedi

Emacs

  1. Install Emacs 24+.
  2. Install ELPY




소스파일 사용하기

Now let's get back to programming. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the 'Hello World' program - all it does is just say 'Hello World' when you run it. As Simon Cozens1 says, it is the "traditional incantation to the programming gods to help you learn the language better."

Start your choice of editor, enter the following program and save it as hello.py.

If you are using PyCharm, we have already discussed how to run from a source file.

For other editors, open a new file hello.py and type this:

print("hello world")

Where should you save the file? To any folder for which you know the location of the folder. If you don't understand what that means, create a new folder and use that location to save and run all your Python programs:

  • /tmp/py on Mac OS X
  • /tmp/py on GNU/Linux
  • C:\py on Windows

To create the above folder (for the operating system you are using), use the mkdir command in the terminal, for example, mkdir /tmp/py.

IMPORTANT: Always ensure that you give it the file extension of .py, for example, foo.py.

To run your Python program:

  1. Open a terminal window (see the previous Installation chapter on how to do that)
  2. Change directory to where you saved the file, for example, cd /tmp/py
  3. Run the program by entering the command python hello.py. The output is as shown below.
$ python hello.pyhello world
Screenshot of running program in terminal

If you got the output as shown above, congratulations! - you have successfully run your first Python program. You have successfully crossed the hardest part of learning programming, which is, getting started with your first program!

In case you got an error, please type the above program exactly as shown above and run the program again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character in each line - we will see why this is important later.

How It Works

A Python program is composed of statements. In our first program, we have only one statement. In this statement, we call the print statement to which we supply the text "hello world".





도움 얻기

파이썬에서 함수나 표현식에 대한 빠른 정보를 얻고 싶다면 빌트인 help기능을 사용할 수 있다. 인터프리터 프롬프트를 사용할 때 특히 유용하다. 예를 들어 help('len') 은 아이템 갯수를 세는데 사용하는 len 함수의 도움말을 보여준다. 

팁: q를 누르면 help에서 빠져 나온다

비슷하게, 파이썬의 대부분의 정보를 help함수를 통해 얻을 수있다. help 자체에 대한 정보는 help()를 사용한다.

In case you need to get help for operators like return, then you need to put those inside quotes such as help('return') so that Python doesn't get confused on what we're trying to do.


갈무리


You should now be able to write, save and run Python programs at ease.

Now that you are a Python user, let's learn some more Python concepts.






덧글

댓글 입력 영역