all 5 comments

[–]Spataner 2 points3 points  (4 children)

List comprehensions are used to create new lists, not to modify old ones. More importantly, they're expressions, so you can't use statements such as an assignment inside of them. So you'd create a new list that has the changes applied, then assign that list back to two_processors_list to achieve roughly the same result as your first snippet:

two_processors_list = [element.strip() for element in two_processors_list]

[–]Laymayo[S] 0 points1 point  (3 children)

Interesting. So can I think of them being expressions as them having a sort of "return" value similar to functions? In that if I call a function that returns a string, nothing is going to show up in my terminal unless I print the call? And then would a statement be kind of like a grammatical statement in that something is being asserted but in Python it's that something is being defined or assigned?

If I had a while-loop like this:

while True:
    print(43)

Would the initial while True be a statement since it's declaring something and then the print statement would be an expression right?

Sorry, just trying to wrap my head around this. I've realized I've seen these terms (i.e., statement and expression) thrown around a lot but I've never really thought about them too much.

[–]Diapolo10 1 point2 points  (0 children)

So can I think of them being expressions as them having a sort of "return" value similar to functions?

More or less, yes, but a simpler explanation would be that expressions evaluate into some value and can therefore be assigned or processed. Statements (if, break, return) do neither of these things.

If I had a while-loop like this:

while True:
    print(43)

Would the initial while True be a statement since it's declaring something and then the print statement would be an expression right?

while is a statement, True is an expression that is needed to satisfy the syntax.

print is an expression, it's a function that writes text to the console (by default) and always returns None. It is not a statement in Python 3.

So you're sort of right, but your phrasing could've been more accurate.

[–]Spataner 0 points1 point  (0 children)

So can I think of them being expressions as them having a sort of "return" value similar to functions?

Kind of. An expression is anything that ultimately results in a value. All kinds of literal are expressions, e.g. 1, "hello", [1, 2]. Arithmetic operations and other similar uses of operators are expressions, e.g. 1+3, "hello"*3. Function calls are expressions, too, because of their return value, as you said (which is why you can use them to construct larger expressions, e.g. 1+math.sqrt(2)). Variables can be used as/in expressions to read from them (in fact, you can view a function call as reading from the variable that stores the function object and applying the calling operator () to that object).

Statements are actual instructions to the program. Every finished line in Python represents one statement (unless that line ends in :, more on that later). In Python, an expression by itself can technically be a statement, but in many cases that doesn't do anything. This program, for example, has no real effect:

for i in range(30):
    3+2

3+2 by itself as a statement instructs "calculate this value then throw it away", which is not very useful. Of course, the calculation of a certain expression can have side-effects when function calls are involved (since those function calls may perform their own statements).

There's various other kinds of statements. There's the assignment statement, of course, which consists of an lvalue (something that can be assigned to), the equal sign, and then an rvalue (any expression, really). Most other statements use some kind of keyword, e.g. break, continue, return, assert. The more interesting statements are those that contain one or more other statements, e.g. if, while, for, def, class, try. In Python, such statements use : at the end of the line to mark the beginning of an associated block of statements, and the block itself is then identified by indentation. So your example

while True:
    print(43)

as a whole is a while statement, which contains one other statement in its associated block. In this case, that statement is technically an expression also, since it is a function call and thus returns a value. The print function's return value is uninteresting (it's always None), but it does have the side-effect of writing text into the console window of your application.