Skip to playerSkip to main content
In this video, you will learn Python functions and how they are used to build structured cybersecurity tools. Functions in Python help you organize code, reuse logic, and build modular security scripts for automation and penetration testing.

Understanding functions is essential when building Python tools for cybersecurity because real-world security programs rely on reusable and clean code structures.

What you will learn:

. What are functions in Python

. How to define and call Python functions

If you want to build professional Python tools for cybersecurity, mastering functions in Python is a must.

This lesson is part of the course: Building Python Tools for Cybersecurity.

Python Functions Explained | Functions in Python for Cybersecurity Tools

#pythonfunctions #functionsinpython #PythonForCybersecurity #cybersecurity #pythonprogramming #securityautomation #ethicalhacking #hjcyberx #cybersecurity
Transcript
00:06Let's talk about functions.
00:09Function is repeatable block of code.
00:12So you can have some code, for example x is 3, y is 5, print x, print y, whatever.
00:21And now whenever you want to output that you would have to call those print functions,
00:24right?
00:26So, let's say you want to do that 10 times, you would have to copy those lines of code
00:32again, again, again, again, again, well you see where this is going.
00:38It would be just very inconvenient, especially if we have hundreds or thousands of lines of
00:42code to keep repeating those lines of code.
00:48So what programmers did a long, long time ago is to create functions.
00:52A function is one or more lines of repeatable code, so you can just see it as a function,
01:01can be any name, for example, show x, y.
01:07You write dev, Python knows that you're defining a function.
01:11You need those brackets and a colon at the end.
01:14Four spaces for everything that belongs to the function, and we'll say print x, print y.
01:20Now, the function wouldn't know about x and y now, so we need to pass that here, x, y.
01:27You can remove all of this, and say show x, y with whatever numbers you want, x and y.
01:34When we run it, you'll see it shows x and y.
01:40And whenever we want to repeat those lines of code, we just call the function again.
01:44So now you'll see it does it many times.
01:48Every time it jumps to this function, runs the code, goes back, jumps here again, goes
01:54here, does it again, and so on.
01:56So you can use functions to repeat code, and they can take parameters.
02:01This can be any parameters you want, so we can also, for example, write numbers here.
02:13And when you run it, you'll see it outputs those numbers.
02:17So functions are repeatable box of codes, in this case two lines.
02:20They can be hundreds of lines, it doesn't really matter, even one line is possible.
02:25And you can have different functions, for example, maybe you want to show some information.
02:30So show info, maybe name, job and city, then you can say print.
02:41Name is name, so you can output that.
02:48And we can say show info, with that information, let's say he's a programmer, from Peru.
03:06And now you'll see it calls the function.
03:11It seems we made a small error here.
03:13We need to have a colon here on the end, otherwise it won't run.
03:16So all functions should have a colon in the end.
03:19And you'll see it outputs that text, so whatever you pass to the function.
03:24And if you call it again, you can write your own variables in here.
03:29So let's say Alice is an airline pilot from Aruba.
03:39And you'll see it calls those, it calls that information, of course you can do it again.
03:47So show info, Bob is a builder from Belgium.
03:54See it runs those same lines of code, in this case one line of code, but of course you can
03:59have multiple, let's say you have multiple lines of code.
04:10And if you run it, you'll see it runs those lines of code, every time you call the function
04:15with the variables.
04:17Now this also works for lists, so for example if you have a list of people, let's say people,
04:26and there is Peter, Alice, Bob, then you can say for p in people, or let's make it a more
04:40explicit person, call the show info function, with the person's name.
04:46And you'll see it also needs the job and city, but for now we don't have that, so we can
04:51say
04:51it like this.
04:54We run it, and you'll see it calls the function for every item in the list.
05:02You'll see Peter here, it runs the function for Peter.
05:05Then it goes to Alice, so it does that for Alice, and runs the function, and finally we
05:12have Bob.
05:13So it calls the function show info, and it does it for Bob, that's why you'll see the
05:17output like this.
05:19Of course you can also do the same for job and city, you could have a list of jobs.
05:32And a list of cities, but perhaps you would want them to be together.
05:39So I would have a list in a list.
05:51So now we have lists inside lists.
06:08And instead of, we can still call it Peter, but now we have person zero, person one, and
06:15person two.
06:16Now that doesn't refer to another person, but to the position of their parameter.
06:22If you run it, you'll see now it outputs all of the information for every person.
06:27To make it more explicit, we can put some line here.
06:32So you'll see it calls the function for every item.
06:36So functions let you repeat code.
06:38You can run it, use it even with long lists in this case.
06:42It can be much more simple.
06:44For example, you can have a function sumab, which just turns a plus b, or sorry outputs
06:52a plus b, and show for example 10 plus 10, and you'll see.
07:01In this case, we're not calling the function sum, and you'll see it outputs 20.
07:09So you can have also like functions.
07:12If you have a variable in a function, like variable x, then that variable x only exists
07:21in those two lines of code here.
07:22So only within the four spaces.
07:24If we try to print x here, you'll see it throws an error.
07:30Even it shows already an underscore inside Visual Studio code.
07:33That's because x doesn't exist at this point.
07:35It only exists within a function.
07:37If you run it, you'll see an error.
07:39You'll see x is not defined.
07:41We defined it here.
07:42But when you define a function inside a function, it only exists within a function.
07:47So any variable you define inside here, that only exists within the block of code of the
07:55function.
07:55And that block of code is indicated by four spaces.
07:59If you want to keep the output of a function, you can say return x, and then store that output.
08:06So x is, and then the function name, and output it.
08:09If you run it, you'll see it outputs the data.
08:13So that's all about functions.
08:16Functions let you pass parameters.
08:18So the parameters you want.
08:28And you'll see, now we have a function with one parameter.
08:32So functions are just repeatable blocks of code.
08:35You can pass any variable.
08:36And if you want to store the output, you use the return statement.
08:40You can get the
08:40button to view all the elements.
08:42And now we are going to take reference to functions.
Comments

Recommended