r/PythonLearning • u/[deleted] • Aug 31 '25
Im dumb (for context). Can someone explain the difference in line 15 and 18
10
u/c1boo Sep 01 '25
If you want to throw tomatoes to someone,
in line 15 you store tomatoes(num1 num2 and num3) in a bucket (sum) so you can throw them later.
In line 18 you don’t store any tomatoes you throw them immediately.
4
1
u/Square-Control1892 Sep 04 '25
and the fun thing is now you can keep throwing that bucket of tomato infinitely without adding tomatoes together ever again.
2
1
u/Overall-Screen-752 Sep 01 '25
If you’re wondering why 15 instead of 18 (or vice versa) it’s a matter of code clarity. If the objective is “print the sum of 3 numbers” line 18 does that AND shows which numbers comprise the sum. If the objective is “print the sum” (where the numbers are less important) you may do line 15 and 16 instead. This is a pretty contrived example so its okay of it doesn’t make sense right now — they do the same
1
u/poopaloompa666 Sep 01 '25
Theres a few things to take note of between the last few lines. 15 is a mat operation seeking 'sum' but it only does math, the other line is a different type of atatement, a print statement, and does just print the sum attained in the other line. The difference between the two print lines is a bit more nuanced. They both (i think) do the same thing but one the sum one is cleaner and more sustainable in a large coding project.
1
u/Abhi__1729 Sep 01 '25
In 15th line it creates a new variable and assigns it with (num1 +num2+num3) 18 th line directly prints the sum of (num1+num2+num3) without creating any variable
1
u/the_fish_king_sky Sep 02 '25
Stdout is a "variable" in some sense and print writes to it So your basically saying
Stdout = Stdout + str( num1 + num2 + num3) on line 18 And on line 15 your saying Sum = num1 + num2 + num3 Stdout = Stdout + str(sum)
(This is invalid becuase of some funky stuff with how its orginized in memory. Basically stdout is a file somewhere (if you use unix not sure if windows does it like this) in ram and in order to write to it without pissing off the operating system you need to use its api) The main effective difference is that stdout is both a string and read only without some finagling. so you cant reuse it in another expression for instance:
Import sys Sum = num1 + num2 + num3;
sys.stdout.write("this is the sum of all the numbers " +str(Sum)+ "\n")
Sum = Sum + 1;
sys.stdout.write("and this is the sum of all the numbers + 1" + str(Sum))
(Do note that my syntax is likely invalid somewhere becuase i am not good at finding sytax errors without running it lol)
1
u/wayofaway Sep 05 '25
Use the first if you need the sum again, use the second if you only print it... At least that's how I do it.
1
u/Confident-Orange-628 Sep 20 '25
15 ---> cooking the dish and serving it in a plate 18 ---> cooking the dish and serving it in the pot in which it was cooked
I hope this makes it easier to understand
0
u/EdFenty Sep 01 '25
In 15 you are assigning to the variable sum which as a value will supposedly have the concatenation of the values of the other three variables and in 18 you are printing in the terminal the result of adding the values of those variables
-5
u/ninhaomah Aug 31 '25
Can explain which is it that you don't get ?
One print , the other doesn't.
Technically , difference can be complicated but just look at those 2 lines and they are different if purely English and obvious if I may say so.
2
Sep 01 '25
Sorry bro. Very very new to coding. Wasn't sure what difference it made (knowing the codes are completely different and do make a difference). Was just asking for knowledge dude
-3
u/ninhaomah Sep 01 '25
no no...
thats the thing... maybe I don't understand the question at all.
let try again.
before asking , have you done print ? as in print("Hello World") ?
3
u/CallMeABeast Sep 01 '25
Let's try again
Before answering condescendingly, have you read the code where he clearly tests the print function?
0
Sep 01 '25
[deleted]
5
u/Adsilom Sep 01 '25
I doubt they would want that, as they don't print anything else than the result.
1
u/littleprof123 Sep 01 '25
Why would you want to do that? print already converts its arguments to strings.
32
u/Chasne Aug 31 '25
15 creates a new variable sum containing the value of the addition
18 just prints the result without keeping it in memory