Skip to content
CBT Nuggets

Understand Functions in Python

This skill, led by John McGovern, delves into the fundamental concepts of using functions in Python. It covers the creation and utilization of Python functions, including built-in and custom functions. Key topics include the use of positional, default, and keyword arguments, as well as the importance of the return statement for returning values from functions. The skill emphasizes writing modular, reusable code and adhering to best practices such as the DRY principle.

Full lesson from Python Functions. Preview the IT training 23,000+ organizations trust.

54m 5 Videos 4 Questions

Skill 1 of 16 in Python Functions

Overview

Join John McGovern as he covers the fundamental concepts around the use of functions within Python.

Recommended Experience

  • At least six months of Python programming experience is recommended, but not required
  • An understanding of concepts taught in Cisco CCNA is recommended, but not required

Related Job Functions

  • Network engineer
  • DevOps engineer

John McGovern has been a CBT Nuggets trainer since 2020. His areas of expertise include Python, networking, network automation, and cybersecurity.

Introduction to Functions

In this Nugget we take an introductory look at functions within Python!

Exploring Built-In Functions

In this Nugget we take a look at some of Python's built-in functions!

Knowledge Check

Which of the following is a parameter, used by the built-in print function, that will add a separator between strings to be printed?

  1. Asep
  2. Bseparator
  3. Cend
  4. Dflush

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

Creating Functions

In this Nugget we learn how to create our own custom functions!

Knowledge Check

Which of the following is a keyword used to create a new user defined function?

  1. Adef
  2. Bfunc
  3. Ccustom
  4. Dfunction

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

Passing Arguments

In this Nugget we learn how to leverage positional, default, and keyword arguments within our functions!

Knowledge Check

Positional arguments cannot follow keyword arguments. True or false?

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

The Return Statement

In this Nugget we learn how to use the "return" keyword within our functions!

Knowledge Check

A Python function will return which value by default?

  1. ANone
  2. BZero
  3. C""
  4. D0

Verify your team's readiness — Request a Demo to verify practice assessments, completion reporting, and CSV / SCORM exports on the Team plan.

Conclusion

I hope this has been informative for you and I would like to thank you for consuming.

View Transcript

Introduction to Functions

0:00[AUDIO LOGO]

0:12Hey, guys, and welcome back to another skill on Python

0:15for network engineers.

0:16Functions happen to be a really common concept

0:19throughout programming, and by using Python,

0:22you're going to be using functions all the time.

0:24So what exactly as a function then?

0:27What are we talking about here?

0:29Really, at its core, just conceptualize a Python function

0:33as a group of code.

0:35And the type of code we're talking about

0:37can literally be anything.

0:38And we'll see this throughout the scale--

0:40you can have really, really, really

0:42simple functions that just print out something.

0:44Now functions can be built-in.

0:46There's going to be built-in functions within the Python

0:48library.

0:49You can also have third-party functions,

0:52which you can install and import into your Python code.

0:55And we can also have custom functions written by us.

0:59Now like I said, most functions are simple a group of code,

1:02and that code can literally be anything.

1:05If the code can literally be anything,

1:06what is the advantage to using a function?

1:08Why put into a function?

1:10What is so important here?

1:11Well realistically, what functions

1:13do is it allows code to become much more reusable,

1:16and it makes your code much more modular.

1:19And using functions allows you to adhere to what

1:22is called the DRY principle.

1:24What is the DRY principle?

1:25Well it means, simply, don't repeat yourself.

1:29As opposed to just continually repeating

1:31the same type of code, the best thing to do

1:33is to put that code into a function,

1:35and then you can just call that code as many times as you need.

1:39Now another important concept to understand

1:41about Python functions is that functions will always

1:45return a result. And by default, that value

1:48is going to be the special value within Python, none.

1:52And we've talked about this value in previous skills.

1:55However, if we need our function to actually return

1:58something and not just none by default,

2:00then we are able to instruct their function to do so.

2:04And we'll be able to see how we can do that

2:06later on within this very skill using the return keyword.

2:10So let me just give you a short example

2:12to help you conceptualize the utility and usefulness

2:15of Python functions.

2:17How we have the ability to help us

2:18be able to achieve that principle of DRY,

2:21why don't repeat yourself, and why

2:23it makes your code much more modular and easy to manage.

2:26OK.

2:26So let's dive into Visual Studio code then.

2:29So let me go into my directory here.

2:31I'll say, cd Python-Net-Eng, and I'll

2:34go into my head and directory where

2:36my virtual environment is.

2:38And let just activate this.

2:39And I'll make a directory just called maybe, function-examples

2:43and we'll see the end.

2:45Cool.

2:45So I'm going to--

2:46take credit.

2:47Let's clear the screen.

2:48What I'm going to do here is to create an example just called

2:52example1, super duper simple.

2:54And what we'll do is we'll open this in VS code then.

2:57Python-Net-Eng, function-examples, and we're

3:00going to example1.

3:01OK.

3:02So this won't be the most realistic example.

3:04It's just going to help you conceptualize the concept here.

3:07So let's imagine that we wanted to--

3:09what will we do here?

3:10Let's imagine that we wants to print out some type of warning,

3:12OK.

3:13Just say this device as owned by IPvZero system,

3:18if you access this without authorization,

3:21bad stuff will happen.

3:22OK.

3:23So let's say we have this warning here

3:25and we want to present this at the beginning of our message

3:28and at the very end.

3:30So what we can do is just print out some random text

3:32here, blah, blah, blah, blah, blah.

3:34It doesn't actually matter--

3:36similarly blah, blah, blah.

3:39OK.

3:39Now the crucial thing is that we want

3:41to have this message at the start and at the end.

3:45OK.

3:45So what I'll do here is just copy and I'll paste that

3:49in again.

3:49So let's just save this.

3:50It's just now I'll run the script.

3:52So let me clear the screen for all of the stuff

3:54that we touched.

3:55And we'll say, python 3 example1.py.

3:57And hit enter.

3:58And as we can see here at the very top,

4:00we have our warning message, we have some text in between--

4:03doesn't matter what that is.

4:04And at the very end, we close off the message

4:07with the exact same warning.

4:10So that isn't the most useful thing, but it's there

4:12and it has its purpose.

4:13We've got our warning at the top and our warning at the bottom.

4:16OK, so let's just say we want to change our warning message.

4:19And we can maybe say--

4:21I don't know like, all devices in thus network are owned by

4:26and let's say the company name.

4:28It may be changed or something.

4:29Let's just say CBTN systems and just delete this part here.

4:33And we'll change the end here.

4:34And let's just say, do not touch, dot, dot, dot or else.

4:39It's some really ominous message OK.

4:41So as we can see here, our warning message has changed.

4:45Now because it's changed, we've changed it here,

4:48but we also have to change it here as well.

4:51So then I'd also need to alter the code at this part here.

4:54I have to go in and take all this away, just add all that.

