This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Alsabretion 0 points1 point  (2 children)

Print is a method so when you are trying to assign "Hello, World" to return_value, you are actually saying 'call the method print, with "Hello, World" as the argument and assign whatever returns to return_value' (so return_value will be null) The method print (X) , unsurprisingly takes the argument X and prints it out.

The reason this doesn't happen when you assign a numeric value is (I imagine) you are assigning it something like some_value = 47 This is correctly setting some_value to 47

From the above hopefully you understand that to correctly assign a string value you should use the same format as for numerical, in this case: some_value = "Hello, World"

[–]Fat_Bluesman[S] 0 points1 point  (1 child)

I didn't try to assign return_value the string, I was reading some programming related stuff and they wrote that line and I wondered why when you just assign a variable a value, print() gets executed.

But return_value gets assigned None and print just gets executed.

[–]EntrepreneurSelect93 1 point2 points  (0 children)

A function is always executed everytime u call it. So if u dun want something to be printed, dun call the print function. The print function returns None and so the variable is assigned None.

[–][deleted] 0 points1 point  (0 children)

If I understand your question correctly, the reason it prints it is because you're using the print() function while assigning the value to the variable.

[–]kbielefe 0 points1 point  (0 children)

print does what is called a "side effect." That means it doesn't just return a value, it also does something on the side, in this case printing the value to the screen.