List Comprehension

“More List Comprehensions” - https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-loops/exercises/list-comprehension-ii?action=resume_content_item

In exercise on “More List Comprehensions” I curious as to why when I use the following code, it produces the result (specifically, returns [None, None, None]) that follows it:

Code:

usernames = ["@coolguy35", "@kewldawg54", "@matchamom"]

messages = [print(user + (" please follow me!")) for user in usernames]

print(messages)

Result:
@coolguy35 please follow me!@kewldawg54 please follow me!@matchamom please follow me![None, None, None]

Thanks.

What do you think it should have done instead?
How many times do you mean for something to be printed, how many times do you use print?
You’re making a list using list comprehension, what values did you mean should go into this list? What value did you provide?

Thanks for responding.

I know that removing ‘print()’ command from list will produce correct result. I also know that removing last print command will result in correct answer. I don’t understand why I’m receiving “None, None, None” as the result of the last print command.

What is the last print command using/not using as input to produce “None, None, None”?

the list you made ^
but it says that in your code already. maybe you mean to ask something else.

If you print a list you get shown the list… right? Right.
So what did you put in the list then?

Where did you make the list? You used list comprehension to create that list on the line above. What’s the anatomy of list comprehension, how does list comprehension say what goes into the resulting list, and what did you specify for that?

Inputting print command in my list comprehension was simply an attempt to find out what would happen if it was included.

Does this portion of my code return “None, None, None” with the print command ?:

‘print(user + (" please follow me!")’

Thanks

print is a function
if you call a function you get something back and you’re free to use that value

1 is an expression, it evaluates to 1
a variable name is an expression, it evaluates to the value it refers to
a variable referring to a function thus evaluates to a function
if you have a value you can make operations on that value
for example you can add two values to obtain a third value
1 + 1
or you can call a value
so if you have a value named print
print
then you can call it, which just like addition produces a value
print()
what is an expression? an expression is a description of a value

in a list comprehension you are providing an expression for the results
what expression did you use
what does that evaluate to?

So you called print and put the result in a list.

What does print return? What is prints purpose, its behaviour? What does print promise to do, what does print promise about its return value?
You could say that you don’t know what print promises about its return value, but that’s a lot like saying that you don’t know how to use its return value since you don’t know what it at all is. But it makes some kind of promise about that, that’s something you can look at print’s documentation for (the documentation is the promise about what it does)

For anything you use you need to ask it … hey, how do you behave? So that you can then leverage that behaviour. And if you don’t know some aspect then you can’t use it. Like print’s return value.

But you also gotta be aware that you are using print’s return value. Hey, I’m using this thing, and relying on it to behave in a certain way, and this is okay because it promised in its documentation that it would do that.

I will go back to try and better understand exactly what the print function does.
Thanks.

Try this:

help(print)

Note that it doesn’t say anything about its return value, what does that tell you about how you can use its result? (Nothing, so don’t use it)

As an extreme example, it also doesn’t say anything about its barbecue attribute. So. Don’t use that either.

print.barbecue 

There’s not much reason to expect print to return anything useful, that’s not not what it does. It writes text, it has a side effect, the side effect is the purpose.

Most functions will have no side effect and instead produce some value:

>>> help(math.atan2)
Help on built-in function atan2 in module math:

atan2(y, x, /)
    Return the arc tangent (measured in radians) of y/x.
    
    Unlike atan(y/x), the signs of both x and y are considered.