#🔒 What does “Def” mean?
32 messages · Page 1 of 1 (latest)
@wide pond
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Def is short for "define" and is used to define custom functions
So where I can use it in codes?
Is it like checkpoints or something
I want example in coding
a function is a block of code we can reuse so we don't need to repeat ourselves
it's like a variable but it's a series of commands
So it’s like
While true:
not quite
def say_hello():
print("Welcome to the program!")
for example, we can define this
!e
def say_hello():
print("Welcome to the program!")
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
if we were to run this, we can see "nothing" happens
that's because we've just told python what instructions to save for later. When we want to actually run the code inside function, we must "call" it
We "call" a function by typing the name of the function later in our code followed by ()
!e
def say_hello():
print("Welcome to the program!")
say_hello()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Welcome to the program!
Tysm , I think I understood , can I show you if what I learned is true or false?
Sure
def generate():
Print(“hello”)
generate()
So now whenever I say “generate” it will print hello?
yes exactly
we can put a lot more than just a print inside the function block though
this is really useful to write complex code that we want to reuse later on
Its also important to remember variables created in a function only exist in that function unless you "return" them
it's not the variable that is returned. It's the data
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.