프로그래머가 아닌 이들을 위한 파이썬 3 자습서/들어가기

위키책, 위키책
둘러보기로 가기 검색하러 가기

먼저 할 일[+/-]

음, 당신은 프로그램을 짜본 적이 없군요. 우리는 이 자습서를 익혀가면서, 나는 프로그램을 어떻게 짜는지 가르치려고 시도할 겁니다. 프로그램 짜는 법을 배우는 방법은 딱 한 가지 뿐이죠. 당신이 코드를 읽어보고 코드를 써보는 겁니다 (컴퓨터 프로그램들이 종종 그러듯이 말이죠). 이제 많은 코드를 보여줄 겁니다. 당신은 내가 보여준 코드를 타이핑해서 무슨 일이 일어나는지 보야야 합니다. 가지고 놀면서 여러번 바꿔도 보세요. 최악의 상황은 그게 작동하지 않는 겁니다. 내가 코드를 타이핑하면, 그것은 아래와 같은 포맷(형태)일 겁니다:

##파이썬은 배우기 쉽습니다.
print("Hello, World!")
print("안녕하세요!")

그렇죠, 이건 다른 글과 구분하기 쉽습니다. 이것을 웹페이지에서 읽는다면, 당신은 코드에 색깔이 입혀진 것을 발견하게 될 겁니다. -- 알기 쉽게 하고, 코드의 서로 다른 부분임을 알기 쉽게 합니다. 당신이 입력한 코드는 색깔이 엽혀지지 않았거나, 색깔이 조금 다를 수 있는데, 그건 코드에 별 영향을 미치지 않습니다 - 당신이 입력한 코드가 여기에 출력된 것과 같은 것이라면 그렇습니다.

만약 컴퓨터가 뭔가를 출력한다면 아래와 같은 포맷일 겁니다:

Hello, World!
안녕하세요!

(Note that printed text goes to your screen, and does not involve paper. Before computers had screens, the output of computer programs would be printed on paper.)

Note that this is a Python 3 tutorial, which means that most of the examples will not work in Python 2.7 and before. As well, some of the extra libraries (third-party libraries) have not yet been converted. You may want to consider learning from the. However, the differences between versions are not particularly large, so if you learn one, you should be able to read programs written for the other without much difficulty.

There will often be a mixture of the text you type (which is shown in bold) and the text the program prints to the screen, which would look like this:

정지!
거기 누구냐? Josh
Josh, 지나가도 좋다.

(Some of the tutorial has not been converted to this format. Since this is a wiki, you can convert it when you find it.)

I will also introduce you to the terminology of programming - for example, that programming is often referred to as coding or hacking. This will not only help you understand what programmers are talking about, but also help the learning process.

Now, on to more important things. In order to program in Python you need the Python 3 software. If you don't already have the Python software go to http://www.python.org/download/ and get the proper version for your platform. Download it, read the instructions and get it installed.

파이썬 설치하기[+/-]

For Python programming you need a working Python installation and a text editor. Python comes with its own editor IDLE, which is quite nice and totally sufficient for the beginning. As you get more into programming, you will probably switch to some other editor like emacs, vi or another.

The Python download page is: http://www.python.org/download. The most recent version is Python 3.4.1 (as at 19th May 2014); Python 2.7 and older versions will not work with this tutorial. There are various different installation files for different computer platforms available on the download site. Here are some specific instructions for the most common operating systems:

리눅스, BSD, 유닉스 사용자[+/-]

You are probably lucky and Python is already installed on your machine. To test it type python3 on a command line. If you see something like that in the following section, you are set.

If you have to install Python, first try to use the operating system's package manager or go to the repository where your packages are available and get Python 3. Python 3.0 was released in December 2008; all the distributions should have Python 3 available, so you may not need to compile Python 3 from scratch after downloading the source 코드. Ubuntu and Fedora do have Python 3 binary packages available, but they are not yet the default, so they need to be installed specially.

