Python



Python is a clear and easy to learn programming language created by Guido van Rossum It is an interpreted language, which means that you can write a program or script and execute it directly rather than compiling it into machine code. Interpreted languages are a bit quicker to program with, since you don’t have to explicitly tell the computer whether a variable is a number, a list, or a string; the interpreter figures out the data types when you execute the script.

The Python interpreter can be run in two ways: as an interactive shell to execute individual commands, or as a command line program to execute standalone scripts. The integrated development environment (IDE) bundled with Python and the Raspberry Pi is called IDLE.

The reason you see two versions of IDLE is that there are two versions of Python installed on the Pi. This is common practice (though a bit confusing). Python 3 is the newest, but changes made to the language between versions 2 and 3 made the latter not backward compatible. Even though Python 3 has been around for years it took a while for it to be widely adopted, and lots of user contributed packages have not been upgraded to Python 3. Make sure that when you are looking at documentation you look at the version of Python that you are using.

Basics

  • Python is not a typed language.

  • Python is case sensitive.

  • The end of a line marks the end of a statement.

  • Single line comments begin with a #

  • Multiline comments begin and end with '''

  • Python source files use the .py extension.





Exercise 1

  1. Open LXTerminal.


  2. Make sure that you have installed emacs. To check that you have a program installed, type the name of the program and press RETURN. If you don't have emacs installed, type:
    sudo apt-get install emacs



  3. Navigate to the desktop by typing cd ~/Desktop


  4. If you want to see what is in the Desktop directory, type
    ls



  5. Create a new directory (folder) by typing mkdir name_of_your_directory


  6. Go inside the directory by typing:
          cd name_of_your_directory



  7. Create new file by typing emacs hello.py. This will open the emacs editor and create a new file named hello.py.


  8. Type
          print 'hello'



  9. Press CTRL-x CTRL-s to save the current file


  10. Press CTRL-x CTRL-c to exit emacs


  11. Type python hello.py to run the program.


  12. Create a new python file in the same directory and write a program to print out something else. Run it. If you want to create a new file from within emacs press CTRL-x CTRL-f


  13. Type exit to close LXTerminal






Emacs help

Key Bindings

In the following, the prefix CTRL- refers to the CONTROL key, the prefix ESC- refers to the ESCAPE key. For example, CTRL-n means to simultaneously press the CONTROL key and the key "n".
Lines
CTRL-a             go to the beginning-of-line
CTRL-e             go to the end-of-line
CTRL-n             go to next-line
CTRL-p             go to previous-line
CTRL-k             kill the current line
CTRL-o             open-line

Words
ESC f           forward-word
ESC b           backward-word
ESC d           kill-word
ESC DEL         backward-kill-word
Characters
CTRL-f             forward-char
CTRL-b             backward-char
CTRL-d             delete-char
DEL             delete-backward-char
CTRL-q             quoted-insert
CTRL-t             transpose-chars
Regions
CTRL-space         set a region mark
CTRL-w             kill-region (between cursor and mark)
ESC-w           memorize the contents of the region (without kill)
CTRL-y             yank (i.e., insert text last killed or memorize)
Screen control
CTRL-l             recenter
CTRL-v             scroll-up (forward)
ESC-v           scroll-down (backward)
ESC <           beginning-of-buffer
ESC >           end-of-buffer
Search
CTRL-s             isearch-forward
CTRL-r             isearch-backward
Files
CTRL-x CTRL-f         find-file
CTRL-x CTRL-r         find-file-read-only
CTRL-x CTRL-s         save-current-file
Windows
CTRL-x 1           delete-other-windows
CTRL-x 2           split-window-vertically
CTRL-x 4 f         find-file-other-window
CTRL-x o           other-window
Command execution
ESC !           shell-command
ESC x compile   compile ("make -k" is default)
CTRL-x `           next-error
                (used after "compile" to find/edit errors)
Miscellaneous
CTRL-x CTRL-c         save-buffers-kill-emacs
CTRL-u             universal-argument
CTRL-x CTRL-z         suspend-emacs
                (resume by typing "fg" to unix)
Help!
CTRL-g             keyboard-quit
CTRL-h             help-command
CTRL-h t           help-with-tutorial
CTRL-h b           describe-bindings
                (complete list of emacs commands)