开发者

Syntax error iterating over tuple in python

开发者 https://www.devze.com 2023-04-02 03:35 出处:网络
I am new to Python and am unsure of the best way to iterate over a tuple. The syntax for 开发者_StackOverflow中文版i in tuple

I am new to Python and am unsure of the best way to iterate over a tuple.

The syntax

for 开发者_StackOverflow中文版i in tuple
    print i

causes an error. Any help will be much appreciated! I am a ruby programmer new to python.


That is an error because the syntax is invalid, add a colon:

for i in tup:
    print i

Also, you should not use tuple as the name for a variable, as it is the name of a built-in function.


for i in my_tuples_name:
    print i

You don't iterate over the keyword tuple, you iterate over your variable.


You seem to have forgotten a colon.

for i in my_tuple:
    print i

Also look at this related answer.

EDIT: And I didn't notice you were iterating over the keyword tuple, as noted. by F.J. and Jakob Bowyer.

0

精彩评论

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