Log in

No account? Create an account

Previous Entry | Next Entry

Video notes

Lec 2 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008 - 50:49

In programming we're trying to describe processes. To write programs we need representation of fundamental data and a way to give instructions to manipulate it. We saw two kinds of data: numbers and strings. We'll introduce booleans. Associated with every primitive value was a type. We have a value and a type. We want to combine them and we do so in expressions. Expressions are formed out of operands and operators. If we type it into an interpreter it evaluates and prints. Script is a word for program. Values aren't always printed. Inside of a script there's no print unless it's explicitly done. 3+"ab" is a syntax error. Type conversion is important. The previous was a static semantic error. The syntax is okay, operand operator operand. Python is doing type checking. Why didn't it assume you wanted them both to be treated as strings? That could be dangerous, type checking is good. Some languages have weak others have strong typing. Lisp is weakly typed language, python is on the strong side. a < 3 results in False. That's a boolean. 4<"3" is True because 3 was a string not a number. We must exercise type discipline. "3" is probably less than the number 4 because of ASCII ordering. 9/5 = 1 because it's integer division. To get remained we do 9%5 = 4. 3+4*5. Python has operator precedence. The result would have been 23. We can do (3+4)*5 to get the other order.

Variables get values from assign statements. x=3*5. Python creates a binding for that value. We are creating a link, or a pointer, from that name, X, to that value, 15. We could have y=15. That makes binding from Y to that same value. We could then have z=x. Type of variable is inherited from it's value. If x=3 then x is an integer because 3 is an integer. The variable bindings are dynamic, it changes depending on the current value. If somewhere later we define x="abc", x turns into a string. A rule of good style is not to change types arbitrarily. Variables can be used any place where it's legal to use a value. Statements are legal commands that Python can interpret. such as print, assignment of values. Comments start with #. Good programming style is putting comments to explain pre conditions, explanations of specific things you're doing, that explain what the code does. Good points of style is choices of variable names. x, y, z are lousy choices. Variable names are great ways of commenting code. Some key words cannot be used as variables, such as keywords. print is a keyword, it's a command, therefore we cannot use it as a variable. The raw_input command takes in a set of characters, numbers or strings. So far we can only make straight line programs, where we execute program lines one by one. Even in real recipes you have statements such as if needed add sugar. These are branching programs, something that can change the order of instructions based on some test, usually value of a variable. if is a conditional.
if (x/2)*2 == x:
print 'even'
else: print 'odd'
The colon at the end of the if statement is important, it identifies a block. Why are there two equal signs? One equal sign is used for assignment of variables, therefore something else must be used. Two equal signs is a comparison.
if :
block of instructions
block of instructions
When designing test cases, we should have a test for each specific branch of the code, to avoid bugs. Boolean combination, we can use and, or, not. Booleans can be either true or false. We now have simple branching. The complexity of the code is constant. Each algorithm has different costs associated with it. In recipes we have something like beat egg whites until stiff. That's called iteration or loops, or recursion. We can do iteration with a while statement
while (x>0):
print x
It's important to enforce input at times.


Future Master

Latest Month

September 2018
Powered by LiveJournal.com
Designed by Tiffany Chow