4:57And I suppose I could, in this case,

4:59just use a copy paste job.

5:01But like I said, this is a very, very simple example.

5:04Now as we can see here, this warming statement

5:06is only being used in two areas of our code,

5:08in line one and line four.

5:10It really isn't too big of a hassle

5:12for us to just delete one and then maybe

5:15copy paste a little bit, and we're done.

5:17But can you imagine if the code was way, way, way more complex

5:21than this?

5:21And we had to update the code in multiple different areas.

5:24Having to go into each iteration of that code

5:28and make the very same change to all instances of the code

5:31really isn't very efficient.

5:33And it's also prone to error.

5:35So if you happen to maybe say CBTN systems

5:37and on the second one you're typing this again,

5:39and you'll say, system, then suddenly you

5:41have a difference between this part of the code

5:43and this part of the code, when, in fact, they

5:45should be the same.

5:47Now again, this is a really trivial example.

5:49But let me show you how we can solve this type of problem

5:51using a function.

5:53OK.

5:54So do not worry about the syntax here.

5:56We'll definitely go through the details

5:57as we go through this skill.

5:59What I'm going to do is create a function

6:00and we're going to call this function warning text.

6:03OK.

6:04And like I said, a function can literally be any type of code,

6:07and all this function is going to do a print out that warning

6:10statement.

6:10So I could just say something like, this network belongs

6:13to CBTN.

6:15Don't mess around with it.

6:17That's our new warning text.

6:18Now what we're going to do here is

6:20pretty much do the same thing, but instead

6:22of having to manually type this out, what we're going to do

6:25is to call this function.

6:26So what I'm going to say here is warning text.

6:28And what I can also do is just print out the blah, blah, blah,

6:31codes, and again, more blah, blah, blah, whatever.

6:35And at the very end of our text, we

6:37could just call the warning function yet again.

6:39So if I save this, watch what happens OK.

6:42Let's clear the screen.

6:43We arrow up and hit Enter.

6:45Look at this.

6:45We're getting the effect we want.

6:47That's network belongs to CBTN.

6:48Don't mess around with it.

6:49We get that warning at the top.

6:51We have a random text in the middle, and at the very bottom,

6:54we have the exact same code executing that print statement.

6:58This networks belongs to CBTN.

7:00Don't mess around with it.

7:01And here's the thing.

7:02If we wanted to update this, well,

7:05we just update it in one place.

7:07So let's just say this belongs to CBT Nuggets instead.

7:10Let's be a little bit more explicit.

7:12We don't have to change any of this code here.

7:14We don't have to change it in multiple places

7:16or do any copy and pasting.

7:17Just update this little function and that

7:20is what the code as referencing to now.

7:22So if I just arrow up, hit Enter, look at this.

7:24It's been updated in all the same areas.

7:26We don't need to worry about going

7:28into this part of the code, and then deleting this and adding

7:30and the new change.

7:31And then going into another line the code,

7:33deleting another part, and adding in another section.

7:36Because we have that nice, tightly defined,

7:39modular function, we can just make the change in one area,

7:43and we can call that function as many times as we want.

7:45Like I said, we can just keep calling it.

7:47Let's say, warning text, and if we save this,

7:50we can use this function as many times as we want.

7:52So this is what we're going to be exploring in this skill

7:55right here, the concept of functions, how we can use them,

7:57and how we can call them.

7:58What are the rules at play here?

8:00So we really do have a lot to get to within this skill.

8:03And how about we start by finding out

8:05what built-in functions that Python has to offer us.

8:08Well that's what we're going to be

8:10doing in the very next Nugget.

8:11So I hope this has been informative for you.

8:13And I'd like to thank you for viewing.

Exploring Built-In Functions

0:12Hey, guys.

0:13And welcome back.

0:13So in the previous Nugget, we had just taken a broad overview

0:16about what functions are and why we might want to implement them

0:20within our codes.

0:21What we also learned was that Python also

0:23has these built-in functions, which

0:25are ready-made and ready to be used.

0:28So what exactly is in this standard library

0:30of built-in functions?

0:31Well, how about we do a little bit of exploration,

0:34find out what these functions are,

0:35and what we can do with them?

0:37OK, so let's go and open up a browser.

0:39And what I'm going to search for is "python built in functions,"

0:43pretty self-explanatory.

0:44So how about we click on this top link right here?

0:47And as we can see here, within the Python documentation,

0:51we have this chart of all of these built-in functions which

0:55are available to us.

0:55And as you can see here, it tells us

0:57that the Python interpreter has a number

0:59of functions and types built into it

1:01that are always available.

1:03We don't have to do any kind of installation.

1:05These are just built in straight from the installation

1:08of the language, effectively.

1:09OK, so straight away, we can see one of our favorites right

1:13here, the print() function.

1:15So we've seen this one many times.

1:16So let's just dive a little bit deeper into it.

1:19OK, so what we'll do here just now is we'll dive into IPython.

1:23And like I say, we've already explored this function here.

1:26We just say print, and we know this actually

1:28takes an argument.

1:29And the argument is going to be whatever we want

1:31to print, like, say, my name--

1:34or rather, "My name is John."

1:36And if we pass this argument into this function,

1:40then this is going to be printed to the terminal here.

1:43OK, cool.

1:44But what if we wanted to explore a little bit more

1:46about what this function can do?

1:48What we can say is help, and then

1:51within parentheses, pass in the function

1:53we want to get information about.

1:54So basically we're saying, help, tell me about the print()

1:57function.

1:58So if I hit Enter here, as we can see here,

2:01there actually are some arguments

2:03which we haven't actually explored yet,

2:05things like sep and end.

2:07And we can actually see the way we can format this here.

2:10We have our function right here, which

2:12is print, as we happen to specify our value.

2:15That would be, like, "My name is John."

2:17And we also have an argument here, such as sep or end.

2:21OK, cool.

2:21So let's actually try this out, then.

2:23OK, now to get out of this, we can just

2:25press the letter q to quit.

2:27OK, so as we saw from the help page,

2:29we now have this new parameter that you might not

2:31have known about called sep.

2:33And we can follow the advice of the help page

2:35to explore this built-in function.

2:37And we can use this within our built-in function.

2:39So we can say print.

2:41We could say "john", "lauren", "paul" and that would just

2:45print out "john", "lauren", "paul".

2:47And like I said, let's make use of this sep parameter

2:49that we just found out about.

2:51We'll say "lauren" and we'll say "paul".

2:54And as per the documentation, we'll say sep is equal to,

2:57and we can just give it a value.

2:59And let's just put in a plus sign.

3:01And if we hit Enter now, look at the print statement.

3:04We're separating, all of these values, "john", "lauren",

3:08"paul".

3:09Everyone has been separated with this plus sign.

3:11So we're basically putting a plus sign

3:13in between all the values.

3:15So again, we could say "30", "July", "2006".

3:19And we could say sep, separate them with a hyphen.

