1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Education
  6. >
  7. Python Functions | Complete Python Tutorial for Beginners #1

Python Functions | Complete Python Tutorial for Beginners #1

Available In Following Subtitles
English
Variant 1
Posted on:
Learn how to write your own functions in Python to break down your code into smaller, maintainable chunks for better organization and reusability. Follow step-by-step guide on defining custom functions with best practices on naming and structuring. Dive into the world of Python functions starting with the essential DEF keyword and indentation. Perfect for absolute beginners in Python programming.
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:03
so far you have learned how to use some of the built-in functions in Python such
0:09
as print round and so on in this section you're gonna learn how to write your own
0:14
functions now you might ask but why do you even need to write our own functions
0:18
well when you build a real program that program is going to consist hundreds or
0:25
thousands of lines of code you shouldn't write all that code in one file like we
0:29
have done so far you should break that code into smaller more maintainable and
0:34
potentially more reusable chunks we refer to these chunks as functions so
0:40
let me show you how to create your own custom functions we start with the DEF
0:45
keyword which is short for define next we need to give our function and name so
0:51
let's call this greet all the best practices you'll learn about naming your
0:55
variables also apply to naming your functions so make sure your function
0:59
names are meaningful descriptive use lowercase letters to name your functions
1:05
and an underscore to separate multiple words now after the name we need to add
1:11
parentheses you will see why shortly and then we'll add a colon now what is going
1:17
to happen you know it you're gonna get indentation which means the following
1:22
statements will belong to this function so here I'm gonna add two statements hi
1:29
there and welcome aboard both designs belong to this function because they're
1:36
indented now we're done with this function we need to call it so we remove
1:42
the indentation and we add two line breaks after this function this is what
1:48
Papp 8 recommends to keep our code clean and maintainable now if you forget to
1:52
add two line breaks don't worry as soon as you save the changes auto-pay of
1:57
eight will automatically add these line breaks for you let me show you so I'm
2:01
gonna remove these line breaks and call this function create with parentheses
2:06
just like how we call the built-in functions now save the changes there you
2:12
go so we get two line breaks after our function now
2:17
let's run this program so we get these two messages on a terminal
2:29
now here is a question for you what is the difference between the greed and
2:34
trent functions the difference is that this print function takes an input
2:40
whereas our greet function doesn't take any inputs so let me show you how to
2:45
pass inputs like first name and last names of this function when defining a
2:50
function in between parentheses will list our parameters so here we add two
2:56
parameters like first under like name and last underline name now when calling
3:03
this function we need to supply two values for those parameters we refer to
3:08
them as arguments so maash Hamadan e these are the arguments to the greet
3:15
function that's one of the terms that a lot of developers out there don't know
3:19
they don't know the difference between parameters and arguments a parameter is
3:24
the input that you define for your function whereas an argument is the
3:28
actual value for a given parameter okay now let's change line two and instead of
3:35
saying hi there we can greet a person by their full name so we can convert this
3:39
to a formatted string and has two fields here first name as well as last name
3:48
save the changes run the program and this is what we get in terminal now this
3:54
function is more useful we can reuse it and call it with different arguments
3:58
so let's greet John Smith as well run the program so we get hi Muhammad on e
4:07
and hi John Smith now note that by default all the parameters that you
4:12
define for a function are required so here our greet function takes two
4:17
parameters if I exclude one of these arguments and save the changes you can
4:23
see we have this red underline so pilant is complaining and saying there is no
4:28
value for argument last name also if we run the program we get this type error
4:33
greet missing one required positional argument
4:38
so let's put this back now later I will show you how to define optional
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome