CS150 - Fall 2012 - Class 3
http://www.youtube.com/watch?v=vgEFC8Eb6i4
exercises
admin
- tutor hours Sun-Thur evenings 7-9pm
- make sure to keep looking at the exercises
- keep up with the reading
- general lab/assignment schedule
- Wednesday I'll post the lab prep for the week. This will be due Friday at the beginning of lab.
- Friday you will start work on the assignment for the week. The assignments will generally be due before class on Wednesday.
- Use the examples in the notes and in the book to help you figure out the assignments
print vs. return
- print
- the print statement prints the value to the screen/shell
- return
- a return statement has two parts:
return [expression]
- when the program gets to this line, it evaluates the expression. Whatever value this expression evaluates to then is "returned" from that function and represents the value at where the function was called.
- Consider the following two functions (found in
print_vs_return.py code
):
def print_square(number):
print number * number
def return_square(number):
return number * number
- They do similar things, but their behavior is VERY different
- What would be printed out if the following was typed into the interpreter after defining these functions?
print_square(10)
return_square(10)
x = print_square(10)
x
y = return_square(10)
y
- the first two statements appear to do the same thing, but it is different. print_square is actually printing to the shell inside the function. return_square(10) evaluates to 100, then that value is printed because the default behavior for the shell is to print the value
- This difference is highlighted in the next 4 statements
- when we call print_square, it prints out the value, but does NOT return a value. Therefore, x remains undefined
- when we call return_square, it will NOT print out the value, because it is part of an assignment statement. However, if we look at y, it gets the value returned.
- What would happen if we added the lines below to the end of a file and the run it?
print_square(5)
return_square(5)
print print_square(5)
print return_square(5)
- The first call to print_square prints 25
- The first call to return_square does NOTHING. It returns a value, but then we don't do anything with it (just as if we'd typed 5*5 there)
- The second call the return_square print 25 again, then when we return we try and print out the value that was returned from print_square. Since print_square does not return a value, we get "None"
- Finally, we print out another 25 for the 2nd call to return_square. In this case when the value is returned, we print it out.
- other differences
- return also affects the flow of the program. When you hit a return statement, that indicates to the program to leave the function
strings
- strings are another type (just like int of float)
- They represent text
- You can either use double quotes "this is a string" or single quotes 'this is also a string'
- but you can't mix the two for any given string 'this is not a valid string"
- if you want to concatenate two strings you can us the plus sign
>>> "this is one string " + "plus another one"
'this is one string plus another one'
- If you want to include an int or a float in a string, you need to convert it to a string (just like int to float)
>>> x = 4
>>> "The value of x is " + str(x)
'The value of x is 4'
advanced bbqing
- if we look back at our bbq code, we notice that we only need information from some of the people to calculate the number of hot dogs
- Who?
- only tim and amy affect the number of hot dogs required
- look at the hotdogs method in
bbq-functions.py code
- What does this function do?
- Any things we haven't seen before?
multi-line strings
- So far we've seen double quotes and single quotes for strings
- If we want a string to span over multiple lines we have a few options
- there is a special character '\n' that represents the end of the line
- we can put this in a string
>>> print "This is a string\nthat spans over multiple\nlines"
This is a string
that spans over multiple
lines
- drawbacks of this approach?
- hard to read as a human
- hard to get formatting/alignment right
- if it's a long string (e.g. a paragraph) it's going to go off the screen
- pain to copy and paste multiline text from somewhere else
- Python also includes multiline strings
- Use triple quotes (or triple single quotes) around a multiline string
>>> print """This is a multiline string
... I can continue to type
... over many different lines
... and it won't stop until
... I close the strings"""
This is a multiline string
I can continue to type
over many different lines
and it won't stop until
I close the strings
- notice that until we close/end the triple quotes, Python continues to prompt us with "..." to let us know that we're still in the middle of something (at least at the shell)
docstrings
- So, the long string after the function definition of hotdogs is a multiline string
- Any idea why we put it here?
- We can put a string immediately following the definition. This string is called a "docstring".
- The docstring is another form of commenting
- Besides being a useful comment, once we run this program, we can also get the docstring from the shell using the "help" functions
>>> help(hotdogs)
Help on function hotdogs in module __main__:
hotdogs(tim, amy)
Returns the number of hotdogs required for the party.
Parameters:
tim -- the number of hotdogs tim will eat
amy -- the number of hotdogs amy will eat
- This can be VERY useful when you're using code that you haven't written
>>> help(abs)
Help on built-in function abs in module __builtin__:
abs(...)
abs(number) -> number
Return the absolute value of the argument.
- conventions
- We're going to be defining docstrings for ALL functions we write from here on out
- We'll always use triple quotes for docstrings (even if they're just one line)
- For simple functions, a one line docstring is sufficient
- For longer ones, first give a description of what it does, then describe what each of the parameters represents.
Look at the other functions in
bbq-functions.py code
- Anything unusual? What do they do?
constants
- We have two variables defined at the top of the file, but they're in CAPITAL LETTERS?
- Some variables are special in that their value never changes
- so special that in some languages (though not Python) the language enforces that they can't change
- These are called constants
- To indicate to the programmer that these are special, we use all caps
- Why use constants and not just put the value in the code?
- Makes the code easy to change
- what if the price of soda changes?
- what if we use it in multiple places and then the price of soda changes?
- Make the code easier to read/understand
- We'll be using constants in this course
bbq functions
- The rest of these functions are fairly straightforward
- notice that we can build up more complicated functions by using the simpler functions
- don't forget that if you want to print a string and and int/float, you need to convert the int/float to a string using str
scope
- The "scope" of a variable is the portion of the code we can use (reference) that variable and have it be valid
- When we declare a variable in the interpreter, what is its scope?
>>> x = 10
- the scope is any shell statements/expressions typed after it
- When we declare a variable (outside a function) in a file, what is its scope?
- anywhere below it in the file
- remember, running a program is very similar to typing those commands into the shell
- When we declare a variable inside a function, what is its scope?
- anywhere below it in the FUNCTION
- notice, for example, that if we run the bbq-functions.py file and then type todd the variable is undefined
>>> todd
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
NameError: name 'todd' is not defined
- this is called a "local" variable, since it's scope is local to the function
- it doesn't matter if we run the function or not
>>> hotdogs(1, 4)
25
>>> todd
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
NameError: name 'todd' is not defined
- the variable's scope is only local to the function
- What is the scope of the parameters?
- anywhere in the FUNCTION
- it doesn't really make sense outside of the function since we wouldn't have a value for it
- Why do programming languages limit a variable's scope?