CS30 - Spring 2015 - Class 8
Example code in this lecture
simple.py
contains.py
Lecture notes
Administrative
- Assignment 2 back soon
- Submit pictures for assignment 2 by the end of the weekend
- Assignment 3 due tonight
- Assignment 4 our soon
- Mentor sessions working?
- Lab issues in 219
What happens when a function is called?
- Look at the simple functions in
simple.py code
- We can walkthrough what happens when we make a call to add_then_double, e.g.
>>> add_then_double(10, 20)
1. The arguments to the function (10 and 20) are evaluated
- they could have been more complicated expressions, e.g.
add_then_double(5+5, 40/2)
2. The function is then called and the parameters are associated with the values that are passed in
- think of it like saying:
a = 10
b = 20
we now have two variables in the function that have the values of the information passed in
- the only tricky thing is that these variables are unique to this function, so even if they're defined globally, they will be different than the global variables
3. The code for add_then_double is executed a statement at a time
- This will in turn call add(a, b)
- like any other function call we:
- evaluate the arguments, in this case, we evaluated a, which gives us 10, and b, which gives us 20
- we then pass these values to add
- these values get associated with parameters, num1 and num2
- add is only one line of code, it adds the number and then returns that value
- num1 + num2 gives us 30 (10 + 20)
- the "return" statement means wherever this function was called, that is the value that call should represent
- the call add(a, b) will then represent the value "30"
- We then assign what add(a,b) returns to the variable "added"
- double is called with what was in the variable "added" (30) and gives us back 60
- 60 is then associated with the variable "doubled"
- Finally, add_then_double returns "doubled", which has the value 60
- If we called add_then_double from the console, we will see the value 60 echoed, not because it was printed out but because that call (add_then_double(10, 20)) now represents the value "60"
- We could just have easily done other things with it, e.g.
>>> add_then_double(10,20) * 2
120
>>> result = add_then_double(10,20)
>>> result
60
>>> add_then_double(add_then_double(10, 20), 4)
128
write a method called "contains" that takes a list and an item and returns True if the item is in the list, False otherwise, i.e.
def contains(some_list, value):
- Look at the first option in
contains.py code
- loops through each value in the list
- if it finds one that is equal to the item we're looking for, then return True
- remember that return immediately exits the function
- this will stop the loop and then give back True from the function
- if we make it through the entire list without finding one that's equal (and therefore exiting the function) then the loop will finish and we'll return False
- this is similar to the isprime function we saw before in
while.py code
- the contains_alternative function in
contains.py code
is another way of accomplishing this
- we use a variable to keep track of whether or not we've found the item
- initially, we set it to False
- we then loop through all of the values
- if we find the item we're looking for, we set the variable to True
- not that this could happen multiple times if the item occurs multiple times, but it's fine to set it to True again if it's already set
- after the loop finishes, we return found
- if we didn't find any, it will still be False
- otherwise, it will have been set to True
"in"
- python actually has the "contains" functionality built-in
>>> 1 in [1, 2, 3]
True
>>> 2 in [1, 2, 3]
True
>>> 5 in [1, 2, 3]
False
>>> "banana" in [1, 2, 3]
False
- the item has to be in the list as one of the individual items, e.g. the following doesn't work:
>>> [1, 2] in [1, 2, 3]
False
but this would:
>>> [1, 2] in [[1, 2], 2, 3]
True
- in works for other things like strings (does a character exist in a string) or tuples:
>>> "a" in "banana"
True
- For strings, you can actually ask if a substring is in a string
>>> "ana" in "banana"
True
objects
- Almost everything in Python is a class of objects!
- ints, floats, bools, etc.
- object have:
- data
- and they also have methods
>>> x = -10
>>> x.bit_length()
4
>>> x.__abs__()
10
>>> (-10).__abs__() # we need parenthesis to avoid confusion with floating point numbers
10
- We can see all the methods associated with an object using "help" just like we did for functions
- for ints
>>> help(int)
gives all of the methods that can be called on an int object
>>> x = 10
>>> help(x)
- for strings
>>> help(str)
- You can also ask help on any object of that type, e.g.:
>>> help("banana")
- for lists
>>> help(list)
string methods
- a few that might be useful (though there are many there)
- lower
>>> s = "Banana"
>>> s.lower()
'banana'
>>> s
'Banana'
- Remember, strings are immutable!
- replace
>>> s.replace("a", "o")
'Bonono'
>>> s
'Banana'
- remember that strings are immutable
- if you want to update a variable after the method has been applied, you can do the following:
>>> s
'Banana'
>>> s = s.replace("a", "o")
>>> s
'Bonono'
- find
>>> s.find("a")
1
>>> s.find("q")
-1
>>> s.find("a", 2)
3
- count
>>> s.count("a")
3
>>> s.count("b")
0