# Python Beginner to Expert/Structured Python
##### Starting the shell

As suggested in the introduction, Python was conceived as a bridge between a command shell and an application development language, so it's important to learn how to use the Python command shell. The Python shell can be invoked by opening a terminal window and entering the word python at the prompt. If more than one version of Python is installed, the version number may be required as part of the command, for example python3.2. Selecting from versions may vary depending on the OS used and the method of installation.
Starting python3.2 on an Ubuntu Linux system will create the following messages on the terminal window.
Python 3.2.2 (default, Sep 5 2011, 21:17:14) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
The opening message shows the Python version, some build information, and four commands that may be used to get more information about python. license provides a brief history of python including release years of major version and detailed information about the python distribution and license agreement. credits provides a brief message of thanks to organizations and individuals who have supported python. copyright provides a short list of copyrights that pertain to the Python. help will execute the python help subsystem, which offers detailed information about python functions primarily derived from help strings.
The Python help command will be discussed in detail later in this tutorial in a separate section.
Python's default prompt command prompt is >>>. Python displays a cursor to the right of the >>> prompt and python commands should be entered there.
To exit the shell and return to the system prompt, type exit or Ctrl-D.
The shell itself does not offer much functionality to the user other than to provide a command line for entering python commands and functions. Editing capabilities in the shell are extremely limited.
Commands entered into the shell may be recalled by scrolling backward and forward through the command history using the up arrow and down arrow key. The left and right arrow keys are useful to reposition the cursor while editing the current line. The backspace key is destructive and will delete the character to the left of the cursor. It is possible to toggle between insert and overwrite modes by pressing the insert key. The delete key works like the backspace key except that it deletes the character at the cursor rather than the character to the left of the cursor. The home key repositions the cursor to the leftmost character on the current line while the end key repositions the cursor to the end of the line. Pressing enter will attempt to execute the current line no matter where the cursor is positioned. Note that pressing enter will not break the current at the cursor.
The shell maintains a partial history of commands that have been entered starting each time the shell is invoked. The history is not preserved between sessions.
The PageUp selects the first command in the history list for editing while the PageDown key performs the complementary function editing the last command entered.
Most python users will have little use for the command shell interpreter. Probably it's most common use is as a calculator. Arithmetic expressions entered at the command line will be evaluated immediately and the result displayed. The result of the most recent calculation is assigned to the underscore character. The following shows the use of the underscore character.
>>> 2+2 4 >>> _ 4 >>> _*_ 16

##### Basic Arithmetic with Python

Python has two basic numeric types: int (integer) and float (floating point). A python integer can be of any length. Python integers do not have a minimum or maximum (lower or upper) limit although naturally processing speed is reduced when dealing with very long integers. At the command line, Python will recognize that a number is an int if it is entered with no decimal point and that it is a float if a decimal point is included. Python will recognize that 1 is an int while 1.0 is a float. A real can be cast to an int and vice versa like this: float(1) casts an integer to a float, while int(1.1) casts an int to a float.
Python 3.2.2 for 32-bit Windows provides the following information regarding the numeric floating point type:
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
Python offers the following well known and understood symbols for arithmetic operations.
Operation Symbol Exponentiation ** Modulus %; Multiplication *; Division / (equal precedence) Addition +; Subtraction - (equal precedence)
Python observes typical precedence for these operators in the order shown. Calculations enclosed between parentheses have the highest precedence. When precedence of operations is not dictated by operator precedence or grouping by parentheses, calculations are performed from left to right.

### Exercise:

