Because they are
for statements which contain a code block. Generators and comprehensions are not statements, they are iterative expressions. They produce a sequence of results. A comprehension gives us back an object, a generator gives us a consumable object (iterator). A
for statement merely iterates within the confines we set for it. But it is a statement, not something we can pass around.
Because they are
Why doesn’t the
if conditions have colons after? They are supposed to create code blocks like
Is this the same type of thing(or term) as this:
Are they generators?
Can I use
yield instead of
return in a comprehension?
Not as far as I know.
yield is a generator statement that can produce a sequence. A comprehension is a sequence.
No. The one is a conditional expression, the other is an explicit loop over a range. No connection.
Because it is an expression, not a statement. There is no code block for
else. The outcome is a value, not an action.
Does Python internally include
yield in a sequence.
What’s the term/concept for
for loops that are in one line with other statements functions. Just recently I searched up why there were
elses all in one line in Python. I saw the term is called “tenary operator”. Is this right? The website that I found that in:
I am going to read the article and find more about tenary operators. Is the thing you call comprehensions or statements(I’m not that sure about the term for it) actually meant to be called ternary operators. Are ternary operators a fundamental concept in Python or are they advanced or whatever(asking in general context)?
Maybe you should read the article/website?
No, the term is not ternary operators it’s Conditional expressions. I saw that in the article.
Good question. If that is the case, which it very well might be, a comprehension is the immediate iteration of a generator. Somewhere behind the scenes,
yield would be at work. Interesting call out.
That is a loose application of the term. There is no ternary operator in Python. Conditional expressions are the closest thing we have, sometimes called, “Python’s version of a ternary expression”.
return n > 0 ? "positive" : n < 0 ? "negative" : "zero"
The equivalent in Python is,
return "positive" if n > 0 else "negative" if n < 0 else "zero"
There is no special operator, only the standard logical operators. We phrase it as an expression so it can be returned. That is the same goal of the ternary expression in JS.
Bottom line, they are both expressions. That’s where the comparison and terminology ends. Glad you caught that.
What kind of thing is this?
Also part of this is a conditonal expression.
You’re not breaking the code into its respective parts. Every keyword in that statement has a special meaning. And, it is a statement. The conditional expression is only a small part of it, the argument to the print function call.
That’s the end of the candle. Seems you are doing a bang up job of wasting our time. Only hope is that the next person gets more from it than you have.
You should look at my edit:
This creates a loop that repeats 10 times and each time prints the loop variable squared if the loop variable is less than 5. If not it prints 0. Is it a one liner statement?