1. Python Code Commenting Style Requirements
Comments
• In-line/Multiline comments: more in-depth explanations → section of lines all
beginning with #
o # This section of code
# Requires a longer comment
# So we can add as many lines as needed
• Multi-line comments.
"""
This is a multi-line comment
that continues on a second line.
And even uses a third line.
D
"""
O
Formatting style
• Classes: capitalized (Member instead of member) & "CamelCase"
N
• Functions: should be lowercase → separated by underscores (calculate_sqrt).
•
O
Objects: (parameters, instance/local variables) lowercase noun (grade)
• Constants: all capital letters and underscores (MAX_SIZE).
T
C
2. Introduction to Python 3
O
The Python interactive interpreter
PY
• Arrow key: Scrolls through lines previously executed with the interpreter.
• Code: The text that represents a computer program.
• Prompt (">>>"): Informs the programmer that the interpreter is ready to accept
commands → displayed by the Phyton interpreter
Programming in Python
• Statement: a program instruction
• Program: series of statements
• Expressions: code that return a value when evaluated
• Variables: named references to values stored by the interpreter.
Basic output
• Print an output → print('hello’+’world') = hello world
• Print and maintain same line → print('Hello', end=' ')
• Good practice
o single quotes for shorter strings
o double quotes for more complicated text or text that contains single quotes
,Basic input
• Reading input → statement: variable_name = input(‘text inside the Text box’)
• convert strings to integers
o my_string = '123'
o my_int = int('123')
• combine input and int → wage = int(input())
Errors
• SyntaxError: The program contains invalid code that can not be understood.
• IndentationError: The lines of the program are not properly indented.
• ValueError: An invalid value is used – can occur if giving letters to int().
• NameError: The program tries to use a variable that does not exist.
• TypeError: An operation uses incorrect types – can occur if adding an int to a string.
• Logic error/bug: the program is logically flawed.
Computer tour
• Operating system: Manages programs and interfaces with peripherals.
D
• Disk: Non-volatile storage with slower access.
O
• RAM: Volatile storage with faster access usually located off processor chip.
• Cache: Relatively small volatile storage with fastest access located on processor chip.
N
• Clock: Rate at which a processor executes instructions.
• Moore’s law: The doubling of IC capacity roughly every 18 months.
O
T
Language history
• Machine language: founded on a two-character alphabet → bit
C
• Machine Instruction: Particular sequence of 0s and 1s rep
•
O
Executable program: sequence of machine instructions together
• Assemblers: translate human readable instructions (assembly language) into an
PY
executable program
• High-level languages: support programming using formulas or algorithms
o FORTRAN (for "Formula Translator")
o ALGOL (for "Algorithmic Language")
• High level instruction: statement
• Compilers: to automatically translate high-level language, into assembly-language
• Scripting languages: to execute programs without the need for compilation
• Script: program whose instructions are executed by another program → an interpreter
→ slower but no need for compilation
• Python: scripting language
o Python 3.0 is not backwards compatible.
o Python 3.x versions are becoming more widely
o Open source
,3. ADTs and Algorithms
Introduction to algorithms
• Algorithm: sequence of steps to solve a problem or calculation
• Computational problem: input + question about the input + output.
• Common algorithm
o Longest common substring: between two inputs strings.
o Binary search: an efficient algorithm for searching a list.
o Dijkstra's shortest path: determines the shortest path from a start vertex to each
vertex in a graph.
Efficient algorithms and hard problems
• Algorithm efficiency: measured by the algorithm runtime
• Efficient algorithm: runtime increases no more than polynomially (nk)
• NP-complete problems: no known efficient algorithm exists
Data structures
D
• Data structure: a way of organizing, storing, and performing operations on data
O
• Selection of data structure: depends on
o type of data being stored
o operations the program may need to perform on that data
N
• the algorithms to implement operations are typically specific to each data structure
O
Basic data structures.
T
• Record: stores subitems, with a name associated to each
•
C
Array: stores an ordered list of items → each item is accessible by a positional index.
• Linked list: stores ordered list of items in nodes → each node stores data and has a
O
pointer to the next node.
•
PY
Binary tree: each node stores data and has up to two children → left and right child.
• Hash table: stores unordered items by mapping (or hashing) each item to a location in
an array.
• Heap:
o Max-heap: node's key is greater than or equal to the node's childrens' keys
o Min-heap: node's key is less than or equal to the node's childrens' keys.
• Graph: representing connections among items
o A vertex represents an item in a graph
o An edge represents a connection between two vertices in a graph.
Abstract data types
• Data type described by predefined user operations, such as "insert data at rear," without
indicating how each operation is implemented
• Programmer need not have knowledge of which data structure is used to use the list
ADT.
• Common ADTs.
, •
D
Common list ADT operations
O
N
O
T
C
O
PY
• Common operations for a queue ADT→ items are inserted at the end of the queue
and removed from the front of the queue.
Applications of ADTs
• ADTs support abstraction (hiding the underlying implementation details) → designers to
focus on higher-level operations → improves programmer efficiency
• Knowledge of an ADT's underlying implementation → to analyze runtime efficiency.
• ADTs in standard libraries