Practice Problems - Day 1

Please solve these problems in the empty cells provided after the problem.

Problem 1.1: Write a function double to doube given number.

>>> double(5)
10

What happens you pass a string as argument to this function? Try to understand why works that way.

In [1]:
# your code here

def double(x):
    return x*x
In [2]:
# try for a sample input
double(5)
Out[2]:
25
In [ ]:
 
In [ ]:
 
In [ ]:
 

Problem 1.2: Write a function average that takes a list of numbers as argument and returns average of all those numbers.

>>> average([1, 2, 3, 4])
2.5

Hint: The built-in function sum can be used to compute sum of a list of numbers.

>>> sum([1, 2, 3, 4])
10
In [3]:
# your code here

def average(numbers):
    return sum(numbers) / len(numbers)
In [4]:
# try it out
average([1, 2, 3, 4])
Out[4]:
2.5
In [ ]:
 
In [ ]:
 
In [ ]: