05-first-steps.pd 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # First Steps
  2. 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.
  3. 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.
  4. ## Using The Interpreter Prompt
  5. Open the terminal in your operating system (as discussed previously in the [Installation chapter](#installation)) and then open the Python prompt by typing `python3` and pressing enter key.
  6. Once you have started `python3`, you should see `>>>` where you can start typing stuff. This is called the *Python interpreter prompt*.
  7. At the Python interpreter prompt, type `print('Hello World')` followed by the `enter` key. You should see the words `Hello World` as output.
  8. 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.
  9. ~~~
  10. $ python3
  11. Python 3.3.0 (default, Oct 22 2012, 12:20:36)
  12. [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin
  13. Type "help", "copyright", "credits" or "license" for more information.
  14. >>> print('hello world')
  15. hello world
  16. >>>
  17. ~~~
  18. 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.
  19. How to Quit the Interpreter Prompt
  20. : If you are using a Linux or Unix 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.
  21. ## Choosing An Editor
  22. 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.
  23. 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.
  24. 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.
  25. If you have no idea where to start, I would recommend using [Komodo Edit](http://www.activestate.com/komodo-edit/downloads) software which is available on Windows, Mac OS X and Linux.
  26. 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 such as Komodo Edit will automatically do this.
  27. If you are an experienced programmer, then you must be already using [Vim](http://www.vim.org/) or [Emacs](http://www.gnu.org/software/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](http://www.swaroopch.com/notes/vim). 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 Komodo Edit and focus the learning on Python rather than the editor at this moment.
  28. To reiterate, please choose a proper editor - it can make writing Python programs more fun and easy.
  29. For Vim users
  30. : There is a good introduction on how to [make Vim a powerful Python IDE by John M Anderson](http://blog.sontek.net/blog/detail/turning-vim-into-a-modern-python-ide). Also recommended is the [jedi-vim plugin](https://github.com/davidhalter/jedi-vim) and my [own dotvim configuration](https://github.com/swaroopch/dotvim).
  31. For Emacs users
  32. : There is a good introduction on how to [make Emacs a powerful Python IDE by Pedro Kroger](http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/). Also recommended is [BG's dotemacs configuration](https://github.com/ghoseb/dotemacs).
  33. ## Using A Source File
  34. 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 (the author of the amazing 'Beginning Perl' book) puts it, it is the "traditional incantation to the programming gods to help you learn the language better."
  35. Start your choice of editor, enter the following program and save it as `hello.py`.
  36. If you are using Komodo Edit, click on `File` --- `New` --- `New File`, type the lines:
  37. ~~~python
  38. print('Hello World')
  39. ~~~
  40. In Komodo Edit, do `File` --- `Save` to save to a file.
  41. 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:
  42. - `C:\\py` on Windows
  43. - `/tmp/py` on Linux
  44. - `/tmp/py` on Mac OS X
  45. To create a folder, use the `mkdir` command in the terminal, for example, `mkdir /tmp/py`.
  46. Important
  47. : Always ensure that you give it the file extension of `.py`, for example, `foo.py`.
  48. In Komodo Edit, click on `Tools` --- `Run Command`, type `python3 hello.py` and click on `Run` and you should see the output printed like in the screenshot below.
  49. ![Screenshot of Komodo Edit with the Hello World program](assets/komodo-edit-hello-world.png)
  50. The best way, though, is to type it in Komodo Edit but to use a terminal:
  51. #. Open a terminal as explained in the [Installation chapter](#installation).
  52. #. *C*hange *d*irectory where you saved the file, for example, `cd /tmp/py`
  53. #. Run the program by entering the command `python3 hello.py`.
  54. The output is as shown below.
  55. ~~~
  56. $ python3 hello.py
  57. Hello World
  58. ~~~
  59. 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!
  60. 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](#indentation).
  61. **How It Works**
  62. A Python program is composed of *statements*. In our first program, we have only one statement. In this statement, we call the `print` *function* which just prints the text `'Hello World'`. We will learn about functions in detail in a [later chapter](#functions) - what you should understand now is that whatever you supply in the parentheses will be printed back to the screen. In this case, we supply the text `'Hello World'`.
  63. ### Executable Python Programs
  64. This applies only to Linux and Unix users but Windows users should know this as well.
  65. Every time, you want to run a Python program, we have to explicitly call `python3 foo.py`, but why can't we run it just like any other program on our computer? We can achieve that by using something called the *hashbang* line.
  66. Add the below line as the *first line* of your program:
  67. ~~~python
  68. #!/usr/bin/env python3
  69. ~~~
  70. So, your program should look like this now:
  71. ~~~python
  72. #!/usr/bin/env python3
  73. print('Hello World')
  74. ~~~
  75. Second, we have to give the program executable permission using the `chmod` command then *run* the source program.
  76. The chmod command is used here to *ch*ange the *mod*e of the file by giving e*x*ecute permission to *a*ll users of the system.
  77. ~~~
  78. $ chmod a+x hello.py
  79. ~~~
  80. Now, we can run our program directly because our operating system calls `/usr/bin/env` which in turn will find our Python 3 software and hence knows how to run our source file:
  81. ~~~
  82. $ ./hello.py
  83. Hello World
  84. ~~~
  85. We use the `./` to indicate that the program is located in the current folder.
  86. To make things more fun, you can rename the file to just `hello` and run it as `./hello` and it will still work since the system knows that it has to run the program using the interpreter whose location is specified in the first line in the source file.
  87. So far, we have been able to run our program as long as we know the exact path. What if we wanted to be able to run the program from folder? You can do this by storing the program in one of the folders listed in the `PATH` environment variable.
  88. Whenever you run any program, the system looks for that program in each of the folders listed in the `PATH` environment variable and then runs that program. We can make this program available everywhere by simply copying this source file to one of the directories listed in `PATH`.
  89. ~~~
  90. $ echo $PATH
  91. /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin
  92. $ cp hello.py /home/swaroop/bin/hello
  93. $ hello
  94. Hello World
  95. ~~~
  96. We can display the `PATH` variable using the `echo` command and prefixing the variable name by `$` to indicate to the shell that we need the value of this "environment variable". We see that `/home/swaroop/bin` is one of the directories in the PATH variable where *swaroop* is the username I am using in my system. There will usually be a similar directory for your username on your system.
  97. If you want to add a directory of your choice to the `PATH` variable - this can be done by running `export PATH=$PATH:/home/swaroop/mydir` where `'/home/swaroop/mydir'` is the directory I want to add to the `PATH` variable.
  98. This method is very useful if you want to write commands you can run anytime, anywhere. It is like creating your own commands just like `cd` or any other commands that you use in the terminal.
  99. ## Getting Help
  100. 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(print)` - this displays the help for the print function which is used to print things to the screen.
  101. Note
  102. : Press `q` to exit the help.
  103. Similarly, you can obtain information about almost anything in Python. Use `help()` to learn more about using `help` itself!
  104. 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.
  105. ## Summary
  106. You should now be able to write, save and run Python programs at ease.
  107. Now that you are a Python user, let's learn some more Python concepts.