Introduction to Programming for Business Analytics - Lecture 10: Control Flow and Functions
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 22 | |
Author | ||
License | CC Attribution 4.0 International: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/64411 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Producer |
Content Metadata
Subject Area | ||
Genre | ||
Keywords |
00:00
Computer animationLecture/Conference
00:58
Computer animation
09:32
Computer animation
18:05
Computer animation
26:39
Computer animation
35:13
Computer animation
43:46
Computer animation
53:30
Computer animation
Transcript: English(auto-generated)
00:10
Hi, welcome everyone to the 10th lecture of the Introduction to Programming for Business Analytics class. In today's lecture, we're going to talk about control flow and functions using Python.
00:24
And in particular, we're going to revisit the topics we talked about in the third lecture of this course, where we talked about how we can do while loops, for loops and conditional execution using Julia. And also we're going to talk about the things that we talked about in the fourth lecture,
00:43
where we talked about how we can define functions using the standard form, functions call and the flow of execution, as well as parameterized and null array function, fruitful and void functions, and finally recursive functions. Alright, so let's start by talking about how we can define control flow statements in the generic form,
01:07
or what is the generic form of control flow statements. So in Python, control flow statements have the following generic form, which is a header and a body. So similarly to Julia in Python, the header declares the start and the specifics of the control flow statement
01:26
you're trying to define, and then the body contains the part of the program that's being controlled. However, differently to Julia, Python, or in Python, the header ends with a colon.
01:40
This is an important distinction or an extremely important distinction compared to Julia syntax, as well as that expressions in the body must be indented. So unlike Julia where indentation was optional, here indentation in the body of a conditional or any type of control flow statement is necessary.
02:05
Finally, control flow statements do not have a termination expression. So in Julia, we had this end keyword at the end of every control flow statements. This is not the case in Python. So three important things just to recap, the header ends with a colon.
02:24
That's always the case. The body always has to be indented and we do not have a termination expression at the end of the control flow statement. So as we did in Julia, we'll start with while loop statements. So a while loop statement in Python has the following generic form.
02:45
We have the keyword while in the header, followed by a boolean expression. As I mentioned earlier, we have the colon at the end of the header and then we have the body. And that's pretty much it. For example, if we remember from the Julia part, we had the Julia code for executing a simple countdown.
03:06
And that was in lecture three. We did that first using a while loop and then we did it using a for loop. But for now, we'll start with the while loop and then we'll do the same thing again with a while loop. How did that code look like? So in the first line, we had the countdown, which is a variable that was set equal to 10.
03:25
And then we had the while countdown is strictly greater than zero. We printed the value of countdown and then we subtract one from countdown. And then we end the body of the while loop using the termination keyword. So this is just to clarify, this is in the Julia syntax.
03:44
And now we want to do the same in Python. So how do we do that? All right. So it's pretty much going along the lines of what we did in the previous lecture. So first I'll define countdown equals 10.
04:01
And then here I'll type while countdown is strictly greater than zero. And here I have to put the colon. OK, that's the first important distinction. And once I press enter, the Jupyter notebook is smart enough to recognize that I'm creating a control flow statement. So it immediately creates the indentation.
04:22
And I can start directly typing the body or the expressions that are evaluated within the body of that while loop. So we want to print the value of countdown. So remember, we use the print function. We don't have println here. And I'll just type in countdown. So very straightforward.
04:42
And then in the following line, I'll type in countdown minus equal one. Right. So update operators are also available in Python and we do not have a termination expression. So that's pretty much what we need to do for this task. So I hit or execute the cell.
05:04
I mean, as you can see immediately here, we get the desired result. So if you know, for example, if you try to do something like this, you will get an error. That's because you don't have the appropriate indentation. If I also remove the column here, I'll also get an error because I don't have that column.
05:25
And if I try to add the end keyword at the end of the expression, I still get an error. Of course, the code will evaluate. But once it exits the while loop, it doesn't know how to deal with this end. So it returns an error.
05:41
So just remember those three things. A column at the end of the header. The body has to be indented and we don't have an end keyword in comparison to Julie. So similarly to the while loop, a for loop statement in Python has the following generic forms. So for we have the iterator variable in the iterable sequence that we're going to iterate through.
06:06
Then, of course, the header is followed by a colon and then the body is indented and we do not have an end keyword. So here's how we can implement that same countdown example. But this time we're going to do this using a for loop.
06:24
So let's do the same thing. So I'll first type in countdown equals 10. And then I'll say for i in range between parentheses, I'll type in 10 colon because this is the header.
06:42
And then I print the value of countdown. And then in the following line, I'll type in count down minus equal one. And then I execute cell. Indeed, I get that desired result.
07:03
So here's a question. What values does the iterator variable countdown take in the above example? So here we actually did not use the value of i at all. We just use this for loop as a way to iterate or to create 10 iteration.
07:20
And then in every iteration, we subtract it from the value of countdown. And then we printed the value of countdown or vice or the other way around. But why wasn't or the letter i was not involved at all in the body of that for loop. So we need to know what actually what values this i iterator takes within the for loop.
07:46
And the answer is that it takes the values zero one all the way up to nine. And this is actually because if you remember from the previous lecture, we said that Python is zero based indexing.
08:00
So once we use this range function, which I will elaborate on what it means in a second. And but for now, what we need to keep in mind that because Python is zero based indexing, the values that this iterator variable take starts from zero all the way up to nine. To see this, I'm going to type in for i in range 10.
08:27
Of course, I have the colon to go to the next cell, not execute. So print here. And then execute the cell. So as you can see, you have zero one all the way up to nine.
08:45
All right. So let's start by elaborating a little bit on the iterable sequence of the for loop statement that we've just discussed. In particular, we're going to start by the range function. So in the previous example, the iterable sequence is given by the built in function range, which returns an iterator of integers.
09:09
In particular, the function rain actually has three parameters. These are start, stop and step. This is actually not the case in the example that we've done where we only used one parameter.
09:23
But you will see why that is the case in a little bit. For now, this range function actually has these three parameters where start is the value of the first element in the sequence. Stop is an upper bound on the value of the last element in the sequence.
09:42
And step is a difference or space between two consecutive values in the sequence. So let's take a look at an example how we can use this function and in particular using these three parameters. So first, I will define a value for start. Maybe I can say start equals one.
10:03
And then I will define a value for stop. So I'll set that to 11, for example. And then I will define step to be equal to three. Then in the next line, I'll type in my for loop, which is going to be for i in range.
10:24
Start comma stop comma step. I must not forget my colon. And in the following line, I'll just type in print i. I just want to see how this value i looks like in comparison to the previous example where we only had one parameter.
10:41
So now I'll execute the cell. And as you can see here, we have one, four, seven and ten. So why that is the case. When the value of step is strictly greater than zero, the sequence generated by the function range has the elements start plus step multiplied by i.
11:08
But there is a condition that i has to be strictly greater than or equal to zero. And that start plus step multiplied by i or the values of these elements is strictly less than stop.
11:22
So remember, stop is an upper bound on the value of the sequence. Start is where I'm starting from and step is the space or the difference between two consecutive values in the sequence. So let's actually plug these into the formula that we have here and see what we get.
11:41
So here I start from one. So I know my starting point is one. And I know that step is equal to three in my example. And then in the first iteration, the value of i is always going to be equal to zero. Right. That's because Python is, like I said, is zero based indexing.
12:03
So I start from one plus step multiplied by zero. This guy goes away. So I only have start, which in the first iteration equals to one. Now, in the second iteration, the value of i equals to one, one multiplied by step.
12:22
That's going to be three and then three plus start. That's going to be four. Third iteration, i is going to be two, two multiplied by step. It's going to be six, six plus one. That's going to be seven.
12:40
In the fourth iteration, i is going to be equal to three, three multiplied by three. That's nine, nine plus one. That's equal to ten. So that's the first, the fourth value. If I want to go one step further, which is to assume that i is now equal to, or I'm in the fifth iteration, i now is equal to four. So I have four multiplied by three.
13:05
That's twelve. Twelve plus start is going to be equal to thirteen. But thirteen is strictly greater than the value of stop, which is eleven. And therefore that value will not be part of that sequence.
13:22
So like I said, there are these two conditions where i has to be greater than or equal to zero. And start plus step multiplied by i or the values in the sequence has to be strictly less than stop. So let's take a look at a different example.
13:43
And in this case, I'm going to slightly modify the one that we've just done here. So it's going to be exactly the same. The only difference now is that instead of eleven, I'm going to make this value to be ten.
14:03
So as you can see here, I only printed the first three values of the sequence. That's because the fourth one, as we know from the previous example, is equal to ten. But we know that stop is equal to ten. And the condition is that the values in the sequence has to be strictly less than stop.
14:21
And therefore ten is not part of the sequence. All right. So I mentioned earlier that this is the case when step value is strictly greater than zero. But what happened when the value of step is strictly less than zero or the value of step is a negative number?
14:40
So when the value of step is strictly less than zero, the sequence generated by the function range actually has the same elements in the sense that it's also equal to start plus step multiplied by i, where i is greater than or equal to zero. The only difference is that now we have to keep track until where start plus step multiplied by i becomes less than or equal to stop.
15:06
So the condition that we have here is that start plus step multiplied by i is strictly greater than stop, unlike the case where i is positive, where the condition was start plus step multiplied by i is strictly less than stop.
15:21
So let's take a look at an example. So here I will use the same example as before. I will just tweak it a little bit. So you start from one and I'm going to make step a negative number. And to simplify things, I'm also going to make step one, and it's also going to be a negative number.
15:44
So let's execute the cell and see what happens. All right, so the first observation is that the elements in the sequence of the case where step is strictly greater than zero, the numbers were going in an increasing order, so 147, whereas here they're going in a decreasing order.
16:03
That's because every time I'm going one step backward, here we have negative one. OK, that's the first observation. The next observation is that we continued the iteration or we continued having adding elements in the sequence until we had negative 10.
16:21
So the natural following elements here in this sequence would have been negative 11. But if we check why negative 11 is not included in the sequence, that's because negative 11 here is equal to stop. So once I reach negative 11, I can no longer add that into the sequence and therefore the sequence stopped.
16:45
However, all of these elements satisfy the condition that start plus step multiplied by i strictly greater than stop. So I want to point out the following characteristics of the range function.
17:01
So first of all, that start, stop and step must all be integers. Moreover, if the step argument is omitted, it usually defaults to one. For example, in the case of the simple countdown example I mentioned earlier, we only had one parameter. That's because the step actually defaulted to one since we did not include it in the range function.
17:24
So here we only had for i in range 10, because we did not include or we did not specify a step value, it usually defaults to one. OK, that's the second observation. The third one is that if the start argument is omitted, it defaults to zero.
17:43
This is also the case in the simple countdown example. So the simple countdown example, as you can see, we only had one parameter here, which is 10. And here in this case, 10 corresponds to the value of stop. So we continue until we reach nine and then 10 is equal to the value stop and therefore it's not included.
18:06
In this case, the value of start defaulted to zero and the value of step defaulted to one. Finally, the last observation, if the step argument is set to zero, a value error is raised.
18:23
So if I, for example, try to replicate this same example that we had here, where I, instead of using a non-zero value for the step argument here, and I try to execute the function, you'll see here that I'm getting a value error.
18:44
So I cannot step zero. All right, so as it is the case in Julia, we can also use strings and lists as iterable sequences. So in the following example, we're going to create an iterable sequence using a string. So here I'll have for i in the string, hello, comma, world, exclamation mark.
19:10
And then I have to or I must not forget my colon. And then I print the value of i. So if I execute this, as you can see here, we can print all the characters inside the string, hello, world.
19:25
And we can also do the same if we have a list, for example, for i in the list 1, 2, 100, say 200, 999.
19:43
Again, I must not forget my colon. And then here I print the value of i and I execute the cell. So as you can see, you can also create iterable sequences using strings and lists in Python. Similarly to Julia, nested while and for loops can also be created.
20:03
And this is going to be left as an exercise. All right, so let's talk about conditional execution. So similarly to Julia, conditional execution in Python is done using an if statement, which has the following generic form. So we start where the header starts with f followed by a boolean expression.
20:22
And again, we have that colon. And then the body of that conditional execution statement is indented without having a termination expression at the end. Like I said, the header consists of the keyword if followed by a boolean expression. And the boolean expression is the condition that determines whether the body of the statement is executed or not.
20:47
In particular, if that boolean expression is true, the body will be executed. And if the condition or if that boolean expression is false, the body will not be executed. So in the following example, we check if the value of countdown is even by using the modulus operator, which is the percentage sign.
21:12
All right. So I'll type countdown equals 10. And I will check if countdown modulus operator two.
21:26
That's how we check whether a number is even or odd by checking if the remainder is equal to zero. Or by checking in the remainder when we divide by two is equal to zero. Then I'll type in print that countdown is even and I'll execute the cell.
21:49
As we know, countdown in this case equals 10 and 10 is an even number. Therefore, we can see that the body in the expression has been evaluated. If I change the example to have nine here instead of 10 and I executed the cell, nothing happens.
22:07
That's because nine is an odd number. Again, similarly to Julia, alternative execution, i.e. conditional execution where we have two possibilities is done using the keyword else.
22:22
But it's here followed by a colon. OK, so it's similar to the header. So unlike Julia, we don't have anything followed following the keyword else. But here we also have a colon. So if I type in count down and I set that equal to 10 again.
22:42
So I'll just use the same example we do using the Julia part. So if countdown modulus operator two equals equals zero colon. Then I'm going to print that countdown is even.
23:03
And then here I'll type in else followed by a colon. So this is the most important part is that else here is followed by a colon. And then here I have print. And then between the parentheses, I'll type in that string where I have countdown is odd.
23:23
And now I execute the cell. As you can see here, it says that countdown is even in the first one. That's because we know that the first branch is the one that gets evaluated. If I change the number here to nine, as we can see, it says that countdown is odd because the second branch is now evaluated.
23:45
All right. So as far as chained conditional execution, where we have more than two possibilities. We can do them using the keyword ELIF. So ELIF in comparison to Julia, we had else if so else was written explicitly and then followed by the F with no space between them.
24:06
And everything is in lowercase letters. Here we just have ELIF. And then the ELIF is also followed by a boolean expression. And it's also followed by a colon. So a colon is always at the end of the keywords that define a conditional execution statement.
24:23
Moreover, the value of the boolean expression determines whether or not the respective branch is going to be executed or not. So this is similar to the Julia part. So the only distinction here is that, first of all, we have ELIF instead of else if. And we also have this colon following the boolean expression in that branch.
24:45
So let's take a look at an example. Again, I will use the same example from the Julia part. So we have countdown here equals to 10. And I'll type in while countdown is strictly greater than zero. It's just to create the while loops. I haven't started the conditional or chain conditional yet.
25:06
First I'll type in if countdown is divisible by three equals equals zero. That's the first branch. I'm going to type print.
25:20
And then between the parentheses, I can either use commas or use an F string. So first I'll type in the F and then between the quotation, I have these curly brackets and I have countdown. Here I have is divisible by three.
25:42
And then in the second branch, so here I type ELIF. As you can see immediately turn green because it's a keyword. So I have countdown is divisible by two or not. So I check for whether the remainder is equal to zero.
26:04
And here I'll type in print and then F and then between the quotation mark, I have curly bracket countdown is divisible by two.
26:29
And finally, I have else I print gain another F string where I have between the quotation mark countdown.
26:47
Here I have is not divisible by two nor divisible three.
27:02
I must not forget the colon here. I almost did. And now finally here we just type in countdown minus one. And notice how the alignment or the indentation of countdown here is similar to everything in a while loop.
27:23
And here F or the body of in this branch of the conditional is indented or in all of the conditional expressions that we have are indented in the same manner. So let's execute the cell.
27:40
As you can see, ten is divisible by two, nine is divisible by three, eight is divisible by two, seven is not divisible by two nor divisible by three and so on. Similarly, we can also create nested conditional execution and this is going to be left as an exercise.
28:01
All right. So we also talked about propositional logic truth tables. So in Python, the symbol for logical operator is actually quite intuitive. So for or it's literally just or everything is written in lowercase letter. Similarly for and and not it's just and not everything is written in lowercase letters for both of them respectively.
28:26
So here we have in this table the truth table for or and and not. And as you can see, we have different values for P and different values for Q. And what happens when we evaluate P or Q, P and Q and finally not P.
28:43
OK, so to simplify this, I'm going to define one list that contain all the values of P. So it contains true, true, false, false and all the values of Q. So true, false, true, false. And then we're going to evaluate all of these expression using a for loop.
29:05
So let's do that together and hopefully it will become clear as we do. So first I will just store everything here in this list for the value for the values of P. So here I have true, true, false, false.
29:26
And then for Q I have true, false, true, false. And now I'm going to run a for loop that's going to go through all of the elements in P.
29:45
And I'm just going to evaluate P or Q, P and Q and not P. So first I'll type in for i, that's just an iterator variable in the range. Here I'm going to go until the length of P and I have to put my column here.
30:06
So remember the len function just tells us what is the length of each list here. So because both of them are same, I'm just going to use the one for P. And this is what I want to evaluate and in particular I'm going to do that and display it using the print function.
30:22
So first I'll do print i or Q i. OK, that's going to give me P or Q. That's the first part. And as you can see here immediately or turns to green. And then I have comma P i and Q i.
30:44
Finally, I have not P i. Now let's execute the cell and see what happens. So you have true, true, true, false. This is indeed the case here. We have true, false, false, false.
31:01
This is indeed the case here. And we have false, false, true, true. This is indeed the case here. All right. So once again, I'm going to use the same example as we did in the Julia part. So in the following example, we display the value of countdown only if it's divisible by two and divisible by three.
31:21
The way we do this is we use the modulus operator to check if the remainder when we divide by two is equal to zero and the remainder when we divide by three is equal to zero. So we type countdown equals ten. Here I have my while loop starting from countdown is strictly greater than zero.
31:48
I had the colon here. And then if countdown modulus operator two equals equals zero and countdown modulus operator three equals equals zero.
32:04
I have to not forget my colon always. And then here I'm just going to print the value of countdown. And here I need to subtract from countdown one.
32:22
Otherwise the loop will not be terminated. So I execute the cell. And as you can see here, the only value that gets printed is or that gets displayed is six because it's the only one that satisfy this condition here.
32:41
Similarly, we're going to do the same thing. But now we're checking if it's divisible by two or divisible by three. So instead of and we're just going to replace this with or and now I execute the cell.
33:01
And as you can see here, several values have been displayed. That's because several of them either satisfy the first condition or the second condition. All right. So we also talked about functions. And in particular, we talked about how we can define functions in the standard form.
33:20
So in Python, user defined functions declared in the standard form have the following generic structure. So the header usually starts with the keyword def. So this is slightly different than the Julia part where we use the keyword function. So here we use the keyword def. And this keyword is followed by the function name as well as the delimiters, which are the parentheses.
33:48
And these these have, in this case, the parameters parameter one, parameter two, etc. And then finally, we have a column so you can already see a pattern. Right. So we have this column and then we have the body, which is indented, etc.
34:02
So the key difference in addition to everything that we've talked about regarding control for statements in general is that the function is defined using the keyword def. So we're going to take a look at an example. So if you remember the function concatenate and print we defined in lecture four using the Julia syntax.
34:24
This function had two string values, first string and second string. And we used those two strings to create a new word, which is obtained by concatenating the two and printing the results.
34:41
So here is how we defined it in the Julia syntax. So we type the keyword function followed by the function name, the two parameters. And inside we do the concatenation using the asterisk or the multiplication sign. But if you remember, we do the concatenation here in the Python part using the plus operator.
35:03
Finally, we print the function. So let's do the same thing. We're now in Python. So to define the function, I'll type in def and I'll type in concatenate underscore and underscore print.
35:26
And then between the parentheses, I'll have first underscore string comma second underscore string. And I have my column here and then here I'll type in concatenated
35:43
underscore string is going to equal to first underscore string plus second underscore string. And in the following line, I'll type in print the value of concatenated underscore string.
36:11
And this completes the definition of the function. We don't have a keyword, a termination keyword. The body is indented. Everything looks good. So this just the definition of the function.
36:23
We still have to call the function. In this case, we're going to call the function using two string values. So water comma melon. Now, as you can see here, I have a new
36:46
string value which concatenate the two parameters which were passed as arguments to the function. We also talked about parameterizing null array functions, and this is going to be left as an exercise. Moreover, we talked about user input and to obtain a user input, we use the function called input in Python.
37:08
So in this example, I'm going to print between quotation mark what is your name, explanation mark.
37:23
And in the following line, I'm going to assign to a variable called name this function here input. So that's the function that obtain a user input. And in the following line, I'm going to type in print between quotation mark my name is comma name.
37:50
Now I will execute the cell. And as you can see here, it immediately gave this option to type an input. In this case, I'll type in, for example, Python.
38:04
And once I execute the cell or after I enter the input, it says my name is Python. Just a quick thing that I would like to point out is that similarly to the Julia or to Julia, the Python function input reads the user input as a string or str value.
38:24
So if I ask Python, what is the type of this variable that we defined in the previous cell? It says that it's an str because it's a string value. All right. So we also talked about fruitful and void functions in Python.
38:42
The values returned by a fruitful function must be declared explicitly using the return keyword. So this is a clear distinction compared to Julia. In Julia, if we don't have the keyword returned, the last line or the last expression in the body of that function is typically by default returned.
39:03
But here in Python, we have to explicitly declare that this is the value that we want to return from this function. So if you don't have a return keyword, the function are going to be considered as void functions.
39:21
So let's take a look at an example. First, we're going to create a function that does not have a keyword. First of all, we're going to create a function that has the return keyword. And then we will see how it is the case that functions that do not have the return keyword are considered to be void functions.
39:41
So let's just define a simple function, which is called square. And it takes one parameter, which is x, and it returns x squared. So remember, squaring or exponentiation is obtained by the asterisk symbol.
40:05
And now I simply execute the cell because now the definition is complete. And I can test the function by calling it as we always do. For example, here, we'll just use square of five. And in the following line, I'll type in print result.
40:25
Indeed, I have square of five, which is our five squared, which is twenty five. As I mentioned, functions that do not contain the return keyword or that only perform an action but do not return a value are considered as void functions.
40:45
So here we're going to take a look at two examples. The first one is a void function that does not use the return keyword. The second example is going to be of a function that uses the return keyword but only prints.
41:03
So it does not return a value. So for the first one, it's going to be the same function. But to just make the distinction, we're just going to call it square underscore void underscore one.
41:22
And then between the parentheses here, it also takes the same parameter, which is x. And here I'll just type in x exponentiation two. OK, so I don't have a return keyword. Python will happily define this function. However, the return or just it's going to be a void function.
41:41
And in the second example, you're going to have the same thing. We're going to call it square void underscore two. And here inside the function, I will return print x exponentiation two.
42:06
So I'll execute the cell and let's see what happens. So I'll just copy stuff that we had from before.
42:21
I'll modify things slightly. So maybe you call this result void underscore one here. Also void underscore one. And I'll give it the same parameter. And I'll still print, but now I want to print this result.
42:45
And in the following line, I'll just type of this result. OK, we just want to see if it actually returns a value or not. And then in the next cell, I'm going to do the same thing. And now it's going to be two here, two here, two here,
43:06
and last but not least, two here. OK, so let's execute the first one. As you can see here, it says none because there is no value. I did not add the return keyword. So it's a void function.
43:22
Similarly, I printed this function. So it actually printed 25, but it says that the value of none type. So it printed 25 because this operation actually happens inside the function. So inside the function, we actually print the value of 25.
43:42
However, that 25 value is not retrieved. All right, so we also talked about how the return keyword can be used for several things. And in particular, the return keyword can be used to exit the function immediately.
44:02
It could also be used to return values, as we've seen. So if the keyword return is followed by an expression, the value of the expression is going to be returned. And finally, we could also return multiple values using a comma between the expressions
44:21
or several commas between expressions that we want to return. So here I prepared three examples. They're all similar to the ones that we did in the Julia part related to Newton's method, which is used to calculate the square root of a given value x
44:41
that does not equal to 0 as the initial guess. So here in the first variant of this function, the return keyword is used to exit the while loop only. So let's take a look at how the function looks like. So here we have just a header, which
45:01
has the keyword def, followed by the name of the function, the value of which we're trying to find the square root, and x, which is the initial guess. Here we initialize the value of y, and then we run the while loop. So while true, set the value of x to be equal to y.
45:21
And here we calculate the updated value of y by using the formula, Newton's method formula. And we check if y is now equals to x, we're going to return. So here the return keyword is used exclusively to just exit the while loop. It does not return any value.
45:42
In the second example, the return keyword is used to exit the while loop, as it is the case for the first one. But now it's also used to return the value of x, which is essentially the square root of a. Because here we set the value of x to be updated,
46:02
the value of y in every iteration, which is essentially updated using the Newton's method formula. So just the distinction between this one and this first one, the second one and the first one is that we also returned the value of x or the square root of a. The third one
46:21
is also similar to the first and the second one. So it returns, or it exits, the return keyword is used to exit the while loop immediately. It is also used to return the value of x or the square root of a, and the number of iterations it took to actually find the square root of a.
46:43
So here, in the first example, we just use it to exit the function immediately, which corresponds to the first use of the return keyword. In the second example, we use it to exit the while loop and return the square root of a. And in the third example,
47:02
we use it to exit the while loop, return the value of the square root of a, and the number of iterations, so multiple return values. We do this in a similar fashion. The only difference here is that we have this else conditional or else branch,
47:22
where this iteration variable, which is initialized here to be zero, is incremented by one in every iteration. So here in the following three cells, I'm just evaluating all three functions. And as you can see, as you will see once we execute the cell,
47:43
they should give us the desired result. So let's execute the first one here, and the second one, and the third one. Now, when I evaluate the function, when I call the function, that's the first one, I should not expect it to return a value because I'm only using it
48:00
to just exit the function immediately. So it says here none, which is indeed the case. Now let's see what happens when you evaluate the second one. It returns nine. So it successfully exited the while loop using the return keyword, as well as returning the value of x.
48:20
And for the third one, if I execute it, it actually returns a tuple that has two values, nine and 23. And nine, as we know, is the square root of 81, and 23 is the number iteration it took
48:41
to reach that number. Just a quick note on the last function. So we could also assign the return value of Newton's square root three into two separate variables. In particular, we can, instead of just saying this guy is equal to square root three,
49:03
we can split that into square root comma say number underscore of underscore durations. And we can here then, instead of printing square root or square root underscore three,
49:21
we just print square root and then we print here number of iterations. And as you can see, we separate the elements of that tuple into two variables. Alright, so similarly to Julia, the returned value of fruitful functions
49:41
in Python do not have to be numeric. So in the following example, we show how a fruitful Boolean function can be created. A fruitful Boolean function is a function that returns a value of type Boo.
50:00
So here we just check if we check by having the function is divisible whether x is divisible by y or not. So in the first branch, we check if the remainder of dividing x by y is equal to zero then x is divisible by y
50:22
and we return true. Otherwise, we return false. And in the following cell, we evaluate several values. So let's just do them together. So I'll execute the first one. That's an example very similar to the Julia part. So we don't need to go through it in deep details.
50:41
So as you can see here, it returns true, true, true, false, true. So 10 is divisible by 2. 9 is divisible by 3. 8 is divisible by 4. 7 is not divisible by 5 and 6 is divisible by 1.
51:00
Again, similarly to the Julia part, we can also define recursive functions in Python. So we're just going to define the factorial function that we've defined in Julia. So we'll say def my underscore factorial and then between the parentheses we have n and then we check
51:21
if n equals equals 0, we're going to return 1. This is very similar to the Julia part, so I will not go through the logic here. You can check the notes for the Julia part. So in the first branch we return 1 and in the second branch we return
51:43
n multiplied by my underscore factorial between the parentheses we now have n minus 1. So the important thing to note here is that my factorial function is actually calling itself as well.
52:02
But now here instead of n, we're using n minus 1. So I can execute the cell and I can evaluate this function by, for example, finding the factorial of 3 and which we know to be 6.
52:21
All right, so just to summarize, control flow statements have the same generic form as in Julia except that the header ends with a colon, expressions in body must be indented and they do not have a termination expression. So for conditional execution statements, we talked about how the keyword l if indicates
52:41
chain execution and for logical operators or and not can be used to define each operator with each operator written explicitly using lowercase letters and Python's user-defined functions are declared using the keyword
53:03
def. Moreover, we talked about how the value is returned by a fruitful function must be declared explicitly using the return keyword and we talked about how the return keyword can also be used to exit the function immediately. In the next lecture,
53:21
we're going to talk about advanced data types and libraries in Python. That's it for this video and I'll see you in the next lecture.
Recommendations
Series of 22 media