3:22And hit Enter.

3:23Now we can separate all our values with a hyphen.

3:25OK, so there is the built-in function print().

3:28And as we can see, we can get a little bit more information

3:30about our built-in functions by using our help keyword.

3:34So let's go back and check out a few more.

3:36OK, so we have one called isinstance().

3:39Let's check that one out.

3:40How can we use this?

3:41Well, again, if we're unsure, what we can say here

3:43is say help is instance, and hit Enter.

3:46And again, it tells us this is help on a built-in function

3:49called isinstance.

3:51And what it's going to do is return

3:53whether an object is an instance of a class or a subclass.

3:57What does this mean?

3:58Well basically, we can test if something is a string,

4:00if something is a dict, if something

4:02is a list, so on, so forth.

4:04And the way we use this is in the form of a tuple.

4:07We say isinstance, and then we pass

4:09in what we want to evaluate.

4:11And then we pass in the data type, is this an instance of,

4:14effectively.

4:15So let's check this one out.

4:16Again, we'll push q.

4:17So what I can do is, for example,

4:19let's create a variable called number.

4:21And we'll say number.

4:22And let's make it a string value.

4:24We'll put it within quotation marks.

4:25And what I can say here is isinstance and then

4:29pass in what we want to test.

4:30We want to test our number variable.

4:32And let's check if this is an integer.

4:35So we'll say int.

4:35And if we hit Enter, it's going to say false

4:38because the variable number is not an integer.

4:42In fact, it's a string value.

4:43So we're getting a false.

4:44However, if I happen to update this,

4:47we'll say number is equal to the number 1.

4:50Again, this is an integer now.

4:51We're not using our quotation marks.

4:53I can say the very same thing as before,

4:55isinstance, a variable number, is this an integer?

4:58Cool.

4:58And that should be isintance, not

5:00instance-- isinstance, number, int, hit Enter.

5:04And this is true because it is an integer.

5:06Same again, we could say something

5:08like my_list is equal to all the eights, 192.168.1.1.

5:13And we can say isinstance, my_list.

5:15Is it a tuple?

5:17No, it's not.

5:18Is it a dict?

5:19No, it's not.

5:20Is it a list?

5:21Oh yes, it is.

5:22Now, this might seem a little trivial right now

5:24because you think, why can't we just say type?

5:26Because we know we can do this-- we can say type(my_list),

5:29and it tells us.

5:30Well, this built-in function isinstance

5:32is very good for conditional logic

5:34because, again, it's given us this nice Boolean

5:36value, true or false.

5:37So what we could do here--

5:39just let me just exit this out.

5:40And I'll just create a quick example here.

5:42Let's go into example2.

5:44So what I can say here is my_list is equal to.

5:46And I'll say "john".

5:48And let's create a list of numbers.

5:49We'll just say 1, 2, 3, And we know

5:52from previous skills we can actually

5:53put lists within lists.

5:54So let's say we had a list, again, within this, a sublist.

5:57We'll just say 2, 2, 5, and maybe 6, 45, 78.

6:03It doesn't really matter.

6:04So let's say we wanted to test that the number 5 happened

6:07to be within this list.

6:09So we know we can use a for loop to unwrap

6:12those individual values.

6:13So we'll say for num in my_list.

6:15And what we can say, let's say we're looking for the number 5.

6:18We can say if num is equal to 5, then we'll print out,

6:23"The number 5 is present".

6:25So if we save this and rerun this script here,

6:29nothing actually happens.

6:30Why is the number 5 not printing out?

6:33Because we can see within our list,

6:34the number 5 happens to be right here.

6:37So what is going on?

6:38So what we can say here instead is just print out the nums

6:41before we run our condition.

6:42Let's see what we're actually getting.

6:44Aha.

6:44So as you can see here, we're unwrapping these values.

6:47But this one here is in fact a sublist.

6:49So basically, this element here actually requires

6:53a second round of iteration.

6:55So what we could actually use here

6:57is the built-in function isinstance right here.

7:00Watch this.

7:00We can say if isinstance, your number variable,

7:03if that happens to be a list object, what we're

7:06going to have to do is to iterate through again

7:08to unwrap that in our list.

7:10And I can say for n in num, and say

7:13if n is equal to the value 5, then we

7:17can now print, "The number 5 is present".

7:20However, if what we are iterating through

7:23is not a list, we can just say else,

7:26and we don't need to add that other round of iteration.

7:28We could just do a test straight from the get-go

7:30and say if num is equal to 5, print,

7:33"The number 5 is present".

7:36OK, so this example is a little bit clunky.

7:38But you can kind of get the drift.

7:39In fact, let me make that a double equal sign

7:40because it's a comparison.

7:42But as you can see here, we have this cool built-in function,

7:44which can pretty much say, as we loop through every item,

7:47if any of these items happens to be a list object, i.e.

7:50this part here, then we need to go

7:52through another round of iteration

7:54to unwrap the individual values within that list.

7:57And then we can test on it.

7:59However, if we don't have a list and it's just integer values,

8:02like these, we can just straight away test,

8:04is this integer equal to the number 5?

8:07So now what we can do here is run this test once again.

8:10And as we can see here, we're getting a hit now.

8:12The number 5 is present because we used the built-in function

8:16isinstance to test if we happen to have a list, which we do.

8:19and then, we further iterated through that sublist

8:22before running our test.

8:23So now we have the hit.

8:24So as you can see here, the Python built-in functions,

8:27there really are a ton of them.

8:28And there are so many useful cases

8:30where you're going to be using these functions.

8:33And no doubt you can probably recognize some of them already.

8:36We've used the id() function before,

8:38which gives us a unique ID for a place and memory.

8:41We can use the type() function to tell a particular type

8:44of a variable.

8:44Is it a dictionary?

8:46Is it a list?

8:46We can use the len() function.

8:48We've done that before.

8:50This can tell us, for example, how many

8:52items are within our list.

8:53And another one we've used before is the sorted()

8:56function.

8:57This can do many things, for example,

8:59sort elements within our list, which we did before.

9:02We've also seen the max() function and the min()

9:04function.

9:05So really, these built-in functions

9:07are going to be your best friend in so many cases.

9:10In fact, in this lesson right here on built-in function,

9:13we've actually been using a built-in function,

9:16the help() function.

9:17So I really would encourage you to explore

9:19all of these functions.

9:20Pick a few.

9:21Use the help() function.

9:22Find out what you can do with them.

9:24And test them out because you're going

9:26to find that in so many cases, these built-in functions have

9:28been built-in for a reason.

9:30They're going to solve a lot of your problems.

9:33So definitely check them out.

9:34Okeydoke, so I hope this has been informative for you,

9:37and I'd like to thank you, for viewing.

Creating Functions

0:12Hey, guys and welcome back.

0:13So let's talk about how we can write our very own custom

0:17functions.

0:18This is a super useful practice.

