first_steps.asciidoc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 http://www.lighttable.com/[Light Table]
  53. software which is available on Windows, Mac OS X and GNU/Linux. Details in the next section.
  54. If you are using Windows, *do not use Notepad* - it is a bad choice because it does not do syntax
  55. highlighting and also importantly it does not support indentation of the text which is very
  56. important in our case as we will see later. Good editors will automatically do this.
  57. If you are an experienced programmer, then you must be already using http://www.vim.org[Vim] or
  58. http://www.gnu.org/software/emacs/[Emacs]. Needless to say, these are two of the most powerful
  59. editors and you will benefit from using them to write your Python programs. I personally use both
  60. for most of my programs, and have even written an http://swaroopch.com/notes/vim[entire book on
  61. Vim].
  62. In case you are willing to take the time to learn Vim or Emacs, then I highly recommend that you do
  63. learn to use either of them as it will be very useful for you in the long run. However, as I
  64. mentioned before, beginners can start with Light Table and focus the learning on Python rather than
  65. the editor at this moment.
  66. To reiterate, please choose a proper editor - it can make writing Python programs more fun and
  67. easy.
  68. === Light Table
  69. http://www.lighttable.com[Light Table] is a free editor which you can use for writing Python
  70. programs.
  71. Click on +File+ -> +New file+, type the following:
  72. [source,python]
  73. --------------------------------------------------
  74. print "hello world"
  75. --------------------------------------------------
  76. Click on +File+ -> +Save+ and call it +hello.py+.
  77. Click on +View+ -> +Console+.
  78. Now, place your cursor at the end of the above line and press kbd:[command + enter] to _evaluate_
  79. the line and you'll see the output in the console.
  80. Do watch the videos in the http://docs.lighttable.com/tutorials/python/[LightTable for Python
  81. tutorial] to understand how to use Light Table.
  82. image::light_table_screenshot.png[Screenshot of Light Table,594,536]
  83. === Vim
  84. . Install http://www.vim.org[Vim]
  85. .. Mac OS X users should install +macvim+ package via http://brew.sh/[HomeBrew]
  86. .. Windows users should download the "self-installing executable" from
  87. http://www.vim.org/download.php
  88. .. GNU/Linux users should get Vim from their distribution's software repositories, e.g. Debian and
  89. Ubuntu users can install the http://packages.ubuntu.com/saucy/vim[vim] package.
  90. . Read the http://blog.sontek.net/blog/detail/turning-vim-into-a-modern-python-ide[Vim as Python
  91. IDE] article by John M Anderson.
  92. . Install https://github.com/davidhalter/jedi-vim[jedi-vim] plugin for autocompletion.
  93. === Emacs
  94. . Install http://www.gnu.org/software/emacs/[Emacs 24].
  95. .. Mac OS X users should get Emacs from http://emacsformacosx.com
  96. .. Windows users should get Emacs from http://ftp.gnu.org/gnu/emacs/windows/
  97. .. GNU/Linux users should get Emacs from their distribution's software repositories, e.g. Debian
  98. and Ubuntu users can install the http://packages.ubuntu.com/saucy/emacs24[emacs24] package.
  99. . Install https://github.com/jorgenschaefer/elpy[ELPY]
  100. . Read https://github.com/jorgenschaefer/elpy/wiki[ELPY wiki] for details.
  101. . Also recommended is the https://github.com/bbatsov/prelude[Emacs Prelude] distribution.
  102. === Using A Source File
  103. Now let's get back to programming. There is a tradition that whenever you learn a new programming
  104. language, the first program that you write and run is the 'Hello World' program - all it does is
  105. just say 'Hello World' when you run it. As Simon Cozens footnote:[the author of the amazing
  106. 'Beginning Perl' book] says, it is the "traditional incantation to the programming gods to help you
  107. learn the language better."
  108. Start your choice of editor, enter the following program and save it as +hello.py+.
  109. If you are using Light Table, click on +File+ -> +New file+, type the lines:
  110. [source,python]
  111. --------------------------------------------------
  112. print "hello world"
  113. --------------------------------------------------
  114. In Light Table, click on +File+ -> +Save+ to save a file called +hello.py+.
  115. Where should you save the file? To any folder for which you know the location of the folder. If you
  116. don't understand what that means, create a new folder and use that location to save and run all
  117. your Python programs:
  118. - `/tmp/py` on Mac OS X
  119. - `/tmp/py` on GNU/Linux
  120. - `C:\\py` on Windows
  121. To create the above folder (for the operating system you are using), use the +mkdir+ command in the
  122. terminal, for example, +mkdir /tmp/py+.
  123. IMPORTANT: Always ensure that you give it the file extension of +.py+, for example, +foo.py+.
  124. To run your Python program:
  125. . Open a terminal window (see the previous <<installation,Installation>> chapter on how to do that)
  126. . **C**hange **d**irectory to where you saved the file, for example, +cd /tmp/py+
  127. . Run the program by entering the command +python hello.py+. The output is as shown below.
  128. --------------------------------------------------
  129. $ python hello.py
  130. hello world
  131. --------------------------------------------------
  132. image::terminal_screenshot.png[Screenshot of running program in terminal,593,395]
  133. If you got the output as shown above, congratulations! - you have successfully run your first
  134. Python program. You have successfully crossed the hardest part of learning programming, which is,
  135. getting started with your first program!
  136. In case you got an error, please type the above program _exactly_ as shown above and run the
  137. program again. Note that Python is case-sensitive i.e. +print+ is not the same as +Print+ - note
  138. the lowercase +p+ in the former and the uppercase +P+ in the latter. Also, ensure there are no
  139. spaces or tabs before the first character in each line - we will see <<indentation,why this is
  140. important>> later.
  141. .How It Works
  142. A Python program is composed of _statements_. In our first program, we have only one statement. In
  143. this statement, we call the +print+ _statement_ to which we supply the text "hello world".
  144. === Getting Help
  145. If you need quick information about any function or statement in Python, then you can use the
  146. built-in +help+ functionality. This is very useful especially when using the interpreter
  147. prompt. For example, run `help('len')` - this displays the help for the +len+ function which is
  148. used to count number of items.
  149. TIP: Press +q+ to exit the help.
  150. Similarly, you can obtain information about almost anything in Python. Use +help()+ to learn more
  151. about using +help+ itself!
  152. In case you need to get help for operators like +return+, then you need to put those inside quotes
  153. such as `help('return')` so that Python doesn't get confused on what we're trying to do.
  154. === Summary
  155. You should now be able to write, save and run Python programs at ease.
  156. Now that you are a Python user, let's learn some more Python concepts.