Python Review 1

We have learned a LOT since the start of the semester!  Hopefully you have some pride in knowing that you are using the same tools used to create your favorite games, Windows, iPhone apps, etc.

I’ve remixed some review notes from Loyola University Chicago released under the Creative commons Attribution-Noncommercial-Share Alike 3.0 United States License.  Thanks Dr. Andrew N. Harrington!

  • Python Shell
    1. A Shell window may be opened from the Idle menu: Run -> Python Shell
    2. Entering commands:

      1. Commands may be entered at the >>> prompt.
      2. If the Shell detects that a command is not finished at the end of the line, a continuation
        line is shown with no >>>.
      3. Statements with a heading ending in a colon followed by an indented block, must be
        terminated with an empty line.
      4. The Shell evaluates a completed command immediately, displaying any result other than
        None, starting on the next line.
      5. The Shell remembers variable and function names.
    3. An earlier Shell line may to copied and edited by clicking anywhere in the previously displayed line
      and then pressing Enter.
  • Idle editing
    1. Start a new window from the File menu by selecting New, Open…, or Recent Files.
    2. Make your Python file names explicitly end with ‘.py’
  • To run a program from an Idle Editor Window:

    1. Select Run -> Run Module or press function key F5. The program runs in the Shell window,
      after resetting the shell so all old names are forgotten.

      1. If the program is expecting keyboard input, the text cursor should appear at the end of
        the Shell history. If you somehow move the cursor elsewhere, you must explicitly move
        it back.
      2. Press Ctrl-C to stop a running program in a long or infinite loop.
      3. After a program terminates, the Shell remembers function definitions and variable names
        define outside of any function.
  • Type int, (short for integer):

    1. Literal integer values may not contain a decimal point.
    2. Integers may be arbitrarily large and are stored exactly.
    3. Integers have normal operations, with usual precedence (highest listed first):
      1. *, /,//, %: multiplication, division with float result, integer division (ignoring any
        remainder), just the remainder from division
      2. +, -: addition, subtraction
  • Type float, (short for floating point): approximations of real numbers

    1. Literal values must contain a decimal point to distinguish them from the int type
    2. Has the same operation symbols as for integers
    3. A mixed operation with an integer and a float produces a float result.
  • Type str, (short for string):
    Literal values contain a sequence of characters enclosed in matching quotes.

    1. Enclosed in or : The string must be on one line.
    2. Enclosed in ”’or “”": The string may include multiple lines in the source file.
    3. Escape codes inside literals include \’ for a single quote, \t for tab and \n for a newline.
    4. Binary operations (operation symbols have the same precedence order as when the symbols
      are used in arithmetic)

      1. stringExpression1 + stringExpression2
        concatenation (running together) of the two strings
      2. stringExpression * integerExpression
        integerExpression * stringExpression
        Repeat the string the number of times given by the integer expression.
  • Variables are identifiers used to name Python data

    1. Variables are named pieces of RAM
    2. When a variable is used in an expression, its latest value is substituted.
    3. Variable names may only contain letters, digits, and the underscore, and cannot start with a digit. They
      are case sensitive.
    4. You cannot use a reserved word as a variable name, nor are you recommended to redefine an
      identifier predefined by Python. In the Idle editor you are safe if your variable names remain
      colored black.
    5. By convention, multi-word variable names either

      1. use underscores in place of blanks (since blanks are illegal is identifiers), as in
        initial_account_balance
      2. use camelcase: all lowercase except for the starting letter of the second and later words,
        as in initialAccountBalance
    6. Variable assignment:

    variable = expression

    1. The expression on the right is evaluated, using the latest values of all variables, and
      calculating all operations or functions specified.
    2. The expression value is associated with the variable named on the left, removing any
      earlier association with the name.
  • The for loop
  • for item in range(count) :    
            consistently indented statement block, which may use the variable item

    Repeat the indented statements count times.

  • Function calls

    functionName ( expression, expression, and so on )

    1. The number of expressions must correspond to a number of parameters allowed by the
      function’s definition.
    2. Even if there are no parameters, the parentheses must be included to distinguish the name of
      the function from a request to call the function.
    3. Each expression is evaluated and the values are passed to the code for the function, which
      executes its defined steps.
  • Functions that are built-in

    1. Print function:

      print(expression)

      print(expression, expression, expression)

      print()

      1. Print the value of each expression in the list to the standard place for output (usually
        the screen) separating each value by individual blanks
      2. The string printed ends with a newline
      3. With no expression, the statement only advances to a new line.
    2. Type names can be used as function to do obvious conversions to the type, as in int(’234′),
      float(123), str(123).
    3. type(expression)
      Return the type of the value of the expression.
    4. raw_input(promptString)

      Print the promptString to the screen; wait for the user to enter a line from the keyboard, ending with
      Enter. Return the character sequence as a string
  • Functions defined by a user:

    def functionName (parameter1, parameter2):
         consistently indented statement block, which may include a return statement

    1. There may be any number of parameters. The parentheses must be included even if there are no parameters.
    2. When a function is first defined, it is only remembered: its lines are not executed.
    3. When the function is later called in other code, the actual parameters in the function call are
      used to initialize the local variables parameter1, parameter2, and so on in the same order as
      the actual parameters.
    4. Functions should be used to :

      1. Emphasize that the code corresponds to one idea and give an easily recognizable name.
      2. Avoid repetition. If a basic idea is repeated with just the data changing, it will be easier
        to follow and use if it is coded once as a function with parameters, that gets called with
        the appropriate actual parameters when needed.
      3. It is good to separate the internal processing of data from the input and output of data.
        This typically means placing the processing of data and the return of the result in a
        function.
      4. Separate responsibilities: The consumer of a function only needs to know the name,
        parameter usage, and meaning of any returned value. Only the writer of a function needs
        to know the implementation of a function.