6.introProg
6.introProg
‣ Literals
Python Review
Operator Precedence
Type Operators
High **
Arithmetic *, /, //, %
+,-
Relation ==, !=, <=, >=, >, <
not
Logical
Low and
or
Python Review
ent
• while <condition>: ( ) [ ] , . : ' " #
comm
if/loop
• for v in <iterable>: e.g., range(start, num, step) slice
Decimal def
• Functions (def, return, call) Name’s context
- Built-in: abs, len, print, input, open, readline, readlines, write, join, append, strip
writelines, close, split ..
‣ Reserved words: import, True, None, with, as, False ..
Python Overview
• Incorrect algorithm (or its coding) Debug incrementally, test each change
- Produces the wrong answer (sometimes or every time)
• Examples:
- Used unintended or improperly initialized variables (copy-paste?)
- Incorrect operator applied (relational, logical, indexing)
- Misunderstood order of evaluation (precedence, associativity of operators)
- Incorrect loop conditions (continuation/termination) or nesting
- Misunderstood what a function does, or how to control its operations
Objects, Operations, Names Python Review
‣ Objects have type (class) int, bool, float, str, list .. see built-in function: type(..)
• Types determine the operations that they can be operands for
• Operations produce object is of a certain type
‣ Encapsulation
• Module, Function, Class
• Modules and class can expose names in their name-space, Functions do not
• Names issued inside a function have life only inside the function see global
‣ Module is a collection for names: import to use those names import time
mod.py
x=x+1
y = x*x import mod
def f():
print(y)
if __name__ == '__main__':
f()
Name Assignment Python Review
‣ input('prompt string')
• Receive string of keyboard-typed characters until a “Return” is pressed
• Return character: '\n', which is not included in the received string
‣ print(comma, separated, values) Converted to string, see str(..), and sent to console
• Output on screen each value separated by space ending with '\n'
Caller can mention
‣ print(comma, separated, values, sep=', ', end='')
named params