开发者

unpacking a tuple with print

开发者 https://www.devze.com 2023-02-22 07:24 出处:网络
the second unpacking is not working with print, what is the reason? for a in stok.iteritems(): ...c, b = a

the second unpacking is not working with print, what is the reason?

for a in stok.iteritems():
...  c, b = a
...  print c开发者_运维百科, b

this one is valid

but this one is not

for a in stok.iteritems():
...  print c, b = a


You can't do an assignment (a = b) inside a print statement. They're both statements, so they have to be done separately.

If it helps, you can do: for c, b in stok.iteritems():.


The reason is that c, b = a is a statement and not an expression (i.e., it does something, but doesn't have a value) and therefore you can't print it.


Does not make much sense. You want

for a in stok.iteritems():
...  print a

You can not mix assignments within a print...why would you think that this should work? Inventing new syntax?


= is assignment. I'm not sure what you are trying to achieve in the second piece of code, but it doesn't make sense: are you trying to print or are you trying to assign? You cannot print and assign in the same statement.

If you want to compare two numbers, use ==. For example:

print a == b

will tell you whether a and b are equal or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消