EECS1 – read 2, 3 of 19 chapters of Think Python. That sounds good until you realize it’s a remedial portion of the class. Also, I haven’t started the real reading in that class.
Intro to CS & Python – lectures 1, 2 of 12.
Extra – pseudo code for Huffman’s Algorithm (see Computation Structures notes from yesterday). I did this first thing in the morning and it already looks bad in light of some of what I learned today.
I dictated my written notes into my phone. The automated dictation software has issues. For instance, “new line” sometimes prints out “new line” and sometimes verbally commands a carriage return. “New paragraph” doesn’t seem to have this issue. “Colon” always prints “colon” despite “semicolon” working as a verbal command for “;”. “Paren” or “parenthesis” almost never gives me a “(” and I’ve never successfully commanded a close parenthesis “)”. Dictation was fine for prose, but unusable for lists and code. Fixing the dictation for lists and code takes longer than typing them.
Part of that may be due to using WordPress. Perhaps the text would be easier to work with in Word or Google docs and then I can transfer that to here. That would mean my process is (1) write notes, (2) dictate notes, (3) clean notes in a word processor, (4) copy notes here, (5) clean notes for web presentation. Alternatively, (1) type notes in WordPress.
What is Computation, Intro to CS and Python lecture #1
A program is a recipe.
- Sequence of steps
- Flow of control, determining when steps execute
- A means of determining when to stop
Basic machine architecture
- Memory – stores programs and data.
- Control unit – program counter, tells ALU what ops to do on what data, increments when ALU performs, accesses programs.
- Arithmetic logic unit – does primitive operations / tests, accesses data, write data.
Alan Turing proved you could do anything with the six primitives. The six primitives are move left, move right, read, right, scan, and do nothing. We can abstract methods to create new primitives, so modern languages have more primitives. Anything computable in one language is computable in another language.
- Syntactic errors (e.g. 3″hi”) – easily caught
- Static semantic errors (e.g. 3 + “hi”) – some languages check prior to running. These can cause unpredictable behavior.
- No semantic errors, but different meaning than user intends
- Program crashes
- Program runs forever
- Program returns “incorrect” answer
A program is a sequence of definitions and commands. It evaluates definitions and executes commands. These are the tests and operations from basic machine architecture.
Programs manipulate data objects. Objects have types defining allowable operations for that type. Objects are scalar or non-scalar. Scalar examples: int, float, bool, NoneType. You can use type parentheses parentheses to see an object’s type. You can convert objects from one type to another (float(3) converts integer to float, int(3.9) truncates a float to the integer 3, str(3) converts 3 to “3”). Such conversion is called casting.
+ → sum, concatenation for strings
– → difference
* → product, repetition for a number of times for strings (“Ho ” * 3 is “Ho Ho Ho “)
/ → division (always a float)
% → modulus
** → power (^ in some languages, the ^ is a bitwise operator in Python)
= → bind a value to a variable. Variables are case sensitive in Python!
# → comment a line
// → floor division, it rounds the result down (result is an integer)
x<<y → x is suffixed by y zeroes. Equivalent to x*2**y.
x>>y → x is shifted right y places. Equivalent to x//2**y.
x&y → (and) for each bit, if x & y are 1, 1, else 0.
x|y → (or) for each bit, if either x or y are 1, 1, else 0.
~x → bitwise complement of x. 01 = 10.
x^y → bitwise exclusive or (xor). Flip a bit in x if bit in y is 1.
Branching and Iteration, Intro to CS and Python lecture #2
Input prints what is in quotes, user type something and presses enter, and the input is bound as a string to a variable.
text = input("Type a string.") print(text)
If you want the input as another type, cast it as that type.
num = float(input("Enter a number.")) print(num)
i > j #compares strings lexicographically i >= j i < j i <= j i == j i != j
Boolean operators: not, and, or.
Control flow through branching (and here I thought if statements were nothing special).
if: else: if <condition for case 1>: elif <condition for case 2>: elif <condition for case 3>: else:
Control flow through iteration (loops). Loops and if statements pay close attention to the indentation. If it’s not indented (4 spaces by convention), it’s not in the loop or part of the if statement.
while <condition>: Indented code More indented code Code not in the while loop #This: n = 0 while n < 5: print(n) n = n + 1 #is equivalent to this: for n in range(5): print(n)
Range(start, stop, step)
Stop is the only required argument; start defaults to zero and step defaults to one. If provided two arguments, the function interprets them as the start and stop. The for loop continues until stop minus one. The arguments must be integers.
Break ends the loop it is found in.
- known number of iterations
- uses a counter
- can be written as a while loop
- unbounded number of iterations
- can use a counter, but must initialize before the loop and increment it in the loop
- not all loops can be written as for loops
If you enter into an infinite loop, try Ctrl + C.
“\n” starts a new line in text.
Think Python, CH 3
Arguments are assigned to variables called parameters. Variables and parameters are local. Void functions don’t return anything; return functions do.
Modules contain collections of related functions.
import math import [module] math.pi math.[math function] # Instead of import math print(math.pi) # you can from math import pi print pi # or get everything from the math module from math import * print pi
Define a new function using def. First line is the header, the rest is the body.
def print_lyrics() print("Somewhere over the rainbow...")
Flow of execution does not necessarily flow from top to bottom. As functions must be defined before they are executed, each may serve as a detour from the top down flow. Reading programs following the flow of execution is smoother than reading at top to bottom.
Stack diagrams. Shown in a coding block for the fixed-width font. Stack diagrams track variables and their values across modules and functions. <module> line1 "homage" #think the French pronunciation line2 "to orange" function1() part1 "homage" part2 "to orange" part3 "and Omaha" function2() ...
Functions help by:
- Dividing things up so you can name a function, its arguments, and its variables. This makes reading and debugging easier.
- Eliminating repetitive code.
- Allowing you to build pieces and assemble them into a whole.
- Allowing you to reuse well-designed code in other programs.
Python doesn’t recognize tabs, so if your text editor isn’t converting the tabs to spaces, it’ll have run errors.
When debugging, ensure you are calling the most recently debugged version of the program and not the original bugged program.