0:20You're going to use it quite a lot,

0:21and it's going to go a long way for you

0:23to be able to write cleaner code, which is easily

0:26maintained and much better suited for team collaboration.

0:29I like to say functions can be very simple.

0:32They can be extremely complex.

0:34It really depends how far you want to take it.

0:36Generally speaking, when applied to network engineering,

0:39functions tend to be not too complex.

0:41You typically want to perform a simple task

0:43and have as innate little reusable piece of code.

0:47Therefore you dump it into a function,

0:48and then you can just keep using it.

0:50So how about we dive in and see how we can create these custom

0:53functions.

0:54Before we start, what you might have noticed

0:56was that the very first Nugget within this skill, I

0:59wrote a short function.

1:01So let's just talk about this.

1:02This is the key what we're really going to be relying on.

1:05That's def key.

1:06This is crucial.

1:08This is basically how we define our functions,

1:10think of that as what it means defining a function.

1:13So the syntax is going to be used in this keyword, def.

1:16And then you're going to, in essence,

1:18choose a name for your function.

1:21Now, the kind of convention to name your functions

1:24is to use lowercase and to separate out

1:26your words using underscores.

1:28So something like maybe, my_function,

1:32and then what we're going to do is have an open parentheses

1:35and then a closed parentheses followed by a colon.

1:40And then, after that, we're going

1:41to write our code as an indented block within this function.

1:45Now the convention is to use four spaces here.

1:48No, this is not a hard rule within Python.

1:50You can use one, you can use two.

1:52However, it is a pretty strong convention within Python

1:55to use four spaces if you happen to use something called

1:58a linter, which basically checks your code

2:01and examines it for potential errors and flaws.

2:04If you use maybe one or two or three

2:07or five spaces indentation, then the linter

2:09is going to show that.

2:10So to keep that happy, I would recommend you just

2:13use four like say as a bit of a convention.

2:15OK, so let's dive into our code then.

2:18So let's go into my directory, activate the ven,

2:21if can type this correctly.

2:23And let's go into the Functions-Examples,

2:25and let's create one called example3.

2:27OK, great.

2:28Let's go in here, OK, so let's first

2:30start with a really simple example,

2:33so how do we create a function?

2:34We use the keyword def, and you can

2:37see it's actually colored blue within my ID

2:39because this is a recognized keyword.

2:41And then I do a space.

2:42And no, this point here is were we're

2:44going to name or function.

2:46Again, the convention is to use lowercase.

2:48We don't want to be seeing capital letters here.

2:50We want to be doing something like, say, my_test_function.

2:54And again, we separate out words using those underscores.

2:57And then we're going to say open parentheses

2:59and close parentheses, and then our colon.

3:02Now, if you're using something like, say, Visual Studio

3:05Code, if you just hit enter, naturally, it's

3:08going to indent your four spaces here

3:10because it knows to do this.

3:11And like I say, we can do any type of code

3:13within this indented block like just say, welcome

3:16to CBT Nuggets.

3:17OK great.

3:18Now to call this code, the important part

3:20is that we don't do it within the indented block here.

3:23So we have our indented style that will have one, two, three,

3:27four, indentation.

3:28We want to go back out, so all we do here

3:31and go as many lines down as we want.

3:33From this point here, we can do what is

3:34known as calling the function.

3:37Now simply put, to call the function,

3:39all we have to do is to specify the function name followed

3:42by a parentheses, and that's all it takes

3:45to call a Python function.

3:47So say my_test_function, followed by parentheses,

3:50and if we just save this example3 and we'll say Python3,

3:55exampl3.py hit enter, and that's no printout.

3:59OK, so not a big revelation here, but let's expand on this.

4:02Now what I want to stress here is

4:03that even though we went out to the margin here,

4:06that was just to escape the actual definition

4:09of the function here.

4:10Once we're out of that, we can call

4:13it pretty much from anywhere.

4:14So let me just highlight what I'm talking about here.

4:17So let me just delete this here, and let

4:18me do a simple for loop.

4:20I'll say for num in range.

4:23And I can just say 0 to 4.

4:26And if I hit colon and then enter,

4:28that's going to indent me again.

4:30But what I can do here is I can call this test function,

4:34I can say my_test_function, and how do we call?

4:36We use a parentheses.

4:37Now, this is actually going to work,

4:39even though it's indented one, two, three, four as part

4:43of this code block here.

4:44It's not within the definition of the function.

4:48So as long as we escape that, we're going to be OK.

4:51So after I show you this here, and if we run the script,

4:53that should not print out multiple times.

4:55We should loop through it and continually

4:57keep calling the function.

4:58So arrow up, hit enter, and as you can see here,

5:01we've actually called that function

5:02multiple times due to a loop.

5:04So ultimately, we can't be part of this indented block

5:07when we're calling the function.

5:08Once we're out of this block, we're fine.

5:11Now you may be thinking, well, John,

5:12you told me about the print function, the belt

5:15and print function.

5:16That looks a little bit different when you call it.

5:18What is the deal with that?

5:19Let me just show you this.

5:20OK, so I'll just comment these out just now.

5:22So when we call the print function,

5:24now again because this is a special built-in function.

5:27We do get a little bit of color highlighting ground here,

5:30which we don't get for our regular functions.

5:32But notice this, we don't just call the function

5:34by saying the function name and parentheses.

5:37That's not how we use the print function.

5:39That is because the print function actually

5:41takes what is called arguments.

5:43It actually has to pass something

5:44into the print function, like my name as John,

5:48something like that.

5:49Now, as it transpires right here,

5:51a little test function doesn't take any arguments.

5:55So we don't pass in any arguments.

5:56However, what if we did want to add in some argument,

6:00we want to add additional functionality?

6:01How would we do that?

6:02OK, so let's delete this, and we'll create a new function.

6:05So how do we do that?

6:06You guess that we're going to see def, and now create

6:09the name of the function.

6:10So let's create a function just called adder.

6:13Now within our parentheses, we're

6:15going to specify the parameters we want to take.

6:18Now just conceptualize these as variables, which

6:20we can pass into our function.

6:23So to create my first parameter, I'll just get a variable name.

6:26I'll just say a.

6:27And then to add another parameter, I'll do a comma

6:31and add another variable name, so I'll just say b.

6:33And as before, we do our colon, and then if we hit enter,

6:37we get our indented block.

6:39So what we are seeing here is that when someone is going

6:42to use this function, they're going

6:44to have to specify adder and then supply something which

6:48is going to represent a, and then supply a second thing

6:51which is going to represent b.

6:53And within our indented block, we're

6:55going to specify what we want to do with these variables.

6:58We're going to be taking into a function,

7:00as [INAUDIBLE] function, we'll just do a print statement

7:02once again.

7:03And I should just highlight that not all functions

7:05are about printing.

7:07This is just the easiest way to get it going.

7:09So what I'm going to do is take the first argument,

