Premium Only Content
Functions - The basics |Section 3|Celestial Warrior
1
00:00:00,500 --> 00:00:06,120
Hi again, in this video you'll learn how
to create a function in Python and how
3
00:00:06,120 --> 00:00:13,380
to use that function, so let me go ahead
and create a script. Let me name this my
5
00:00:13,380 --> 00:00:23,730
function.py and let's go ahead and
create a function. Creating a function
7
00:00:23,730 --> 00:00:30,210
will always begin with the DEF keyword
which is, which means define. Now that is
9
00:00:30,210 --> 00:00:33,980
followed by the name of the function, so
you need to give your function a name
11
00:00:33,980 --> 00:00:43,350
let me name this minutes to hours and
as you may guess already what I'm
13
00:00:43,350 --> 00:00:50,910
creating is a function that will convert
minutes to hours, so you create the
15
00:00:50,910 --> 00:00:56,940
procedure on how to convert minutes to
hours and then you can call that
17
00:00:56,940 --> 00:01:02,129
procedure, so you can call that function.
You can give that function any values
19
00:01:02,129 --> 00:01:07,380
when you call it and the function will
produce an output depending on the value
21
00:01:07,380 --> 00:01:12,659
and depending on the formula, on the
procedures that you write in here, so
23
00:01:12,659 --> 00:01:16,860
let's go ahead and do that, minutes
to hours. Now functions normally they have
25
00:01:16,860 --> 00:01:23,549
some input and they produce some output.
The input goes inside these brackets
27
00:01:23,549 --> 00:01:29,130
here. In our case the input could be
minutes so this is just a variable, you
29
00:01:29,130 --> 00:01:32,430
can give it any name that you want, you
can give it any name that you want for
31
00:01:32,430 --> 00:01:38,939
the function name as well, so the input
is minutes and then the output will be
33
00:01:38,939 --> 00:01:43,610
hours, but you don't write the output here.
Here what to do you always choose a
35
00:01:43,610 --> 00:01:52,340
column, press enter and that will be auto
indented, so as you see the functions
37
00:01:52,340 --> 00:01:56,969
everything that you write below the
first line of a function definition is
39
00:01:56,969 --> 00:02:02,850
indented and every line after that, so the
DEF keyword, the name of function, the
41
00:02:02,850 --> 00:02:09,720
input and here we write the body of the
function. What we want to do is let's
43
00:02:09,720 --> 00:02:18,850
say variable house equals to
minutes / 60 that should give us the
45
00:02:18,850 --> 00:02:26,290
hours. Note that if you are on Python 2
you want to do 60 point zero because
47
00:02:26,290 --> 00:02:31,540
if you do that you get an integer as
an output, not a float, so in
49
00:02:31,540 --> 00:02:37,030
Python 2 you have to explicitly pass
a float when you do division to get a
51
00:02:37,030 --> 00:02:42,190
number, to get a float with decimal
point. I am in Python 3, so I don't
53
00:02:42,190 --> 00:02:48,340
need to put 60.0 there. And yeah, this is a
simple function, so this is what the
55
00:02:48,340 --> 00:02:52,450
function will perform and then the last
line of the function is normally a
57
00:02:52,450 --> 00:02:59,790
return keyword, so notice that these are
reserved keywords def and return, and
59
00:02:59,790 --> 00:03:06,430
what I want to return is, well gets what?
The output which is hours, so that's the
61
00:03:06,430 --> 00:03:11,769
idea, we are getting minutes. The user
will input a value for that later when
63
00:03:11,769 --> 00:03:17,230
you call a function and Then minutes
will be divided by 60 and that operation,
65
00:03:17,230 --> 00:03:22,810
that number will be stored in the hours
variable which is a variable that exists
67
00:03:22,810 --> 00:03:27,700
only inside the function so this is
called a local variable. If you call it
69
00:03:27,700 --> 00:03:32,470
outside the function it will not work,
but we'll cover that later. And so then
71
00:03:32,470 --> 00:03:41,130
we return the value of that variable.
Now I'll save this and open the terminal.
73
00:03:45,359 --> 00:03:58,329
Make some space there and let me execute
the script. You get nothing and the
75
00:03:58,329 --> 00:04:04,390
reason for that is because what you did
here is you just define a blueprint, you
77
00:04:04,390 --> 00:04:10,450
are not using the blueprint, so what you
do next is you need to go ahead and use
79
00:04:10,450 --> 00:04:17,530
that blueprint, so which means you
need to call that function, so backspace
81
00:04:17,530 --> 00:04:24,550
to go here to unindent. Indenting
means that you are done with the
83
00:04:24,550 --> 00:04:27,700
function definition so when you start a
new line,
85
00:04:27,700 --> 00:04:32,680
right here that means you are not
writing inside function and so to call a
87
00:04:32,680 --> 00:04:37,840
function what you do is you just call
the name of the function, minutes to hours
89
00:04:37,840 --> 00:04:44,920
and then you open brackets, then inside
brackets you pass a value that you want
91
00:04:44,920 --> 00:04:51,400
to use as input, so let's say you want
70 minutes, you want to convert them to
93
00:04:51,400 --> 00:04:58,510
hours, save the script go and execute
again you don't get any output, but let's see.
95
00:04:58,510 --> 00:05:02,770
As I said you won't get any output,
so why is that?
97
00:05:02,770 --> 00:05:07,630
Well that's because you didn't perform a
print operation there, you didn't call a
99
00:05:07,630 --> 00:05:16,780
print function so what you do is you
print so open brackets close brackets
101
00:05:16,780 --> 00:05:22,630
and inside brackets give the function
code, save your script, go here, execute
103
00:05:22,630 --> 00:05:28,900
and yeah you get the hours, so the
minutes is equal to one point one six
105
00:05:28,900 --> 00:05:37,060
six six hours so it's important to know
that this value here, this is just a
107
00:05:37,060 --> 00:05:44,550
number, so you can see that by doing type.
Execute. Sorry, I didn't print it out.
109
00:05:53,130 --> 00:05:59,530
And so this is a float number, so just a
number. The function gets an input and it
111
00:05:59,530 --> 00:06:04,060
generates an output which is defined by
the hours variable so whatever is
113
00:06:04,060 --> 00:06:10,170
storing the hours variable, that is the
value that the function will output and
115
00:06:10,170 --> 00:06:16,510
yeah, you can do things like you know you
can use this output to do other
117
00:06:16,510 --> 00:06:20,860
operations, let's say you want to add ten
hours there and you want to print that
119
00:06:20,860 --> 00:06:28,210
output, so expect this to be 11.1666.
And so on, and of course
121
00:06:28,210 --> 00:06:34,470
now in your script you can add more
functions if you like, you know like that.
123
00:06:34,470 --> 00:06:40,050
Like seconds to hours. You pass seconds
there
125
00:06:40,050 --> 00:06:47,310
and here seconds.
Return hours and so on and of course this
127
00:06:47,310 --> 00:06:56,639
works separately, so you can say print
seconds to hours and past there actually
129
00:06:56,639 --> 00:07:05,449
we have to change this because this
would be divided by 3600, and let's say
131
00:07:05,449 --> 00:07:14,400
this number of seconds, execute and you get
2.0 hours and you also get the other
133
00:07:14,400 --> 00:07:21,259
output in here and this one here.
Now something important to know is that
135
00:07:21,259 --> 00:07:28,080
these are custom functions and this one
here is also function but this is a
137
00:07:28,080 --> 00:07:33,659
built in function, so constant functions
are functions created by you and built in
139
00:07:33,659 --> 00:07:39,000
functions are functions that are created
by someone else, so in this case print is
141
00:07:39,000 --> 00:07:44,370
created by Python authors and print also
has its own code, but it's more a
143
00:07:44,370 --> 00:07:49,680
low-level code and it's actually
read-only, so you won't be able to see
145
00:07:49,680 --> 00:07:53,669
the code for print. Yeah, this is an
introduction to functions. In the next
147
00:07:53,669 --> 00:07:58,080
lecture I'll show you some more advanced
features of functions, so let's see
149
00:07:58,080 --> 00:08:00,529
you there.
-
5:53
SLS - Street League Skateboarding
5 days agoBraden Hoban’s San Diego Roots & Hometown Win | Kona Big Wave “Beyond The Ride” Part 2
48K12 -
6:03:57
TheBedBug
7 hours ago🔴 LIVE: EPIC CROSSOVER - PATH OF EXILE 2 x MARVEL RIVALS
36.2K1 -
1:12:45
The Quartering
5 hours agoTerror In New Orleans, Attacker Unmasked, Tesla BLOWS UP At Trump Tower! Are We Under Attack?
85.2K98 -
1:32:08
Robert Gouveia
7 hours agoNew Year TERROR; Trump Speaks at Mar-a-Lago; Speaker Johnson FIGHT
75.2K63 -
22:21
Russell Brand
1 day agoVaccines Don't Cause Autism*
152K695 -
2:05:27
The Dilley Show
7 hours ago $20.92 earnedNew Years Agenda, New Orleans Terror Attack and More! w/Author Brenden Dilley 01/01/2025
88.1K34 -
25:45
Outdoor Boys
3 days ago3 Days in Arctic Survival Shelter - Solo Bushcraft Camping & Blacksmithing
65.5K21 -
2:59:05
Wendy Bell Radio
13 hours agoAmerica Is Back
113K112 -
1:45:57
Tucker Carlson
5 days agoAaron Siri: Everything You Should Know About the Polio Vaccine, & Its Link to the Abortion Industry
148K185 -
1:46:38
Real Coffee With Scott Adams
9 hours agoEpisode 2707 CWSA 01/01/25
54.3K36