FUNCTIONS in Python (Syntax, Create, Call, Arguments, Return, Docstring, Nested, Recursive)

preview_player
Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Absolute Beginners, in which you will discover Functions in Python.

▬▬▬▬ 📖 Content ▬▬▬▬

🖥️ In Python, a function is a group of statements that performs a specific task and can be used repeatedly, throughout your program.

Functions in Python help break your program into blocks of code. As your program grows larger and larger, functions make it more organised. It also avoids repetition, makes the code more clear, reusable, and allows you to work collaboratively.

🎯 In this video, you will discover Functions in Python: How to Create and Call a Function in Python, How to Pass arguments to a Function in Python and How to Return a value from a Function in Python? - and more...

Let's play this video, stick around and watch until the end! 👍🏻

- Digital Academy™ 🎓

Create a Function in Python
---------------------------------------------
To define a function in Python, use the keyword def, followed by your function name with parenthesis, optional arguments, and one colon.

def my_function(arg1, arg2):
statement 1
statement 2
return result

Call a Function in Python
-----------------------------------------
The def statement only creates a function, but does not call it afterwards. You must call (run) the function explicitly by adding parentheses after the function’s name. Therefore, once you have defined your function, you can call it from another function and anywhere in your program.

my_function()

Pass Arguments to a Function
-------------------------------------------------
You can send information to a function in Python, by passing values - also known as arguments. Arguments are declared after the function's name, in parentheses. When you call a function with arguments, the values of those arguments are copied into their corresponding parameters in the function. You can send as many arguments as you like, separated by commas.

my_function(value1, value2)

Return Values from a Function
-------------------------------------------------
The return statement in Python is used to exit a function, and go back to the place from where it was called. This return statement can contain an expression that gets evaluated, then the value is returned. If there is no expression in the statement, or the return statement itself is not present inside a function, the function will return None.

def my_function()
return like is True

Function Documentation
----------------------------------------
Although optional, documentation is a very good programming practice - especially if you want to get your dreamed job at Google, Apple, Facebook or anywhere in the World - and work collaboratively!

You can attach documentation to a function definition by including a string literal, called docstring - just after the function header. It is briefly used to explain what a function does.

Nested Functions
----------------------------
A Nested function in Python is a function defined within another function. They are very useful when performing complex tasks multiple times within another function, to avoid loops and code duplication outside.

Recursive Function in Python
----------------------------------------------
A recursive function is a function that calls itself and repeats its behaviour, until some condition is met, to return a result.

def my_recursive_function(arg1):
if arg1 == 0: return 'Done'
else: my_recursive_function(arg - 1)

▬▬▬▬ 🎬 Watch NEXT ▬▬▬▬

▬▬▬▬ 🔗 Links ▬▬▬▬

▬▬▬▬ ⏱ Timestamps ▬▬▬▬

00:00 Introduction
00:34 What is a Function?
01:22 Functions in Python
02:05 Create a Function in Python
02:26 Call a Function in Python
03:12 Pass Arguments to a Function (Positional, Keyword, Default, *args, **kwargs)
07:54 Return Value from a Function
09:50 Practical Exercise
11:18 Docstring in Python
12:26 Nested Functions in Python
13:06 Recursive Function in Python
13:35 Conclusion

#Python #Tutorial #Beginners #Functions

▬▬▬▬ ⭐ FOLLOW US ▬▬▬▬

▬▬▬▬ ⚡ SUPPORT US ▬▬▬▬

Рекомендации по теме
Комментарии
Автор

In Python, a function is a group of statements that performs a specific task, can be used repeatedly throughout your program, and help break your program into blocks of code. As your program grows larger and larger, functions make it more organized. Are you familiar with functions in Python? What result did you get, during the practical exercise?

DigitalAcademyOnline
Автор

I CANNOT WAIT to discover your new Tutorials on Python Development with Web Frameworks, like Django, Flask, etc

xJrmy
Автор

As always, I love the animated way you present these lessons 👍🏻

xJrmy
Автор

Function in Python is required as soon as you want to design more complex programs 👍🏻

xJrmy
Автор

this is really amazing, i love that .

lamsiahkim
Автор

Alright... I tried another solution that aims at taking a changing number of items for a list but didn´t succeed:

def sum_list(*args):
result = 0
for i in args:

result = result + i

return result

mylist = [1, 2, 4, 6, 8, 10]

x = sum_list(1, 2, 4, 6, 8, 10)

print(x)

If I try to call the function sum_list with mylist, x = sum_list(mylist), it doesn't work... It would be great to be able to take each item of the list and use it as an *args in the sum_list function...

daacosta
Автор

i have a question
how to create 3 functions with the same name?

lamsiahkim
welcome to shbcf.ru