1
1

00:00:00,550  -->  00:00:01,960
<v ->Hi guys and welcome back.</v>
2

2

00:00:01,960  -->  00:00:03,800
In this video, we learn about
3

3

00:00:03,800  -->  00:00:06,643
default parameter values in Python.
4

4

00:00:07,480  -->  00:00:08,320
Let's get started.
5

5

00:00:08,320  -->  00:00:10,050
We've got a function here called add
6

6

00:00:10,050  -->  00:00:11,430
and it takes two arguments.
7

7

00:00:11,430  -->  00:00:14,220
So it has two parameters, x and y.
8

8

00:00:14,220  -->  00:00:15,053
And then it's gonna print
9

9

00:00:15,053  -->  00:00:16,330
the result of adding them together.
10

10

00:00:16,330  -->  00:00:18,780
So normally, you would call this add function
11

11

00:00:18,780  -->  00:00:20,200
with two arguments.
12

12

00:00:20,200  -->  00:00:22,020
So five comma eight.
13

13

00:00:22,020  -->  00:00:24,380
Gives them five and eight to x and y.
14

14

00:00:24,380  -->  00:00:27,010
However, you can have default values
15

15

00:00:27,010  -->  00:00:29,370
for one or two of these parameters
16

16

00:00:29,370  -->  00:00:31,420
by just saying in the function definition,
17

17

00:00:31,420  -->  00:00:32,763
y equals eight.
18

18

00:00:33,910  -->  00:00:36,780
Again, we usually don't put spaces around this equal sign
19

19

00:00:36,780  -->  00:00:39,130
when we use it for a default parameter value
20

20

00:00:39,130  -->  00:00:41,930
and what this is that x is a required parameter
21

21

00:00:41,930  -->  00:00:44,410
and y is now an optional parameter.
22

22

00:00:44,410  -->  00:00:47,520
If you don't give y a value when you call the function,
23

23

00:00:47,520  -->  00:00:49,080
it will use the value eight,
24

24

00:00:49,080  -->  00:00:50,800
which is defined here.
25

25

00:00:50,800  -->  00:00:53,420
So, instead of doing add five comma eight,
26

26

00:00:53,420  -->  00:00:55,570
you can just do add five.
27

27

00:00:55,570  -->  00:00:57,620
And five will become the value for x,
28

28

00:00:57,620  -->  00:01:00,470
since that is the first parameter and the first argument,
29

29

00:01:00,470  -->  00:01:02,640
and because there is no second argument,
30

30

00:01:02,640  -->  00:01:05,760
y will use its default value eight.
31

31

00:01:05,760  -->  00:01:07,520
So if we run this code here,
32

32

00:01:07,520  -->  00:01:10,260
you'll see that 13 comes out.
33

33

00:01:10,260  -->  00:01:12,870
Similarly, you can do, of course,
34

34

00:01:12,870  -->  00:01:15,030
x equal five, if you like,
35

35

00:01:15,030  -->  00:01:17,250
and still y doesn't have a value,
36

36

00:01:17,250  -->  00:01:19,900
so it will use the default value of eight.
37

37

00:01:19,900  -->  00:01:22,030
So this means exactly the same thing.
38

38

00:01:22,030  -->  00:01:26,190
What can't do though is do this, y equal five,
39

39

00:01:26,190  -->  00:01:28,700
because x is a required parameter,
40

40

00:01:28,700  -->  00:01:30,230
you need to give it a value,
41

41

00:01:30,230  -->  00:01:31,860
and here, we're not given it,
42

42

00:01:31,860  -->  00:01:33,550
so if we were to run this code,
43

43

00:01:33,550  -->  00:01:35,540
you would get the add is missing
44

44

00:01:35,540  -->  00:01:37,840
one required positional argument, x.
45

45

00:01:37,840  -->  00:01:39,633
So you need to give x a value.
46

46

00:01:40,640  -->  00:01:44,490
As usual, you can still do x equal five, y equal five,
47

47

00:01:44,490  -->  00:01:45,510
that's totally fine.
48

48

00:01:45,510  -->  00:01:48,660
And of course, you cannot do x equals five comma five,
49