Roughly, here are the steps to compile Python in UNIX (If these totally don't make sense, you may want to read another introduction to UNIX, such as Introduction to Linux):

$ tar -xvzf ~/Download/Python-3.4.1.tgz
... list of files as they are uncompressed 
  • Change to the directory and tell the computer to compile and install the program
$ cd Python-3.4/
$ ./configure --prefix=$HOME/python3_install 
 ... lots of output.  Watch for error messages here ... 
$ make
 ... even more output.  Hopefully no error messages ... 
$ make install
  • Add python 3 to your path. You can test it first by specifying the full path. You should add $HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3
Python 3.4.1 (... size and date information ...) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The above commands will install Python 3 to your home directory, which is probably what you want, but if you skip the --prefix, it will install it to /usr/local. If you want to use the IDLE graphical 코드 editor, you need to make sure that the tk and tcl libraries, together with their development files, are installed on the system. You will get a warning during the make phase if these are not available.

macOS 사용자[+/-]

Starting from Mac OS X (Tiger), Python ships by default with the operating system, but you will need to update to Python 3 until OS X starts including Python 3 (check the version by starting python3 in a command line terminal). Also IDLE (the Python editor) might be missing in the standard installation. If you want to (re-)install Python, get the MacOS installer from the Python download site.

Windows 사용자[+/-]

Download the appropriate Windows installer (the x86 MSI installer, if you do not have a 64-bit AMD or Intel chip). Start the installer by double-clicking it and follow the prompts.

See https://docs.python.org/3/using/windows.html#installing-python for more information.

PATH 환경 변수 설정하기[+/-]

PATH 환경변수는 세미콜론으로 구분되는 폴더들의 리스트로, 명령 프롬프트에서 파일이름으로 실행시킬 때 윈도우가 해당 폴더들에서 우선적으로 찾는다. 당신은 명령 프롬프트에서 현재의 PATH 설정을 아래의 명령을 통해 확인할 수 있다:

echo %PATH%

환경변수를 바꾸는 가장 쉬운 방법은 윈도우에 내장된 환경 변수 에디터를 사용하는 것이다. 이 에디터를 사용하는 방법은 윈도우의 버전마다 다르다.

윈도우 8: 윈도우키를 누르고 제어판을 타이핑하면 윈도우 제어판으로 연결된다. 제어판을 열었다면, 보기 기준을 큰 아이콘으로 바꾸고, 시스템을 클릭한다. 새 창이 뜬다면, 고급 시스템 설정 링크를 클릭하고, 환경 변수... 버튼을 클릭한다.

윈도우 7 또는 비스타: 좌하단의 시작버튼을 누르고, 컴퓨터위로 마우스 커서를 옮긴 후, 우클릭한 뒤, 팝업 메뉴에서 속성을 선택한다. 고급 시스템 설정링크를 클릭하고, 환경 변수... 버튼을 클릭한다.

윈도우 XP: 내 컴퓨터 아이콘에서 우클릭 한 후 속성을 선택한다. 고급 탭을 선택하고, 환경 변수... 버튼을 클릭한다.

Once you've brought up the environment variable editor, you'll do the same thing regardless of which version of Windows you're running. Under System Variables in the bottom half of the editor, find a variable called PATH. If there is is one, select it and click Edit.... Assuming your Python root is C:\Python34, add these two folders to your path (and make sure you get the semicolons right; there should be a semicolon between each folder in the list):

C:\Python34
C:\Python34\Scripts

Note: If you want to double-click and start your Python programs from a Windows folder and not have the console window disappear, you can add the following 코드 to the bottom of each script:

print("Hello World")
#stops console from exiting
end_prog = ""
while end_prog != "q":
        end_prog = input("type q to quit")

인터렉티브 모드[+/-]

IDLE(파이썬 GUI라고도 합니다)를 실행합시다. 당신은 아래와 비슷한 글이 있는 창을 볼 겁니다:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 

>>> 는 당신이 인터렉티브 모드에 있다는 것을 알려주는 파이썬의 방식입니다. 인터렉티브 모드에서는 당신이 타이핑한 것은 즉시 실행됩니다. 1+1을 타이핑해 봅시다. 파이썬은 2라고 반응할 겁니다. 인터렉티브 모드는 당신이 테스트할 수 있게 하고 파이썬이 어떻게 하는지 볼 수 있습니다. 당신이 새로운 파이썬 구문과 놀아볼 필요가 있다고 느끼면, 인터렉티브 모드로 가서 시험해보면 됩니다.

프로그램들을 만들고 실행하기[+/-]

아직도 IDLE을 실행하지 않았다면 IDLE을 실행합시다. 위쪽의 메뉴에서, File을 선택하고 New File을 선택합니다. 새 창이 나타났다면, 다음을 타이핑합니다.:

print("Hello, World!")

이제 프로그램을 저장합니다: File메뉴에서, Save를 선택합니다. "hello.py"로 저장합니다 (당신이 원하는 폴더에 저장하면 됩니다). 저장이 되면 실행할 수 있습니다.

다음으로 프로그램을 실행하겠습니다. Run메뉴에서 Run Module을 선택합니다 (구버전의 IDLE에서는 Edit메뉴에서 Run script을 선택합니다). 이것은 *Python Shell* 창에 Hello, World! 를 출력할 것입니다.

For a more in-depth introduction to IDLE, a longer tutorial with screenshots can be found at http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html

Program file names[+/-]

It is very useful to stick to some rules regarding the file names of Python programs. Otherwise some things might go wrong unexpectedly. These don't matter as much for programs, but you can have weird problems if you don't follow them for module names (modules will be discussed later).

  1. Always save the program with the extension .py. Do not put another dot anywhere else in the file name.
  2. Only use standard characters for file names: letters, numbers, dash (-) and underscore (_).
  3. White space (" ") should not be used at all (use underscores instead).
  4. Do not use anything other than a letter (particularly no numbers!) at the beginning of a file name.
  5. Do not use "non-english" characters (such as ä, ö, ü, å or ß) in your file names—or, even better, do not use them at all when programming.

Using Python from the command line[+/-]

If you don't want to use Python from the command line, you don't have to, just use IDLE. To get into 인터렉티브 모드 just type python3 without any arguments. To run a program, create it with a text editor (Emacs has a good Python mode) and then run it with python3 program_name.

Additionally, to use Python within Vim, you may want to visit Python wiki page about VIM

유닉스 계열에서 파이썬 프로그램 실행하기[+/-]

유닉스를 사용한다면 (리눅스든, 맥 OS X나, BSD든지), chmod를 통해 실행가능한 프로그램으로 만들고 싶다면 , 첫번째 줄에 다음을 추가합니다.:

#!/usr/bin/env python3

그러면 당신은 ./hello.py 를 다른 명령들처럼 실행시킬 수 있습니다.

도움을 얻을 수 있는 곳[+/-]

At some point in your Python career you will probably get stuck and have no clue about how to solve the problem you are supposed to work on. This tutorial only covers the basics of Python programming, but there is a lot of further information available.

파이썬 문서[+/-]

First of all, Python is very well documented. There might even be copies of these documents on your computer, which came with your Python installation:

  • The official Python 3 Tutorial by Guido van Rossum is often a good starting point for general questions.
  • For questions about standard modules (you will learn what this is later), the Python 3 Library Reference is the place to look at.
  • If you really want to get to know something about the details of the language, the Python 3 Reference Manual is comprehensive but quite complex for beginners.

파이썬 사용자 모임[+/-]

There are a lot of other Python users out there, and usually they are nice and willing to help you. This very active user community is organised mostly through mailing lists and a newsgroup:

  • The tutor mailing list is for folks who want to ask questions regarding how to learn computer programming with the Python language.
  • The python-help mailing list is python.org's help desk. You can ask a group of knowledgeable volunteers questions about all your Python problems.
  • The Python newsgroup comp.lang.python (Google groups archive) is the place for general Python discussions, questions and the central meeting point of the community.
  • Python wiki has a list of local user groups, you can join the group mailing list and ask questions. You can also participate in the user group meetings.

In order not to reinvent the wheel and discuss the same questions again and again, people will appreciate very much if you do a web search for a solution to your problem before contacting these lists!