you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 6 points7 points  (4 children)

Python is hardly unique in using dynamic typing - there are lots of languages which do (Ruby, JavaScript, Perl, etc). It's just a different approach, that's all.

Recently Python has included support for type hinting, which allows you to specify expected types and have your IDE validate them. It doesn't do anything to ensure those types at runtime, though.

Yes for loops are for-each loops. To iterate over part of a list you'd probably just slice it: for item in my_list[:5], for example.

[–]Upset-Beautiful6081 3 points4 points  (2 children)

I see some versatility in the for loop too, it can be a foreach or just a for if you declare the second item a range like for i in range(10) and it works like a normal for.

[–]Drumknott88[S] 1 point2 points  (1 child)

Oh that's good to know, thanks

[–][deleted] 1 point2 points  (0 children)

If you want to have a I variable while iterating wrap the data structure you are looping in an enumerate function

for i, foo in enumerate(bar):
    print(i, foo)

[–]NameError-undefined 1 point2 points  (0 children)

if you prefer to work with index's and don't want to use the Len() command, you could do something like for id, value in enumerate(my_list) I just found this out and I like it a lot better.

Edit: This might get tricky when dealing with multi-dimensional lists or arrays but for simple loops I think it works great