This is a machine-generated translation. If you're not at the in-person workshop, try the DjangoGirls tutorial for an intro to Python!
Do you remember functions like
randrange() from the
They are like magical spells from a leather-bound book: if you know their names and can write them correctly, they will do something for you.
Now we will move on to the next level: we will come up with our own spells! How? We will combine commands that we already know.
Perhaps a function that greets you could:
The definition of a function in Python starts with the keyword 'def', followed by the name and parentheses (currently empty). Then, like with 'if', there is a colon and indented commands - this time the commands that the function should execute.
def function_name(): # commands to be executed by the function
Write in your program:
def greeting(): print('Hello!') print('How are you?')
Your first function is ready!
When you run this program, it won't do anything. That's because this is just a definition of a function. Python now knows how to say hello - but it wasn't told to actually do it!
At the end of the program, add a call. This is no longer part of the function, but a continuation of the program itself. Therefore, it must not be indented.
def greeting(): print('Hello!') print('How are you?') greeting()
What happens when you call the function several times in a row?
def greeting(): print('Hello!') print('How are you?') greeting() greeting() greeting()
What happens when you place a function call above the function definition, instead of at the end of the program?
greeting() def greeting(): print('Hello!') print('How are you?')
Your function can only be called as
greeting(). However, functions like
print(1 + 2) can also work with values.
Let's now write a function that will greet you by name.
def greeting(name): print('Welcome,', name) greeting('Ola') greeting('Soňa') greeting('Hubert') greeting('Anička')
How does it work? In the function definition, you specify a parameter in parentheses - the name of the variable that the function will work with. You then enter a value for this parameter when calling the function.
Can you write a program that asks for a name and then greets you?
What happens when you call a function without a value for the parameter?
The function can contain any code. For example, a conditional statement, 'if'. Commands after 'if' then need to be indented by an additional four spaces:
def greet(name): print('Hello,', name) if name == 'Ola': print('You know how to program!') greeting('Hubert') greeting('Ola') greeting('Soňa')
The next thing that functions like
len can do is return the result.
length = len('Ola') print(length) # writes: 3
How to do it if you wanted to write such a function?
In the function definition, you can use the
It immediately terminates the function and returns the given value.
def double(x): return x * 2 print(double(42))
Try to think about how to write a function that returns the fifth case of a noun. For example:
These are examples of a function called "paty_pad" being applied to names in Czech. The function is changing the last letter of the name to match its grammatical case.
This is a very complicated task, so let's simplify it a bit. The function should do this:
If the name is "Hubert":
This is a Python code that defines a function named "paty_pad" which takes a name as an input argument and returns the name in a modified form. Here is the translation of the code comments and function:
# This function modifies the name based on certain conditions def paty_pad(name): # If the name is 'Hubert', return 'Huberte' if name == 'Hubert': return 'Huberte' # If the last letter of the name is 'a', replace it with 'o' elif name[-1] == 'a': return name[:-1] + 'o' # Otherwise, return the name as is else: return name
So, if you call this function with the input argument 'Jana', it will return 'Jano'. If you call it with 'Petr', it will return 'Petr'. If you call it with 'Hubert', it will return 'Huberte'.
Can you change the function
greeting to greet in Czech? You can use the function
What was new this time?
returnterminates the function and returns a value.