first_steps.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. [[first_steps]]
  2. == First Steps
  3. We will now see how to run a traditional 'Hello World' program in Python. This will teach you how
  4. to write, save and run Python programs.
  5. There are two ways of using Python to run your program - using the interactive interpreter prompt
  6. or using a source file. We will now see how to use both of these methods.
  7. === Using The Interpreter Prompt
  8. Open the terminal in your operating system (as discussed previously in the
  9. <<installation,Installation>> chapter) and then open the Python prompt by typing +python+ and
  10. pressing kbd:[enter] key.
  11. Once you have started Python, you should see `>>>` where you can start typing stuff. This is called
  12. the _Python interpreter prompt_.
  13. At the Python interpreter prompt, type:
  14. [source,python]
  15. --------------------------------------------------
  16. print "Hello World"
  17. --------------------------------------------------
  18. followed by the kbd:[enter] key. You should see the words +Hello World+ printed to the screen.
  19. Here is an example of what you should be seeing, when using a Mac OS X computer. The details about
  20. the Python software will differ based on your computer, but the part from the prompt (i.e. from
  21. `>>>` onwards) should be the same regardless of the operating system.
  22. --------------------------------------------------
  23. $ python
  24. Python 2.7.6 (default, Feb 23 2014, 16:08:15)
  25. [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
  26. Type "help", "copyright", "credits" or "license" for more information.
  27. >>> print "hello world"
  28. hello world
  29. >>>
  30. --------------------------------------------------
  31. Notice that Python gives you the output of the line immediately! What you just entered is a single
  32. Python _statement_. We use +print+ to (unsurprisingly) print any value that you supply to it. Here,
  33. we are supplying the text +hello world+ and this is promptly printed to the screen.
  34. .How to Quit the Interpreter Prompt
  35. [NOTE]
  36. --
  37. If you are using a GNU/Linux or OS X shell, you can exit the interpreter prompt by pressing
  38. kbd:[ctrl + d] or entering +exit()+ (note: remember to include the parentheses, +()+) followed by
  39. the kbd:[enter] key.
  40. If you are using the Windows command prompt, press kbd:[ctrl + z] followed by the kbd:[enter] key.
  41. --
  42. === Choosing An Editor
  43. We cannot type out our program at the interpreter prompt every time we want to run something, so we
  44. have to save them in files and can run our programs any number of times.
  45. To create our Python source files, we need an editor software where you can type and save. A good
  46. programmer's editor will make your life easier in writing the source files. Hence, the choice of an
  47. editor is crucial indeed. You have to choose an editor as you would choose a car you would buy. A
  48. good editor will help you write Python programs easily, making your journey more comfortable and
  49. helps you reach your destination (achieve your goal) in a much faster and safer way.
  50. One of the very basic requirements is _syntax highlighting_ where all the different parts of your
  51. Python program are colorized so that you can _see_ your program and visualize its running.
  52. If you have no idea where to start, I would recommend using
  53. https://www.jetbrains.com/pycharm-educational/[PyCharm Educational Edition] software which is
  54. available on Windows, Mac OS X and GNU/Linux. Details in the next section.
  55. If you are using Windows, *do not use Notepad* - it is a bad choice because it does not do syntax
  56. highlighting and also importantly it does not support indentation of the text which is very
  57. important in our case as we will see later. Good editors will automatically do this.
  58. If you are an experienced programmer, then you must be already using http://www.vim.org[Vim] or
  59. http://www.gnu.org/software/emacs/[Emacs]. Needless to say, these are two of the most powerful
  60. editors and you will benefit from using them to write your Python programs. I personally use both
  61. for most of my programs, and have even written an http://swaroopch.com/notes/vim[entire book on
  62. Vim].
  63. In case you are willing to take the time to learn Vim or Emacs, then I highly recommend that you do
  64. learn to use either of them as it will be very useful for you in the long run. However, as I
  65. mentioned before, beginners can start with PyCharm and focus the learning on Python rather than the
  66. editor at this moment.
  67. To reiterate, please choose a proper editor - it can make writing Python programs more fun and
  68. easy.
  69. [[pycharm]]
  70. === PyCharm
  71. https://www.jetbrains.com/pycharm-educational/[PyCharm Educational Edition] is a free editor which
  72. you can use for writing Python programs.
  73. When you open PyCharm, you'll see this, click on +Create New Project+:
  74. image::pycharm_open.png[When you open PyCharm,439,372]
  75. Select +Pure Python+:
  76. image::pycharm_create_new_project.png[PyCharm New Project,439,372]
  77. Change +untitled+ to +helloworld+ as the location of the project, you should see details similar to
  78. this:
  79. image::pycharm_create_new_project_pure_python.png[PyCharm project details,439,372]
  80. Click the +Create+ button.
  81. Right-click on the +helloworld+ in the sidebar and select +New+ -> +Python File+:
  82. image::pycharm_new_python_file.png[PyCharm -> New -> Python File,777,494]
  83. You will be asked to type the name, type +hello+:
  84. image::pycharm_new_file_input.png[PyCharm New File dialog box,734,452]
  85. You can now see a file opened for you:
  86. image::pycharm_hello_open.png[PyCharm hello.py file,777,494]
  87. Delete the lines that are already present, and now type the following:
  88. [source,python]
  89. --------------------------------------------------
  90. print "hello world"
  91. --------------------------------------------------
  92. Now right-click on what you typed (without selecting the text), and click on +Run 'hello'+.
  93. image::pycharm_run.png[PyCharm Run 'hello',777,494]
  94. You should now see the output (what it prints) of your program:
  95. image::pycharm_output.png[PyCharm output,777,494]
  96. Phew! That was quite a few steps to get started, but henceforth, every time we ask you to create a
  97. new file, remember to just right-click on +helloworld+ on the left -> +New+ -> +Python File+ and
  98. continue the same steps to type and run as shown above.
  99. You can find more information about PyCharm in the
  100. https://www.jetbrains.com/pycharm-educational/quickstart/[PyCharm Quickstart] page.
  101. === Vim
  102. . Install http://www.vim.org[Vim]
  103. .. Mac OS X users should install +macvim+ package via http://brew.sh/[HomeBrew]
  104. .. Windows users should download the "self-installing executable" from
  105. http://www.vim.org/download.php
  106. .. GNU/Linux users should get Vim from their distribution's software repositories, e.g. Debian and
  107. Ubuntu users can install the http://packages.ubuntu.com/saucy/vim[vim] package.
  108. . Install https://github.com/davidhalter/jedi-vim[jedi-vim] plugin for autocompletion.
  109. === Emacs
  110. . Install http://www.gnu.org/software/emacs/[Emacs 24].
  111. .. Mac OS X users should get Emacs from http://emacsformacosx.com
  112. .. Windows users should get Emacs from http://ftp.gnu.org/gnu/emacs/windows/
  113. .. GNU/Linux users should get Emacs from their distribution's software repositories, e.g. Debian
  114. and Ubuntu users can install the http://packages.ubuntu.com/saucy/emacs24[emacs24] package.
  115. . Install https://github.com/jorgenschaefer/elpy/wiki[ELPY].
  116. === Using A Source File
  117. Now let's get back to programming. There is a tradition that whenever you learn a new programming
  118. language, the first program that you write and run is the 'Hello World' program - all it does is
  119. just say 'Hello World' when you run it. As Simon Cozens footnote:[the author of the amazing
  120. 'Beginning Perl' book] says, it is the "traditional incantation to the programming gods to help you
  121. learn the language better."
  122. Start your choice of editor, enter the following program and save it as +hello.py+.
  123. If you are using PyCharm, we have already <<pycharm,discussed how to run from a source file>>.
  124. For other editors, open a new file +hello.py+ and type this:
  125. [source,python]
  126. --------------------------------------------------
  127. print "hello world"
  128. --------------------------------------------------
  129. Where should you save the file? To any folder for which you know the location of the folder. If you
  130. don't understand what that means, create a new folder and use that location to save and run all
  131. your Python programs:
  132. - `/tmp/py` on Mac OS X
  133. - `/tmp/py` on GNU/Linux
  134. - `C:\\py` on Windows
  135. To create the above folder (for the operating system you are using), use the +mkdir+ command in the
  136. terminal, for example, +mkdir /tmp/py+.
  137. IMPORTANT: Always ensure that you give it the file extension of +.py+, for example, +foo.py+.
  138. To run your Python program:
  139. . Open a terminal window (see the previous <<installation,Installation>> chapter on how to do that)
  140. . **C**hange **d**irectory to where you saved the file, for example, +cd /tmp/py+
  141. . Run the program by entering the command +python hello.py+. The output is as shown below.
  142. --------------------------------------------------
  143. $ python hello.py
  144. hello world
  145. --------------------------------------------------
  146. image::terminal_screenshot.png[Screenshot of running program in terminal,593,395]
  147. If you got the output as shown above, congratulations! - you have successfully run your first
  148. Python program. You have successfully crossed the hardest part of learning programming, which is,
  149. getting started with your first program!
  150. In case you got an error, please type the above program _exactly_ as shown above and run the
  151. program again. Note that Python is case-sensitive i.e. +print+ is not the same as +Print+ - note
  152. the lowercase +p+ in the former and the uppercase +P+ in the latter. Also, ensure there are no
  153. spaces or tabs before the first character in each line - we will see <<indentation,why this is
  154. important>> later.
  155. .How It Works
  156. A Python program is composed of _statements_. In our first program, we have only one statement. In
  157. this statement, we call the +print+ _statement_ to which we supply the text "hello world".
  158. === Getting Help
  159. If you need quick information about any function or statement in Python, then you can use the
  160. built-in +help+ functionality. This is very useful especially when using the interpreter
  161. prompt. For example, run `help('len')` - this displays the help for the +len+ function which is
  162. used to count number of items.
  163. TIP: Press +q+ to exit the help.
  164. Similarly, you can obtain information about almost anything in Python. Use +help()+ to learn more
  165. about using +help+ itself!
  166. In case you need to get help for operators like +return+, then you need to put those inside quotes
  167. such as `help('return')` so that Python doesn't get confused on what we're trying to do.
  168. === Summary
  169. You should now be able to write, save and run Python programs at ease.
  170. Now that you are a Python user, let's learn some more Python concepts.