WEBVTT

0
00:00.210 --> 00:05.210
In the last lesson, we saw how different it was when we tried to use the turtle's

1
00:05.400 --> 00:06.480
write function

2
00:06.780 --> 00:11.780
and we saw all of the arguments available listed, versus when we tried to use the

3
00:12.210 --> 00:14.940
Tkinter label's pack method,

4
00:15.330 --> 00:20.100
and notice how there's actually very few arguments that are listed.

5
00:20.520 --> 00:24.000
And yet, somehow there's all of these parameters that we can change

6
00:24.240 --> 00:28.650
when we look at the documentation, like expand or side.

7
00:29.010 --> 00:32.340
So let's set that side to equal left,

8
00:32.700 --> 00:35.730
and I'm going to delete the rest of the turtle code.

9
00:36.150 --> 00:41.150
And now you can see our label is placed on the left side of our screen.

10
00:42.900 --> 00:45.030
How is it that these parameters,

11
00:45.090 --> 00:48.090
even though they're not listed in the list of properties,

12
00:48.330 --> 00:52.770
how is it that we're able to use them just by typing them in? Well,

13
00:52.800 --> 00:57.510
to understand this, we have to learn a bit more about advanced arguments.

14
00:57.900 --> 00:59.430
Not how to argue better,

15
00:59.520 --> 01:04.520
but how to use advanced Python arguments in order to specify a wider

16
01:06.330 --> 01:07.890
range of inputs.

17
01:08.790 --> 01:12.870
We've already seen how keyword arguments work. For example,

18
01:12.870 --> 01:17.430
here I have a function and I have three keyword arguments,

19
01:17.490 --> 01:20.280
a, b, and c. And when I call the function,

20
01:20.610 --> 01:25.320
I can provide those inputs, a, b, and c in any order I want

21
01:25.650 --> 01:27.930
as long as I've got the keyword in front.

22
01:27.990 --> 01:31.020
So c = 3, a = 1, and b = 2.

23
01:31.980 --> 01:34.980
Now what if when I use this function,

24
01:35.370 --> 01:40.370
it's nine out of ten times, a is going to be equal to 1, b is 2 and c as 3.

25
01:41.460 --> 01:46.110
Why is it that when I call the function, I always have to put in these values?

26
01:46.170 --> 01:48.510
That seems a bit of a wasted effort, right?

27
01:49.020 --> 01:53.850
Python has a very neat way of solving this by creating arguments that have

28
01:53.940 --> 01:55.320
default values.

29
01:55.830 --> 01:59.160
We can do this by simply changing the function declaration.

30
01:59.550 --> 02:01.470
So when we create our function,

31
02:01.590 --> 02:05.970
we can already give it some values to start off with. So we can say that a

32
02:05.970 --> 02:09.870
should be equal to 1, b = 2 and c = 3, and these

33
02:09.940 --> 02:11.370
are the default values.

34
02:11.790 --> 02:15.900
So that means when I call this function and I want to use the default values,

35
02:16.290 --> 02:20.670
I don't actually have to provide any inputs and it will just go along as it

36
02:20.670 --> 02:22.740
would before. Now,

37
02:22.800 --> 02:26.400
if, however, I wanted to modify one of those inputs,

38
02:26.430 --> 02:29.220
let's say I wanted to give a custom value for b

39
02:29.520 --> 02:32.880
rather than let it be equal to the default value of 2, well,

40
02:32.880 --> 02:37.880
then I can just change the value of b, b = 5. And the rest,

41
02:38.640 --> 02:42.240
so a and c, will still take on their default values.

42
02:42.990 --> 02:46.140
Notice when I called tim.write,

43
02:46.590 --> 02:50.730
this write method takes five inputs; self,

44
02:50.760 --> 02:54.240
because it's a method and associated with the turtle class,

45
02:54.690 --> 02:57.750
it takes an argument which is what it is going to write,

46
02:58.050 --> 03:00.910
move, align and font. Now,

47
03:01.360 --> 03:04.690
if I wanted to use tim.turtle to just write

48
03:04.690 --> 03:06.370
some sort of text,

49
03:08.350 --> 03:12.820
I can actually just put in this. And when I run this code,

50
03:13.720 --> 03:17.320
you'll see that I've got some piece of text being written.

51
03:18.280 --> 03:22.300
But how is it that I've got all of these other inputs

52
03:22.360 --> 03:26.620
which I've just basically completely ignored? What about move?

53
03:26.650 --> 03:31.000
What about align? What about fonts? Well, as you can see,

54
03:31.330 --> 03:34.330
they have the =...,

55
03:34.660 --> 03:38.860
which is trying to tell you that they've already got a default value.

56
03:39.370 --> 03:42.160
And in fact, if I hover over this write function,

57
03:42.220 --> 03:45.940
you can see that in the quick docs that pops up,

58
03:46.150 --> 03:47.860
it tells me that the argument,

59
03:48.210 --> 03:50.100
<v 1>or the arg,</v>

60
03:50.490 --> 03:54.450
which is what it is to be written to the screen, move

61
03:54.510 --> 03:58.380
which is optional which can be set as true or false, align

62
03:58.380 --> 04:01.140
which is optional, font which is optional.

63
04:01.710 --> 04:06.420
The reason why they're optional is because all of these actually have a default

64
04:06.420 --> 04:07.860
value. For example,

65
04:07.860 --> 04:12.860
move, by default, is false; align by default is centered,

66
04:14.070 --> 04:15.960
and there's also a default font.

67
04:16.920 --> 04:21.920
So that means that all we have to do is just to provide the required arguments.

68
04:23.340 --> 04:27.930
If I don't add anything at all when I call this write method, you can see

69
04:27.930 --> 04:30.090
I do in fact get an error here.

70
04:30.300 --> 04:34.650
And it says write is missing one required positional argument,

71
04:34.890 --> 04:37.260
which is that first arg

72
04:37.620 --> 04:41.130
which is the thing that it's expecting to write. I mean,

73
04:41.130 --> 04:44.520
if you're calling turtle to do some writing and you not telling it what to write,

74
04:44.790 --> 04:45.870
that's a bit of a problem.

75
04:46.200 --> 04:51.200
So this ARG is a required argument because it doesn't have that =... at

76
04:52.590 --> 04:56.910
the end. So this you have to provide. But once you've done that,

77
04:56.940 --> 04:59.430
then the rest of them all have default values.

78
04:59.430 --> 05:01.710
They already know how to behave and what to do

79
05:02.010 --> 05:05.850
even if you don't tell them anything extra. Now, however,

80
05:05.850 --> 05:08.640
if you wanted to modify one of those arguments,

81
05:08.700 --> 05:12.990
let's say I decide to change the font to something completely different

82
05:13.110 --> 05:18.110
like Times New Roman, and make the font super large

83
05:18.870 --> 05:22.710
80 size font and change it to bold,

84
05:23.220 --> 05:28.220
then you can see that that optional setting gets implemented and it's now

85
05:28.650 --> 05:32.490
changed the font and its changed the size and also made it bold.

86
05:33.120 --> 05:35.100
<v 1>But this was completely</v>

87
05:35.160 --> 05:36.510
optional.

88
05:37.470 --> 05:42.390
And this is all down to creating the function with default values.