1. Start the python interpreter. Note the version number that is displayed on the welcome screen. Exit the shell. Restart the shell. Try entering the different commands that are listed in the welcome message (help, copyright, credits, license). Try some calculations. Try dividing two integers that you would expect would produce an fractional result (for example, 5/2). Note the result. Try various combinations of operations to convince yourself that the order of precedence is predictable. Be sure to experiment with parentheses. Enter 2*(5+ at the command line. What happens when you don't match each left parenthesis with a right parenthesis?
2. Consider other ways to start the python command line interpreter. Consider which method you favor and why.

##### IDLE

Python scripts may be created in any text editor with a couple of caveats. 1. It is generally advisiable to include a shebang line when writing for Unix types of systems. 2. In some Unix type environments, the interpreter may have problems with DOS type line endings.
A full installation of Python includes an IDE called IDLE. The name IDLE looks as if it is an acronym for something but like Python itself, it is a name borrowed from Monty Python, in this case the last name of Monty Python regular Eric Idle. IDLE was written by Python progenitor's, Guido von Rossum.
IDLE is included with most or all versions of Python available from the official python website, Python.org. It is often not included with other alternate versions of Python, such as IronPython and ActivePython.
You can start IDLE from the shell or command window, however the preferred method would be to start IDLE via your OS's GUI menu. By default, IDLE open's two Windows: the editor and the shell.
The IDLE shell resembles the command interpreter shell but provides significant additional functionality. Before discussing the additional functionality, note that we lost one of the handier features of the command interpreter shell, the capability to scroll and forwards through history list of commands with the up and down arrows keys. In the IDLE shell, if you want to edit re-execute a previously issued command you can use Alt-p and Alt-n to scroll through the recent command list. You can also find the previously issued command in the window, position the cursor on it, and press the enter key. Unlike the command shell interpreter, IDLE keeps a history of all commands in the shell from the beginning of the session until the shell is closed.
Both IDLE shell and the IDLE editor support the mouse and scroll bars for navigating and editing content in their Windows.
The following is quoted verbatims from the IDLE README
This may prove sufficient introduction for many users. The IDLE help menu offers addition instruction information regarding uses and features of IDLE's menus and options and the entire contents of the help are reproduced here.
IDLE Exercises
Rather than try to elaborate on or reword the lucid explanations provided in the IDLE documentation, what follows is just a series of exercises that are designed to familiarize the reader with menu items and features of the IDLE IDE.
1. Find and open IDLE from your OS's GUI menu. Try closing the editor. Note that closing the editor does not terminate the shell. Use the File|New Window menu item from the shell to open a new Editor window. Using the same menu item, determine whether you can have more than editor window open at a time.
2. Now close the shell. Reopen the shell by using the IDLE editor's Windows|*Python Shell* menu item.
3. Enter a short program in the shell, something like
Be sure to press enter after the colon at the end of the first line. Notice that IDLE indents automatically. What do you think triggers the identation?
4. Re-enter the line with deliberate mistakes. What happens?
5. Locate a valid copy of the code in the shell window. Using the mouse or arrows position the cursor (the thin blinking vertical bar) to the line reading for n in range(0,10): and press enter. Notice that the entire block of code is copied to the active prompt where you can edit it. Try copying the same code by selecting it with mouse and pressing Ctrl-C to copy and then paste at the current position. Consider which method is easier for you to use.
6. Copy the same code and paste in into the editor window. Check the script with the IDLE editor's Run|Check Module menu item. Try to run the script with the IDLE Editor's Run|Run Module menu item or with the F5 key. You should have found that in order to do either you need to save the script. There is a way to direct the IDLE editor to save script changes automatically. Where do you suppose that option is found? Be advised that IDLE does not create backup copies of your scripts but that it does keeps 1000 undo steps in memory. Consider methods that you might use to keep an external audit trail so that you revert to an earlier version of your code if necessary.
7. Save the code in the text editor if you have not already. Note whether you need to type the .py extension or whether the save dialog automatically does it for you.
8. Save the contents of the shell window. Open the saved contents in the IDLE editor. Can you think of any situation where saving the contents of the shell window might be useful?
9. Tear off a copy of any drop down menu by clicking on the dashed lined at the top of the menu. Close the floating copy of the drop down menu. Consider whether this is a useful feature and what you might use it for.
10. Compare the main menu items shown in the shell with those in the editor. Notice that each has seven menus but that the editor has a Format item where the shell has a Shell item.
11. Try out the various options from the IDLE editor's Format menu. You may cut and past the following snippet into your IDLE editor for practice: