Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
Hi and welcome back. 2
2
In this video, we're looking 3
3
at default values for parameters. 4
4
If you already know a bit of Python, 5
5
you may wonder why we didn't cover this earlier. 6
6
And quite simply the reason is that we didn't need it. 7
7
Now we do and we're gonna start looking 8
8
at a couple of instances where having default values 9
9
with parameters is gonna be useful. 10
10
So let's get right on it. 11
11
Whenever we call a function that has parameters, 12
12
we are expected to give it an equal number of parameters. 13
13
We already know this, so I'll show you some example code. 14
14
Here's some accounts, like a checking account. 15
15
Let's see, that has $1,958 in it 16
16
or pounds if you're in the UK. 17
17
3695.50 in your savings. 18
18
That's a pretty respectable amount of money. 19
19
Then we're gonna have a function add_balance, 20
20
which is going to take an amount, 21
21
which is going to be a float and a name, 22
22
which is going to be a string. 23
23
And it's going to return a float. 24
24
The reason I'm including it here, 25
25
the type hinting that we've looked at in the past. 26
26
Also, just for completeness, I am going to say function 27
27
to update the balance of an account 28
28
and return the new balance. 29
29
Include that log string in there 30
30
just so you know what this function's all about. 31
31
As you can see, this function, add_balance, 32
32
is going to update the balance of an account 33
33
and return the new balance. 34
34
The two arguments it takes are the amount 35
35
to increase the balance by 36
36
and the name of the account that we're gonna change. 37
37
Then it's gonna go over to your accounts global dictionary 38
38
and it's going to change it. 39
39
So, accounts name 40
40
plus equal amount 41
41
and then it's going to return accounts name. 42
42
It's going to return the new balance of the account. 43
43
Accounts name, for example, for checking 44
44
is going to be 1958. 45
45
Let's say we say 200 in here is gonna go, 46
46
increase it by 200. 47
47
I'm not even gonna try to do the math here. 48
48
Then it's going to return the new amount 49
49
of accounts name, so that should be a float. 50
50
However, what we can do, of course, 51
51
is add balance 500.00 to savings 52
52
and then print account savings 53
53
and it should tell us the new balance 54
54
which should be in the four thousands. 55
55
Oh, sorry, accounts savings. 56
56
As you can see, 4,195.5. 57
57
Notice how the zero at the end gets dropped, 58
58
but in my code I like having two decimal places 59
59
for currency just to keep things clearer. 60
60
What you didn't know that you could do is you can say 61
61
that by default, all new balances are going to go 62
62
to your checking account. 63
63
So the name here is going to be checking, by default. 64
64
And the way we do that is we add equal checking. 65
65
Okay, so the name is a string 66
66
and its' default value is checking. 67
67
That means that if we do not pass a string 68
68
through this argument, which we can do now, 69
69
we can not pass a string to it. 70
70
We can not pass anything to it 71
71
and we print out the checking at the end, 72
72
we should see it has increased by $500, 73
73
not 500 units. 74
74
If you don't wanna use type hinting, 75
75
then all you have to do is get rid of that. 76
76
Okay and this stays the same, name equals checking. 77
77
And that's that, okay. 78
78
Frequently, actually, if you don't have type hinting, 79
79
you'll see this written like so, without the spaces 80
80
and when you do have type hinting, 81
81
you'll see it written like so, all with spaces. 82
82
It's up to you how many spaces you want 83
83
to put in there, whether you wanna put spaces 84
84
or not spaces, totally fine. 85
85
Now, something important that you have to know 86
86
about these names arguments, this is what they're called 87
87
as soon as you have the default argument there, 88
88
is that you cannot have this 89
89
here, oops sorry, 90
90
you cannot have this here. 91
91
This will be illegal. 92
92
When you run it, it'll tell you here. 93
93
Syntax error, known default argument 94
94
follows default argument. 95
95
So you cannot do this. 96
96
Any argument that has a default value 97
97
has to follow arguments that do not 98
98
have default values. 99
99
So in this case, the amount 100
100
does not have a default value. 101
101
The name does, so the name has to go at the end. 102
102
If you have many arguments, 103
103
some of which have default values, 104
104
they must all go at the end. 105
105
Okay? 106
106
That's it. 107
107
This is default values for arguments. 108
108
You can have many arguments, many default values. 109
109
One default value per argument, 110
110
of course and we're gonna learn more 111
111
about these default values 112
112
and how to use them in subsequent videos, 113
113
in just a couple of videos. 114
114
In fact, in the next video, 115
115
and in coming videos too. 116
116
So, that's it for this one. 117
117
I'll see you on the next one.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.