+ 1
what is the use of def in python
def
3 ответов
+ 1
Some times you need a function that's not included in python , so you can create your own function with (def) and use it as much as you want by calling it.. Ex.
def my_func():
print("My own function")
my_func()
>>>
'My own function'
>>>
+ 1
Def indicates you are defining your own function (also known as a method). What a function is is simply a piece of code you can use over and over and over.
If I were to say:
Def MyFunc(args):
print(args)
MyFunc(5)
This would print out the number 5. I am sending the argument of 5 as the variable 'args' and printing it out in the function's code.
0
It is used if you want to run a code multiple times in your program but you don't want to repeat it many times thus saving space and making your code more readable and less complicated. example:
def print_i():
print("Hi")
print_i()
print("Hello")
print_i()
Result:
Hi
Hello
Hi