Here are our solutions for the day 13 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Define a exponentiate
function that takes in two numbers. The first is the base, and the second is the power to raise the base to. The function should return the result of this operation.
This function is very similar to the operation functions we defined in yesterday's exercises. We just have to return a value, rather than printing it.
First let's define the function and set up the parameters.
def exponentiate(base, exponent):
pass
The exponentiation operator in Python is **
, so we just need to return base ** exponent
.
def exponentiate(base, exponent):
return base ** exponent
Remember that the order of the operands matters here. You can't do this:
def exponentiate(base, exponent):
return exponent ** base
2) Define a process_string
function which takes in a string and returns a new string which has been converted to lowercase, and has had any excess whitespace removed.
We've done a lot of string processing at this point, so this should be a fairly simple exercise.
First, let's define the skeleton of your function again. This time we're just taking in a single parameter, which I'm going to call raw_string
.
def process_string(raw_string):
pass
Now I'm going to call strip
and lower
on this raw string. We can return the result in the same step:
def process_string(raw_string):
return raw_string.strip().lower()
3) Write a function that takes in a tuple containing information about an actor and returns this data as a dictionary.
In this case we're told to expect data in the following format:
("Tom Hardy", "English", 42)
So, first we have a name, then the actor's nationality, and then their age.
Once again we need to define a function that takes in a single parameter. I'm going to call my function dictify
and the parameter is going to be actor
.
def dictify(actor):
pass
Next I'm going to destructure the tuple so that we can make use of nice variable names when constructing our dictionary.
def dictify(actor):
name, nationality, age = actor
Finally, I'm going to create and return the new dictionary:
def dictify(actor):
name, nationality, age = actor
return {
"name": name,
"nationality": nationality,
"age": age
}
4) Write a function that takes in a single number and returns True
or False
depending on whether or not the number is prime.
For this exercise, I'm going to use the following implementation from day 8:
dividend = int(input("Please enter a number: "))
for divisor in range(2, dividend):
if dividend % divisor == 0:
print(f"{dividend} is not prime!")
break
else:
print(f"{dividend} is prime!")
Instead of getting a number from the user, we're going to be provided an argument, so dividend
is going to become a parameter. The function itself is going to be called is_prime
.
def is_prime(dividend):
pass
Now instead of printing whether or not the dividend
is prime, we just need to return True
or False
.
def is_prime(dividend):
for divisor in range(2, dividend):
if dividend % divisor == 0:
return False
else:
return True
Note that we no longer need the break
statement, because the return
keyword already ends the function execution, and therefore the loop.
We also don't really need the else
clause anymore either.
def is_prime(dividend):
for divisor in range(2, dividend):
if dividend % divisor == 0:
return False
return True
A few things we do need to watch out for are the numbers 1
, 0
, and any negative numbers. These numbers are all valid values for our parameter, but they will give erroneous output.
A simple conditional statement can catch these problematic numbers.
def is_prime(dividend):
if dividend < 2:
return False
for divisor in range(2, dividend):
if dividend % divisor == 0:
return False
return True