6. 첫 번째 단계 First Steps
파이썬으로 전통적인 'Hello World' 프로그램을 실행하는 방법을 볼 수 있습니다.
이 방법을 통하여 파이썬 프로그램을 작성하고, 저장하고, 가동하는 방법을 배울 수 있습니다.
프로그램 실행을 위한 파이썬 사용 방법으로는
대화형 인터프리터 프롬프트를 사용하는 방법과 소스파일을 사용하는 방법 두 가지가 있습니다.
지금 이러한 방법을 모두 사용하는 방법을 볼 수 있습니다.
We will now see how to run a traditional 'Hello World' program in Python. This will teach you how to write, save and run Python programs.
There are two ways of using Python to run your program - using the interactive interpreter prompt or using a source file. We will now see how to use both of these methods.
6.1. 인터프리터 프롬프트 사용하기 Using The Interpreter Prompt
(설치 장에서 전술한 바와 같이) 운영시스템에서 터미널을 열고 다음 파이썬을 입력하여 파이썬 프롬프트를 열고 enter 키를 눌러 키를 입력합니다.
파이썬을 시작하면, 당신은 타이핑을 시작할 수 있는 >>>을 볼 수 있습니다.
이것을 파이썬 인터프리터 프롬프트라고 합니다.
파이썬 인터프리터 프롬프트에서 다음을 입력하십시오:
Open the terminal in your operating system (as discussed previously in the
Installation chapter) and then open the Python prompt by typing python and pressing enter
key.
Once you have started Python, you should see
>>>
where you can start typing stuff. This is called the Python interpreter prompt
.
At the Python interpreter prompt, type:
print "Hello World"
followed by the enter key. You should see the words Hello World printed to the screen.
Here is an example of what you should be seeing, when using a Mac OS X computer. The details about the Python software will differ based on your computer, but the part from the prompt (i.e. from >>>
onwards) should be the same regardless of the operating system.
$ python
Python 2.7.6 (default, Feb 23 2014, 16:08:15)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>>
파이썬은 즉시 줄의 출력을 제공하는 것이 주목하십시오!
방금 입력한 것이 하나의 파이썬 명령문입니다.
(당연히) 그것에 공급 값을 인쇄하기 위하여 print를 사용합니다.
여기서, 텍스트 hello world를 공급하고 이것이 즉시 화면에 출력됩니다.
Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement
. We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text hello world and this is promptly printed to the screen.
NOTE |
How to Quit the Interpreter Prompt If you are using a GNU/Linux or OS X shell, you can exit the interpreter prompt by pressing ctrl+ d or entering exit() (note: remember to include the parentheses, ()) followed by the enter key. If you are using the Windows command prompt, press ctrl+z followed by the enter key. |
6.2. 에디터 선택하기 Choosing An Editor
뭔가를 실행할 때마다 메시지를 표시 인터프리터에서 프로그램을 입력할 수 없기 때문에, 파일에 저장해야 프로그램을 여러 번 실행할 수 있습니다.
We cannot type out our program at the interpreter prompt every time we want to run something, so we have to save them in files and can run our programs any number of times.
파이썬 소스 파일을 만들려면, 입력하고 저장할 수있는 편집 소프트웨어가 필요합니다.
좋은 프로그래머의 편집기를 사용하여 소스 파일을 서면으로 여러분의 인생을 더 쉽게 만들 것입니다. 따라서, 편집기의 선택은 참으로 중요합니다.
자동차를 선택하는 것처럼 편집기를 선택해야 합니다.
좋은 편집기, 쉽게 파이썬 프로그램을 작성하는 데 도움이 여행을 더욱 편안하게 훨씬 빠르고 안전한 방법으로 (목표를 달성) 목적지에 도달하는 데 도움이됩니다.
To create our Python source files, we need an editor software where you can type and save. A good programmer’s editor will make your life easier in writing the source files. Hence, the choice of an editor is crucial indeed. You have to choose an editor as you would choose a car you would buy. A good editor will help you write Python programs easily, making your journey more comfortable and helps you reach your destination (achieve your goal) in a much faster and safer way.
아주 기본적인 요구사항 중 하나는 프로그램을 참조하고 실행을 시각화 할 수 있도록 파이썬 프로그램의 모든 다른 부분이 색상화되는 구문 강조(syntax highlighting) 입니다.
One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can
see your program and visualize its running.
어디서부터 시작할지 아무 생각이 없다면, 윈도우, 맥 OS X와 GNU/리눅스에서 사용할 수 PyCharm 교육 Edition 소프트웨어를 사용하는 것이 좋습니다.
다음 섹션의 세부 사항.
If you have no idea where to start, I would recommend using PyCharm Educational Edition software which is available on Windows, Mac OS X and GNU/Linux. Details in the next section.
Windows를 사용하는 경우, Notepad를 사용하지 마십시오 - 그것은 구문 강조를하지 않고, 또한 중요한 것은 나중에 보겠지만 우리의 경우에 매우 중요한, 텍스트 들여 쓰기를 지원하지 않기 때문에 그것은 나쁜 선택이 될 것입니다. 좋은 편집기는 이 작업을 자동으로 수행합니다.
If you are using Windows, do not use Notepad - it is a bad choice because it does not do syntax highlighting and also importantly it does not support indentation of the text which is very important in our case as we will see later. Good editors will automatically do this.
숙련된 프로그래머라면, 이미 Vim 또는 Emacs
를 사용하고 있을 것입니다.
말할 필요도 없이 이들 둘은 강력한 편집기 이며, 파이썬 프로그램을 작성하기 위해 그들을 사용에서 도움이됩니다.
나는 개인적으로 내 프로그램의 대부분을 모두 사용, 심지어 빔에 전체 책을 작성했습니다.
If you are an experienced programmer, then you must be already using Vim or Emacs. Needless to say, these are two of the most powerful editors and you will benefit from using them to write your Python programs. I personally use both for most of my programs, and have even written an entire book on Vim.
Vim 또는 Emacs를 배우고자 시간을 낸다면 이들이 장기적으로 매우 유용할 것이기 때문에 강력히 추천합니다.
전에 언급 한 바와 같이, 초보자는 PyCharm으로 시작하고 이 시점에는 편집기보다 파이썬의 학습에 집중할 수 있습니다.
In case you are willing to take the time to learn Vim or Emacs, then I highly recommend that you do learn to use either of them as it will be very useful for you in the long run. However, as I mentioned before, beginners can start with PyCharm and focus the learning on Python rather than the editor at this moment.
반복하려면 적절한 편집기를 선택하십시오 - 그것은 파이썬 프로그램을 작성하기가 더 쉽고 재미있게 만들 수 있습니다.
To reiterate, please choose a proper editor - it can make writing Python programs more fun and easy.
6.3. PyCharm
PyCharm Educational Edition is a free editor which you can use for writing Python programs.
When you open PyCharm, you’ll see this, click on Create New Project:
Select Pure Python:
Change untitled to helloworld as the location of the project, you should see details similar to this:
Click the Create button.
Right-click on the helloworld in the sidebar and select New → Python File:
You will be asked to type the name, type hello:
You can now see a file opened for you:
Delete the lines that are already present, and now type the following:
print "hello world"
Now right-click on what you typed (without selecting the text), and click on Run 'hello'.
You should now see the output (what it prints) of your program:
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.
6.4. Vim
Install Vim
Mac OS X users should install macvim package via HomeBrew
Windows users should download the "self-installing executable" from http://www.vim.org/download.php
GNU/Linux users should get Vim from their distribution’s software repositories, e.g. Debian and Ubuntu users can install the vim package.
Install jedi-vim plugin for autocompletion.
6.5. Emacs
Install Emacs 24.
Mac OS X users should get Emacs from http://emacsformacosx.com
Windows users should get Emacs from http://ftp.gnu.org/gnu/emacs/windows/
GNU/Linux users should get Emacs from their distribution’s software repositories, e.g. Debian and Ubuntu users can install the emacs24 package.
Install ELPY.
6.6. Using A Source File
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 Cozens [1] 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/LinuxC:\\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:
Open a terminal window (see the previous Installation chapter on how to do that)
Change directory to where you saved the file, for example, cd /tmp/py
Run the program by entering the command python hello.py. The output is as shown below.
$ python hello.py
hello world
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 importantlater.
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".
6.7. Getting Help
If you need quick information about any function or statement in Python, then you can use the built-in help functionality. This is very useful especially when using the interpreter prompt. For example, run help('len')
- this displays the help for the len function which is used to count number of items.
TIP | Press q to exit the help. |
Similarly, you can obtain information about almost anything in Python. Use help() to learn more about using help itself!
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.
6.8. Summary
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.
'Python 배우기' 카테고리의 다른 글
Applications for Python 파이썬 적용 (0) | 2016.03.05 |
---|---|
PyQt5 Beginner tutorial (0) | 2016.03.04 |
Installation (0) | 2016.01.05 |
Introduction (0) | 2016.01.05 |
Dedication Preface (0) | 2016.01.05 |
댓글