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!
- A Shell window may be opened from the Idle menu: Run -> Python Shell
- Entering commands:
- Commands may be entered at the >>> prompt.
- If the Shell detects that a command is not finished at the end of the line, a continuation
line is shown with no >>>. - Statements with a heading ending in a colon followed by an indented block, must be
terminated with an empty line. - The Shell evaluates a completed command immediately, displaying any result other than
None, starting on the next line. - The Shell remembers variable and function names.
- An earlier Shell line may to copied and edited by clicking anywhere in the previously displayed line
and then pressing Enter.
- Start a new window from the File menu by selecting New, Open…, or Recent Files.
- Make your Python file names explicitly end with ‘.py’
- 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.
- 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. - Press Ctrl-C to stop a running program in a long or infinite loop.
- After a program terminates, the Shell remembers function definitions and variable names
define outside of any function.
- If the program is expecting keyboard input, the text cursor should appear at the end of
- Literal integer values may not contain a decimal point.
- Integers may be arbitrarily large and are stored exactly.
- Integers have normal operations, with usual precedence (highest listed first):
- *, /,//, %: multiplication, division with float result, integer division (ignoring any
remainder), just the remainder from division - +, -: addition, subtraction
- *, /,//, %: multiplication, division with float result, integer division (ignoring any
- Literal values must contain a decimal point to distinguish them from the int type
- Has the same operation symbols as for integers
- A mixed operation with an integer and a float produces a float result.
Literal values contain a sequence of characters enclosed in matching quotes.
- Enclosed in ‘or “: The string must be on one line.
- Enclosed in ”’or “”": The string may include multiple lines in the source file.
- Escape codes inside literals include \’ for a single quote, \t for tab and \n for a newline.
- Binary operations (operation symbols have the same precedence order as when the symbols
are used in arithmetic)
- stringExpression1 + stringExpression2
concatenation (running together) of the two strings - stringExpression * integerExpression
integerExpression * stringExpression
Repeat the string the number of times given by the integer expression.
- stringExpression1 + stringExpression2
- Variables are named pieces of RAM
- When a variable is used in an expression, its latest value is substituted.
- Variable names may only contain letters, digits, and the underscore, and cannot start with a digit. They
are case sensitive. - 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. - By convention, multi-word variable names either
- use underscores in place of blanks (since blanks are illegal is identifiers), as in
initial_account_balance - use camelcase: all lowercase except for the starting letter of the second and later words,
as in initialAccountBalance
- use underscores in place of blanks (since blanks are illegal is identifiers), as in
- Variable assignment:
variable = expression
- The expression on the right is evaluated, using the latest values of all variables, and
calculating all operations or functions specified. - The expression value is associated with the variable named on the left, removing any
earlier association with the name.
for item in range(count) : consistently indented statement block, which may use the variable item
Repeat the indented statements count times.
functionName ( expression, expression, and so on )
- The number of expressions must correspond to a number of parameters allowed by the
function’s definition. - 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. - Each expression is evaluated and the values are passed to the code for the function, which
executes its defined steps.
- Print function:
print(expression)
print(expression, expression, expression)
print()- Print the value of each expression in the list to the standard place for output (usually
the screen) separating each value by individual blanks - The string printed ends with a newline
- With no expression, the statement only advances to a new line.
- Print the value of each expression in the list to the standard place for output (usually
- Type names can be used as function to do obvious conversions to the type, as in int(’234′),
float(123), str(123). - type(expression)
Return the type of the value of the expression. - 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
-
def functionName (parameter1, parameter2): consistently indented statement block, which may include a return statement
- There may be any number of parameters. The parentheses must be included even if there are no parameters.
- When a function is first defined, it is only remembered: its lines are not executed.
- 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. - Functions should be used to :
- Emphasize that the code corresponds to one idea and give an easily recognizable name.
- 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. - 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. - 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.





