Notes: Recitation 2
From 6.00 reference wiki
Contents |
Python Control Flow
You should be familiar with the way Python evaluates basic code. We will be moving on to more advanced concepts in the coming weeks.
The following animation/PDF below covers some simple code examples, demonstrating in detail exactly how the Python interpretor will step through your code.
If you want to play with the examples, use the following Python files:
Scopes / Namespaces
When Python encounters a name, it must look up what value that name refers to. A namespace (or scope) is a binding of names to values.
For now, this is what you need to know about namespaces (this is a simplification):
- An assignment statement creates a binding in the namespace:
x = 0 # binds x to the value 0 (see slides)
- A def statement creates a binding in the namespace:
def greet(): # when executed, def binds the name greet to this function definition. (see slides) print "Hello"
Global and Local
- There is a global namespace. Any names (variables or functions) you define at the top indentation level of your file are in the global namespace.
- Each function-call has its own namespace (see slides).
- A function-call can never access the namespace of any other function-call.
- Any assignment statement inside a function, can only modify a local variable. The first assignment statement for a name, inside a function, creates a local binding for that name. (This variable is accessible only-and-only from inside this function). (There is a way to avoid this behaviour, using the global keyword, but we're not going to talk about that for now.)
- When you use a name in an expression, inside a function, Python will first look in the function-call's local namespace for the value of that name. If no binding exists in the function's namespace, Python will look in the global namespace. If no binding exists in the global namespace, you will get an error.
Useful Python stuff
Modulo operator
Simply put, given two integers, a and n, a modulo n is the remainder after numerical division of a by n. So, for example, 4 modulo 3 is 1, 5 modulo 3 is 2, and 9 modulo 3 is 0.
In Python you can use the % operator with two int values to get the modulo.
>> 9 % 3 0 >> 5 % 3 2 >> 4 % 3 1
Suppose you wanted to see if the value of the (integer) variable x was divisible by the value of the (integer) variable y, you could use the following if statement:
# x and y are integers if x % y == 0: print "divisible" else print "not divisible"
This displays 'divisible' if y is a factor of x, or 'not divisible' otherwise.
The abs operator
The abs operator gives you the absolute value of its numerical argument (it discards the sign, returning only the magnitude).
>> abs(-1) 1 >> abs(1) 1
It works for both floating point and integer numbers.
>> abs(-3.1452) 3.1452
abs can be particularly useful when you want to know the difference between two numbers, but you don't know or care which one is larger.
# x and y are numbers diff = abs(y - x)
If we didn't use the abs operator, we would have to do something like the following to calculate diff:
diff = y - x if diff < 0: diff = -diff
Printing on the same line
Normally, the print statement displays what you tell it to, and moves to the next line. The next print then starts on the new line. Sometimes you want two print statements to display something on the same line. By putting a comma (,) at the end of a print statement you tell it to display the argument without moving to a new line. So, for example:
print "Hello", print "world", print "is a", print "boring example"
Displays:
Hello world is a boring example
Or a better example:
for x in range(10): print x, print
Displays
0 1 2 3 4 5 6 7 8 9
We need the print outside the loop to tell Python to move to a new line, otherwise it will keep printing after the 9, until it runs out of space.
Programming Exercise: Nested Loops
Write a print_matrix function that does the following:
def print_matrix(start, width, height): """" Prints a matrix of numbers width columns 'wide' and 'height' columns high. The top-left entry is equal to 'start'. Keeping the row constant, numbers increase by one as we move bone column left. Keeping the column constant, numbers increase by 'width' as we move one row down. width, height: int > 0 start: int Sample output for start=10, width=3, height=3: 10 11 12 13 14 15 16 17 18 """ # ...
For example:
>>> print_matrix(10, 5, 5) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
You can write a for loop inside a for loop for this.