7:11the pass in which as represented by the letter e,

7:14and I'm going to actually add it to the second argument,

7:18the person which is going to be represented by b.

7:21And quite simply, that is it.

7:22If we just go back out, we are indented again,

7:25and now we have defined our new function.

7:28So how on Earth do we use this function?

7:29Well, if we just say adder, and try to call it like this,

7:32this is not going to work because this other function

7:35actually takes two parameters.

7:37It takes one here and one here.

7:39OK.

7:40So let's check this out.

7:41OK, so we'll clear the screen, if we hit arrow up, hit enter.

7:44And it tells us straight away, we have a type error,

7:46but actually missing two arguments, a and b.

7:50So clearly, we need to pass in these arguments

7:52because the function has been defined

7:54as to accept two arguments.

7:57So let's check this out then.

7:58Now just to be clear here, we don't have to type a coma b.

8:02We can type anything we want.

8:04So if I type in the number 3 as the first one,

8:08that is going to be represented by the variable a, which

8:11means that we're going to pass in here.

8:13So we'll have 3 in this place, so we'll say

8:153 add whatever the place for b.

8:19So let's say for b, we'll pass in the number 7.

8:23OK.

8:23So all that is happening here is that we call this function here

8:26and are passing and the two required arguments.

8:29What this function is going to do

8:30is going to map this one to a, and it'll map this one to b,

8:35and then just do whatever the code said.

8:37Well, the code says then here, we

8:39have to print out whatever a plus b is.

8:41Well, a is equal to 3 in this case, and b is equal to 7.

8:45So we have to print out whatever 3 plus 7

8:47is, which is equal to 10.

8:49And in effect, this other function

8:51has taken the arguments 3 and 7 and added them together.

8:55So let's check this out.

8:57Arrow up, hit enter, and our other function is printing out

9:00correctly the value 10.

9:02Now, what if we use the other function yet again?

9:04And if we passed in the variable John, which

9:07is going to act as the variable a,

9:09and we pass in the second string McGovern,

9:11which is going to take the variable b.

9:13What is the other function going to effectively do?

9:16Well, let's comment out this first call,

9:18and we'll just use the second one.

9:20OK, so clear the screen, arrow up, hit enter.

9:22Look at this.

9:23It's actually joined the two words together.

9:25And if I happen to reverse these and say McGovern, and then

9:29John, save you, arrow up, hit enter,

9:32then it's going to put them round the other way.

9:34That is because in this case, McGovern

9:37is mapping to the variable a, and John

9:40is mapping to the variable b.

9:41And what does the function say to do?

9:43It says add a first, so put whatever

9:46a is, which is McGovern, and add it to b, well, what is b?

9:50b is John.

9:50So let's add in to John here.

9:52So let's look at another example.

9:54We could create a function which would generate email addresses.

9:57So we can say def, and we just say create_email.

10:01Now we're going to pass in some type of arguments.

10:04OK, so let's say we wanted to have a username and a provider.

10:08Provider being like say outlook.com or gmail.com

10:12or cbtnuggets.com or protonmail.com,

10:16and the username is whatever username you want.

10:18Now, a good kind of practice to do here

10:21is to not just say a and b because they

10:23are pretty non-destructive.

10:24Even though you could use a and b.

10:27It's probably better to give the variables

10:30a kind of meaningful name.

10:32So we could say we want to have a username to be taken in,

10:35and then we could say, provider.

10:37OK, so we'll do a colon, and then we get our indented block.

10:41So whenever someone is going to call this function,

10:44they're going to have to provide two arguments that

10:46is going to be represented by the username variable,

10:49as well as the provider variable.

10:51So how do we actually want to manipulate these variables?

10:54Well, again, we'll just use a simple print statement

10:56for simplicity, and we'll use an f string.

10:58And what we'll say is, your new email is,

11:02and then we're going to take whatever username they pass in.

11:05So we'll pass in that variable here.

11:07And then we'll do the @ sign, because that's

11:09what we do for emails, and then we're

11:11going to specify the provider, i.e.

11:14as a @gmail as a @outlook.

11:16So we'll say provider, and we'll always just end each one with

11:20.com.

11:20So now, if we want to create an email,

11:23we have to provide two arguments.

11:25The first one that's going to be representing

11:26the username which you would put in before the @ symbol.

11:30The second argument we give is going

11:32to be represented by this provider variable, which

11:34is going to be inserted after the @ symbol here.

11:37OK, so let's try this and let's call this new function here.

11:40We'll say create_email, and I'll say my email as IPvZero.

11:46That's the first argument, which is

11:47going to be the username and then a comma.

11:49And I'll also add in--

11:51my account is going to be, let's see, I don't know, Gmail.

11:54OK.

11:55So if I call this function--

11:56I've given it my two arguments, that's what I need,

11:59let's see what happens then.

12:00OK, So if we arrow up, hit enter.

12:03Now, we have used this function to generate our email address,

12:06IPvZero@gmail.com.

12:07Now if you recall, when we dealt with Python strings,

12:11we talked about something called a docstring.

12:14A docstring is basically a string

12:16which is used for documentation, and it really is very often

12:19used within our function.

12:20So let me just show you what this is all about.

12:22So before we actually do anything with the code,

12:26let's go here, and we just go to our indentation block.

12:28What I'm going to do is use my three quotation marks.

12:31That's how we can use a docstring.

12:32We'll do 1, 2, 3, and VS code automatically

12:35adds another 3 to close off.

12:37And anything within these three quotation marks

12:40is going to be treated as a docstring.

12:42And basically, we just want to describe

12:45what this function does.

12:46So we can say this function takes two arguments.

12:50First, pass in a username.

12:53Second, pass in your email provider.

12:57And then we could just say, e.g, Gmail, Outlook, et cetera.

13:01So that means anyone reading this code has got a rough idea

13:04of what you intend to do.

13:05Now, our code, or rather our function here,

13:08is pretty simplistic.

13:09You can probably infer just by looking at it.

13:11But with code, which is more complex,

13:13then these docstrings can be really invaluable.

13:15Now here's the thing, remember when

13:17we used the help function earlier on within this skill?

13:20Ultimately, what the help function is going to do

13:22is show us what that docstring is.

13:24So as opposed to calling this create_email function,

13:28let's pass that into the help function.

13:30We'll say help(create_email).

13:32OK.

13:33So let's save this and see what happens here

13:35if we run this script just by using the help

13:37function with our newly created custom create_email function.

13:41So let's arrow up, hit enter, and look at this.

13:44It's showing us our docstring that we

13:46created within our triple quotation marks.

13:48This function takes two arguments.

13:50First, pass in the username.

13:51Second, pass in your email provider, e.g.

13:54Gmail, Outlook, et cetera.

13:56That is exactly what we wrote within our docstring,

13:58and that is, in essence, what the help function does.

14:01It pulls the docstring.

14:02Now just for variety, well, I'll show you

