def fence(original, wrapper):
new_string = wrapper + original + wrapper
return new_string
Your pseudocode might look like:
Python will first execute the function add with a = 7 and b = 3, and, therefore, print 10. However, because function add does not have a line that starts with return (no return “statement”), it will, by default, 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:
10
None
If we want to return the value rather than print it we must replace print in our function with return.
2003/2/12018/11/213 7 is the correct answer. 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
a = 3
b = 7
def swap(a, b):
return b, a
a, b = swap(a, b)
print(a, b)
This reverses the order of the parameters when they are returned and does swap the two values.
Your function should look something like:
def first_negative(values):
for v in values:
if v<0:
return v
What happens if you pass an empty list is passed to this function? What happens if all the values are positive?