49

00:01:48,660  -->  00:01:50,050
because as we learned earlier on,
50

50

00:01:50,050  -->  00:01:53,170
you can't put a positional argument after a keyword
51

51

00:01:53,170  -->  00:01:55,290
or named argument.
52

52

00:01:55,290  -->  00:01:57,490
Similarly, that also applies for
53

53

00:01:57,490  -->  00:01:59,160
the add function definition.
54

54

00:01:59,160  -->  00:02:02,290
You cannot have a default parameter
55

55

00:02:02,290  -->  00:02:05,080
be followed by a non default parameter.
56

56

00:02:05,080  -->  00:02:06,490
So here, it'll tell you
57

57

00:02:06,490  -->  00:02:09,310
that a default value must be specified here,
58

58

00:02:09,310  -->  00:02:12,310
in y, because you've specified one earlier on.
59

59

00:02:12,310  -->  00:02:15,690
So default parameter values must go at the end,
60

60

00:02:15,690  -->  00:02:19,943
after all the required non default parameters.
61

61

00:02:22,500  -->  00:02:24,310
Something to take into consideration
62

62

00:02:24,310  -->  00:02:25,980
when you are defining functions
63

63

00:02:25,980  -->  00:02:28,790
is to usually not do this.
64

64

00:02:28,790  -->  00:02:31,570
So here we've got a variable called default_y,
65

65

00:02:31,570  -->  00:02:32,970
and it's equal to three.
66

66

00:02:32,970  -->  00:02:34,610
And then we're defining an add function
67

67

00:02:34,610  -->  00:02:39,610
and making the y parameter equal to our default_y variable.
68

68

00:02:39,740  -->  00:02:42,220
So, y will equal three,
69

69

00:02:42,220  -->  00:02:45,280
and that will be the default value for the y parameter.
70

70

00:02:45,280  -->  00:02:48,320
Now we can call add with the value two, let's say,
71

71

00:02:48,320  -->  00:02:51,380
and then we do for x and three for y,
72

72

00:02:51,380  -->  00:02:53,520
giving us a total of five.
73

73

00:02:53,520  -->  00:02:58,110
However, if we then go and change default y to four,
74

74

00:02:58,110  -->  00:03:02,170
and then we add again, let's say add two,
75

75

00:03:02,170  -->  00:03:04,960
now you'll see that actually,
76

76

00:03:04,960  -->  00:03:07,640
we get the same value back from the function.
77

77

00:03:07,640  -->  00:03:10,610
So changing a variable that has been used
78

78

00:03:10,610  -->  00:03:12,770
as a default parameter value,
79

79

00:03:12,770  -->  00:03:14,630
does not modify the function.
80

80

00:03:14,630  -->  00:03:17,770
So this value here, this three,
81

81

00:03:17,770  -->  00:03:20,550
is defined when the function gets created
82

82

00:03:20,550  -->  00:03:22,190
and it doesn't change after that,
83

83

00:03:22,190  -->  00:03:24,020
if you reassign the variable.
84

84

00:03:24,020  -->  00:03:26,470
So this is why I don't usually recommend doing this,
85

85

00:03:26,470  -->  00:03:29,200
because it can get really confusing really quickly.
86

86

00:03:29,200  -->  00:03:32,650
So stick to putting the actual value on the function,
87

87

00:03:32,650  -->  00:03:34,990
unless you really have to do something different,
88

88

00:03:34,990  -->  00:03:36,850
and then make it clear that that value
89

89

00:03:36,850  -->  00:03:41,180
is not going to change after defining the function.
90

90

00:03:41,180  -->  00:03:42,490
So that is how you define
91

91

00:03:42,490  -->  00:03:44,940
default parameter values in Python.
92

92

00:03:44,940  -->  00:03:47,410
Just put them in the function definition,
93

93

00:03:47,410  -->  00:03:48,260
just make sure that they are
94

94

00:03:48,260  -->  00:03:50,700
at the end of the parameter lists.
95

95

00:03:50,700  -->  00:03:52,500
Thank you for joining me in this video
96

96

00:03:52,500  -->  00:03:54,150
and I'll see you in the next one.
