#🔒 What does “Def” mean?

32 messages · Page 1 of 1 (latest)

wide pond
#

Hi , so I’m new on python and I started learning how to code yesterday , and I’m wondering what does “Def” means , and how I can use it , I searched on Google but the answer wasn’t clear for me , so if anyone can help I will appreciate that , tysm everyone

clever beaconBOT
#

@wide pond

Python help channel opened

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.

worn mango
#

Def is short for "define" and is used to define custom functions

wide pond
#

Is it like checkpoints or something

#

I want example in coding

worn mango
#

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

wide pond
worn mango
#

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!")
clever beaconBOT
worn mango
#

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()
clever beaconBOT
wide pond
#

Tysm , I think I understood , can I show you if what I learned is true or false?

worn mango
#

Sure

wide pond
#

def generate():
Print(“hello”)

generate()

#

So now whenever I say “generate” it will print hello?

worn mango
#

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

royal heath
#

Its also important to remember variables created in a function only exist in that function unless you "return" them

worn mango
wide pond
#

Alright , tysm sir , have a great day
And I really learned from you so much

#

!close

clever beaconBOT
#
Python help channel closed

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.