Hello everyone and welcome to the Python crash course exercise solutions lecture and this lecture I'm
just going to be working through the solutions notebook and explaining the code and the solutions.
No but let's go ahead and jump to the exercise notebook so we can actually begin coding this out.
All right.
Here I am at the crash course exercises.
The first question we wanted to answer is what is 7:2 to the power of four.
Now there's technically two ways you could of done this.
You could've just typed seven times seven times seven times seven and gotten the correct answer.
But hopefully remember the correct way which is just calling to Asterix for exponents.
So seven to the power of four.
Run that cell and there is your answer.
Next up we want to split the string S is equal to.
Hi there Sam into a list we can go ahead and just grab this and we can see the example output lists
we want to actually create there.
So if I run a sequel to hi there Sam and call estop splits this will automatically split the string
into a list on the whitespace.
I remember we can actually pass any string character for example to what we actually want to split on
which we actually see that later on in another exercise.
Let's go into the next question which was given the variables plena equals earth and diameter is equal
to twelve thousand seven hundred forty two.
Use that format to print the following string.
The diameter of Earth is twelve thousand seven hundred forty two kilometers.
And go going to run the cell so we have these two variables to find and then I will say in a string
the diameter of Earth is told thousand seven hundred forty two kilometers.
Remember I want to use that format and I want these these two variables.
So I will replace earth with just a pair of curly brackets and also do the same for this diameter number
then I'm going to say that format and I will in planets diameter and I'll go ahead and print the whole
thing.
So it's this works.
And there we have it.
I could have also passed then some variable names here such as A and B and that set these equal to A
and B.
And that's the exact same thing.
So to you when you use that for the first one you have to make sure it's in the right order.
Next up given this nested list use indexing to grab the word Hello.
This is pretty tricky.
So you're going to take your time with this and start from the inside to the outside.
You can see here that we have hello.
And that's an a list and Jupiter and up it kind of helps you out with the bracket highlighting then
this is also inside of a list which is inside of another list which is inside of the most outer list
.
So this whole list right here is what we want to grab first and let's located at index 3 0 1 2 3.
We'll go ahead and grab that.
Now that I have this list I can see that the list that has my target word hello is the second item in
the list meaning it's an index one.
And now I'm going to break this down again so 0 1 2
and then I have a list of one element.
So I can go ahead and just grab that again for zero.
And then finally I have the string hello OK.
This is quite tricky and you'll never really have a situation like this in real life.
But when I work with pandas you will have to do things that are two levels deep.
So just keep that in mind.
All right.
Then the next question was given this nece dictionary grabbed the word hello.
Be prepared this will be annoying slash tricky.
So this is pretty tricky.
But let's go ahead and break it down step by step.
First thing you have to do is grab key one.
So if we look at the we can actually just call for the keys themselves.
And we actually only have one key in the most outside dictionary ok one we will go ahead and call that
.
And then now I have a list of some elements and notice that I want to grab this dictionary right here
which is at 0 1 2 3.
So index 3 of that list.
So keep in mind we're kind of mixing list indexing with key value pairs of a dictionary.
And then I have a dictionary again and I want to grab the value at the key.
Tricky.
Then I have a list again and I want to grab this dictionary which is 0 1 2 3.
And then I have a dictionary again and I want to grab a key target.
And then finally I have a list and I want to grab that last element 0 1 2 3.
And that is hello.
OK.
Quite tricky quite hard.
So don't worry too much if you weren't able to figure this out quickly because it is quite annoying
.
Sless tricky but hopefully you are able to actually understand the logic of this solution.
Next question is what is the main difference between a tuple and a list.
Well a tuple is immutable and a list is mutable.
That's the main difference.
After that we wanted to create a function that grabs the email Web site domain from a string in the
form user at domain dot com.
So for example passing user out domain dot com would return domain dot com.
Let's go ahead and create a function called domain gets.
It will take in an e-mail variable which will be a string and then we will return e-mail that splits
and we'll split on the symbol.
Let's go in and see how that works.
If I just run that.
OK great.
So if I just run that I end up getting split on this at symbol and I get a list back.
I want the second item in that list meaning I want to go ahead and grab index one on top of that.
And then if I run this I get the right answer.
That domain dot com.
Next up we want to create a basic function that returns true.
That's the word dog is contained in the input string.
And again don't worry about edge cases like punctuation being attached.
The word dog well we should account for capitalization or go ahead and do is create a function.
The fourth find dog takes in some variable and go ahead and call the variable S and then we will say
return we'll check if dog is in S in order to account for capitalization.
I will go ahead and lower everything in S and then I will split it so that it actually turns into a
list of objects and then when I run this it ends up being true.
Is there a dogs there.
Let me go in and show you what's actually happening when I passen this string.
I'll say s is equal to.
Is there a dog here.
Run the.
And then if I call estopped lower I will lower case everything and then if I say estopped split after
that lower I get this list and then I'm finally just checking is dog in that list.
So those are the steps that I'm performing in this one line statement that I want to create a function
that counts the number of times the word dog occurs in a string.
And again we can ignore edge cases.
We'll go ahead and say the F create a function called Count dog ticks and a string as t will set our
initial count to 0 and we'll say for word in and again will say s t.
Lower split in the same way we did for the last question and then colon if that word is equal to dog
we will go ahead and add 1 to the counts.
So say count is equal to count plus 1.
So you could say count is equal to count plus 1 or for shorthand.
What some people like to do is just say plus equals 1.
And that's the exact same thing.
Then finally I'll just return at the very end.
My count which is the number of times I saw that word dog.
Now if we go ahead and run this we get the correct answer here.
Next we wanted to use lambda expressions in the filter function to filter out words from a list that
don't start the letter S.
For example we have CQ is a list of words that should be filtered down to soup and then salad Let's
go and see how we can do this.
I'm going to say filter and then I get to use the land expression.
It's going to take in a word and it's going to check if the first elements in that word string is equal
to S and then not going to pass a sequence and then finally I will cast the whole thing as a list.
And that's the answer.
Soup and then salad.
So again for philtre this lambda expression or function the pudding what you passen needs to return
a boolean value and we go ahead and filter out the sequence based off these boolean values.
All right.
Here we are at the final problem.
The problem statement is this.
You're driving a little too fast and a police officer stops you.
You'll need to write a function to return one of three possible results.
No ticket small ticket or big ticket.
If your speed is 60 or less the result is no ticket.
If the speed is between 61 and 80 inclusive The result is small ticket.
If the speed is 81 or more the result is big ticket and the less it is your birthday which is encoded
as a boolean value in the parameters of the function on your birthday your speed can be 5 higher.
In all cases.
Meaning if it is your birthday.
So if is underscore birthday is true than the speed limit is raised five miles or five kilometers per
hour for you.
OK let's go ahead and get started.
We will delete this past statements.
First thing I want to do is check if it is someone's birthday and adjust the speed accordingly.
Will say if it is birth they
will say speeding is equal to speed minus five and then else.
Speeding is just equal to speed and then we'll go ahead and check now against speeding the conditions
for the tickets.
So again if speeding is greater than 80 then that's a big ticket.
So I'll go ahead and return the string big ticket.
Then we're going to say if speeding is greater than 60 Well that's going to then return a small ticket
and otherwise we'll have.
Else which would just be a return ticket.
And then we can go ahead and run this.
So 81 would usually be a big ticket but since it's your birthday we'll say it's true.
That should be a small ticket.
And there we have it and 81 false Well that's definitely a big ticket.
All right.
Hope you enjoyed the solutions.
Feel free to post any questions you have to the Q&A forums and I'll be happy to help you out there.
Thanks everyone and I'll see you at the next lecture