Solutions

Combining Strings

Your pseudocode might look like:

  1. Define function with two parameters original and wrapper
  2. Combine the parameters into a new string the order: wrapper, original, wrapper
  3. Return the new string

As for the function code, you can see it below:

In [20]:
def fence(original, wrapper):
    '''
    Takes a string, original, and a character (length 1 string) wrapper.
    Returns a new string which has the character in wrapper at the start and end of original.
    '''
    new_string = wrapper + original + wrapper
    return new_string
print(fence('name', '*'))
*name*

Return versus print

Python will first execute the function add with a = 7 and b = 3, and, therefore, print 10. However because the function add does not have a line that starts with return (no return “statement”), it will return nothing which, in Python world, is called None. Therefore, A will be assigned None and the last line (print(A)) will print None. As a result, we will see:

In [21]:
def add(a,b):
    print(a + b)
A = add(7, 3)
print(A)
10
None

If we want to return the value rather than print it we must replace print in our function with return.

In [22]:
def add_return(a,b):
    return a+b
A = add_return(7, 3)
print(A)
10

Calling by name

The outputs produced are

In [24]:
print_date(day=1, month=2, year=2003)
print_date(month=11, year=2018, day=21)
2003/2/1
2018/11/21

Using named arguments can make code more readable since one can see from the function call what name the different arguments have inside the function. It can also reduce the chances of passing arguments in the wrong order, since by using named arguments the order doesn’t matter.

The Old Switcheroo

3 7 is the correct answer, as we can see if we run the code below:

In [25]:
a = 3
b = 7
def swap(a, b):
    temp = a
    a = b
    b = temp
swap(a, b)
print(a, b)
3 7

Initially, a has a value of 3 and b has a value of 7. When the swap function is called, it creates local variables (also called a and b in this case) and trades their values. The function does not return any values and does not alter a or b outside of its local copy. Therefore the original values of a and b remain unchanged.

If we want to swap two values in Python we need to use something like the following

In [26]:
a = 3
b = 7
def swap(a, b):
    return b, a
a, b = swap(a, b)
print(a, b)
7 3

This reverses the order of the parameters when they are returned and does swap the two values.

Find the First

Your function should look something like:

In [27]:
def first_negative(values):
    for v in values:
        if v<0:
            return v
print(first_negative([0, -1, 1, -2, 2]))
-1

Now if we pass an empty list [] into first_negative, we get None as an output:

In [28]:
print(first_negative([]))
None

This is because there are no items in the empty list [], and thus the loop doesn't run over any values (and so the code in it's body never runs). Hence, the return v statement is never reached, so Python defaults to returning None once the function finishes running.

A similar thing will happen if all the values are positive:

In [29]:
print(first_negative([1,2,3,4,5]))
None

This time, the for loop can loop over each of the values in the list, but because none of them are negative, the condition v < 0 is never True. Thus, the body of the if statement is never run, and so return v never happens. So first_negative returns the default None again.