Hello everyone and welcome to part three of the Python crash course.
Let's go ahead and jump right back to Jupiter notebook where we left off last time.
All right.
Here I am at the Jupiter notebook.
Last time we left off on if elif and L statements.
Now we're going to go and quickly cover for loops and while loops for loops allow you to iterate through
a sequence I'm going to make an object called S-C Q For sequence and say it's one two three four five
.
So it's just the list of numbers.
And then I can use a for loop to perform or execute some block of code for every single item in that
sequence.
Meaning you can do something like this for.
And that's the key word here.
For item in and then that object that's a sequence as the queue perform or execute some block of code
.
In this case that's going to be prints item.
And now I've been able to print the item for each element in that sequence something to note here is
that this temporary variable name item can be whatever you want it to be.
So for instance I can go ahead and replace this with something like jelly and it will still work.
So for jelly in sequence or in a CQ print jelly and there you have it it's the exact same thing.
So you probably want to choose these words or temporary variable names.
Carefully here.
These are numbers.
So a good choice might be something like and an M for number and that way when it come back this code
at some later date I can remember what I'm actually performing or doing here something to keep in mind
is that this block of code that executes it doesn't have to be actually related to the items inside
of the sequence.
You can do anything you want here so I can say prints hello.
And that's just going to go and say OK for every element in here I going to perform this action meaning
I'm just going to print Hello five times.
Now let's go ahead and show you how you can use a while loop to continually perform an action until
some condition has been met.
I'm going to go ahead and type I is equal to 1 and then say while I is less than five colon and then
I'm going to perform some block of code while this condition happens to be true and I'm going to go
in and say print I is colon curly brackets and then use that format to so passen either there.
Now don't run this quite yet because look what's going to happen I set eyes equal to one and then I
said OK well I'm less than five.
Go ahead and print this.
If I just run this well I will get an infinite loop meaning the cell would just go forever because I
will always be less than five.
If you happen to have a cell it's accidently only running forever and you'll notice either by continually
getting output or by having an asterix here instead of a number for a very long time.
What you should do is go to Colonel and then click restart in order to avoid this issue.
Going to say why is equal to plus 1.
And now when I run this I execute this block of code.
So we start off with I is one which was the first value we gave it.
Then we printed eyes 1 and we added 1.
I then the while loop again checked ok as to less than five.
Well that's true.
So print's eyes to add that add 1 to it is three less than five.
Well that's true.
Prince has three.
Add 1 to it is for less than five.
That's true.
Prince is for add 1 to it.
And then finally is five less than five.
Well that is not true.
So I went ahead and just finished and broke out of that loop.
And that's how a while loop works.
It's basically going to execute some block of code while some condition happens to be true.
OK let's go in and discuss a few more useful functions that are inside of Python.
One really useful one is range.
A lot of times you're going to want to do a for loop something like this for x in the cue for sequence
print X or remember as Q was just this one two three four five.
A lot of times you're going to want to do something a certain amount of times.
Now one way to do that is say for x in and then make a really long sequence but you don't have to want
to type that much.
So Python has a range function and range is going to be a generator of numerical values so if you say
range you'll start with the number you want to start at and the number you want to end that if you run
that you get this range object.
And then what you can do is say for x in range from zero comma 5 colon prints X and then you will see
0 1 2 3 4 and it is kind of a shorthand way to generate those values.
It's a generator if you actually want this to be a list you would pass this in to the list function
and that converts this into a list.
Now I see a list of 0 1 2 3 4 and this is kind of a very quick way to actually execute something a certain
number of times.
So you can say then even at just one number range of 10 and by default the starting number is zero.
So now this goes all the way up to 10 elements.
But since it starts at zero it will end at nine.
The reason being because 0 1 2 3 4 5 6 7 8 9 10 this is 10 elements.
All right.
That is the range function.
Now let's go ahead and discuss list comprehension list comprehension allows you to save a bunch of writing
when you're trying to create a for loop to create a list.
Let me go ahead and show you an example of using that for a loop and then I'll show you an example of
list comprehension in action.
I'm going to do this I want to say x is equal to a list consisting of the numbers 1 2 3 4 and then I'm
going to make an empty list out which is just a set of brackets so it's an empty list there's nothing
in there.
And then I will say for item in X and I'm going to go ahead and make this clear by just changing item
to numb.
So for now I'm in X I'm going to take my empty list out and I'm going to pends numb to that power of
two.
I run that cell and then I'm going to go ahead and print out that was that empty list.
And notice what it has here.
It basically has every number from X squared 1 4 9 16 instead of 1 2 3 for this sort of operation is
so common in Python where you're continually appending a value to a list that Python has something called
list comprehension and it's basically a way of instead of having to define a list and continually append
logic to a list and use a for loop with Colon indentation.
You can actually type all of this essentially into a set of brackets and quickly create a list.
Let me go ahead and show you how to translate this for loop code into list comprehension lets comprehension
is kind of a for loop.
But backwards you end up saying what you want item is going to call it numb.
So the power of two for numb in and then x number x is this list.
And this is essentially the same code here.
So when I run it it ends up working because it's all inside of brackets.
Let me go to break down these steps again instead of saying this out append and this out empty list
you'll just say something like.
Out is equal to and then a set of brackets.
And then you can translate this for loop into a list comprehension by breaking it down in the following
manner.
You'll grab everything before the colon.
So for now I'm an X Copy and paste that.
And then you will grab whatever we actually want to a pen.
In this case that's number squared and put it in here before that for loop and this is it basically
list comprehension you're saying Give me the numbers squared for nothing in X and now you can see that
output has been cleared in one line so the list comprehension is just a way to save some typing for
you if you're confused by the syntax here or just don't like it.
And this is more readable for you.
Go ahead and just use for loops of the pen for beginners with Python.
This sort of syntax tends to be hard to read at first but as you gradually get better and better of
Python you'll usually switch over to list comprehension just to have things in one simple line.
OK.
Now let's go ahead and talk about functions functions allow you to basically not have to continually
write code over and over again you can just write it inside of a function and then call that function
itself.
The keyword for a function is DPF or Def and DCF allows you to define a function you'll say DPF space
the name of your function.
So go ahead and call this my underscore func and functions should start with lower case letters.
You passen the parameters you want such as Parama one colon and then you'll go ahead and execute some
sort of code so we can say go ahead and Prince Coram one.
OK.
This is a very simple function.
We just call now my phunk passes the parameter such as Hello and it will run and prints the parameter
.
And that's my phunk.
Let me go in and show you a more complicated example.
Well go ahead and say my funk and a set of parameters.
It's going to take a name and we'll say Prince and we'll say hello and we can use a plus sign of a string
in order to concatenate two strings together.
So this will say print Hello plus name.
Go ahead and run that.
And now for my flunk I'm going to pass on a name.
It's going to Jose and now returns back.
Hello Jose if you want a default value for one of these parameters so a default name you can just say
equals and then whatever the primary you want to be the false name.
And if I run that again and if I don't pasand anything to my phunk I will get.
Hello.
Default name only when I say Jose or some other name in there.
Will it replace the default value with the value I specified.
You can also say to more fully explain what you're passing into the functioning and say name is equal
to and then the new value.
Or as long as you put the values in the correct order.
Doing it in this fashion of just one piece of just Jose like this is also fine.
Something to note here is if you just call with parentheses my phunk it will execute the function.
If you get rid of those parentheses you will not call the function.
You will just ask Python hey what is this object.
And it will report back.
Oh that's an object.
A main function called my underscore func.
So keep that in mind there's a difference between calling my underscore phunk like this.
This is the actual function closing the princes off of it actually executes that function.
Now right now we should very simple functions that just print out a statement but a lot of times you'll
actually want to return a value so you can set it equal to another variable.
Let me show you what I mean by that.
Well go ahead and say DPF and create a function called Square that takes in some and U-M some number
colon and then it will return that number.
It won't just print out the result or return the square of that number and you can use the keyword return
in order to do that.
So you'll return numb to the power of two.
And so then I can say output is equal to square of two.
And then when I go ahead and check what output is it's for.
And that's the difference between printing something and returning something.
When you returned this you're going to want to set it equal to some other variable.
And that way you can call the results in that variable versus when you're printing something you're
just going to print a value but not actually return it or store it anywhere.
Finally something you should know about functions is that they can have documentation strings you can
add the documentation string to a function by putting in a set of triple enclosing quotes and tripling
closing quotes.
Basically allows you to put in a giant string over multiple lines so we can say this is a docstring
and this is essentially just a really long comments.
So this is like multi-line comments.
You can go multiple lines and we'll go ahead and say this function squares a number.
So I run that.
Now I have the documentation string inside square if I just run Square again nothing is going to change
what will change is when they actually call the documentation string for more information about this
function.
Jupiter notebook has a really great ability to do this.
You start typing the function you wants and then move your cursor at the end of that function.
You click shift tab and you will get a signature call of the actual function.
Meaning what the function expects to receive.
And then you can expand this to get the documentation string.
And it says this is the docstring can go multiple lines.
This function Square is a number and that's going to be really useful not just so much for your own
functions but for functions that are already built into the libraries that we're going to be learning
.
For instance if we go back to when we were discussing range here you might think it's a little weird
and you might have to memorize stuff as far as what numbers go where.
Well if you're using you Brunow book you can just type in range shift tab and notice it gives you the
signature and then it also has the docstring where you basically say range stop.
And it gives you a range object or you can say range and then start and stop.
And it gives you more information like returns an object that produces a sequence of integers etc. and
has a lot more information there for you.
And this is the sort of thing we're going to be using all the time throughout this course.
This quick access a documentation strings.
All right.
Well go ahead and end it there.
We're going to discuss in the next section of this lecture series Arlanda expressions using map and
filter and methods that you can use on common data type objects.
All right.
Let me go out and quickly review everything that went over in this lecture.
We went over how to create a function.
The key word is if you pass in the name of your function and then you can pass in the parameters your
function expects to get.
And then the documentation string for them you want to use return if you want to actually return the
value of that function.
Remember the difference between calling the function of parentheses or without the function with parentheses
.
You can just get the function itself.
We also learned about miscomprehension which kind of breaks down the sort of for loop into a single
line inside of these brackets.
Then we learned about range which allows you to quickly create a list of a sequence of integers from
a start to a stop point.
We learned about four loops and while loops as well.
All right thanks everyone and I'll see you at the next lecture