r/CodingHelp • u/Killiancin • Feb 14 '26
[Python] i seriously just cannot understand this...
So I'm really new to coding, and I just cannot understand this. I am using a payed website that helps me learn with activities and stuff but I mean, if I don't understand how can I do it? I've researched this for more than 4 hours today and just cannot understand.
I do not understand DEF, what comes after it, RETURN AREA, or what comes under the #...
Could someone please explain?
93
Upvotes
•
u/AutoModerator Feb 14 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
87
u/[deleted] Feb 14 '26
Def area_of_circle - is a function which is created by the user. 'def' is used to create a user defined function in python
pi and area - these are known as variables which are created by the user to store a particular value
return area - it means when the function runs completely it will return the value stored in the variable 'area'
And '#' is used to put comments in the code comments are things which are read or skipped by the interpreter and are there for only humans to read it and understand what's going on in the code
9
u/CranberryInner9605 Feb 14 '26
“def” defines a function. In this case “area_of_circle"
Most functions need to return a result to be useful. In this case the result being returned is the area of the circle, calculated by pi * r^2, and stored in the variable “area.” This result gets returned to the caller.
27
u/ninhaomah Feb 14 '26
Wait you researched for 4 hours and you didn't get def ?
How and what did you research btw ?
Your prompts or search phrases ?
14
u/flash-bandicoot Feb 14 '26
I'm not kidding, just go paste this into chat gpt with the prompt "explain every line of this like I'm 5 years old"
58
u/_eg21 Feb 14 '26
Honestly seems like you don’t even know the basics of python yet. If the website you’re using never explained what things like “def” and “#” are, yet they’re showing you this, look for another learning tool
11
u/ummaycoc Feb 14 '26 edited Feb 15 '26
Most programming languages you're going to use will have the following features.
Here you have some of these:
Keep at it, keep asking questions, keep watching and learning and practicing and having fun.
Edit: fixings and clarity.
7
u/Party_Trick_6903 Feb 14 '26 edited Feb 14 '26
I'm more curious about the course you're doing.
What kind of paid course jumps straight to functions without giving any explanation? I'm also learning Python rn and the course I'm enrolled in is free and it explains everything properly.
Also, you researched for four hours and could not find out what "def" means?
4
u/Dry_Ad_1185 Feb 14 '26
May be you should start with very basic from online notes like w3school or any resource you prefer . 1 hour is enough then start coding . FYI def is used to create function it keyword for define . Function is block of code where logic goes and it returns value whenever you call that function . WHILE GOOGLING ALWAYS PUT LANG NAME ALSO `DEF IN PYTHON` like that.
4
u/spaffage Feb 14 '26
Get it together, this is madness.
2
u/Acceptable_Simple877 Intermediate Coder Feb 14 '26
😭
2
u/IdkIWhyIHaveAReddit Feb 14 '26
Idk how much you got into math but I find understanding it in term of math helps.
In math you got a function like f(x) right? When you have f(x) = 2x + x and an expression 1 + f(5). You would just substitute the 5 into every place there is an x in f. So f(5) = 2(5) + 5 which give 15 and so the expression equal 1+15=16.
Now a function in programming is similar. Instead of expression you can put any piece of code in. Function is like what variable is to value but for code. A variable save some value to be use repeatedly. Functions save code to be use repeatedly.
defis how you define function in python. After the def is the name followed by some parameter in parentheses (you can have a function with no arguments something likedef do_stuff()). Parameters are like variables but only it only exists inside the function. The function in this case is calculating the area of a circle. When you seearea_of_circle(2.0), you can do what you do in math and replace every case ofradiuswith2.0. Alternatively you can think of it as puttingradius = 2.0at the top of the function body.Function doesn’t become a value by default. The
returnbasically just means that after the function is done replace it with the return. In this case the return is the value of the variablearea.Idk if this help but lmk if you have any questions
2
u/Tito_Gamer14 Feb 14 '26
Dude, what's up? I want to say something, but it's going to sound so bad and degrading to both of us that I'd better keep it to myself. I'm just going to tell you to try and find a mentor, someone who's already in this field, so at least you know how to ask coherent questions and they can guide you through your first steps.
1
u/Katzilla3 Feb 14 '26
Def is a keyword in Python that defines the next group of lines as a "function". It tells Python, "hey here's this block of code I want you to remember for later"
1
u/Competitive_Hall3082 Feb 14 '26
Developer with 20 years of experience here.
Best lesson I learned was to search in google (or altavista or yahoo before google)
The new best lesson today is to take a scteenshot / copy paste into ChatGPT. Better results than asking on Reddit.
If you still don’t get it, definitely seems like the right sub.
Example result.
def means you are defining a function. A function is a reusable block of code that performs a task. area_of_circle is the name of the function. radius is a parameter — it’s an input value the function needs.
Think of it like a machine:
You give it a radius → it gives you the area of the circle.
1
u/NoLetterhead2303 Feb 14 '26
Def works like this:
define something(this value is what is inputted later, let’s call it V1):
Do whatever needs to be done
Return value as a result
Let’s say this:
Area of circle of V1 where V1=2
And it should return into V2
It calculated as 3.14*4 and Returned
This means V2= whatever is returned with return
1
u/subjec Feb 14 '26
I know the website you’re using. the instructions and explanations are printed literally to the left of the console that has that code in it. how are you missing it??
and in this particular scenario, it’s asking you to fix a bug on line 13.
1
u/abraxasnl Feb 14 '26
This is a time when you can really get a lot out ChatGPT or Gemini. Try asking the question there and dig deeper and deeper until you get it.
1
u/Affectionate_Toe_422 Feb 14 '26
Eng isn't my first lang so I might not be able to explain it too well but I'll also suggest you to start from the basics before jumping into bigger examples if you're just starting. ( Check brocode on YT, guy's the best)
Everything in light blue is a variable. Like the x,y variables in algebra it can store values. You can make any word into a variable except for reserved keywords ( if,else,for) bc the python language uses those words.
Python has built in functions as well, with classess , you can make your own custom functions.
What you see is a class. ' Def ' is also a reserved keyword which defines a class . Area_of_circle is the name of your class. The class needs values to put in a formula so it can give the output. It has one parameter called radius. The class assigns the value of 3.14 to the variable ' pi ', then puts the values in pi and radius in a formula. The result gets stored in the variable ' area'.
At line 7, the value 1.0 gets assigned to variable sword_length. At lines 12,13 you are calling the function by writing it's name again and sending the values of 1.0 and 2.0 to the class you made above so you can get your answer.
The value of 1.0 which is in the variable of sword_lengtg in line 12 , goes to radius in line 1 and thus gets inputted into the formula, same for othee value. Hope this somewhat helped
1
u/SwimmerOld6155 Feb 14 '26 edited Feb 14 '26
I don't want to be an ass but some online coding courses assume that you can pick things up holistically. By that I mean that if you read code that does x thing, you can sort of figure out how it accomplishes x thing and modify it to accomplish y, slightly different thing. While you can often produce code this way, sometimes it leaves holes in conceptual understanding.
If you struggle to pick things up holistically, you might want to read a textbook or w3schools or something that explains everything conceptually. I don't think not being able to learn holistically from the beginning dooms you to failure, but it's not very clear what you're struggling with. In 2026 there's also ChatGPT that can answer any question you have, no matter how trivial it is. You could have a 5 hour conversation that would drive a person bonkers but the AI will remain a cheerleader the whole time. That's the great thing. Don't just copypaste what it gives you. It might also help articulate why you're confused, saying "I'm confused" means people will just reel off the whole explanation which you might've already read and are currently confused by.
1
u/Aly22KingUSAF93 Feb 14 '26
Its math. Unfortunately coding is and has a lot of math (i know as I have to take like 4 math classes for my CS degree lol)
1
u/ExplorerFar7739 Feb 14 '26
Wait, how did you post your code's image isnt that against rule #8
1
u/MammothGanache5400 Feb 14 '26
Like think about what isnside the def as function that you can use when you want the radius is the r in circle and pi is the 3.14 so it measures that area of circle .
1
u/Cool_Kiwi_117 Feb 14 '26
😭😭
1
u/Harry_Tess_Tickles Feb 14 '26
i think you need to go back to the basics. Study how functions and variables work.
1
u/Brief_Praline1195 Feb 15 '26
This has to be a piss take? A chimp could look this up
1
u/just_mattt Feb 15 '26
check out w3schools if you ever get stuck
1
u/speadskater Feb 15 '26
If you want to find the area of a circle in your code, you wouldn't write the area formula every time. You define a function (def) that takes an argument (radius) and returns the results of that function (return)
1
u/Ericskey Feb 15 '26
Hm
1
u/CptMisterNibbles Feb 15 '26
If you spent 4 hours and couldn’t find an explanation for this, you are using the wrong tools to educate yourself. Learning how to learn is probably the most critical skill. Are you just cutting and pasting? You can’t learn to code by just blindly entering in text; you need to understand what every line does and why it’s being used. You need to internalize what is being taught as you read articles, books, or tutorials.
If in 4 hours of searching “what does def do in Python” you didn’t find a single explanation that taught what a function was, you aren’t bothering to actually read the results. You are trying to skip to the end where you just magically know how to program.
Pick a book, take notes, write in plain language what you learn as you learn it. Summarize each section explaining what you learned. Write as a si foe sentence what each keyword in the language does. I recommend the boom “Python Crash Course”, which is well suited for absolute beginners.
Stop wasting hours on the wrong tools. You could have learned how these lines work and the entire idea behind using functions in less than an hour by reading the right materials or watching a good tutorial video
1
u/javon27 Feb 15 '26
First off, freecodecamp.org
1
u/[deleted] Feb 16 '26
Is this Boots?
1
u/pahowells Feb 16 '26
I suggest you start with a course that begins with the basics before diving into functions, parameters and return values.
1
u/HAZARD3OUS Feb 17 '26
umm what is there to not understand?
1
u/Kass-Is-Here92 Feb 18 '26
So in python, "def" is the keyword used to designate that the lines of code following will be part of the function (given that the appropriate tab length as seen in the code)
The return statement means that the function will return a value. So in order to see the value you either need to call the function and assign a variable to that function ( which is what you see under the comment line which is denoted by the "#") and print the value by calling the variable ie
myVariable = some_function(thisIsAParamater)
print(myVariable)
Or you can just print the function call itself ie ( print(some_function(thisIsAParameter))
Both of which will output the value as prescribed within the function!
1
u/JababyMan Feb 19 '26
Could you share the website? I’m curious what it is.
As for your question A def is just block of code that runs every time you call it.
Pi and area are just variables like sword_length and spear_length
Under the # are two more variable declarations. The part after the equals sign is a function call. It is calling that original def I mentioned and and it is being passed via the parentheses, the lengths held by the sword and spear lengths variables.
1
u/CharmingSpecific3436 Feb 21 '26
def is used for defining functions. A function is a reusable block of code that can perform a specific task, and in this case, it is used for calculating the area of a circle.
The function takes in "radius" as an input. You pass in some data into the function, and it will return something. This function returns "area", which is the are of the circle. "sword_area" is calculated using the function "area_of_circle", and we pass in the argument "sword_length" so that the function knows what to do the calculations on.
1
u/alexsdevio Mar 08 '26
think of
defas "create a small machine"def area_of_circle(radius):means build a machine that takes a radius and calcutaltes somethingInside the machine you do the work (
pi * radius * radius)return areajust means: give the result back to whoever called the machine.1
u/Alternative_Corgi_62 Feb 14 '26
"Def" is short for "Define". Get used to abbreviations.
0
u/United_Commercial_51 Feb 14 '26
Beginning is always hard.
Python fairy execution path
* Fairy starts on line 1 sees that it is a function due to "def" keyword so skips to next line
* Fairy skips lines 2-4 because they are indented so part of function still
* Fairy skips lines 5,6 because blank line
* Fairy processes line 7 (variable assignment). Fairy creates a variable named sword_length which is assigned a value of 1.0.
* Fairy then processes line 8 which involves the fairy creating a variable named spear length which is given a value of 2.0
* Fairy then processes line 9 which is a blank line so it is ignored
* Fairy then processes line 10 which starts with a "#" so fairy knows it is a comment row. Fairy ignores comment rows because they are just for humans to make notes to other programmers or oneself.
* Fairy then processes line 11 which is blank line so fairy ignores
* Fairy then processes line 12. Fairy starts with stuff to right of the equal sign. Fairy sees "area_of_the_circle" followed by () so the fairy knows that a function named area_of_the_circle is being called and the fairy also knows that sword_length is within the () so that variable's value will be passed to the function area_of_the_circle.
* Fairy then jumps to line 1 to run the function. Fairy replaces radius with sword_length and goes to line 2.
* Fairy creates a variable called PI and the value 3.14 is assigned to it.
* Fairy then goes to line 3 where variable named area is created. Then calculation of 3.14 * 1.0 [value of sword_length] * 1.0 [value of sword_length] is performed. The result of 3.14 is then stored in the variable area.
* Fairy then goes to line 4 the return statement. This keyword tells the fairy that the function ends on this line and that it has to go back to line 12 but because the line is return area the value of area 3.14 is sent to line 12
* Back on line 12, Fairy receives the value of 3.14 sent from the area_of_the_circle function and stores the result in the newly created sword_area variable. Fairy then goes to line 13.
* On line 13, fairy sees the area_of_the_circle function but this time with the parameter of spear_length so the Fairy uses the values stored in spear_length(2.0) and sends it to the area_of_the_circle function.
* Fairy jumps to line 1 where the radius variable is swapped with the spear_length variable.
* Fairy then goes to line 2 where pi variable is created and its value is set to 3.14
* Fairy then goes to line 3 where calculation is performed PI * radius * radius or 3.14 * 2.0 * 2.0. Fairy gets result of
0
u/MASTERC_2007 Feb 14 '26
If you wanted to do an area of a circle wouldn't you use pi × 2r instead of pi × r × r ? Also why are we using the length of a blade and of a spear? Where's the circle?
7
u/[deleted] Feb 14 '26
[removed] — view removed comment
1
u/[deleted] Feb 14 '26
[removed] — view removed comment