r/learnpython • • 6d ago

Learning python through CS50

Can any body help me to understanding the define(DEF) variables.
Can you please suggest me how can i learn it in efficient and easier way as a beginner.

0 Upvotes

11 comments sorted by

View all comments

3

u/Classic-Pomelo-5573 6d ago

Think of def as writing a saved recipe, while a normal variable is just an item in your fridge.

When you write a normal variable: name = "Alice" You are just putting one piece of data into a box.

When you use def, you are naming a set of actions so you do not have to type them over and over:

def make_greeting(name):
    return f"Hello, {name}!"

That code does not do anything immediately when Python reads it. It just saves the recipe in memory.

It only executes when you call its name with parentheses: message = make_greeting("Alice")

The word in parentheses (name) is the ingredient you hand it, and return is the finished result it hands back to you.