Functions in Python | Introduction | Python for beginners #lec56
Summary
TLDRThis video script introduces the concept of functions in Python programming. It explains the necessity and benefits of using functions through real-life analogies, such as sharing a recipe or a mother's multitasking. The script outlines how to define and call functions, emphasizing code reusability, readability, and time-saving aspects. It also distinguishes between built-in and user-defined functions, providing examples and demonstrating the process with a practical coding exercise in Reebok's Blockly World.
Takeaways
- đ Functions in Python are blocks of code that perform specific tasks when called.
- đ Functions help in organizing code by grouping related statements together, making the code more modular and easier to maintain.
- đČ The analogy of a recipe for idli is used to explain the concept of functions, where the recipe (function) can be followed (called) multiple times to achieve the same result.
- đ©âđ« The importance of functions is emphasized by showing how they can save time and effort by avoiding the repetition of code.
- đ The definition of a function in Python uses the 'def' keyword, followed by the function name and parentheses, which may include parameters.
- đ Functions can be called with parameters to perform different tasks based on the input, demonstrating their versatility.
- đ§ Built-in functions are pre-defined in Python and can be used directly, such as 'print', 'len', 'max', and 'min'.
- đ User-defined functions are created by the programmer to perform specific tasks as needed, enhancing the functionality of the program.
- đ The benefits of using functions include increased code reusability, improved readability, and time-saving by avoiding code duplication.
- đ€ A practical example using a robot in Reebok World is provided to illustrate how defining and calling functions can control actions and simplify programming tasks.
Q & A
What is the main topic discussed in this video script?
-The main topic discussed in this video script is functions in Python programming language.
Why are functions important in programming?
-Functions are important in programming because they promote code reusability, increase code readability, and save time by avoiding the need to repeat code blocks.
Can you give an example of a real-life analogy for functions mentioned in the script?
-The script uses the analogy of a recipe for making idli, a South Indian food, to explain the concept of functions. Just as a recipe can be written down and followed multiple times without repeating the steps, functions in programming can be defined once and called multiple times to perform a specific task.
What are the two major steps involved in using a function in Python?
-The two major steps involved in using a function in Python are defining the function using the 'def' keyword and then calling the function by its name followed by parentheses.
What is the purpose of the 'def' keyword in Python?
-The 'def' keyword in Python is used to define a function, marking the start of the function's declaration.
What is the term used for functions that are already defined in Python?
-The term used for functions that are already defined in Python is 'built-in functions'.
How does defining a function help in reducing the number of lines in a code?
-Defining a function helps in reducing the number of lines in a code by allowing you to encapsulate a block of code that performs a specific task and then reuse that block by simply calling the function, instead of repeating the code block multiple times.
What is the significance of indentation in Python function definitions?
-Indentation in Python function definitions is significant because it demarcates the body of the function, indicating which lines of code are part of the function's execution block.
What is the difference between built-in functions and user-defined functions in Python?
-Built-in functions are pre-defined in Python and can be used directly without any additional code. User-defined functions, on the other hand, are created by the programmer to perform specific tasks as per the program's requirements.
Can a function in Python accept parameters?
-Yes, a function in Python can accept parameters. Parameters are specified within the parentheses in the function definition and can be used to pass values into the function to make it more versatile.
What is the purpose of the 'return' statement in a Python function?
-The 'return' statement in a Python function is used to send a value back to the caller of the function. It is optional and not all functions need to return a value.
Outlines
đ Introduction to Functions in Python
The speaker begins by transitioning from a previous Python project, the Hangman game, to a new topic: functions in Python. The video aims to explain the concept of functions, their necessity, and benefits. A real-life analogy is introduced to explain functions, comparing them to a recipe for making idli, a South Indian dish. The analogy illustrates how sharing a recipe (function) multiple times can be streamlined by writing it down once, emphasizing the idea of reusability and efficiency. The speaker also hints at introducing a new tool in the video that will help viewers become more comfortable with functions.
đ Defining and Using Functions
This paragraph delves into the technical aspects of defining and using functions in Python. The speaker explains the syntax for defining a function using the 'def' keyword, followed by the function name and parentheses. Parameters and return statements are introduced as optional components of a function. The importance of indentation in Python is highlighted. The concept of built-in functions, such as 'print', is contrasted with user-defined functions, which are created as per specific needs. The speaker emphasizes the benefits of using functions, such as code reusability, readability, and time-saving, using the example of a simple greeting function.
đĄ Benefits of Functions and Practical Example
The speaker discusses the advantages of using functions, such as increased code reusability, readability, and time efficiency. A practical example is provided to demonstrate how defining a function can reduce the number of lines of code and make the program easier to understand. The example involves creating a Python file named 'intro.py' and defining a simple function called 'greet' that prints a greeting. The speaker then shows how to call this function multiple times in the program, thereby avoiding the repetition of code.
đ€ Applying Functions in a Coding Tool
The speaker introduces a coding tool, Reebok's World, to provide a visual and interactive way to understand functions. The tool allows users to write Python code and execute commands to control a robot. Built-in functions like 'move' and 'turn left' are used to give commands to the robot. The speaker then demonstrates how to define a custom function, 'turn right', to make the robot turn right by using the 'turn left' function three times. This practical demonstration helps to reinforce the concept of functions and their application in programming.
đ Defining Custom Functions for Robot Commands
In this paragraph, the speaker encourages viewers to define their own function, 'turn around', for the robot in the coding tool. The function is defined by turning left twice, which effectively makes the robot turn around. The speaker then integrates this custom function into the robot's sequence of commands, demonstrating how it simplifies the code and makes it more efficient. The video concludes with an exercise for viewers to practice defining and using functions, emphasizing the importance of understanding functions in programming.
Mindmap
Keywords
đĄFunction
đĄDefining a Function
đĄFunction Call
đĄParameters
đĄReturn Statement
đĄCode Reusability
đĄReadability
đĄBuilt-in Functions
đĄUser-Defined Functions
đĄIndentation
đĄReebok's World
Highlights
Introduction to the concept of functions in Python programming.
Explanation of why functions are necessary in programming.
Real-life example of a function using a recipe for making idli.
Discussion on the benefits of using functions in programming.
Definition of a function in Python and its syntax.
Mandatory and optional parts of a function definition.
How to call a function in Python.
Difference between built-in functions and user-defined functions.
Practical demonstration of defining and calling a function.
Introduction of a new tool for learning functions: Reebok's World.
Using built-in functions in Reebok's World to control a robot.
Defining a custom function 'turn right' in Reebok's World.
Advantages of functions in terms of code reusability and readability.
Practical exercise on defining a 'turn around' function.
Final thoughts on the importance and utility of functions in programming.
Transcripts
so in the series of learning Python
programming language in the previous
video we have seen a project we have
created a project Hangman game right so
from this video I'm going to start a new
topic that is functions in Python right
and this will in this video we'll be
discussing basic about function what is
a function why basically we need a
function this answer of this question is
very important y functions what are the
benefits of using function how to define
a function what are different types of
function
right with the help of real life example
I'll tell you what is function and with
the help of program also practical also
I will show you and I will introduce you
with a new tool also in this video
after using that tool you will be you
know you know uh very friendly you can
say with functions
right it would be very easy for you guys
maybe someone of you are aware about
functions they have learned other
languages CC Plus for Java but let's
start this with a real life example
suppose I know I know a recipe I know
how to make idli one of my favorite from
South Indian food actually
and suppose one Sunday my friend came to
me came to visit me and I made idli for
them
and she was like oh wow Italy like they
they are so delicious and amazing so
she asked me the recipe for that idli
and I told her like like this
even to be very Frank I don't know what
is the recipe but let's take let's
assume I know so Suppose there are these
five or six or seven steps one two three
four five six seven steps
in the recipe so I told her these seven
steps right maybe uh on next day or on
another friend came to me and
She also asked me for the same recipe
because I made idli again
because in this I am expert you can say
so I told her the same recipe like this
maybe every Sunday or every after two to
three days someone is coming to me and I
am telling them the recipe I am
repeating these seven steps again and
again I'm telling them the recipe right
so
what I can do rather than this what I
can do now I'm feeling very frustrated
after telling the recipe again again and
again so what I can do I can just write
down this recipe on a paper right
and like r e c i p e
recipe
of idli you can say making idli right
and whenever anyone comes to me and asks
for the recipe I just hand over this
piece of paper too
damn right
so yeah it is going to say what what is
the benefit my energy and my time
right I have just
grouped these steps
in US
you can say an entity this is an entity
on a paper and I'll just hand over that
paper too
whoever is going to ask me the recipe
about the recipe
right so yeah this is beneficial for me
right same
we have some function like uh in
programming also we have to do something
like some task again and again
right suppose let's take one example
and see if you take one more example
like we human being also we are
performing different different
functionality we are multitasker like
let's take a female as a mother as a
wife as a daughter as a working woman
and many functionalities are there
right so different different
functionalities are there for the same
entity for the same person right means
like if you are in front of your student
you are working you are you know uh you
know your responsibilities work you know
acting as a teacher right if you are in
front of your
children then your responsibility means
your functionality is your working as a
mother something like this
so whatever is in front of you according
to that you have to perform your task
right for different different inputs you
are performing your specific task
right so like this example also you can
relate with the functions let's take uh
this example if in programming also like
two lines there are two lines simplest
example I am taking like print hi and
like Jenny
and good morning
like this
I want to print these two lines again
and again in my program like perform
some specific task then execute these
line then perform some specific task
then again I want to
before I want to you know greet like I
want to uh print hi Jenny good morning
then again perform some specific task in
the same program again I want to perform
these two lines perform some specific
tasks again I want to greet like hi
Jenny good morning right in program
maybe 50 times so 50 times you need to
write down these two lines so rather
than this what we can do rather than
repeating the two lines again and again
what we can do the same thing
just wrap up this thing
here
like print these two lines print
then Jenny and
good morning these two lines here
right and just call it what I'm calling
this a recipe right so it's like once
you're functioning so let's call this
thing what a function name greet
and whenever you want to execute the
these two lines then just call this
function greet greet greet so rather
than writing these two lines again and
again just have to write down simple
function name greet that's it
right now I'll tell you how to define a
function how to call the function there
are two major steps basically first of
all you have to define a function then
you have to call the function right
so now I hope you got it what is
basically a function function is just
what a block of statements or block of
code this is just a block of code this
is just a block of code or statement
these are statements
right
which perform some specific task
when it is cold it is going to perform
those tasks only when you are going to
call that function these lines are very
important so when it is code so function
is a group of
you can say it's or you can say block of
statements or block of code which is
going to perform a specific task which
perform a specific task when it is
called that is function definition very
simple right now how to define a
function let me tell you two steps I
have told you first Define a function
then call the function right so how to
define a function simple what is the
syntax you will write down this def
keyword def then
here you will write down your function
name any name you can take like add
subtract grid any function name display
anything
these parenthesis then colon or in these
parenthesis
if you want to pass you can pass
parameters right then here we have
here we have function body or let me
just write down this thing here we have
function body
and after that we have like return
return statement
return keyword and then some expression
or whatever you want to return so this
is what the complete one
it is not necessary to pass parameter
every time it is not necessary that
every function is going to return
something so this line is optional this
one these parameters are optional but
the mandatory part is to define a
function def keyword function name these
parenthesis maybe these are empty this
colon and then function body
right these are important these are you
can say mandatory part if you are going
to define a function parameters return
expression these are optional part right
and this this this should be indented
right see I have there are some space
at the beginning of these two lines so
they should be indented otherwise it
will give error
right so this is you are going to define
a function how to call the function then
just write down the function name
whatever the function name and
these parenthesis if if you have passed
some parameter if you use some parameter
when you are defining a function then
you have to pass those parameters here
One Two Three or how many parameters you
have
there are no parameters just function
name and these parenthesis this is what
you are going to call the function
right
now
and function will only work when you are
going to call that function only
definition of this function this is not
going to work
you have to call that function
right like me as a teacher
so whenever I am in front of my student
right in front of my class
then only I lacked as a teacher means at
that time students are calling me you
can say they are calling me so at that
time only I'll work as a teacher right
so you have to call that function
right now there are two types of
function built-in function plus user
defined function so built-in functions
are so built-in functions are already
defined in Python already pre-coded or
already defined right you don't need to
write down any code to define those you
can only use the you can directly use
those function like simplest one is
print in every program we use this
function
this is already defined in Python so you
just have to use that function just
print in these parenthesis whatever you
want to print that you can pass here we
pass that parameter the argument right
built in like print we have Len function
we have used this is also built in Max
Min and type also we have used
right
so many functions are there you can also
write down few built-in function in
comment section
now next a user defined a function user
defined means you have to Define user
will define those functions as per our
need
like whatever you want like I want to
define a function of addition of two
number
so according to my head I'll Define a
function the statement the code I'll
write according to myself right and how
to define that I have told you how to
call that I have told you
right
now let's see this thing with the help
of example and I will introduce you with
a new tool also then you will get it
better what function is and why we use
it and now the the benefits or advantage
of using function is what
I guess now you got it what can be the
benefits of using the function
definitely your
code becomes
more reusable rather than writing these
two lines again and again in the program
just call the function so it means we
are reusing these two lines so code
reusability will increase
the code would be more readable right
because obviously if you call a function
if you use functions then the number of
lines in your code will also is going to
decrease will be less and if less line
of lines of code then your code would be
more you can say readable
right
so these are few you can say and it will
be time saving also right just Define
one time and use reuse those thing again
and again so it would be
time saving for you right let me just
show you this thing with the help of
practical we'll see a program on
functions okay so let's create a new
file here for functions and
functions like intro Dot py
right
and
let's define a function depth how to
define depth let's take a function
suppose grid
right colon and here it should be
indented and there I just want to print
high in one line and suppose here I want
to print only gen
these two lines
so now
this is definition of the function we
are not passing any parameter because
these are optional we are not returning
anything here right so
now here what we can do how to execute
this function if I run this code see
suppose we have defined a function
and let's let's run this code
see it is not printing anything the
screen is blank right why so because
to execute the function
I have told you in the definition the
function is a statement or block of
statement block of code which performs
some specific task when it is called so
you have to call the function have to
call the function just
name of the function grid that's it this
is calling of function now let's run
this and this time it will print hygiene
so if you want to print this multiple
times
so again I want to call grid again like
read three times so these two lines will
be printed three times in your program
in the output see hi Jenny hi Jenny hi
Jenny right
so this is how the function will work
now let me just introduce you with a new
ah like tool we have Reeboks World our
website is there okay for that you can
directly search for like python coding
free box world so
first we'll take this example the second
link alone World info python blocky this
keep editor visible right let's run this
and this is going to load
right so here this type of thing would
be opened here the project name is C
along right
okay now see the python code here we can
write down python code so see here we
have some functions like when you click
on this Rebox keyboard then you will get
some function like you can say command
move turn left take put these kind of
commands you can give these commands to
this robot and it will perform according
to your given command so here I have
written one command like move here we
have one command move movements it will
move further One Step only so how to run
this there's a play button right here
just click on that and see
it has moved one step ahead so last
instruction completed so if you want to
move this three steps or two steps then
just use this function again move
or maybe three times move
and now if you want to go back here just
click on this just go back this one and
just click on the play so see it has
moved three steps the robot so last
instruction completed right so here we
can write down our python codes we
haven't written till now we are still
you we are using those uh you know
inbuilt function only we have we haven't
defined our own function till now but we
are going to Define right
if one by one if you want to execute the
instruction suppose first instruction
instruction first and second and third
so you have to click on this one step
through button step
click one then
see it is going to show this one line
would be executed move
see like this
again move again click move
three line would be executed and that's
it last instruction completed so let's
back here
and okay
turn left there is one function use this
turn left just click on that and it
would be here turn left
or rather than first turn left first we
move this robot like three times and
then we use the function
turn left
not at the starting
so if I run this code here so three step
then turn left
see turn left the robot has turned left
this weight is going then turn left this
side
and again
if I
after turn left if I use the move
function again three times and if I run
this again
see now this would be executed move
first three times turn left and move
again
three times right but there is no see
there is no turn around or turn right
function here
right only turn left is there now if you
want to
here
command the robot to turn right then how
you leave that thing
so Define your own function turn right
function at this point of time if I want
to give the command to this robot turn
right
then how will you now you have to Define
your own function now pause the video
and Define your own function
okay how we are going to Define
def keyword function name I am taking
turn right
parenthesis colon and now the definition
it should be indented right
now turn right means simply at this
point of time if this robot we give this
robot three times turn left command then
it would be
automatically at the last
ultimately turn right command would be
executed so three times turn left so now
this is a turn right function now now if
I call that this function here
turn right
and suppose again
move
One Step move and if I run this let me
just stop this just click on this
reload button and play
like this
right turn right and then one move but
we have defined our own function suppose
if you want to
execute this if you want to command
command this uh you know uh
robot something like this okay the same
thing then
it has to move downward three step
right again and then
simply
the face would be in this in this
Direction only this direction forward
Direction only right so how we are going
to give this this this command see now
at this point of time also
for going downward you have to give the
command obviously turn right like this
we are facing then at this point of time
means turn right right and if suppose
you haven't defined this function
then for turn right you have to write
down this turn left three times
so here rather than turn right you have
to write down these
three times
then again move then again turn right so
again write down these three commands
turn left command here like three times
here there should be no indentation
right
now three times move
right and then finally
turn left
right
this would be the case if we are not
using the function turn right we haven't
defined the function so let's stop this
and now let's run this
so now you are repeating these turn left
and turn left these three lines again
and again whenever you want to give the
command to the robot turn right then
three times you have to write down turn
left again and again
so better to define a function so that's
exactly we have done just Define a
function turn right and rather than
these three lines
just use one line turn
right that's it rather than these three
lines
turn
right
just one line
so yeah this is going to reduce the
number of lines in your code when you
are using function code reusability will
increase and as well as code readability
will also increase right this is also
going to perform the same task
one by one we are going to run this now
this step through option
let's run this once click then again
click
then this move command would be executed
then second move again run again
turn left again again again click on
that now see cursor is a
here at this function turn right now
click on the step function now person
would be here
in the definition of this function so
first turn left
again second turn left again third turn
left would be performed now that's it
now we are we are turning we have turned
right this robot then move then again
turn right then again function would be
cold see the cursor is again the control
is again here within the function
then three times turn left and then move
then this move then this then again turn
left and that's it
right
so this is beauty of function you can
say now one exercise for you is
turn around
there is no function turn around here so
you have to Define your own function how
to turn around
let's suppose move this robot three
times here
three times forward then turn around
then three times backward the same
position how you will Define your
turnaround function pause the video and
try this out
okay I hope you have done this how to
define turn
I think this is very simple if you have
done this turn left turn around is very
simple
just two times turn left and that's it
this should be turn around two time turn
left only right so let's
here we will do that thing after turning
left at the final time then we will call
turn
around function
and then again
move
right let's run this and see what output
you will get
C
turn around and then back one step
so this is what we can Define our own
function turn around turn right function
so I hope you got the basics of
functions why we need functions use of
function benefit of function and all now
in the actually we'll see one more
coding exercise on function now this is
Voir Plus de Vidéos Connexes
Coding Exercise for Beginners in Python with solution | Exercise 18| Python for Beginners #lec57
Functions in Karel - Python
Go (Golang) Tutorial #9 - Using Functions
REPRESENT REAL-LIFE SITUATIONS USING RATIONAL FUNCTIONS || GRADE 11 GENERAL MATHEMATICS Q1
ăPythonć „é #7ăéąæ° | ćŠçăäœżăăŸăăă
PIECEWISE FUNCTIONS || GRADE 11 GENERAL MATHEMATICS Q1
5.0 / 5 (0 votes)