Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Basics!
You have completed Python Basics!
Preview
Let's explore how to create our own functions
Learn more
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
We've used some predefined
functions throughout this course.
0:04
And they've proven to be super handy.
0:07
You know functions like print and
input, we also used len.
0:09
Functions are great,
they provide a way for
0:13
you to group multiple statements
together to use later.
0:15
They help you avoid duplicating your code.
0:18
Now if you ever find yourself writing
the same exact code more than once,
0:21
it's a symptom that
something is likely wrong.
0:24
Now we refer to these symptoms
as code smell, like hmm,
0:27
something about this code smells.
0:31
Here, let me help you
write some smelly code and
0:33
then we'll clean it up with
the power of functions.
0:35
Getting someone's attention
these days is so hard.
0:38
In application development, you like
have to bounce an icon or make a noise,
0:42
[SOUND] to alert them, right?
0:46
So one solution that we could do in our
textual environment here is to yell in
0:48
all upper case.
0:53
And I feel like the more
exclamation points,
0:54
the more likely they're gonna see it.
0:56
Let's do that, let's create some text and
0:58
then convert it to an upper cased and
exclamatory phrase.
1:01
So I'm gonna make a new file actually,
so let's go here, we'll go to File,
1:05
and then New File, and
I'm gonna name it notifications.py.
1:10
So the first thing that I would
love to shout from the rooftops
1:17
is some praise for you.
1:21
So, I'll assign it to
the variable praise and
1:24
well say, "You are doing great".
1:27
So, we want to uppercase this, right?
1:32
And what we'll do is we'll
just reassign it, so
1:34
we'll say praise = praise.upper.
1:38
And that's because strings are immutable
and they can't be changed.
1:42
So it returns a brand new string,
1:45
which we're just going to take our label
off the old one and put it on the new one.
1:47
Now I think there should be at
least as many exclamation marks
1:51
as there are characters in the string,
to really grab your attention.
1:54
So to get the numbers of characters in
the string I can use the len function.
1:59
The len function expects an object,
so we'll pass in our variable.
2:03
And then I'm gonna store that in a new
variable called number_of_characters.
2:07
And I'm gonna call the len function,
I'm gonna pass in our string, here we go.
2:16
So we wanna create a new value that
is a combination of this praise and
2:22
the exclamation points.
2:27
So let's create a new
variable called result.
2:28
And we'll make that praise and
we'll concatenate the exclamation point.
2:33
And we're gonna rely on order
of operations here so we'll say,
2:38
the exclamation point times
the number_of_characters.
2:43
And then finally, we'll print that out.
2:47
And we'll run python notifications,
cuz it's a new file.
2:55
There it is, YOU ARE DOING GREAT.
3:02
And you're doing great.
3:04
That's is some good looking code,
and fine smelling too.
3:05
You know what, I also like to remind
you that you should remember to ask for
3:08
help when you need it.
3:12
So can we yell that too, please?
3:15
I don't see why not, right?
3:17
So let's make a new variable,
we'll call that advice and
3:19
that is, "Don't forget to ask for help".
3:24
And then, well I guess I would
just copy this code, right?
3:28
Copy this code here,
and paste it down here.
3:36
Guess I would change, I need to change
this praise and make that advice.
3:41
And I make this advice.
3:46
Cuz it's the length of device,
3:50
not the length of praise because
that's a different variable.
3:52
And then we do this, so basically I
just copied and pasted that code and
3:55
changed this variable name.
4:00
That style of coding is called copy pasta,
and it's not a good habit to fall into.
4:02
And it's really a bad habit, especially
if you don't understand what the code
4:08
you've copied [LAUGH] is doing,
it happens all the time.
4:12
So here we go, lets keep going.
4:16
You know what,
I've got a another piece of advice for
4:19
you that I would love you to yell for me.
4:22
In coding, there's an acronym that we
call dry or D-R-Y, don't repeat yourself.
4:24
Let's yell that.
4:32
So, I guess that's some more advice,
right?
4:34
So let's call that advice2, and
4:35
we'll say, "Don't Repeat Yourself.
4:40
Keep things DRY".
4:46
Okay so, I guess I'll copy and pasta this.
4:49
We'll put this here and
I'll go ahead and do advice2.
4:53
Advice2, the length of advice2,
4:58
and then we'll do advice2.
5:03
And then let's just make
sure that this is working.
5:08
Okay, It is, but what if I told you that
we had like ten more of these to do?
5:11
Would you be excited about that?
5:18
You know what, actually after seeing
this in action, I'd like to make it so
5:20
that there are half as many.
5:23
This is [LAUGH], this is ridiculous.
5:24
That's way too many exclamation points.
5:27
So I wanna make only half of those
as there are characters, right.
5:28
So half of the amount of
characters we should do so.
5:32
One thing that I can think of doing
is if we look we can make it so
5:35
that we use the floor division.
5:39
That was that the double
division where we get an integer.
5:40
So we can, if we divide the number of
characters by 2 and keep it an integer.
5:43
That should be that, and
5:48
I'm gonna put parens around here because
I want the order of operations to work
5:49
correctly.
5:52
So let's go ahead, let's run that.
5:52
The first one looks great, but
the other two don't seem to be.
5:55
Rats, I forgot to update those too.
6:00
Because my code is duplicated,
it means any change that I need to make
6:03
I have to change every single place
I copied and pasta-ed this code.
6:09
Well, that's really smelly right?
6:14
So let's clean up the smell,
anybody got some Febreze?
6:17
[LAUGH] Totally missed product
placement opportunity right there.
6:20
[LAUGH] I guess functions
are definitely an air freshener for
6:24
these type of code smells.
6:28
So let's create one, shall we?
6:30
So the way that you create a function,
let's get up here.
6:33
The way that you create a function
is with the keyword named def,
6:37
which is short for define.
6:42
And then you give your function a name,
let's name it yell.
6:44
That seems like exactly what we're doing,
right, so we'll say yell.
6:48
And then when you're defining a function,
6:51
you define what parameters
are expected when it's called.
6:53
So in this case, we want to accept
the text that should be yelled.
6:57
Let's go ahead, and we'll call that text.
7:02
And then we add a colon, cuz we're
gonna start the body of the function.
7:04
And you press Enter, and
see how it's indented automatically here?
7:08
And now, we are in what is
called the function body.
7:13
This is the code that is run when
the yell function is called.
7:18
So we basically want to do exactly what
we did in our previous code that we were
7:22
copying and pasting, right?
7:26
So let's just do that one more time,
we're gonna copy this code here.
7:27
I'm gonna cut it out,
when I come in here I'm gonna paste it.
7:32
And if you come in front
of these lines and
7:35
press Tab,
you can see that they tab into our body.
7:37
So instead of praise, this is now text.
7:40
I'm using Shift and
the arrow key to do that highlighting.
7:44
You get better at those and
you forget to say.
7:50
So that's how I'm doing that, so text.
7:52
That looks good, and
7:56
it's really good style to leave at least
one space after your function definition.
7:58
So that it's clear that it's
a function where it ends.
8:03
So now, we can go ahead and call that.
8:06
So let's do this, we'll get rid of this.
8:10
We won't assign it anymore,
we'll say yell("You are doing great").
8:13
And then we don't need this other
code too, let's clean this up.
8:19
So we'll say yell("Don't forget to ask for
help").
8:21
And then finally,
let's go call this last one here.
8:27
We'll say, yell("Don't Repeat Yourself.
8:31
Keep things DRY"), we'll get rid of these.
8:34
That looks a lot better, right?
8:38
Let's go ahead, let's run it and
make sure things are working.
8:40
There we go,
these lines are even shorter now, great.
8:45
So let's review what we've got.
8:48
So, we used the def keyword to
define a new function named yell.
8:51
Yell declares a parameter
which is named text.
8:58
This is the body of the function.
9:01
It's all this code that's indented
four spaces by our style definition.
9:03
Now, the function body has code that
upper cases and concatenates half-ish
9:07
the amount of text in the exclamation
marks, and then it prints.
9:11
We've got a blank line after
our function definition,
9:16
because we're adhering
to good coding style.
9:19
It's important, right?
9:21
I want you to keep in mind that
just defining this function
9:22
doesn't run the code.
9:25
What it does is it creates a new
name of yell that we can call later,
9:27
and we can do it multiple times.
9:32
So here we're calling the yell method that
we just created, and we're passing in
9:34
a brand new string that we just created
that says "You are doing great".
9:38
So that's a brand new string.
9:42
And then the values that you push
into a function are called arguments.
9:45
And since this is the first argument,
right, there's only one.
9:49
It's the first argument here,
and it's called text here.
9:53
So basically what this is saying,
is text equals you are doing great.
9:57
That's what I want you to imagine.
10:01
So text equals you are doing great, and
10:03
then the rest of the code
runs on you are doing great.
10:04
It runs through each one of these texts
you are doing great, you are doing great.
10:09
Prints the result, and then it pops
back out to whatever the next line is.
10:13
In this case,
is another call to the yell function.
10:18
And we create a new string that says,
don't forget to ask for help.
10:20
And imagine again, text equals,
don't forget to ask for help.
10:23
And then the rest of the function runs,
it pops out.
10:27
We get to the next one,
yell, don't repeat yourself.
10:30
So on and so forth,
I feel like I'm repeating myself.
10:34
Now also if we wanted to change the way
that this yell function worked,
10:37
we only need to change it in one place.
10:41
Like for instance,
I think even this too is a little long.
10:44
Why don't we do it to a fourth of that?
10:48
So if I come up in here,
10:50
I can just change this in one place
now and all of those functions run.
10:52
Functions are pretty handy aren't they?
10:57
We've just scratched the surface of
how powerful these functions can be.
11:00
But, they definitely help you
to create nice reusable code.
11:04
And try to stay on the lookout for
lines of code that are repeated.
11:08
Recognize the code smell.
11:12
Don't repeat yourself is
a great mantra to live by.
11:14
It will lead you to write clean,
understandable, and maintainable code.
11:16
Everyone can agree that repeating
what you're saying over and
11:21
over is super annoying.
11:25
Everyone can agree that repeating
what you're saying over and
11:27
over is super annoying.
11:30
Everyone can agree that repeating what,
right.
11:32
Keep things DRY everybody.
11:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up