With the purspose to make a function, you should define it.
The simplest function definition looks like is for instance:
def function_name():
function_body
The main features of a function are:
- It always starts with the keyword def
(that means define) - After def it is placed the name of the function (the rules for naming functions are exactly the same as for naming variables).
- After the function name, there is a place for a pair of parentheses.
- This sentence has to end with a colon.
- After the sentence Name function, begins the function bod, at least one nested instructions, which will be executed every time the function is invoked; You should bear in mind that the function ends where the nesting ends, so you have to be careful.
One example of function is:
When you run it, you see the following output:
We start now.
We end now.
Python read the function and remember it, but it do not launch the function without our permission.
In the following example there is an invocation of the function:
def information():
print(«Enter a value: «)
print(«We start now.»)
message()
print(«We end now.»)
The output looks different now:
We start now.
Enter a value:
We end now
Weekend reading alert! Our Annual Impact Report for 2022 is up now: https://t.co/sM3ah5O39I
Check out all we accomplished together last year, in a lovely package with awesome community photos 🐍 ❤️
— Python Software Foundation (@ThePSF) August 4, 2023
https://platform.twitter.com/widgets.js
Contenido Web de Yolanda Muriel está sujeto bajo Licencia Creative Commons Atribución-NoComercial-SinDerivadas 3.0 Unported.