14:05how you can also access this in another way, which

14:07some people do.

14:08You can just pass this to the print function

14:10and then give the name.

14:12And I can say doc_ _.

14:15This is, in essence, the same thing.

14:17Some people prefer to do this method.

14:19I personally just like to use the help function.

14:21But if you save this, this again is

14:23going to give us the exact same information about our function.

14:26So we'll say Python3 example3.py, hit enter.

14:28And again, this prints out our docstring.

14:30Now the difference here is that this just prints this straight

14:33to the terminal, whereas using the help function

14:35actually takes you into something

14:37called layers, which gives you paging,

14:39which I tend to find as pretty handy if we happen to have

14:42a really long docstring.

14:43We can just keep hitting the space bar

14:45and running through the different pages,

14:47whereas the print function is just

14:48going to splatter it all onto the terminal right here.

14:51So you have these two options.

14:53But really, the thing to remember-- to take away,

14:55is that when they are creating functions,

14:57we are going to be using this def keywords.

14:59You specify the name of the function

15:01we want to be using lowercase and split our words

15:03using underscores.

15:04That's a strong Python convention.

15:06And then use parentheses.

15:08These can take arguments if you want to supply them,

15:11and the case if we want to take in particular variables

15:13and work with those variables.

15:14But as we saw in our earlier function,

15:16that is not a necessity.

15:17That is just a choice.

15:18Then again, we're going to use a colon and then within,

15:22again the strong convention has four spaces in,

15:24we're going to write our code as the indented block.

15:27And within that code is going to be contained

15:29within this my function.

15:31And again, just remember to make good use

15:32of those triple quotation marks, those docstrings.

15:35That is going to really help with the readability

15:37and shareability of your code with your functions

15:41and make it much easier for your team

15:42to understand what exactly is your intent when

15:45creating this function.

15:46How do we actually use it?

15:47Just like we did when we were exploring

15:49our built-in functions.

15:50OK.

15:51So that's how we can create custom function within Python.

15:53I hope this has been informative for you,

15:55and I'd like to thank you for viewing.

Passing Arguments

0:00[CBT NUGGETS JINGLE]

0:12Hey guys, and welcome back.

0:14So let's talk more about those arguments

0:16that we're passing in to our functions.

0:19Now, there are three types of arguments we can pass,

0:21and the first is going to be what

0:22is called a positional argument, and that is the most common way

0:27to assign arguments to parameters

0:29within your functions.

0:31Basically, it corresponds to the arguments

0:33that we pass in are matched positionally to the parameters

0:38that the function accept.

0:39And what we also have are default argument.

0:42With your default arguments, you can

0:44have arguments be made optional by specifying a default value.

0:48If you happen to supply a value, then that

0:51will overwrite the default. But if you

0:53don't happen to supply anything, then the default value

0:56will be used.

0:57Now, the third type is called a keyword argument.

1:01The keyword argument doesn't care

1:03about the order in which you pass in your arguments.

1:05You're going to map the arguments explicitly using

1:08key words.

1:09What does that actually look like?

1:10Well, we'll get to see an example of that in this Nugget

1:13right here.

1:14But for now, let's just recap and see what these look

1:17like in action within VSCodes.

1:19OK, so let's go and create an example here, OK?

1:21So we'll say touch example4.py.

1:25And let's go in and open this.

1:27We'll say example4.py.

1:30OK, so again, let's keep this on the simple side

1:32by just using a print statement.

1:34What we'll do here is we'll pretend

1:36we're going to gather information

1:38about a particular device that we're going to be logging into.

1:41Let's just create a function called connection_details,.

1:44So let's just say this function is going

1:46to set the parameter username.

1:48It's going to accept a hostname for the device,

1:51and we'll also specify a platform for that device.

1:54Not the most realistic example, but don't worry about that just

1:57now.

1:57So again, we're just going to print this out

1:58so we can see what's happening.

2:00But let's just say what we want to do

2:01is say ssh, followed by the username the person passes in.

2:07So we'll use an if string here.

2:09So we'll say ssh username at whatever the hostname is

2:13that they pass in.

2:14And if the platform that they pass in

2:16is equal to the value cisco, once we've

2:19done that connection, we want to automatically say

2:22enable followed by a new line, and then

2:25configure terminal, which is a Cisco command,

2:27and we'll do a new line.

2:29This is not actually creating a real connection.

2:32This is just printing out some text.

2:33It's just to give you an idea of how we may work with arguments

2:36within our functions.

2:38OK, so let's say I wanted to use this-- in fact,

2:40let me just change the name of this function to something

2:42a little bit more descriptive.

2:43We'll just call this device_login function, OK?

2:47It's a little bit more descriptive.

2:48So if we want to log in to the device,

2:50we can just say device_login.

2:52Now, what we're going to use here is positional arguments.

2:55OK, so the first parameter is the username,

2:58so that will be the first argument we supply.

3:00The second position is the hostname parameter,

3:04so that's going to be the second argument we supply.

3:07And then at the end, we have our platform,

3:09which is going to be the third parameter.

3:11So that will be the third argument we supply.

3:13OK, so what we'll do here is we'll

3:15pass in the username John.

3:17That's the first argument, so that's

3:19going to map to username based on its position.

3:22Then we're going to specify the hostname,

3:24and we'll just make this an IP address of 192.168.1.1.

3:29Let's just say that's the address of the device.

3:31And it's a Cisco device.

3:33So those are the three arguments we've passed to this function.

3:37So if we just save this, and we'll run this script here,

3:40example 4, python3 example 4-- oop, [INAUDIBLE] example4.py,

3:44hit Enter.

3:45And basically, we're simulating a login here,

3:48[INAUDIBLE] ssh with our username, with the IP address.

3:51And because it's a Cisco device, we've

3:53automatically done enable to go to privileged EXEC mode

3:55and then done configure terminal to go to global configuration

3:58modes.

3:59So that was based on positional arguments.

4:01Here's the thing.

4:02If we scroll on down, what about we have some default values?

4:06So the way we can specify default values

4:09is actually within our function.

4:12So what I can say here is let's just

4:13assume by default, unless someone supplies

4:16a value for a platform, we're going to assume the default

4:19value is Cisco, OK?

4:21And then when we do that, is as you see here,

4:23we just have an equal sign next to that parameter,

4:26and then we correspond that or rather correlate that

4:28to a particular value, a default value-- in this case, Cisco.

4:32So let's save this now, and what I can do here

4:35is I can actually remove this last argument,

4:38because effectively, we've supplied argument one, which

4:40is going to map to the username based on its position.

4:43We've supplied the second argument,

4:45which is going to map to the hostname based on its position.

4:48And we don't actually have a value for the third argument,

4:52but this function does have a default value,

4:55so if we don't actually explicitly assign

4:57that third value, then it's going

4:59to be platform is equal to Cisco.

5:01So again, let's just supply two arguments this time,

