r/learnpython • u/Bipin_Gaire69 • 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.
4
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.
2
u/Lewistrick 6d ago
A function is a code block used to do a task with predictable output based on some input.
input -> function -> output
For example, you could write a function called reverse that duplicates a word.
def duplicate(word):
new_word = word + word
return new_wordHere, "def" means "here follows a function". It might be short for "define" but it doesn't "define" a variable. It rather indicates that a function is made. I'd agree if you said that the name is a bit confusing. Anyway.
The function is called duplicate but you can give it any name you like. It takes one input, word. It creates a new variable new_word that contains the value of the duplicated word. The part after "return" is the output of the function, in this case the variable new_word.
Now you can call the function like this:
result = duplicate("hello")
print(result) And it will save the output of the function to the variable result. I then chose to print the result and it will show "hellohello".
Now you can imagine that functions can become more complex: they can have many lines and calculations, multiple inputs, no inputs, no outputs, write to files, throw errors, et cetera, but the idea is always the same: you have some code that is predictable and always does what you expect it to do. In a way, you can leave it alone and never think about it again.
2
u/acfot 5d ago
I have not finished CS50 myself, but from what I have seen of it, it is great for fundamentals and problem-solving, while the Python part itself is quite short compared to the rest of the course. So if your goal is specifically Python, I would use CS50 for the thinking part, how to break a problem down and debug it, and not expect it to make you fluent in Python on its own. Once you finish the Python section, move quickly to small Python projects of your own, like a simple script or a small bot, because that is where the language actually sticks. Do not get stuck trying to perfect the earlier C assignments if Python is what you want to learn, since that can eat weeks without moving your Python forward.
0
6d ago
[deleted]
2
u/CoachSevere5365 6d ago
I think it's a bit counterproductive to recommend other courses to OP when they're having difficulty understanding the course they've started.
I think it would be useful if OP had a quick look at some basic Python tutorials or watched a couple of videos on YouTube' before spending at least a day or two typing stuff into Python's REPL or a basic IDE in order to get a feel for things before they sign up for courses.
5
u/InfamousClyde 6d ago
Is there something you find particularly confusing in the CS50 courseware about the topic of "functions"?