Class 7 - Notes
Upcoming Schedule
Due now: Project 1
Before Wednesday, 10 February:
Complete Udacity cs101 Lesson 3: How to Manage Data (Notes) and Lesson 3: Problem Set
Due on Monday, 15 February:
Project 2 (will be posted in a few days)
Before Friday, 19 February:
Udacity cs101 Lesson 4: Responding to Queries (Notes) and Lesson 4: Problem Set
Python Tutor
One great resource for understanding python code is Philip Guo’s Python Tutor. It lets you step through Python code forwards and backwards, and visualize what is going on.
Slides
Binary Numbers and Rules of Evaluation
Binary numbers are base 2, instead of the base 10 decimal numbers we
commonly use. This means we can represent any number using only 0s
and 1s, and the value of each Bit scales as a power of two (so
instead of having a "ones" place, "tens" place, "hundreds" place,
"thousands" place, we have a "ones" place", "twos" place, "fours" place,
"eights" place, etc.). In Python, a number literal that starts with
0b
is interpreted as a binary number:
>>> 0b10 2 >>> 0b101 5 >>> 0b1120 SyntaxError: invalid syntax
What is the value of 0b11111111
(as a decimal number)?
BinLiteral ::= 0bBinDigits
BinDigits ::= BinDigits BinDigit
BinDigits ::= BinDigit
BinDigit ::= 0
BinDigit ::= 1
Show how to derive 0b101 with this grammar starting with IntegerLiteral:
Provide semantic rules for the grammar that give the value (as a decimal number) for every BinaryLiteral:
Value(BinDigit) =
(4) BinDigit ::= 1
Value(BinDigit) =
(2) BinDigits ::= BinDigit
Value(BinDigits) =
(1) BinDigits ::= BinDigits BinDigit
Value(BinDigits) =
Test Grammar
Here is a simplified excerpt of the Test grammar from https://docs.python.org/3/reference/grammar.html.
NotTest ::= not NotTest
NotTest ::= Expression
Expression ::= True
Expression ::= False
0x11 < 0x011 True and False and True 0b11 < 0b100 > 0b101 not not not not not not False not not not not False and TrueYou can try evaluating them in the Python interpreter to check your answers and see how they evaluate.
Develop rules of evaluation for the grammar above that matches how things are interpreted by the Python interpreter.
A voice from the back of the room piped up, "Yeah, right."