5:04and rely on that default value to fill in the third.

5:07Let's arrow up, hit Enter, and this still

5:10works just OK, even though we only supplied

5:13two positional arguments.

5:14The third argument was just a default.

5:16But again, what we can do is we can override that default

5:20by supplying a third argument and its position,

5:22but this time we'll just say junos for Juniper devices.

5:25And if I save this, this part of the code

5:28here is not going to execute.

5:29We're just going to login, but we're not

5:31going to try to say enable configure terminal,

5:33because that's a Cisco command, or rather Cisco commands.

5:36OK, so let's try this again.

5:37Hit Enter, and now we're just doing a login.

5:39We're not doing configure terminal.

5:40That is because the third argument we supplied was junos,

5:44and that overwrote the default value of Cisco,

5:47and because of that, this condition was not true.

5:50Therefore, the indented code here did not execute.

5:52OK, so those are our positional arguments,

5:55and we've seen here a default argument.

5:57And in fact, let's add another condition

5:58and we'll just say elif platform is equal to junos,

6:03we can just say cli, which takes you into the Junos CLI,

6:06and then we'll just say edit, which allows

6:08you to edit the configuration.

6:09So this time, let's actually run this again.

6:11Let's quit to test this.

6:12We're going to override the default value.

6:14Hit Enter.

6:15Because we've got a Junos device supplied,

6:17we're now going to say cli, then edit, as opposed

6:19to enable config terminal.

6:21OK, so like I said, those were positional arguments,

6:23default arguments.

6:24Let's see how we can use keyword arguments.

6:26So basically, the same type of logic does apply.

6:30The only thing is we don't actually

6:31care the order in which we supply

6:34the arguments, whereas earlier, the order was absolutely vital.

6:38The order told us to map John to the username

6:41and map this IP address to the hostname, and at the end,

6:44this was actually a platform.

6:46If we happen to supply Junos at the start

6:48here, if we try to log in with the name

6:50Junos as opposed to the name John, because of its position.

6:54But let's be more explicit by using what

6:56is called keyword arguments.

6:58Here's what we're going to do here.

7:00We can use the exact same function,

7:02so we'll call it by saying device login.

7:04This time, though, as opposed to just saying John and relying

7:08on its position and an IP address

7:11and relying on its position again, what we're going to do

7:13is just specify the parameters that the actual function

7:17takes-- say, for example, username, hostname,

7:19and platform-- and directly map them to our arguments.

7:23And it doesn't matter the order in which we supply them,

7:25because it's based on the keyword that's explicit.

7:28So what I can do now is just say hostname is equal to,

7:31and I'll say the hostname is equal to 192.168.1.1.

7:35And I can say the platform, and you

7:38can see this is auto-completing with VSCode.

7:40So the platform is equal to cisco,

7:42and I'll say the username is equal to john.

7:45So looking at this, this is completely out of order

7:47based on its' position.

7:48We've got hostname here, whereas hostname is a second argument.

7:51We've got platform here in position two,

7:53whereas it's defined here as position three.

7:56And the username I've got in position three,

7:58whereas that's actually the first argument here.

8:00But again, because of being explicit,

8:02the function is not in any doubt.

8:04It'll say when we pass this in, map the username to john, map

8:08the platform to cisco, and map the hostname to 192.168.1.1.

8:13And then we can just fill in all these variables.

8:15Username is John, hostname is 192.168,

8:19and the platform is going to be Cisco,

8:20which this means this condition is going to meet.

8:23So it doesn't actually matter the order any more.

8:25We're being super explicit now.

8:26So watch this.

8:27Even out of order now, we should still

8:29get our Cisco CLI because everything should still work.

8:32Let's hit Enter, and look at this quite the thing.

8:36This is using keyword arguments, so

8:38really is much more explicit.

8:40Then what we can actually do here

8:41is use a mixture of all of these.

8:43So what I could say here at the start here is device login,

8:46and I can use the position number one, john,

8:49without specifying anything, and that's

8:51going to automatically map to the username.

8:53And then, I could say platform is equal to cisco,

8:57and the hostname is equal to 192.

9:00Let's use a different IP address for a bit of change.

9:03We'll do all the 8s.

9:03So clearly, we actually have a mixture of keyword arguments

9:07and positional.

9:08Here is our positional argument, position one,

9:10which is going to be inferred as the username,

9:12but these two have actually been reversed.

9:14But check this out-- this should still work quite the thing.

9:17So let's save here, and we'll clear the screen.

9:20So arrow up, hit Enter, and it works, no problem at all.

9:23Now, the caveat here, and this is a big caveat,

9:25is that once you start using keyword arguments,

9:28you cannot use positional arguments after it.

9:31What do I mean when I say here?

9:32Well, let's see this, OK?

9:33So I have a positional argument here, so that's fine.

9:36But now I specified a keyword argument here, OK?

9:40This is a keyword argument.

9:41So that means that anything that comes

9:43after, if I had lots of arguments here,

9:45anything after this point, they all

9:47have to be keyword arguments.

9:48We can't use positional arguments

9:50after we have specified the keyword argument.

9:53So let me show you this.

9:54So let me start by saying username is equal to john,

9:58so it's a keyword argument.

9:59Now we can't use positional arguments.

10:01We can't say 8.8.8, and that's going to be as inferred

10:04as the hostname, and cisco.

10:06This is not going to be allowed, because like I say,

10:09the positional arguments came after keyword arguments.

10:12We can't do that.

10:13So let's arrow up, try again, and look at this,

10:15positional argument follows keyword argument is not

10:18allowed.

10:19What we could do is just get rid of the keyword argument here,

10:22and suddenly this becomes fine because this is all

10:24positional arguments.

10:25But again, if we happen to make this one a keyboard

10:28argument by saying hostname is equal to,

10:31then this one here is going to get the red squiggly line

10:33because a positional argument has

10:35came after the keyword argument-- not allowed.

10:37So just be aware we have different ways

10:39to pass in arguments to our functions.

10:41We have positional arguments, we also

10:43have default arguments, which allows

10:45you to omit particular arguments and just

10:48have default values set and used,

10:50and we can also specify keyword arguments, which

10:53allows us to jumble up the order in which we supply arguments

10:57to a function.

10:58We can have a mixture of both, so long as we have positional

11:01arguments and then keyword arguments after,

11:04but we cannot have positional arguments come after a keyword

11:07argument has been defined.

11:09OK, those are some of the details of using arguments

11:12with functions.

11:12I hope this has been informative for you,

11:14and I'd like to thank you for viewing.

The Return Statement

0:00[AUDIO LOGO]

0:12Hey, guys, and welcome back.

0:14So like I said at the very start of this skill

0:16is that a function always returns a value.

0:19And by default, that value is going to be none.

0:22Now you may be thinking, well we've

0:23been doing all these print statements.

0:25Is that not returning the value?

0:26Are we not getting something back?

0:28That's actually a little bit different from what

0:30I'm talking about.

0:31So what we're going to do in this Nugget right here

0:33is to use the return statement.

0:36So what the return key, what it's going to do, is it's

0:38going to send back the result of this function as opposed

0:42to just printing something out.

0:43Ultimately, we want to use the return statement

0:46when we want to assign the output of a function

0:49to a new variable.

0:51What exactly am I talking about here?

0:52Well, the best thing to do is to see this in action.

0:55So let's check it out.

0:56OK.

0:56Let's say, touch example5.py.

0:59And let's dive right back in.

1:01So let's use a similar example that we saw before.

1:04We'll just create a function just called generate_email.

1:07And we're going to take a user name as well as a provider.

1:10Now to build on the knowledge we want,

1:12let's say we want to specify a default value for the provider.

1:16Therefore, meaning that if they don't actually

1:18specify a provider, they're going

1:20to have a provider inferred for them.

1:22So the way we can do that, as you know, is an equal sign

1:25and then we'll specify the default values.

1:27So have a default value of a Gmail address.

1:30OK.

1:30Coo.

1:31So now with our colon, we get out indented block

1:33and now we began rating our codes.

1:35So what I'm going to do here is we

1:37can see the email is equal to--

1:39so we'll email_addy is equal to.

1:42And we can use our f-string.

1:43So we'll say username at provider,

1:46and we'll just say .com And we'll close off our quotation

1:49mark.

1:50Now recently, all we've been doing

1:51is using print statements.

1:52So that's what we'll do first.

1:54We'll say print email_addy.

1:55So like I said, we can call this by saying generate_email.

1:58And we can just gather our username

2:00because we have a default value of Gmail.

2:02So unless you specify something else,

2:04we'll get a Gmail address.

2:05We'll say ipvzero and we'll save this.

2:08If we run the script, python3 example5, and hit enter.

2:13Now our Gmail account has been generated.

2:16So this is printed out, quite the thing.

2:18But what if we actually want to do something with this Gmail

2:21account that we've created?

2:23Like we want to manipulate it further within our program?

2:25What we want to do here is not just print out.

2:28We want to instead return our email address.

2:31So instead of printing it, we'll use this keyword return.

2:34And we'll just give the name of the variable here.

2:37Now watch what happens.

2:38If I save this and I try it this script,

2:41the output might surprise you.

2:43We'll hit enter, and it appears like nothing has happened.

2:46But something has happened.

2:48We returned our variable back to the caller.

2:52So what am I talking about here?

2:53Well this part here is where we called the function.

2:56Now what we can do here is we can actually

2:58assign the result of this function call

3:02to a variable now.

3:03So let's say outside here, we can say, my_email is equal to.

3:08And we can make it equal to the function.

3:10Because what were effectively saying here

3:12is that this variable here, my_email,

3:16is equal to the result of this function, whatever it returns.

3:21Now what is this returning.

3:22It's returning the email addy variable that we generate here

3:26by passing another value.

3:28So now what we can do here is after you return it,

3:31we can still have the option to print it,

3:33but we print it outside here.

3:34We'll say, print my_email.

3:36And let's save this.

3:37So now the process is a little bit different.

3:39We are returning it back to the caller,

3:42assigning it to a new variable, and then printing

3:45that new variable.

3:46But because we have access to this new variable,

3:48we don't just have the option to print it.

3:50We can do other things.

3:51So what do I mean when I say we can work with it?

3:53So let me show you.

3:53Let me just comment this part out here.

3:56And what I'll do here is I'll create a list of names.

3:58We'll just say, my_names.

4:00And we'll say, Trevor.

4:01We'll say Knox.

4:02We'll say John.

4:03We'll say Simona.

4:04And we'll just say Bob.

4:05And what I want to do is to generate an email

4:08for all of these names and to create a new list.

4:11So what I'll say here is my_new_list.

4:13Let's call it my_email_list.

4:14That's a bit more descriptive.

4:16And right now, that is going to be an empty list,

4:18because we don't actually have any emails generated.

4:21What I'm going to do here is I'm going

4:23to look through all the names in the my_names list, Trevor,

4:27Knox, John, Simona and Bob.

4:29So I'll say for name in my_names names.

4:31And if I hit enter, we're going to get an indentation.

4:35So what I want to do is for every single name,

4:37I want to run the generate_email function.

4:41OK.

4:42So all I need to do to call that is say generate_email.

4:44And I need to pass in a username.

4:46And the username is going to be this name

4:49variable we're looking through.

4:50So I'll just say name here.

4:52Now I could just leave it at name,

4:53because as we know we have a default

4:55value for provider which would give everyone a Gmail address.

4:59But because we all work for CBT Nuggets,

5:01let's just say CBT Nuggets will be our provider.

5:04So what we're doing here is for every name within this list,

5:07we're going to take out that name.

5:09And we're going to pass that name

5:11into our generate_email function, which is up here.

5:14So that becomes the username each time.

5:16And we'll specify the provider of CBT Nuggets.

5:19So everyone should have their first name here, i.e.,

5:22trevor@cbtnuggets.com, knox@cbtnuggets.com, so on

5:26and so forth.

5:27But remember, we're actually returning

5:30the email address, which means that we can assign this

5:32to a new variable, assign the result of this function

5:36to in new variable.

5:37So let's do that just now.

5:38And we'll just say, new_email is equal to.

5:42So like I said, we have this empty list right here,

5:44my_email_list.

5:45And all we're going to do here is say, my_email_list,

5:49and we'll use the append method.

5:51And we'll append the new email that we are generating based

5:54on this variable that we can now assign

5:57the result of this function to, because that's

5:59how the return key works.

6:00So now at the end of all this, we

6:02should actually have a Python list with all of our emails.

6:06So what we can do here is move out our indentation level.

6:08And we'll just print out my_email_list.

6:11So if I save this, and say example5.py, and hit Enter.

6:16And look at this.

6:17We have a Python list with all of our usernames appended

6:21to this email.

6:22And that's because we got the actual result of our function

6:26returned back to us.

6:27We could assign it to a variable and then work with it,

6:30i.e. we can append it into this list.

6:32And if we wanted, what we could do

6:33is we could actually unwrap that, and say

6:35for email in my_email_list.

6:38And then we can do a print statement

6:39and say f-string, new email accounts generated.

6:43And then just pass in the email which we are looking through.

6:46Save here, clear the screen, and hit Enter.

6:49And there we go.

6:50So clearly here even though the print statement and the return

6:53statement may seem like they have similar functionality,

6:57the key here is that the return statement

6:58allows us to assign the output of a function

7:01to a new variable.

7:02This is going to be very useful when we actually

7:04want to work with the results of what we get from a function.

7:07Okie dokes, so I hope this has been informative for you.

7:09And I'd like to thank you for viewing.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Request a Demo

Just need Python Functions? Enroll from $300/yr (16 skills)

Request a Demo